OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/browser/safe_browsing/browser_feature_extractor.h" | 5 #include "chrome/browser/safe_browsing/browser_feature_extractor.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
15 #include "chrome/common/safe_browsing/csd.pb.h" | 15 #include "chrome/common/safe_browsing/csd.pb.h" |
16 #include "chrome/browser/history/history.h" | 16 #include "chrome/browser/history/history.h" |
17 #include "chrome/browser/history/history_backend.h" | 17 #include "chrome/browser/history/history_backend.h" |
18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/safe_browsing/browser_features.h" | 19 #include "chrome/browser/safe_browsing/browser_features.h" |
20 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 20 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
21 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 21 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
22 #include "chrome/test/base/testing_profile.h" | 22 #include "chrome/test/base/testing_profile.h" |
23 #include "content/browser/browser_thread.h" | 23 #include "content/browser/browser_thread.h" |
24 #include "content/browser/tab_contents/tab_contents.h" | 24 #include "content/browser/tab_contents/tab_contents.h" |
25 #include "content/browser/tab_contents/test_tab_contents.h" | 25 #include "content/browser/tab_contents/test_tab_contents.h" |
26 #include "content/common/page_transition_types.h" | |
27 #include "content/common/view_messages.h" | 26 #include "content/common/view_messages.h" |
| 27 #include "content/public/common/page_transition_types.h" |
28 #include "crypto/sha2.h" | 28 #include "crypto/sha2.h" |
29 #include "googleurl/src/gurl.h" | 29 #include "googleurl/src/gurl.h" |
30 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
32 | 32 |
33 using ::testing::Return; | 33 using ::testing::Return; |
34 using ::testing::StrictMock; | 34 using ::testing::StrictMock; |
35 | 35 |
36 namespace safe_browsing { | 36 namespace safe_browsing { |
37 namespace { | 37 namespace { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 browse_info_->host_redirects = redirect_chain; | 77 browse_info_->host_redirects = redirect_chain; |
78 } | 78 } |
79 } | 79 } |
80 | 80 |
81 // Wrapper around NavigateAndCommit that also sets the redirect chain to | 81 // Wrapper around NavigateAndCommit that also sets the redirect chain to |
82 // a sane value. | 82 // a sane value. |
83 void SimpleNavigateAndCommit(const GURL& url) { | 83 void SimpleNavigateAndCommit(const GURL& url) { |
84 std::vector<GURL> redirect_chain; | 84 std::vector<GURL> redirect_chain; |
85 redirect_chain.push_back(url); | 85 redirect_chain.push_back(url); |
86 SetRedirectChain(redirect_chain, true); | 86 SetRedirectChain(redirect_chain, true); |
87 NavigateAndCommit(url, GURL(), PageTransition::LINK); | 87 NavigateAndCommit(url, GURL(), content::PAGE_TRANSITION_LINK); |
88 } | 88 } |
89 | 89 |
90 // This is similar to NavigateAndCommit that is in test_tab_contents, but | 90 // This is similar to NavigateAndCommit that is in test_tab_contents, but |
91 // allows us to specify the referrer and page_transition_type. | 91 // allows us to specify the referrer and page_transition_type. |
92 void NavigateAndCommit(const GURL& url, | 92 void NavigateAndCommit(const GURL& url, |
93 const GURL& referrer, | 93 const GURL& referrer, |
94 PageTransition::Type type) { | 94 content::PageTransition type) { |
95 contents()->controller().LoadURL(url, referrer, type, std::string()); | 95 contents()->controller().LoadURL(url, referrer, type, std::string()); |
96 | 96 |
97 static int page_id = 0; | 97 static int page_id = 0; |
98 ViewHostMsg_FrameNavigate_Params params; | 98 ViewHostMsg_FrameNavigate_Params params; |
99 InitNavigateParams(¶ms, ++page_id, url, type); | 99 InitNavigateParams(¶ms, ++page_id, url, type); |
100 params.referrer = referrer; | 100 params.referrer = referrer; |
101 | 101 |
102 RenderViewHost* rvh = contents()->pending_rvh(); | 102 RenderViewHost* rvh = contents()->pending_rvh(); |
103 if (!rvh) { | 103 if (!rvh) { |
104 rvh = contents()->render_view_host(); | 104 rvh = contents()->render_view_host(); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 history_service()->AddPage(GURL("http://www.foo.com/bar.html"), | 171 history_service()->AddPage(GURL("http://www.foo.com/bar.html"), |
172 history::SOURCE_BROWSED); | 172 history::SOURCE_BROWSED); |
173 history_service()->AddPage(GURL("https://www.foo.com/gaa.html"), | 173 history_service()->AddPage(GURL("https://www.foo.com/gaa.html"), |
174 history::SOURCE_BROWSED); // same host HTTPS. | 174 history::SOURCE_BROWSED); // same host HTTPS. |
175 history_service()->AddPage(GURL("http://www.foo.com/gaa.html"), | 175 history_service()->AddPage(GURL("http://www.foo.com/gaa.html"), |
176 history::SOURCE_BROWSED); // same host HTTP. | 176 history::SOURCE_BROWSED); // same host HTTP. |
177 history_service()->AddPage(GURL("http://bar.foo.com/gaa.html"), | 177 history_service()->AddPage(GURL("http://bar.foo.com/gaa.html"), |
178 history::SOURCE_BROWSED); // different host. | 178 history::SOURCE_BROWSED); // different host. |
179 history_service()->AddPage(GURL("http://www.foo.com/bar.html?a=b"), | 179 history_service()->AddPage(GURL("http://www.foo.com/bar.html?a=b"), |
180 base::Time::Now() - base::TimeDelta::FromHours(23), | 180 base::Time::Now() - base::TimeDelta::FromHours(23), |
181 NULL, 0, GURL(), PageTransition::LINK, | 181 NULL, 0, GURL(), content::PAGE_TRANSITION_LINK, |
182 history::RedirectList(), history::SOURCE_BROWSED, | 182 history::RedirectList(), history::SOURCE_BROWSED, |
183 false); | 183 false); |
184 history_service()->AddPage(GURL("http://www.foo.com/bar.html"), | 184 history_service()->AddPage(GURL("http://www.foo.com/bar.html"), |
185 base::Time::Now() - base::TimeDelta::FromHours(25), | 185 base::Time::Now() - base::TimeDelta::FromHours(25), |
186 NULL, 0, GURL(), PageTransition::TYPED, | 186 NULL, 0, GURL(), content::PAGE_TRANSITION_TYPED, |
187 history::RedirectList(), history::SOURCE_BROWSED, | 187 history::RedirectList(), history::SOURCE_BROWSED, |
188 false); | 188 false); |
189 history_service()->AddPage(GURL("https://www.foo.com/goo.html"), | 189 history_service()->AddPage(GURL("https://www.foo.com/goo.html"), |
190 base::Time::Now() - base::TimeDelta::FromDays(5), | 190 base::Time::Now() - base::TimeDelta::FromDays(5), |
191 NULL, 0, GURL(), PageTransition::TYPED, | 191 NULL, 0, GURL(), content::PAGE_TRANSITION_TYPED, |
192 history::RedirectList(), history::SOURCE_BROWSED, | 192 history::RedirectList(), history::SOURCE_BROWSED, |
193 false); | 193 false); |
194 | 194 |
195 SimpleNavigateAndCommit(GURL("http://www.foo.com/bar.html")); | 195 SimpleNavigateAndCommit(GURL("http://www.foo.com/bar.html")); |
196 | 196 |
197 ClientPhishingRequest request; | 197 ClientPhishingRequest request; |
198 request.set_url("http://www.foo.com/bar.html"); | 198 request.set_url("http://www.foo.com/bar.html"); |
199 request.set_client_score(0.5); | 199 request.set_client_score(0.5); |
200 EXPECT_TRUE(ExtractFeatures(&request)); | 200 EXPECT_TRUE(ExtractFeatures(&request)); |
201 std::map<std::string, double> features; | 201 std::map<std::string, double> features; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 | 268 |
269 ClientPhishingRequest request; | 269 ClientPhishingRequest request; |
270 request.set_url("http://www.foo.com/"); | 270 request.set_url("http://www.foo.com/"); |
271 request.set_client_score(0.5); | 271 request.set_client_score(0.5); |
272 std::vector<GURL> redirect_chain; | 272 std::vector<GURL> redirect_chain; |
273 redirect_chain.push_back(GURL("http://somerandomwebsite.com/")); | 273 redirect_chain.push_back(GURL("http://somerandomwebsite.com/")); |
274 redirect_chain.push_back(GURL("http://www.foo.com/")); | 274 redirect_chain.push_back(GURL("http://www.foo.com/")); |
275 SetRedirectChain(redirect_chain, true); | 275 SetRedirectChain(redirect_chain, true); |
276 NavigateAndCommit(GURL("http://www.foo.com/"), | 276 NavigateAndCommit(GURL("http://www.foo.com/"), |
277 GURL("http://google.com/"), | 277 GURL("http://google.com/"), |
278 PageTransition::AUTO_BOOKMARK | | 278 content::PageTransitionFromInt( |
279 PageTransition::FORWARD_BACK); | 279 content::PAGE_TRANSITION_AUTO_BOOKMARK | |
| 280 content::PAGE_TRANSITION_FORWARD_BACK)); |
280 | 281 |
281 EXPECT_TRUE(ExtractFeatures(&request)); | 282 EXPECT_TRUE(ExtractFeatures(&request)); |
282 std::map<std::string, double> features; | 283 std::map<std::string, double> features; |
283 GetFeatureMap(request, &features); | 284 GetFeatureMap(request, &features); |
284 | 285 |
285 EXPECT_EQ(1.0, | 286 EXPECT_EQ(1.0, |
286 features[StringPrintf("%s=%s", | 287 features[StringPrintf("%s=%s", |
287 features::kReferrer, | 288 features::kReferrer, |
288 "http://google.com/")]); | 289 "http://google.com/")]); |
289 EXPECT_EQ(1.0, | 290 EXPECT_EQ(1.0, |
(...skipping 13 matching lines...) Expand all Loading... |
303 request.Clear(); | 304 request.Clear(); |
304 request.set_url("http://www.foo.com/page.html"); | 305 request.set_url("http://www.foo.com/page.html"); |
305 request.set_client_score(0.5); | 306 request.set_client_score(0.5); |
306 redirect_chain.clear(); | 307 redirect_chain.clear(); |
307 redirect_chain.push_back(GURL("http://www.foo.com/redirect")); | 308 redirect_chain.push_back(GURL("http://www.foo.com/redirect")); |
308 redirect_chain.push_back(GURL("http://www.foo.com/second_redirect")); | 309 redirect_chain.push_back(GURL("http://www.foo.com/second_redirect")); |
309 redirect_chain.push_back(GURL("http://www.foo.com/page.html")); | 310 redirect_chain.push_back(GURL("http://www.foo.com/page.html")); |
310 SetRedirectChain(redirect_chain, false); | 311 SetRedirectChain(redirect_chain, false); |
311 NavigateAndCommit(GURL("http://www.foo.com/page.html"), | 312 NavigateAndCommit(GURL("http://www.foo.com/page.html"), |
312 GURL("http://www.foo.com"), | 313 GURL("http://www.foo.com"), |
313 PageTransition::TYPED | | 314 content::PageTransitionFromInt( |
314 PageTransition::CHAIN_START | | 315 content::PAGE_TRANSITION_TYPED | |
315 PageTransition::CLIENT_REDIRECT); | 316 content::PAGE_TRANSITION_CHAIN_START | |
| 317 content::PAGE_TRANSITION_CLIENT_REDIRECT)); |
316 | 318 |
317 EXPECT_TRUE(ExtractFeatures(&request)); | 319 EXPECT_TRUE(ExtractFeatures(&request)); |
318 features.clear(); | 320 features.clear(); |
319 GetFeatureMap(request, &features); | 321 GetFeatureMap(request, &features); |
320 | 322 |
321 EXPECT_EQ(1, | 323 EXPECT_EQ(1, |
322 features[StringPrintf("%s=%s", | 324 features[StringPrintf("%s=%s", |
323 features::kReferrer, | 325 features::kReferrer, |
324 "http://www.foo.com/")]); | 326 "http://www.foo.com/")]); |
325 EXPECT_EQ(1.0, | 327 EXPECT_EQ(1.0, |
(...skipping 28 matching lines...) Expand all Loading... |
354 | 356 |
355 request.Clear(); | 357 request.Clear(); |
356 request.set_url("http://www.bar.com/"); | 358 request.set_url("http://www.bar.com/"); |
357 request.set_client_score(0.5); | 359 request.set_client_score(0.5); |
358 redirect_chain.clear(); | 360 redirect_chain.clear(); |
359 redirect_chain.push_back(GURL("http://www.foo.com/page.html")); | 361 redirect_chain.push_back(GURL("http://www.foo.com/page.html")); |
360 redirect_chain.push_back(GURL("http://www.bar.com/")); | 362 redirect_chain.push_back(GURL("http://www.bar.com/")); |
361 SetRedirectChain(redirect_chain, true); | 363 SetRedirectChain(redirect_chain, true); |
362 NavigateAndCommit(GURL("http://www.bar.com/"), | 364 NavigateAndCommit(GURL("http://www.bar.com/"), |
363 GURL("http://www.foo.com/page.html"), | 365 GURL("http://www.foo.com/page.html"), |
364 PageTransition::LINK | | 366 content::PageTransitionFromInt( |
365 PageTransition::CHAIN_END | | 367 content::PAGE_TRANSITION_LINK | |
366 PageTransition::CLIENT_REDIRECT); | 368 content::PAGE_TRANSITION_CHAIN_END | |
| 369 content::PAGE_TRANSITION_CLIENT_REDIRECT)); |
367 | 370 |
368 EXPECT_TRUE(ExtractFeatures(&request)); | 371 EXPECT_TRUE(ExtractFeatures(&request)); |
369 features.clear(); | 372 features.clear(); |
370 GetFeatureMap(request, &features); | 373 GetFeatureMap(request, &features); |
371 | 374 |
372 EXPECT_EQ(1.0, | 375 EXPECT_EQ(1.0, |
373 features[StringPrintf("%s=%s", | 376 features[StringPrintf("%s=%s", |
374 features::kReferrer, | 377 features::kReferrer, |
375 "http://www.foo.com/page.html")]); | 378 "http://www.foo.com/page.html")]); |
376 EXPECT_EQ(1.0, | 379 EXPECT_EQ(1.0, |
(...skipping 15 matching lines...) Expand all Loading... |
392 features::kIsFirstNavigation))); | 395 features::kIsFirstNavigation))); |
393 | 396 |
394 request.Clear(); | 397 request.Clear(); |
395 request.set_url("http://www.bar.com/other_page.html"); | 398 request.set_url("http://www.bar.com/other_page.html"); |
396 request.set_client_score(0.5); | 399 request.set_client_score(0.5); |
397 redirect_chain.clear(); | 400 redirect_chain.clear(); |
398 redirect_chain.push_back(GURL("http://www.bar.com/other_page.html")); | 401 redirect_chain.push_back(GURL("http://www.bar.com/other_page.html")); |
399 SetRedirectChain(redirect_chain, false); | 402 SetRedirectChain(redirect_chain, false); |
400 NavigateAndCommit(GURL("http://www.bar.com/other_page.html"), | 403 NavigateAndCommit(GURL("http://www.bar.com/other_page.html"), |
401 GURL("http://www.bar.com/"), | 404 GURL("http://www.bar.com/"), |
402 PageTransition::LINK); | 405 content::PAGE_TRANSITION_LINK); |
403 | 406 |
404 EXPECT_TRUE(ExtractFeatures(&request)); | 407 EXPECT_TRUE(ExtractFeatures(&request)); |
405 features.clear(); | 408 features.clear(); |
406 GetFeatureMap(request, &features); | 409 GetFeatureMap(request, &features); |
407 | 410 |
408 EXPECT_EQ(1.0, | 411 EXPECT_EQ(1.0, |
409 features[StringPrintf("%s=%s", | 412 features[StringPrintf("%s=%s", |
410 features::kReferrer, | 413 features::kReferrer, |
411 "http://www.bar.com/")]); | 414 "http://www.bar.com/")]); |
412 EXPECT_EQ(0.0, features[features::kHasSSLReferrer]); | 415 EXPECT_EQ(0.0, features[features::kHasSSLReferrer]); |
(...skipping 19 matching lines...) Expand all Loading... |
432 features::kIsFirstNavigation)]); | 435 features::kIsFirstNavigation)]); |
433 request.Clear(); | 436 request.Clear(); |
434 request.set_url("http://www.baz.com/"); | 437 request.set_url("http://www.baz.com/"); |
435 request.set_client_score(0.5); | 438 request.set_client_score(0.5); |
436 redirect_chain.clear(); | 439 redirect_chain.clear(); |
437 redirect_chain.push_back(GURL("https://bankofamerica.com")); | 440 redirect_chain.push_back(GURL("https://bankofamerica.com")); |
438 redirect_chain.push_back(GURL("http://www.baz.com/")); | 441 redirect_chain.push_back(GURL("http://www.baz.com/")); |
439 SetRedirectChain(redirect_chain, true); | 442 SetRedirectChain(redirect_chain, true); |
440 NavigateAndCommit(GURL("http://www.baz.com"), | 443 NavigateAndCommit(GURL("http://www.baz.com"), |
441 GURL("https://bankofamerica.com"), | 444 GURL("https://bankofamerica.com"), |
442 PageTransition::GENERATED); | 445 content::PAGE_TRANSITION_GENERATED); |
443 | 446 |
444 browse_info_->ips.insert("193.5.163.8"); | 447 browse_info_->ips.insert("193.5.163.8"); |
445 browse_info_->ips.insert("23.94.78.1"); | 448 browse_info_->ips.insert("23.94.78.1"); |
446 EXPECT_CALL(*service_, IsBadIpAddress("193.5.163.8")).WillOnce(Return(true)); | 449 EXPECT_CALL(*service_, IsBadIpAddress("193.5.163.8")).WillOnce(Return(true)); |
447 EXPECT_CALL(*service_, IsBadIpAddress("23.94.78.1")).WillOnce(Return(false)); | 450 EXPECT_CALL(*service_, IsBadIpAddress("23.94.78.1")).WillOnce(Return(false)); |
448 | 451 |
449 EXPECT_TRUE(ExtractFeatures(&request)); | 452 EXPECT_TRUE(ExtractFeatures(&request)); |
450 features.clear(); | 453 features.clear(); |
451 GetFeatureMap(request, &features); | 454 GetFeatureMap(request, &features); |
452 | 455 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
537 history_service()->AddPage(GURL("http://www.host.com/A%21//B"), | 540 history_service()->AddPage(GURL("http://www.host.com/A%21//B"), |
538 history::SOURCE_BROWSED); | 541 history::SOURCE_BROWSED); |
539 SimpleNavigateAndCommit(GURL("http://www.host.com/A%21//B")); | 542 SimpleNavigateAndCommit(GURL("http://www.host.com/A%21//B")); |
540 | 543 |
541 EXPECT_TRUE(ExtractFeatures(&request)); | 544 EXPECT_TRUE(ExtractFeatures(&request)); |
542 EXPECT_EQ(crypto::SHA256HashString("host.com/a!/").substr( | 545 EXPECT_EQ(crypto::SHA256HashString("host.com/a!/").substr( |
543 0, BrowserFeatureExtractor::kHashPrefixLength), | 546 0, BrowserFeatureExtractor::kHashPrefixLength), |
544 request.hash_prefix()); | 547 request.hash_prefix()); |
545 } | 548 } |
546 } // namespace safe_browsing | 549 } // namespace safe_browsing |
OLD | NEW |