| 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 "chrome/renderer/safe_browsing/phishing_dom_feature_extractor.h" | 5 #include "chrome/renderer/safe_browsing/phishing_dom_feature_extractor.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <unordered_map> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "chrome/renderer/chrome_content_renderer_client.h" | 16 #include "chrome/renderer/chrome_content_renderer_client.h" |
| 16 #include "chrome/renderer/safe_browsing/features.h" | 17 #include "chrome/renderer/safe_browsing/features.h" |
| 17 #include "chrome/renderer/safe_browsing/mock_feature_extractor_clock.h" | 18 #include "chrome/renderer/safe_browsing/mock_feature_extractor_clock.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 43 // CompleteURL() functions. This is to work around the fact that | 44 // CompleteURL() functions. This is to work around the fact that |
| 44 // ChromeRenderViewTest object does not know where the html content is hosted. | 45 // ChromeRenderViewTest object does not know where the html content is hosted. |
| 45 class TestPhishingDOMFeatureExtractor : public PhishingDOMFeatureExtractor { | 46 class TestPhishingDOMFeatureExtractor : public PhishingDOMFeatureExtractor { |
| 46 public: | 47 public: |
| 47 explicit TestPhishingDOMFeatureExtractor(FeatureExtractorClock* clock) | 48 explicit TestPhishingDOMFeatureExtractor(FeatureExtractorClock* clock) |
| 48 : PhishingDOMFeatureExtractor(clock) {} | 49 : PhishingDOMFeatureExtractor(clock) {} |
| 49 | 50 |
| 50 void SetDocumentDomain(std::string domain) { base_domain_ = domain; } | 51 void SetDocumentDomain(std::string domain) { base_domain_ = domain; } |
| 51 | 52 |
| 52 void SetURLToFrameDomainCheckingMap( | 53 void SetURLToFrameDomainCheckingMap( |
| 53 const base::hash_map<std::string, std::string>& checking_map) { | 54 const std::unordered_map<std::string, std::string>& checking_map) { |
| 54 url_to_frame_domain_map_ = checking_map; | 55 url_to_frame_domain_map_ = checking_map; |
| 55 } | 56 } |
| 56 | 57 |
| 57 void Reset() { | 58 void Reset() { |
| 58 base_domain_.clear(); | 59 base_domain_.clear(); |
| 59 url_to_frame_domain_map_.clear(); | 60 url_to_frame_domain_map_.clear(); |
| 60 } | 61 } |
| 61 | 62 |
| 62 private: | 63 private: |
| 63 // LoadHTML() function in RenderViewTest only loads html as data, | 64 // LoadHTML() function in RenderViewTest only loads html as data, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 return blink::WebURL(full_url); | 116 return blink::WebURL(full_url); |
| 116 } | 117 } |
| 117 | 118 |
| 118 // If there is only main frame, we use base_domain_ to track where | 119 // If there is only main frame, we use base_domain_ to track where |
| 119 // the html content is hosted. | 120 // the html content is hosted. |
| 120 std::string base_domain_; | 121 std::string base_domain_; |
| 121 | 122 |
| 122 // If html contains multiple frame/iframe, we track domain of each frame by | 123 // If html contains multiple frame/iframe, we track domain of each frame by |
| 123 // using this map, where keys are the urls mentioned in the html content, | 124 // using this map, where keys are the urls mentioned in the html content, |
| 124 // values are the domains of the corresponding frames. | 125 // values are the domains of the corresponding frames. |
| 125 base::hash_map<std::string, std::string> url_to_frame_domain_map_; | 126 std::unordered_map<std::string, std::string> url_to_frame_domain_map_; |
| 126 }; | 127 }; |
| 127 | 128 |
| 128 class TestChromeContentRendererClient : public ChromeContentRendererClient { | 129 class TestChromeContentRendererClient : public ChromeContentRendererClient { |
| 129 public: | 130 public: |
| 130 TestChromeContentRendererClient() {} | 131 TestChromeContentRendererClient() {} |
| 131 ~TestChromeContentRendererClient() override {} | 132 ~TestChromeContentRendererClient() override {} |
| 132 // Since visited_link_slave_ in ChromeContentRenderClient never get initiated, | 133 // Since visited_link_slave_ in ChromeContentRenderClient never get initiated, |
| 133 // overrides VisitedLinkedHash() function to prevent crashing. | 134 // overrides VisitedLinkedHash() function to prevent crashing. |
| 134 unsigned long long VisitedLinkHash(const char* canonical_url, | 135 unsigned long long VisitedLinkHash(const char* canonical_url, |
| 135 size_t length) override { | 136 size_t length) override { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 147 bool GetSuccess() { return success_; } | 148 bool GetSuccess() { return success_; } |
| 148 void ResetTest() { | 149 void ResetTest() { |
| 149 success_ = false; | 150 success_ = false; |
| 150 message_loop_ = new content::MessageLoopRunner; | 151 message_loop_ = new content::MessageLoopRunner; |
| 151 extractor_->Reset(); | 152 extractor_->Reset(); |
| 152 } | 153 } |
| 153 | 154 |
| 154 void ExtractFeaturesAcrossFrames( | 155 void ExtractFeaturesAcrossFrames( |
| 155 const std::string& html_content, | 156 const std::string& html_content, |
| 156 FeatureMap* features, | 157 FeatureMap* features, |
| 157 const base::hash_map<std::string, std::string>& url_frame_domain_map) { | 158 const std::unordered_map<std::string, std::string>& |
| 159 url_frame_domain_map) { |
| 158 extractor_->SetURLToFrameDomainCheckingMap(url_frame_domain_map); | 160 extractor_->SetURLToFrameDomainCheckingMap(url_frame_domain_map); |
| 159 LoadHTML(html_content.c_str()); | 161 LoadHTML(html_content.c_str()); |
| 160 | 162 |
| 161 extractor_->ExtractFeatures( | 163 extractor_->ExtractFeatures( |
| 162 GetMainFrame()->GetDocument(), features, | 164 GetMainFrame()->GetDocument(), features, |
| 163 base::Bind(&PhishingDOMFeatureExtractorTest::AnotherExtractionDone, | 165 base::Bind(&PhishingDOMFeatureExtractorTest::AnotherExtractionDone, |
| 164 weak_factory_.GetWeakPtr())); | 166 weak_factory_.GetWeakPtr())); |
| 165 message_loop_->Run(); | 167 message_loop_->Run(); |
| 166 } | 168 } |
| 167 | 169 |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 // A page with nested iframes. | 370 // A page with nested iframes. |
| 369 // html | 371 // html |
| 370 // iframe2 / \ iframe1 | 372 // iframe2 / \ iframe1 |
| 371 // \ iframe3 | 373 // \ iframe3 |
| 372 TEST_F(PhishingDOMFeatureExtractorTest, SubFrames) { | 374 TEST_F(PhishingDOMFeatureExtractorTest, SubFrames) { |
| 373 // This test doesn't exercise the extraction timing. | 375 // This test doesn't exercise the extraction timing. |
| 374 // Test that features are aggregated across all frames. | 376 // Test that features are aggregated across all frames. |
| 375 EXPECT_CALL(clock_, Now()).WillRepeatedly(Return(base::TimeTicks::Now())); | 377 EXPECT_CALL(clock_, Now()).WillRepeatedly(Return(base::TimeTicks::Now())); |
| 376 | 378 |
| 377 const char urlprefix[] = "data:text/html;charset=utf-8,"; | 379 const char urlprefix[] = "data:text/html;charset=utf-8,"; |
| 378 base::hash_map<std::string, std::string> url_iframe_map; | 380 std::unordered_map<std::string, std::string> url_iframe_map; |
| 379 std::string iframe1_nested_html( | 381 std::string iframe1_nested_html( |
| 380 "<html><body><input type=password>" | 382 "<html><body><input type=password>" |
| 381 "<a href=\"https://host3.com/submit\">link</a>" | 383 "<a href=\"https://host3.com/submit\">link</a>" |
| 382 "<a href=\"relative\">link</a>" | 384 "<a href=\"relative\">link</a>" |
| 383 "</body></html>"); | 385 "</body></html>"); |
| 384 GURL iframe1_nested_url(urlprefix + iframe1_nested_html); | 386 GURL iframe1_nested_url(urlprefix + iframe1_nested_html); |
| 385 // iframe1_nested is on host1.com. | 387 // iframe1_nested is on host1.com. |
| 386 url_iframe_map["https://host3.com/submit"] = "host1.com"; | 388 url_iframe_map["https://host3.com/submit"] = "host1.com"; |
| 387 url_iframe_map["relative"] = "host1.com"; | 389 url_iframe_map["relative"] = "host1.com"; |
| 388 | 390 |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 "<html><head></head><body>" | 571 "<html><head></head><body>" |
| 570 "<iframe src=\"" + | 572 "<iframe src=\"" + |
| 571 net::EscapeForHTML(iframe1_url.spec()) + | 573 net::EscapeForHTML(iframe1_url.spec()) + |
| 572 "\" id=\"frame1\"></iframe>" | 574 "\" id=\"frame1\"></iframe>" |
| 573 "<form></form></body></html>"); | 575 "<form></form></body></html>"); |
| 574 ExtractFeatures("host.com", html, &features); | 576 ExtractFeatures("host.com", html, &features); |
| 575 ExpectFeatureMapsAreEqual(features, expected_features); | 577 ExpectFeatureMapsAreEqual(features, expected_features); |
| 576 } | 578 } |
| 577 | 579 |
| 578 } // namespace safe_browsing | 580 } // namespace safe_browsing |
| OLD | NEW |