Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: net/url_request/url_request_job_unittest.cc

Issue 2111623003: Reland of Apply Referrer-Policy header when following redirects (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: initialize |read_handler| Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/url_request/url_request_job.h" 5 #include "net/url_request/url_request_job.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "net/base/request_priority.h" 10 #include "net/base/request_priority.h"
11 #include "net/http/http_transaction_test_util.h" 11 #include "net/http/http_transaction_test_util.h"
12 #include "net/test/cert_test_util.h"
13 #include "net/test/test_data_directory.h"
12 #include "net/url_request/url_request.h" 14 #include "net/url_request/url_request.h"
13 #include "net/url_request/url_request_test_util.h" 15 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
15 17
16 namespace net { 18 namespace net {
17 19
18 namespace { 20 namespace {
19 21
20 // Data encoded in kBrotliHelloData. 22 // Data encoded in kBrotliHelloData.
21 const char kBrotliDecodedHelloData[] = "hello, world!\n"; 23 const char kBrotliDecodedHelloData[] = "hello, world!\n";
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 response_data->insert(10, 64 * 1024, 'a'); 56 response_data->insert(10, 64 * 1024, 'a');
55 } 57 }
56 58
57 void BrotliHelloServer(const HttpRequestInfo* request, 59 void BrotliHelloServer(const HttpRequestInfo* request,
58 std::string* response_status, 60 std::string* response_status,
59 std::string* response_headers, 61 std::string* response_headers,
60 std::string* response_data) { 62 std::string* response_data) {
61 response_data->assign(kBrotliHelloData, sizeof(kBrotliHelloData) - 1); 63 response_data->assign(kBrotliHelloData, sizeof(kBrotliHelloData) - 1);
62 } 64 }
63 65
66 void MakeMockReferrerPolicyTransaction(const char* original_url,
67 const char* referer_header,
68 const char* response_headers,
69 MockTransaction* transaction) {
70 transaction->url = original_url;
71 transaction->method = "GET";
72 transaction->request_time = base::Time();
73 transaction->request_headers = referer_header;
74 transaction->load_flags = LOAD_NORMAL;
75 transaction->status = "HTTP/1.1 302 Found";
76 transaction->response_headers = response_headers;
77 transaction->response_time = base::Time();
78 transaction->data = "hello";
79 transaction->test_mode = TEST_MODE_NORMAL;
80 transaction->handler = nullptr;
81 transaction->read_handler = nullptr;
82 if (GURL(original_url).SchemeIsCryptographic()) {
83 transaction->cert =
84 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem");
85 } else {
86 transaction->cert = nullptr;
87 }
88 transaction->cert_status = 0;
89 transaction->ssl_connection_status = 0;
90 transaction->return_code = OK;
91 }
92
64 const MockTransaction kGZip_Transaction = { 93 const MockTransaction kGZip_Transaction = {
65 "http://www.google.com/gzyp", 94 "http://www.google.com/gzyp",
66 "GET", 95 "GET",
67 base::Time(), 96 base::Time(),
68 "", 97 "",
69 LOAD_NORMAL, 98 LOAD_NORMAL,
70 "HTTP/1.1 200 OK", 99 "HTTP/1.1 200 OK",
71 "Cache-Control: max-age=10000\n" 100 "Cache-Control: max-age=10000\n"
72 "Content-Encoding: gzip\n" 101 "Content-Encoding: gzip\n"
73 "Content-Length: 30\n", // Intentionally wrong. 102 "Content-Length: 30\n", // Intentionally wrong.
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 req->set_method("GET"); 255 req->set_method("GET");
227 req->Start(); 256 req->Start();
228 257
229 base::RunLoop().Run(); 258 base::RunLoop().Run();
230 259
231 EXPECT_TRUE(network_layer.done_reading_called()); 260 EXPECT_TRUE(network_layer.done_reading_called());
232 261
233 RemoveMockTransaction(&kRedirect_Transaction); 262 RemoveMockTransaction(&kRedirect_Transaction);
234 } 263 }
235 264
265 TEST(URLRequestJob, RedirectTransactionWithReferrerPolicyHeader) {
266 struct TestCase {
267 const char* original_url;
268 const char* original_referrer;
269 const char* response_headers;
270 URLRequest::ReferrerPolicy original_referrer_policy;
271 URLRequest::ReferrerPolicy expected_final_referrer_policy;
272 const char* expected_final_referrer;
273 };
274
275 const TestCase kTests[] = {
276 // If a redirect serves 'Referrer-Policy: no-referrer', then the referrer
277 // should be cleared.
278 {"http://foo.test/one" /* original url */,
279 "http://foo.test/one" /* original referrer */,
280 "Location: http://foo.test/test\n"
281 "Referrer-Policy: no-referrer\n",
282 // original policy
283 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
284 URLRequest::NO_REFERRER /* expected final policy */,
285 "" /* expected final referrer */},
286
287 // Same as above but for the legacy keyword 'never'.
288 {"http://foo.test/one" /* original url */,
289 "http://foo.test/one" /* original referrer */,
290 "Location: http://foo.test/test\nReferrer-Policy: never\n",
291 // original policy
292 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
293 URLRequest::NO_REFERRER /* expected final policy */,
294 "" /* expected final referrer */},
295
296 // If a redirect serves 'Referrer-Policy:
297 // no-referrer-when-downgrade', then the referrer should be cleared
298 // on downgrade, even if the original request's policy specified
299 // that the referrer should never be cleared.
300 {"https://foo.test/one" /* original url */,
301 "https://foo.test/one" /* original referrer */,
302 "Location: http://foo.test\n"
303 "Referrer-Policy: no-referrer-when-downgrade\n",
304 URLRequest::NEVER_CLEAR_REFERRER /* original policy */,
305 // expected final policy
306 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
307 "" /* expected final referrer */},
308
309 // Same as above but for the legacy keyword 'default'.
310 {"https://foo.test/one" /* original url */,
311 "https://foo.test/one" /* original referrer */,
312 "Location: http://foo.test\n"
313 "Referrer-Policy: default\n",
314 URLRequest::NEVER_CLEAR_REFERRER /* original policy */,
315 // expected final policy
316 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
317 "" /* expected final referrer */},
318
319 // If a redirect serves 'Referrer-Policy: origin', then the referrer
320 // should be stripped to its origin, even if the original request's
321 // policy specified that the referrer should never be cleared.
322 {"https://foo.test/one" /* original url */,
323 "https://foo.test/one" /* original referrer */,
324 "Location: https://foo.test/two\n"
325 "Referrer-Policy: origin\n",
326 URLRequest::NEVER_CLEAR_REFERRER /* original policy */,
327 URLRequest::ORIGIN /* expected final policy */,
328 "https://foo.test/" /* expected final referrer */},
329
330 // If a redirect serves 'Referrer-Policy: origin-when-cross-origin',
331 // then the referrer should be untouched for a same-origin redirect...
332 {"https://foo.test/one" /* original url */,
333 "https://foo.test/referrer" /* original referrer */,
334 "Location: https://foo.test/two\n"
335 "Referrer-Policy: origin-when-cross-origin\n",
336 URLRequest::NEVER_CLEAR_REFERRER /* original policy */,
337 URLRequest::
338 ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected final policy */,
339 "https://foo.test/referrer" /* expected final referrer */},
340
341 // ... but should be stripped to the origin for a cross-origin redirect.
342 {"https://foo.test/one" /* original url */,
343 "https://foo.test/one" /* original referrer */,
344 "Location: https://bar.test/two\n"
345 "Referrer-Policy: origin-when-cross-origin\n",
346 URLRequest::NEVER_CLEAR_REFERRER /* original policy */,
347 URLRequest::
348 ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected final policy */,
349 "https://foo.test/" /* expected final referrer */},
350
351 // If a redirect serves 'Referrer-Policy: unsafe-url', then the
352 // referrer should remain, even if originally set to clear on
353 // downgrade.
354 {"https://foo.test/one" /* original url */,
355 "https://foo.test/one" /* original referrer */,
356 "Location: https://bar.test/two\n"
357 "Referrer-Policy: unsafe-url\n",
358 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
359 URLRequest::NEVER_CLEAR_REFERRER /* expected final policy */,
360 "https://foo.test/one" /* expected final referrer */},
361
362 // Same as above but for the legacy keyword 'always'.
363 {"https://foo.test/one" /* original url */,
364 "https://foo.test/one" /* original referrer */,
365 "Location: https://bar.test/two\n"
366 "Referrer-Policy: always\n",
367 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
368 URLRequest::NEVER_CLEAR_REFERRER /* expected final policy */,
369 "https://foo.test/one" /* expected final referrer */},
370
371 // An invalid keyword should leave the policy untouched.
372 {"https://foo.test/one" /* original url */,
373 "https://foo.test/one" /* original referrer */,
374 "Location: https://bar.test/two\n"
375 "Referrer-Policy: not-a-valid-policy\n",
376 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
377 URLRequest::
378 ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected final policy */,
379 "https://foo.test/" /* expected final referrer */},
380
381 {"https://foo.test/one" /* original url */,
382 "https://foo.test/one" /* original referrer */,
383 "Location: http://bar.test/two\n"
384 "Referrer-Policy: not-a-valid-policy\n",
385 // original policy
386 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
387 // expected final policy
388 URLRequest::CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE,
389 "" /* expected final referrer */},
390
391 // The last valid keyword should take precedence.
392 {"https://foo.test/one" /* original url */,
393 "https://foo.test/one" /* original referrer */,
394 "Location: https://bar.test/two\n"
395 "Referrer-Policy: unsafe-url\n"
396 "Referrer-Policy: not-a-valid-policy\n",
397 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
398 URLRequest::NEVER_CLEAR_REFERRER /* expected final policy */,
399 "https://foo.test/one" /* expected final referrer */},
400
401 {"https://foo.test/one" /* original url */,
402 "https://foo.test/one" /* original referrer */,
403 "Location: https://bar.test/two\n"
404 "Referrer-Policy: unsafe-url\n"
405 "Referrer-Policy: origin\n",
406 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
407 URLRequest::ORIGIN /* expected final policy */,
408 "https://foo.test/" /* expected final referrer */},
409
410 // An empty header should not affect the request.
411 {"https://foo.test/one" /* original url */,
412 "https://foo.test/one" /* original referrer */,
413 "Location: https://bar.test/two\n"
414 "Referrer-Policy: \n",
415 URLRequest::ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* original policy */,
416 URLRequest::
417 ORIGIN_ONLY_ON_TRANSITION_CROSS_ORIGIN /* expected final policy */,
418 "https://foo.test/" /* expected final referrer */},
419 };
420
421 for (const auto& test : kTests) {
422 MockTransaction transaction;
423 std::string request_headers =
424 "Referer: " + std::string(test.original_referrer) + "\n";
425 MakeMockReferrerPolicyTransaction(test.original_url,
426 request_headers.c_str(),
427 test.response_headers, &transaction);
428
429 MockNetworkLayer network_layer;
430 TestURLRequestContext context;
431 context.set_enable_referrer_policy_header(true);
432 context.set_http_transaction_factory(&network_layer);
433
434 TestDelegate d;
435 std::unique_ptr<URLRequest> req(
436 context.CreateRequest(GURL(transaction.url), DEFAULT_PRIORITY, &d));
437 AddMockTransaction(&transaction);
438
439 req->set_referrer_policy(test.original_referrer_policy);
440 req->SetReferrer(test.original_referrer);
441
442 req->set_method("GET");
443 req->Start();
444
445 base::RunLoop().Run();
446
447 EXPECT_TRUE(network_layer.done_reading_called());
448
449 RemoveMockTransaction(&transaction);
450
451 // Test that the referrer policy and referrer were set correctly
452 // according to the header received during the redirect.
453 EXPECT_EQ(test.expected_final_referrer_policy, req->referrer_policy());
454 EXPECT_EQ(test.expected_final_referrer, req->referrer());
455 }
456 }
457
236 TEST(URLRequestJob, TransactionNotCachedWhenNetworkDelegateRedirects) { 458 TEST(URLRequestJob, TransactionNotCachedWhenNetworkDelegateRedirects) {
237 MockNetworkLayer network_layer; 459 MockNetworkLayer network_layer;
238 TestNetworkDelegate network_delegate; 460 TestNetworkDelegate network_delegate;
239 network_delegate.set_redirect_on_headers_received_url(GURL("http://foo")); 461 network_delegate.set_redirect_on_headers_received_url(GURL("http://foo"));
240 TestURLRequestContext context; 462 TestURLRequestContext context;
241 context.set_http_transaction_factory(&network_layer); 463 context.set_http_transaction_factory(&network_layer);
242 context.set_network_delegate(&network_delegate); 464 context.set_network_delegate(&network_delegate);
243 465
244 TestDelegate d; 466 TestDelegate d;
245 std::unique_ptr<URLRequest> req( 467 std::unique_ptr<URLRequest> req(
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
351 573
352 EXPECT_FALSE(d.request_failed()); 574 EXPECT_FALSE(d.request_failed());
353 EXPECT_EQ(200, req->GetResponseCode()); 575 EXPECT_EQ(200, req->GetResponseCode());
354 EXPECT_EQ(kBrotliDecodedHelloData, d.data_received()); 576 EXPECT_EQ(kBrotliDecodedHelloData, d.data_received());
355 EXPECT_TRUE(network_layer.done_reading_called()); 577 EXPECT_TRUE(network_layer.done_reading_called());
356 578
357 RemoveMockTransaction(&kBrotli_Slow_Transaction); 579 RemoveMockTransaction(&kBrotli_Slow_Transaction);
358 } 580 }
359 581
360 } // namespace net 582 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698