1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/url_request/redirect_info.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string_number_conversions.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_util.h"
#include "net/url_request/redirect_util.h"
#include "net/url_request/referrer_policy.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace net {
namespace {
TEST(RedirectInfoTest, MethodForRedirect) {
struct TestCase {
const char* original_method;
int http_status_code;
const char* expected_new_method;
};
const TestCase kTests[] = {
{"GET", 301, "GET"}, {"GET", 302, "GET"}, {"GET", 303, "GET"},
{"GET", 307, "GET"}, {"GET", 308, "GET"}, {"HEAD", 301, "HEAD"},
{"HEAD", 302, "HEAD"}, {"HEAD", 303, "HEAD"}, {"HEAD", 307, "HEAD"},
{"HEAD", 308, "HEAD"}, {"POST", 301, "GET"}, {"POST", 302, "GET"},
{"POST", 303, "GET"}, {"POST", 307, "POST"}, {"POST", 308, "POST"},
{"PUT", 301, "PUT"}, {"PUT", 302, "PUT"}, {"PUT", 303, "GET"},
{"PUT", 307, "PUT"}, {"PUT", 308, "PUT"},
};
const GURL kOriginalUrl = GURL("https://foo.test/original");
const SiteForCookies kOriginalSiteForCookies =
SiteForCookies::FromUrl(GURL("https://foo.test/"));
const url::Origin kOriginalTopFrameOrigin = url::Origin::Create(kOriginalUrl);
const RedirectInfo::FirstPartyURLPolicy kOriginalFirstPartyUrlPolicy =
RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL;
const ReferrerPolicy kOriginalReferrerPolicy = ReferrerPolicy::NEVER_CLEAR;
const std::string kOriginalReferrer = "";
const GURL kNewLocation = GURL("https://foo.test/redirected");
const bool kInsecureSchemeWasUpgraded = false;
const bool kCopyFragment = true;
for (const auto& test : kTests) {
SCOPED_TRACE(::testing::Message()
<< "original_method: " << test.original_method
<< " http_status_code: " << test.http_status_code);
RedirectInfo redirect_info = RedirectInfo::ComputeRedirectInfo(
test.original_method, kOriginalUrl, kOriginalSiteForCookies,
kOriginalFirstPartyUrlPolicy, kOriginalReferrerPolicy,
kOriginalReferrer, test.http_status_code, kNewLocation,
std::nullopt /* referrer_policy_header */, kInsecureSchemeWasUpgraded,
kCopyFragment);
EXPECT_EQ(test.expected_new_method, redirect_info.new_method);
EXPECT_EQ(test.http_status_code, redirect_info.status_code);
EXPECT_EQ(kNewLocation, redirect_info.new_url);
}
}
TEST(RedirectInfoTest, CopyFragment) {
struct TestCase {
bool copy_fragment;
const char* original_url;
const char* new_location;
const char* expected_new_url;
};
const TestCase kTests[] = {
{true, "http://foo.test/original", "http://foo.test/redirected",
"http://foo.test/redirected"},
{true, "http://foo.test/original#1", "http://foo.test/redirected",
"http://foo.test/redirected#1"},
{true, "http://foo.test/original#1", "http://foo.test/redirected#2",
"http://foo.test/redirected#2"},
{false, "http://foo.test/original", "http://foo.test/redirected",
"http://foo.test/redirected"},
{false, "http://foo.test/original#1", "http://foo.test/redirected",
"http://foo.test/redirected"},
{false, "http://foo.test/original#1", "http://foo.test/redirected#2",
"http://foo.test/redirected#2"},
};
const std::string kOriginalMethod = "GET";
const SiteForCookies kOriginalSiteForCookies =
SiteForCookies::FromUrl(GURL("https://foo.test/"));
const RedirectInfo::FirstPartyURLPolicy kOriginalFirstPartyUrlPolicy =
RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL;
const ReferrerPolicy kOriginalReferrerPolicy = ReferrerPolicy::NEVER_CLEAR;
const std::string kOriginalReferrer = "";
const int kHttpStatusCode = 301;
const bool kInsecureSchemeWasUpgraded = false;
for (const auto& test : kTests) {
SCOPED_TRACE(::testing::Message()
<< "copy_fragment: " << test.copy_fragment
<< " original_url: " << test.original_url
<< " new_location: " << test.new_location);
RedirectInfo redirect_info = RedirectInfo::ComputeRedirectInfo(
kOriginalMethod, GURL(test.original_url), kOriginalSiteForCookies,
kOriginalFirstPartyUrlPolicy, kOriginalReferrerPolicy,
kOriginalReferrer, kHttpStatusCode, GURL(test.new_location),
std::nullopt /* referrer_policy_header */, kInsecureSchemeWasUpgraded,
test.copy_fragment);
EXPECT_EQ(GURL(test.expected_new_url), redirect_info.new_url);
}
}
TEST(RedirectInfoTest, FirstPartyURLPolicy) {
struct TestCase {
RedirectInfo::FirstPartyURLPolicy original_first_party_url_policy;
const char* expected_new_site_for_cookies;
};
const TestCase kTests[] = {
{RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL,
"https://foo.test/"},
{RedirectInfo::FirstPartyURLPolicy::UPDATE_URL_ON_REDIRECT,
"https://foo.test/redirected"},
};
const std::string kOriginalMethod = "GET";
const GURL kOriginalUrl = GURL("https://foo.test/");
const SiteForCookies kOriginalSiteForCookies =
SiteForCookies::FromUrl(GURL("https://foo.test/"));
const ReferrerPolicy kOriginalReferrerPolicy = ReferrerPolicy::NEVER_CLEAR;
const std::string kOriginalReferrer = "";
const GURL kNewLocation = GURL("https://foo.test/redirected");
const bool kInsecureSchemeWasUpgraded = false;
const int kHttpStatusCode = 301;
const bool kCopyFragment = true;
for (const auto& test : kTests) {
SCOPED_TRACE(::testing::Message()
<< "original_first_party_url_policy: "
<< static_cast<int>(test.original_first_party_url_policy));
RedirectInfo redirect_info = RedirectInfo::ComputeRedirectInfo(
kOriginalMethod, kOriginalUrl, kOriginalSiteForCookies,
test.original_first_party_url_policy, kOriginalReferrerPolicy,
kOriginalReferrer, kHttpStatusCode, kNewLocation,
std::nullopt /* referrer_policy_header */, kInsecureSchemeWasUpgraded,
kCopyFragment);
EXPECT_TRUE(redirect_info.new_site_for_cookies.IsEquivalent(
SiteForCookies::FromUrl(GURL(test.expected_new_site_for_cookies))));
}
}
TEST(RedirectInfoTest, ReferrerPolicy) {
struct TestCase {
const char* original_url;
const char* original_referrer;
const char* response_headers;
ReferrerPolicy original_referrer_policy;
ReferrerPolicy expected_new_referrer_policy;
const char* expected_referrer;
};
const TestCase kTests[] = {
// If a redirect serves 'Referrer-Policy: no-referrer', then the referrer
// should be cleared.
{"http://foo.test/one" /* original url */,
"http://foo.test/one" /* original referrer */,
"Location: http://foo.test/test\n"
"Referrer-Policy: no-referrer\n",
// original policy
ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
ReferrerPolicy::NO_REFERRER /* expected new policy */,
"" /* expected new referrer */},
// Same as above but for the legacy keyword 'never', which should not be
// supported.
{"http://foo.test/one" /* original url */,
"http://foo.test/one" /* original referrer */,
"Location: http://foo.test/test\nReferrer-Policy: never\n",
// original policy
ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
// expected new policy
ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
"http://foo.test/one" /* expected new referrer */},
// If a redirect serves 'Referrer-Policy: no-referrer-when-downgrade',
// then the referrer should be cleared on downgrade, even if the original
// request's policy specified that the referrer should never be cleared.
{"https://foo.test/one" /* original url */,
"https://foo.test/one" /* original referrer */,
"Location: http://foo.test\n"
"Referrer-Policy: no-referrer-when-downgrade\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
// expected new policy
ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
"" /* expected new referrer */},
// Same as above but for the legacy keyword 'default', which should not be
// supported.
{"https://foo.test/one" /* original url */,
"https://foo.test/one" /* original referrer */,
"Location: http://foo.test\n"
"Referrer-Policy: default\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
// expected new policy
ReferrerPolicy::NEVER_CLEAR,
"https://foo.test/one" /* expected new referrer */},
// If a redirect serves 'Referrer-Policy: no-referrer-when-downgrade',
// the referrer should not be cleared for a non-downgrading redirect. But
// the policy should be updated.
{"https://foo.test/one" /* original url */,
"https://foo.test/one" /* original referrer */,
"Location: https://foo.test\n"
"Referrer-Policy: no-referrer-when-downgrade\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
// expected new policy
ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
"https://foo.test/one" /* expected new referrer */},
// If a redirect serves 'Referrer-Policy: origin', then the referrer
// should be stripped to its origin, even if the original request's policy
// specified that the referrer should never be cleared.
{"https://foo.test/one" /* original url */,
"https://foo.test/one" /* original referrer */,
"Location: https://foo.test/two\n"
"Referrer-Policy: origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::ORIGIN /* expected new policy */,
"https://foo.test/" /* expected new referrer */},
// If a redirect serves 'Referrer-Policy: origin-when-cross-origin', then
// the referrer should be untouched for a same-origin redirect...
{"https://foo.test/one" /* original url */,
"https://foo.test/referrer" /* original referrer */,
"Location: https://foo.test/two\n"
"Referrer-Policy: origin-when-cross-origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::
ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected new policy */,
"https://foo.test/referrer" /* expected new referrer */},
// ... but should be stripped to the origin for a cross-origin redirect.
{"https://foo.test/one" /* original url */,
"https://foo.test/one" /* original referrer */,
"Location: https://bar.test/two\n"
"Referrer-Policy: origin-when-cross-origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::
ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected new policy */,
"https://foo.test/" /* expected new referrer */},
// If a redirect serves 'Referrer-Policy: same-origin', then the referrer
// should be untouched for a same-origin redirect,
{"https://foo.test/one" /* original url */,
"https://foo.test/referrer" /* original referrer */,
"Location: https://foo.test/two\n"
"Referrer-Policy: same-origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::CLEAR_ON_TRANSITION_CROSS_ORIGIN /* new policy */
,
"https://foo.test/referrer" /* expected new referrer */},
// ... but should be cleared for a cross-origin redirect.
{"https://foo.test/one" /* original url */,
"https://foo.test/referrer" /* original referrer */,
"Location: https://bar.test/two\n"
"Referrer-Policy: same-origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::CLEAR_ON_TRANSITION_CROSS_ORIGIN,
"" /* expected new referrer */},
// If a redirect serves 'Referrer-Policy: strict-origin', then the
// referrer should be the origin only for a cross-origin non-downgrading
// redirect,
{"https://foo.test/one" /* original url */,
"https://foo.test/referrer" /* original referrer */,
"Location: https://bar.test/two\n"
"Referrer-Policy: strict-origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
"https://foo.test/" /* expected new referrer */},
{"http://foo.test/one" /* original url */,
"http://foo.test/referrer" /* original referrer */,
"Location: http://bar.test/two\n"
"Referrer-Policy: strict-origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
"http://foo.test/" /* expected new referrer */},
// ... but should be cleared for a downgrading redirect.
{"https://foo.test/one" /* original url */,
"https://foo.test/referrer" /* original referrer */,
"Location: http://foo.test/two\n"
"Referrer-Policy: strict-origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::ORIGIN_CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
"" /* expected new referrer */},
// If a redirect serves 'Referrer-Policy:
// strict-origin-when-cross-origin', then the referrer should be preserved
// for a same-origin redirect,
{"https://foo.test/one" /* original url */,
"https://foo.test/referrer" /* original referrer */,
"Location: https://foo.test/two\n"
"Referrer-Policy: strict-origin-when-cross-origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::REDUCE_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
"https://foo.test/referrer" /* expected new referrer */},
{"http://foo.test/one" /* original url */,
"http://foo.test/referrer" /* original referrer */,
"Location: http://foo.test/two\n"
"Referrer-Policy: strict-origin-when-cross-origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::REDUCE_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
"http://foo.test/referrer" /* expected new referrer */},
// ... but should be stripped to the origin for a cross-origin
// non-downgrading redirect,
{"https://foo.test/one" /* original url */,
"https://foo.test/referrer" /* original referrer */,
"Location: https://bar.test/two\n"
"Referrer-Policy: strict-origin-when-cross-origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::REDUCE_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
"https://foo.test/" /* expected new referrer */},
{"http://foo.test/one" /* original url */,
"http://foo.test/referrer" /* original referrer */,
"Location: http://bar.test/two\n"
"Referrer-Policy: strict-origin-when-cross-origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::REDUCE_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
"http://foo.test/" /* expected new referrer */},
// ... and should be cleared for a downgrading redirect.
{"https://foo.test/one" /* original url */,
"https://foo.test/referrer" /* original referrer */,
"Location: http://foo.test/two\n"
"Referrer-Policy: strict-origin-when-cross-origin\n",
ReferrerPolicy::NEVER_CLEAR /* original policy */,
ReferrerPolicy::REDUCE_GRANULARITY_ON_TRANSITION_CROSS_ORIGIN,
"" /* expected new referrer */},
// If a redirect serves 'Referrer-Policy: unsafe-url', then the referrer
// should remain, even if originally set to clear on downgrade.
{"https://foo.test/one" /* original url */,
"https://foo.test/one" /* original referrer */,
"Location: http://bar.test/two\n"
"Referrer-Policy: unsafe-url\n",
ReferrerPolicy::
ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
ReferrerPolicy::NEVER_CLEAR /* expected new policy */,
"https://foo.test/one" /* expected new referrer */},
// Same as above but for the legacy keyword 'always', which should not be
// supported.
{"https://foo.test/one" /* original url */,
"https://foo.test/one" /* original referrer */,
"Location: http://bar.test/two\n"
"Referrer-Policy: always\n",
ReferrerPolicy::
ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
ReferrerPolicy::
ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected new policy */,
"https://foo.test/" /* expected new referrer */},
// An invalid keyword should leave the policy untouched.
{"https://foo.test/one" /* original url */,
"https://foo.test/one" /* original referrer */,
"Location: https://bar.test/two\n"
"Referrer-Policy: not-a-valid-policy\n",
ReferrerPolicy::
ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
ReferrerPolicy::
ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected new policy */,
"https://foo.test/" /* expected new referrer */},
{"https://foo.test/one" /* original url */,
"https://foo.test/one" /* original referrer */,
"Location: http://bar.test/two\n"
"Referrer-Policy: not-a-valid-policy\n",
// original policy
ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
// expected new policy
ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
"" /* expected new referrer */},
// The last valid keyword should take precedence.
{"https://foo.test/one" /* original url */,
"https://foo.test/one" /* original referrer */,
"Location: https://bar.test/two\n"
"Referrer-Policy: unsafe-url\n"
"Referrer-Policy: not-a-valid-policy\n",
ReferrerPolicy::
ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
ReferrerPolicy::NEVER_CLEAR /* expected new policy */,
"https://foo.test/one" /* expected new referrer */},
{"https://foo.test/one" /* original url */,
"https://foo.test/one" /* original referrer */,
"Location: https://bar.test/two\n"
"Referrer-Policy: unsafe-url\n"
"Referrer-Policy: origin\n",
ReferrerPolicy::
ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
ReferrerPolicy::ORIGIN /* expected new policy */,
"https://foo.test/" /* expected new referrer */},
// An empty header should not affect the request.
{"https://foo.test/one" /* original url */,
"https://foo.test/one" /* original referrer */,
"Location: https://bar.test/two\n"
"Referrer-Policy: \n",
ReferrerPolicy::
ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
ReferrerPolicy::
ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected new policy */,
"https://foo.test/" /* expected new referrer */},
// A redirect response without Referrer-Policy header should not affect
// the policy and the referrer.
{"http://foo.test/one" /* original url */,
"http://foo.test/one" /* original referrer */,
"Location: http://foo.test/test\n",
// original policy
ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
// expected new policy
ReferrerPolicy::CLEAR_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
"http://foo.test/one" /* expected new referrer */},
};
const std::string kOriginalMethod = "GET";
const SiteForCookies kOriginalSiteForCookies =
SiteForCookies::FromUrl(GURL("https://foo.test/"));
const RedirectInfo::FirstPartyURLPolicy kOriginalFirstPartyUrlPolicy =
RedirectInfo::FirstPartyURLPolicy::NEVER_CHANGE_URL;
const bool kInsecureSchemeWasUpgraded = false;
const bool kCopyFragment = true;
for (const auto& test : kTests) {
SCOPED_TRACE(::testing::Message()
<< "original_url: " << test.original_url
<< " original_referrer: " << test.original_referrer
<< " response_headers: " << test.response_headers
<< " original_referrer_policy: "
<< static_cast<int>(test.original_referrer_policy));
std::string response_header_text =
"HTTP/1.1 302 Redirect\n" + std::string(test.response_headers);
std::string raw_headers =
HttpUtil::AssembleRawHeaders(response_header_text);
auto response_headers =
base::MakeRefCounted<HttpResponseHeaders>(raw_headers);
EXPECT_EQ(302, response_headers->response_code());
std::string location_string;
EXPECT_TRUE(response_headers->IsRedirect(&location_string));
const GURL original_url = GURL(test.original_url);
const GURL new_location = original_url.Resolve(location_string);
RedirectInfo redirect_info = RedirectInfo::ComputeRedirectInfo(
kOriginalMethod, original_url, kOriginalSiteForCookies,
kOriginalFirstPartyUrlPolicy, test.original_referrer_policy,
test.original_referrer, response_headers->response_code(), new_location,
RedirectUtil::GetReferrerPolicyHeader(response_headers.get()),
kInsecureSchemeWasUpgraded, kCopyFragment);
EXPECT_EQ(test.expected_new_referrer_policy,
redirect_info.new_referrer_policy);
EXPECT_EQ(test.expected_referrer, redirect_info.new_referrer);
}
}
} // namespace
} // namespace net
|