| 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/browser/safe_browsing/client_side_detection_host.h" | 5 #include "chrome/browser/safe_browsing/client_side_detection_host.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 ClientSideDetectionHost::ClientSideDetectionHost(WebContents* tab) | 253 ClientSideDetectionHost::ClientSideDetectionHost(WebContents* tab) |
| 254 : content::WebContentsObserver(tab), | 254 : content::WebContentsObserver(tab), |
| 255 csd_service_(NULL), | 255 csd_service_(NULL), |
| 256 weak_factory_(this), | 256 weak_factory_(this), |
| 257 unsafe_unique_page_id_(-1), | 257 unsafe_unique_page_id_(-1), |
| 258 malware_killswitch_on_(false), | 258 malware_killswitch_on_(false), |
| 259 malware_report_enabled_(false) { | 259 malware_report_enabled_(false) { |
| 260 DCHECK(tab); | 260 DCHECK(tab); |
| 261 // Note: csd_service_ and sb_service will be NULL here in testing. | 261 // Note: csd_service_ and sb_service will be NULL here in testing. |
| 262 csd_service_ = g_browser_process->safe_browsing_detection_service(); | 262 csd_service_ = g_browser_process->safe_browsing_detection_service(); |
| 263 feature_extractor_.reset(new BrowserFeatureExtractor(tab, csd_service_)); | 263 feature_extractor_.reset(new BrowserFeatureExtractor(tab, this)); |
| 264 registrar_.Add(this, content::NOTIFICATION_RESOURCE_RESPONSE_STARTED, | 264 registrar_.Add(this, content::NOTIFICATION_RESOURCE_RESPONSE_STARTED, |
| 265 content::Source<WebContents>(tab)); | 265 content::Source<WebContents>(tab)); |
| 266 | 266 |
| 267 scoped_refptr<SafeBrowsingService> sb_service = | 267 scoped_refptr<SafeBrowsingService> sb_service = |
| 268 g_browser_process->safe_browsing_service(); | 268 g_browser_process->safe_browsing_service(); |
| 269 if (sb_service.get()) { | 269 if (sb_service.get()) { |
| 270 ui_manager_ = sb_service->ui_manager(); | 270 ui_manager_ = sb_service->ui_manager(); |
| 271 database_manager_ = sb_service->database_manager(); | 271 database_manager_ = sb_service->database_manager(); |
| 272 ui_manager_->AddObserver(this); | 272 ui_manager_->AddObserver(this); |
| 273 } | 273 } |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 web_contents()->GetController().GetActiveEntry()) { | 354 web_contents()->GetController().GetActiveEntry()) { |
| 355 unsafe_unique_page_id_ = | 355 unsafe_unique_page_id_ = |
| 356 web_contents()->GetController().GetActiveEntry()->GetUniqueID(); | 356 web_contents()->GetController().GetActiveEntry()->GetUniqueID(); |
| 357 // We also keep the resource around in order to be able to send the | 357 // We also keep the resource around in order to be able to send the |
| 358 // malicious URL to the server. | 358 // malicious URL to the server. |
| 359 unsafe_resource_.reset(new SafeBrowsingUIManager::UnsafeResource(resource)); | 359 unsafe_resource_.reset(new SafeBrowsingUIManager::UnsafeResource(resource)); |
| 360 unsafe_resource_->callback.Reset(); // Don't do anything stupid. | 360 unsafe_resource_->callback.Reset(); // Don't do anything stupid. |
| 361 } | 361 } |
| 362 } | 362 } |
| 363 | 363 |
| 364 scoped_refptr<SafeBrowsingDatabaseManager> |
| 365 ClientSideDetectionHost::database_manager() { |
| 366 return database_manager_; |
| 367 } |
| 368 |
| 364 void ClientSideDetectionHost::WebContentsDestroyed(WebContents* tab) { | 369 void ClientSideDetectionHost::WebContentsDestroyed(WebContents* tab) { |
| 365 DCHECK(tab); | 370 DCHECK(tab); |
| 366 // Tell any pending classification request that it is being canceled. | 371 // Tell any pending classification request that it is being canceled. |
| 367 if (classification_request_.get()) { | 372 if (classification_request_.get()) { |
| 368 classification_request_->Cancel(); | 373 classification_request_->Cancel(); |
| 369 } | 374 } |
| 370 // Cancel all pending feature extractions. | 375 // Cancel all pending feature extractions. |
| 371 feature_extractor_.reset(); | 376 feature_extractor_.reset(); |
| 372 } | 377 } |
| 373 | 378 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 392 verdict->ParseFromString(verdict_str) && | 397 verdict->ParseFromString(verdict_str) && |
| 393 verdict->IsInitialized()) { | 398 verdict->IsInitialized()) { |
| 394 // We do the malware IP matching and request sending if the feature | 399 // We do the malware IP matching and request sending if the feature |
| 395 // is enabled. | 400 // is enabled. |
| 396 if (malware_report_enabled_ && !MalwareKillSwitchIsOn()) { | 401 if (malware_report_enabled_ && !MalwareKillSwitchIsOn()) { |
| 397 scoped_ptr<ClientMalwareRequest> malware_verdict( | 402 scoped_ptr<ClientMalwareRequest> malware_verdict( |
| 398 new ClientMalwareRequest); | 403 new ClientMalwareRequest); |
| 399 // Start browser-side malware feature extraction. Once we're done it will | 404 // Start browser-side malware feature extraction. Once we're done it will |
| 400 // send the malware client verdict request. | 405 // send the malware client verdict request. |
| 401 malware_verdict->set_url(verdict->url()); | 406 malware_verdict->set_url(verdict->url()); |
| 407 // This function doesn't expect browse_info_ to stay around after this |
| 408 // function returns. |
| 402 feature_extractor_->ExtractMalwareFeatures( | 409 feature_extractor_->ExtractMalwareFeatures( |
| 403 browse_info_.get(), malware_verdict.get()); | 410 browse_info_.get(), |
| 404 MalwareFeatureExtractionDone(malware_verdict.Pass()); | 411 malware_verdict.release(), |
| 412 base::Bind(&ClientSideDetectionHost::MalwareFeatureExtractionDone, |
| 413 weak_factory_.GetWeakPtr())); |
| 405 } | 414 } |
| 406 | 415 |
| 407 // We only send phishing verdict to the server if the verdict is phishing or | 416 // We only send phishing verdict to the server if the verdict is phishing or |
| 408 // if a SafeBrowsing interstitial was already shown for this site. E.g., a | 417 // if a SafeBrowsing interstitial was already shown for this site. E.g., a |
| 409 // malware or phishing interstitial was shown but the user clicked | 418 // malware or phishing interstitial was shown but the user clicked |
| 410 // through. | 419 // through. |
| 411 if (verdict->is_phishing() || DidShowSBInterstitial()) { | 420 if (verdict->is_phishing() || DidShowSBInterstitial()) { |
| 412 if (DidShowSBInterstitial()) { | 421 if (DidShowSBInterstitial()) { |
| 413 browse_info_->unsafe_resource.reset(unsafe_resource_.release()); | 422 browse_info_->unsafe_resource.reset(unsafe_resource_.release()); |
| 414 } | 423 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 } | 492 } |
| 484 // If there is true malware verdict, invalidate weakptr so that no longer | 493 // If there is true malware verdict, invalidate weakptr so that no longer |
| 485 // consider the phishing vedict. | 494 // consider the phishing vedict. |
| 486 weak_factory_.InvalidateWeakPtrs(); | 495 weak_factory_.InvalidateWeakPtrs(); |
| 487 } | 496 } |
| 488 } | 497 } |
| 489 | 498 |
| 490 void ClientSideDetectionHost::FeatureExtractionDone( | 499 void ClientSideDetectionHost::FeatureExtractionDone( |
| 491 bool success, | 500 bool success, |
| 492 ClientPhishingRequest* request) { | 501 ClientPhishingRequest* request) { |
| 493 if (!request) { | 502 DCHECK(request); |
| 494 DLOG(FATAL) << "Invalid request object in FeatureExtractionDone"; | |
| 495 return; | |
| 496 } | |
| 497 VLOG(2) << "Feature extraction done (success:" << success << ") for URL: " | 503 VLOG(2) << "Feature extraction done (success:" << success << ") for URL: " |
| 498 << request->url() << ". Start sending client phishing request."; | 504 << request->url() << ". Start sending client phishing request."; |
| 499 ClientSideDetectionService::ClientReportPhishingRequestCallback callback; | 505 ClientSideDetectionService::ClientReportPhishingRequestCallback callback; |
| 500 // If the client-side verdict isn't phishing we don't care about the server | 506 // If the client-side verdict isn't phishing we don't care about the server |
| 501 // response because we aren't going to display a warning. | 507 // response because we aren't going to display a warning. |
| 502 if (request->is_phishing()) { | 508 if (request->is_phishing()) { |
| 503 callback = base::Bind(&ClientSideDetectionHost::MaybeShowPhishingWarning, | 509 callback = base::Bind(&ClientSideDetectionHost::MaybeShowPhishingWarning, |
| 504 weak_factory_.GetWeakPtr()); | 510 weak_factory_.GetWeakPtr()); |
| 505 } | 511 } |
| 506 // Send ping even if the browser feature extraction failed. | 512 // Send ping even if the browser feature extraction failed. |
| 507 csd_service_->SendClientReportPhishingRequest( | 513 csd_service_->SendClientReportPhishingRequest( |
| 508 request, // The service takes ownership of the request object. | 514 request, // The service takes ownership of the request object. |
| 509 callback); | 515 callback); |
| 510 } | 516 } |
| 511 | 517 |
| 512 void ClientSideDetectionHost::MalwareFeatureExtractionDone( | 518 void ClientSideDetectionHost::MalwareFeatureExtractionDone( |
| 519 bool feature_extraction_success, |
| 513 scoped_ptr<ClientMalwareRequest> request) { | 520 scoped_ptr<ClientMalwareRequest> request) { |
| 514 if (!request) { | 521 DCHECK(request.get()); |
| 515 DLOG(FATAL) << "Invalid request object in MalwareFeatureExtractionDone"; | |
| 516 return; | |
| 517 } | |
| 518 VLOG(2) << "Malware Feature extraction done for URL: " << request->url() | 522 VLOG(2) << "Malware Feature extraction done for URL: " << request->url() |
| 519 << ", with features count:" << request->feature_map_size(); | 523 << ", with features count:" << request->feature_map_size(); |
| 520 | 524 |
| 521 // Send ping if there is matching features. | 525 // Send ping if there is matching features. |
| 522 if (request->feature_map_size() > 0) { | 526 if (feature_extraction_success && request->feature_map_size() > 0) { |
| 523 VLOG(1) << "Start sending client malware request."; | 527 VLOG(1) << "Start sending client malware request."; |
| 524 ClientSideDetectionService::ClientReportMalwareRequestCallback callback; | 528 ClientSideDetectionService::ClientReportMalwareRequestCallback callback; |
| 525 callback = base::Bind(&ClientSideDetectionHost::MaybeShowMalwareWarning, | 529 callback = base::Bind(&ClientSideDetectionHost::MaybeShowMalwareWarning, |
| 526 weak_factory_.GetWeakPtr()); | 530 weak_factory_.GetWeakPtr()); |
| 527 csd_service_->SendClientReportMalwareRequest( | 531 csd_service_->SendClientReportMalwareRequest(request.release(), callback); |
| 528 request.release(), // The service takes ownership of the request object | |
| 529 callback); | |
| 530 } | 532 } |
| 531 } | 533 } |
| 532 | 534 |
| 533 void ClientSideDetectionHost::UpdateIPUrlMap(const std::string& ip, | 535 void ClientSideDetectionHost::UpdateIPUrlMap(const std::string& ip, |
| 534 const std::string& url) { | 536 const std::string& url) { |
| 535 if (ip.empty() || url.empty()) | 537 if (ip.empty() || url.empty()) |
| 536 return; | 538 return; |
| 537 | 539 |
| 538 IPUrlMap::iterator it = browse_info_->ips.find(ip); | 540 IPUrlMap::iterator it = browse_info_->ips.find(ip); |
| 539 if (it == browse_info_->ips.end()) { | 541 if (it == browse_info_->ips.end()) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 595 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 597 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 596 return malware_killswitch_on_; | 598 return malware_killswitch_on_; |
| 597 } | 599 } |
| 598 | 600 |
| 599 void ClientSideDetectionHost::SetMalwareKillSwitch(bool killswitch_on) { | 601 void ClientSideDetectionHost::SetMalwareKillSwitch(bool killswitch_on) { |
| 600 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 602 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 601 malware_killswitch_on_ = killswitch_on; | 603 malware_killswitch_on_ = killswitch_on; |
| 602 } | 604 } |
| 603 | 605 |
| 604 } // namespace safe_browsing | 606 } // namespace safe_browsing |
| OLD | NEW |