OLD | NEW |
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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/pickle.h" | 8 #include "base/pickle.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
28 #include "net/base/test_completion_callback.h" | 28 #include "net/base/test_completion_callback.h" |
29 #include "net/disk_cache/disk_cache.h" | 29 #include "net/disk_cache/disk_cache.h" |
30 #include "net/http/http_cache.h" | 30 #include "net/http/http_cache.h" |
31 #include "net/http/http_response_headers.h" | 31 #include "net/http/http_response_headers.h" |
32 #include "net/http/http_response_info.h" | 32 #include "net/http/http_response_info.h" |
33 #include "net/http/http_util.h" | 33 #include "net/http/http_util.h" |
34 #include "net/url_request/url_request_context.h" | 34 #include "net/url_request/url_request_context.h" |
35 #include "net/url_request/url_request_context_getter.h" | 35 #include "net/url_request/url_request_context_getter.h" |
36 | 36 |
37 static const char* kOriginalLandingURL = "http://www.originallandingpage.com/"; | 37 // Mixture of HTTP and HTTPS. No special treatment for HTTPS. |
38 static const char* kHttpsURL = "https://www.url.com/"; | 38 static const char* kOriginalLandingURL = |
39 static const char* kDOMChildURL = "http://www.domparent.com/"; | 39 "http://www.originallandingpage.com/with/path"; |
40 static const char* kDOMParentURL = "http://www.domchild.com/"; | 40 static const char* kDOMChildURL = "https://www.domparent.com/with/path"; |
41 static const char* kFirstRedirectURL = "http://redirectone.com/"; | 41 static const char* kDOMParentURL = "https://www.domchild.com/with/path"; |
42 static const char* kSecondRedirectURL = "http://redirecttwo.com/"; | 42 static const char* kFirstRedirectURL = "http://redirectone.com/with/path"; |
| 43 static const char* kSecondRedirectURL = "https://redirecttwo.com/with/path"; |
| 44 static const char* kReferrerURL = "http://www.referrer.com/with/path"; |
43 | 45 |
44 static const char* kMalwareURL = "http://www.malware.com/"; | 46 static const char* kMalwareURL = "http://www.malware.com/with/path"; |
45 static const char* kMalwareHeaders = | 47 static const char* kMalwareHeaders = |
46 "HTTP/1.1 200 OK\n" | 48 "HTTP/1.1 200 OK\n" |
47 "Content-Type: image/jpeg\n"; | 49 "Content-Type: image/jpeg\n"; |
48 static const char* kMalwareData = "exploit();"; | 50 static const char* kMalwareData = "exploit();"; |
49 | 51 |
50 static const char* kLandingURL = "http://www.landingpage.com/"; | 52 static const char* kLandingURL = "http://www.landingpage.com/with/path"; |
51 static const char* kLandingHeaders = | 53 static const char* kLandingHeaders = |
52 "HTTP/1.1 200 OK\n" | 54 "HTTP/1.1 200 OK\n" |
53 "Content-Type: text/html\n" | 55 "Content-Type: text/html\n" |
54 "Content-Length: 1024\n" | 56 "Content-Length: 1024\n" |
55 "Set-Cookie: tastycookie\n"; // This header is stripped. | 57 "Set-Cookie: tastycookie\n"; // This header is stripped. |
56 static const char* kLandingData = "<iframe src='http://www.malware.com'>"; | 58 static const char* kLandingData = |
| 59 "<iframe src='http://www.malware.com/with/path'>"; |
| 60 |
57 | 61 |
58 using content::BrowserThread; | 62 using content::BrowserThread; |
59 using content::WebContents; | 63 using content::WebContents; |
60 using safe_browsing::ClientMalwareReportRequest; | 64 using safe_browsing::ClientMalwareReportRequest; |
61 | 65 |
62 namespace { | 66 namespace { |
63 | 67 |
64 void WriteHeaders(disk_cache::Entry* entry, const std::string& headers) { | 68 void WriteHeaders(disk_cache::Entry* entry, const std::string& headers) { |
65 net::HttpResponseInfo responseinfo; | 69 net::HttpResponseInfo responseinfo; |
66 std::string raw_headers = net::HttpUtil::AssembleRawHeaders( | 70 std::string raw_headers = net::HttpUtil::AssembleRawHeaders( |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 // Lets us provide a MockURLRequestContext with an HTTP Cache we pre-populate. | 128 // Lets us provide a MockURLRequestContext with an HTTP Cache we pre-populate. |
125 // Also exposes the constructor. | 129 // Also exposes the constructor. |
126 class MalwareDetailsWrap : public MalwareDetails { | 130 class MalwareDetailsWrap : public MalwareDetails { |
127 public: | 131 public: |
128 MalwareDetailsWrap( | 132 MalwareDetailsWrap( |
129 SafeBrowsingUIManager* ui_manager, | 133 SafeBrowsingUIManager* ui_manager, |
130 WebContents* web_contents, | 134 WebContents* web_contents, |
131 const SafeBrowsingUIManager::UnsafeResource& unsafe_resource, | 135 const SafeBrowsingUIManager::UnsafeResource& unsafe_resource, |
132 net::URLRequestContextGetter* request_context_getter) | 136 net::URLRequestContextGetter* request_context_getter) |
133 : MalwareDetails(ui_manager, web_contents, unsafe_resource) { | 137 : MalwareDetails(ui_manager, web_contents, unsafe_resource) { |
134 | |
135 request_context_getter_ = request_context_getter; | 138 request_context_getter_ = request_context_getter; |
136 } | 139 } |
137 | 140 |
138 private: | 141 private: |
139 ~MalwareDetailsWrap() override {} | 142 ~MalwareDetailsWrap() override {} |
140 }; | 143 }; |
141 | 144 |
142 class MockSafeBrowsingUIManager : public SafeBrowsingUIManager { | 145 class MockSafeBrowsingUIManager : public SafeBrowsingUIManager { |
143 public: | 146 public: |
144 base::RunLoop* run_loop_; | 147 base::RunLoop* run_loop_; |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 GURL(), *redirects, ui::PAGE_TRANSITION_TYPED, | 317 GURL(), *redirects, ui::PAGE_TRANSITION_TYPED, |
315 history::SOURCE_BROWSED, false); | 318 history::SOURCE_BROWSED, false); |
316 } | 319 } |
317 | 320 |
318 scoped_refptr<MockSafeBrowsingUIManager> ui_manager_; | 321 scoped_refptr<MockSafeBrowsingUIManager> ui_manager_; |
319 }; | 322 }; |
320 | 323 |
321 // Tests creating a simple malware report. | 324 // Tests creating a simple malware report. |
322 TEST_F(MalwareDetailsTest, MalwareSubResource) { | 325 TEST_F(MalwareDetailsTest, MalwareSubResource) { |
323 // Start a load. | 326 // Start a load. |
324 controller().LoadURL(GURL(kLandingURL), content::Referrer(), | 327 controller().LoadURL( |
325 ui::PAGE_TRANSITION_TYPED, std::string()); | 328 GURL(kLandingURL), |
| 329 content::Referrer(GURL(kReferrerURL), blink::WebReferrerPolicyDefault), |
| 330 ui::PAGE_TRANSITION_TYPED, std::string()); |
326 | 331 |
327 UnsafeResource resource; | 332 UnsafeResource resource; |
328 InitResource(&resource, true, GURL(kMalwareURL)); | 333 InitResource(&resource, true, GURL(kMalwareURL)); |
329 | 334 |
330 scoped_refptr<MalwareDetailsWrap> report = | 335 scoped_refptr<MalwareDetailsWrap> report = |
331 new MalwareDetailsWrap(ui_manager_.get(), web_contents(), resource, NULL); | 336 new MalwareDetailsWrap(ui_manager_.get(), web_contents(), resource, NULL); |
332 | 337 |
333 std::string serialized = WaitForSerializedReport(report.get()); | 338 std::string serialized = WaitForSerializedReport(report.get()); |
334 | 339 |
335 ClientMalwareReportRequest actual; | 340 ClientMalwareReportRequest actual; |
336 actual.ParseFromString(serialized); | 341 actual.ParseFromString(serialized); |
337 | 342 |
338 ClientMalwareReportRequest expected; | 343 ClientMalwareReportRequest expected; |
339 expected.set_malware_url(kMalwareURL); | 344 expected.set_malware_url(kMalwareURL); |
340 expected.set_page_url(kLandingURL); | 345 expected.set_page_url(kLandingURL); |
341 expected.set_referrer_url(""); | 346 // Note that the referrer policy is not actually enacted here, since that's |
| 347 // done in Blink. |
| 348 expected.set_referrer_url(kReferrerURL); |
342 | 349 |
343 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | 350 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); |
344 pb_resource->set_id(0); | 351 pb_resource->set_id(0); |
345 pb_resource->set_url(kLandingURL); | 352 pb_resource->set_url(kLandingURL); |
346 pb_resource = expected.add_resources(); | 353 pb_resource = expected.add_resources(); |
347 pb_resource->set_id(1); | 354 pb_resource->set_id(1); |
348 pb_resource->set_url(kMalwareURL); | 355 pb_resource->set_url(kMalwareURL); |
| 356 pb_resource = expected.add_resources(); |
| 357 pb_resource->set_id(2); |
| 358 pb_resource->set_url(kReferrerURL); |
349 | 359 |
350 VerifyResults(actual, expected); | 360 VerifyResults(actual, expected); |
351 } | 361 } |
352 | 362 |
353 // Tests creating a simple malware report where the subresource has a | 363 // Tests creating a simple malware report where the subresource has a |
354 // different original_url. | 364 // different original_url. |
355 TEST_F(MalwareDetailsTest, MalwareSubResourceWithOriginalUrl) { | 365 TEST_F(MalwareDetailsTest, MalwareSubResourceWithOriginalUrl) { |
356 controller().LoadURL(GURL(kLandingURL), content::Referrer(), | 366 controller().LoadURL(GURL(kLandingURL), content::Referrer(), |
357 ui::PAGE_TRANSITION_TYPED, std::string()); | 367 ui::PAGE_TRANSITION_TYPED, std::string()); |
358 | 368 |
(...skipping 18 matching lines...) Expand all Loading... |
377 pb_resource->set_id(0); | 387 pb_resource->set_id(0); |
378 pb_resource->set_url(kLandingURL); | 388 pb_resource->set_url(kLandingURL); |
379 | 389 |
380 pb_resource = expected.add_resources(); | 390 pb_resource = expected.add_resources(); |
381 pb_resource->set_id(1); | 391 pb_resource->set_id(1); |
382 pb_resource->set_url(kOriginalLandingURL); | 392 pb_resource->set_url(kOriginalLandingURL); |
383 | 393 |
384 pb_resource = expected.add_resources(); | 394 pb_resource = expected.add_resources(); |
385 pb_resource->set_id(2); | 395 pb_resource->set_id(2); |
386 pb_resource->set_url(kMalwareURL); | 396 pb_resource->set_url(kMalwareURL); |
387 // The Resource for kMmalwareUrl should have the Resource for | 397 // The Resource for kMalwareUrl should have the Resource for |
388 // kOriginalLandingURL (with id 1) as parent. | 398 // kOriginalLandingURL (with id 1) as parent. |
389 pb_resource->set_parent_id(1); | 399 pb_resource->set_parent_id(1); |
390 | 400 |
391 VerifyResults(actual, expected); | 401 VerifyResults(actual, expected); |
392 } | 402 } |
393 | 403 |
394 // Tests creating a malware report with data from the renderer. | 404 // Tests creating a malware report with data from the renderer. |
395 TEST_F(MalwareDetailsTest, MalwareDOMDetails) { | 405 TEST_F(MalwareDetailsTest, MalwareDOMDetails) { |
396 controller().LoadURL(GURL(kLandingURL), content::Referrer(), | 406 controller().LoadURL(GURL(kLandingURL), content::Referrer(), |
397 ui::PAGE_TRANSITION_TYPED, std::string()); | 407 ui::PAGE_TRANSITION_TYPED, std::string()); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 | 449 |
440 pb_resource = expected.add_resources(); | 450 pb_resource = expected.add_resources(); |
441 pb_resource->set_id(3); | 451 pb_resource->set_id(3); |
442 pb_resource->set_url(kDOMParentURL); | 452 pb_resource->set_url(kDOMParentURL); |
443 pb_resource->add_child_ids(2); | 453 pb_resource->add_child_ids(2); |
444 expected.set_complete(false); // Since the cache was missing. | 454 expected.set_complete(false); // Since the cache was missing. |
445 | 455 |
446 VerifyResults(actual, expected); | 456 VerifyResults(actual, expected); |
447 } | 457 } |
448 | 458 |
449 // Verify that https:// urls are dropped. | |
450 TEST_F(MalwareDetailsTest, NotPublicUrl) { | |
451 controller().LoadURL(GURL(kHttpsURL), content::Referrer(), | |
452 ui::PAGE_TRANSITION_TYPED, std::string()); | |
453 UnsafeResource resource; | |
454 InitResource(&resource, true, GURL(kMalwareURL)); | |
455 scoped_refptr<MalwareDetailsWrap> report = new MalwareDetailsWrap( | |
456 ui_manager_.get(), web_contents(), resource, NULL); | |
457 | |
458 std::string serialized = WaitForSerializedReport(report.get()); | |
459 ClientMalwareReportRequest actual; | |
460 actual.ParseFromString(serialized); | |
461 | |
462 ClientMalwareReportRequest expected; | |
463 expected.set_malware_url(kMalwareURL); // No page_url | |
464 expected.set_referrer_url(""); | |
465 | |
466 ClientMalwareReportRequest::Resource* pb_resource = expected.add_resources(); | |
467 pb_resource->set_url(kMalwareURL); // Only one resource | |
468 | |
469 VerifyResults(actual, expected); | |
470 } | |
471 | |
472 // Tests creating a malware report where there are redirect urls to an unsafe | 459 // Tests creating a malware report where there are redirect urls to an unsafe |
473 // resource url | 460 // resource url |
474 TEST_F(MalwareDetailsTest, MalwareWithRedirectUrl) { | 461 TEST_F(MalwareDetailsTest, MalwareWithRedirectUrl) { |
475 controller().LoadURL(GURL(kLandingURL), content::Referrer(), | 462 controller().LoadURL(GURL(kLandingURL), content::Referrer(), |
476 ui::PAGE_TRANSITION_TYPED, std::string()); | 463 ui::PAGE_TRANSITION_TYPED, std::string()); |
477 | 464 |
478 UnsafeResource resource; | 465 UnsafeResource resource; |
479 InitResource(&resource, true, GURL(kMalwareURL)); | 466 InitResource(&resource, true, GURL(kMalwareURL)); |
480 resource.original_url = GURL(kOriginalLandingURL); | 467 resource.original_url = GURL(kOriginalLandingURL); |
481 | 468 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 pb_response->add_headers(); | 553 pb_response->add_headers(); |
567 pb_header->set_name("Content-Type"); | 554 pb_header->set_name("Content-Type"); |
568 pb_header->set_value("text/html"); | 555 pb_header->set_value("text/html"); |
569 pb_header = pb_response->add_headers(); | 556 pb_header = pb_response->add_headers(); |
570 pb_header->set_name("Content-Length"); | 557 pb_header->set_name("Content-Length"); |
571 pb_header->set_value("1024"); | 558 pb_header->set_value("1024"); |
572 pb_header = pb_response->add_headers(); | 559 pb_header = pb_response->add_headers(); |
573 pb_header->set_name("Set-Cookie"); | 560 pb_header->set_name("Set-Cookie"); |
574 pb_header->set_value(""); // The cookie is dropped. | 561 pb_header->set_value(""); // The cookie is dropped. |
575 pb_response->set_body(kLandingData); | 562 pb_response->set_body(kLandingData); |
576 pb_response->set_bodylength(37); | 563 pb_response->set_bodylength(47); |
577 pb_response->set_bodydigest("9ca97475598a79bc1e8fc9bd6c72cd35"); | 564 pb_response->set_bodydigest("5abb4e63d806ec2c16a40b2699700554"); |
578 pb_response->set_remote_ip("1.2.3.4:80"); | 565 pb_response->set_remote_ip("1.2.3.4:80"); |
579 | 566 |
580 pb_resource = expected.add_resources(); | 567 pb_resource = expected.add_resources(); |
581 pb_resource->set_id(1); | 568 pb_resource->set_id(1); |
582 pb_resource->set_url(kMalwareURL); | 569 pb_resource->set_url(kMalwareURL); |
583 pb_response = pb_resource->mutable_response(); | 570 pb_response = pb_resource->mutable_response(); |
584 pb_response->mutable_firstline()->set_code(200); | 571 pb_response->mutable_firstline()->set_code(200); |
585 pb_header = pb_response->add_headers(); | 572 pb_header = pb_response->add_headers(); |
586 pb_header->set_name("Content-Type"); | 573 pb_header->set_name("Content-Type"); |
587 pb_header->set_value("image/jpeg"); | 574 pb_header->set_value("image/jpeg"); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 pb_resource = expected.add_resources(); | 670 pb_resource = expected.add_resources(); |
684 pb_resource->set_id(2); | 671 pb_resource->set_id(2); |
685 pb_resource->set_parent_id(3); | 672 pb_resource->set_parent_id(3); |
686 pb_resource->set_url(kSecondRedirectURL); | 673 pb_resource->set_url(kSecondRedirectURL); |
687 pb_resource = expected.add_resources(); | 674 pb_resource = expected.add_resources(); |
688 pb_resource->set_id(3); | 675 pb_resource->set_id(3); |
689 pb_resource->set_url(kFirstRedirectURL); | 676 pb_resource->set_url(kFirstRedirectURL); |
690 | 677 |
691 VerifyResults(actual, expected); | 678 VerifyResults(actual, expected); |
692 } | 679 } |
OLD | NEW |