| 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/download_protection_service.h" | 5 #include "chrome/browser/safe_browsing/download_protection_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 16 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 17 #include "chrome/browser/safe_browsing/signature_util.h" | 17 #include "chrome/browser/safe_browsing/signature_util.h" |
| 18 #include "chrome/common/net/http_return.h" | 18 #include "chrome/common/net/http_return.h" |
| 19 #include "chrome/common/safe_browsing/csd.pb.h" | 19 #include "chrome/common/safe_browsing/csd.pb.h" |
| 20 #include "content/browser/download/download_item.h" | 20 #include "content/browser/download/download_item.h" |
| 21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 22 #include "content/public/common/url_fetcher.h" | 22 #include "content/public/common/url_fetcher.h" |
| 23 #include "content/public/common/url_fetcher_delegate.h" | 23 #include "content/public/common/url_fetcher_delegate.h" |
| 24 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
| 25 #include "net/url_request/url_request_context_getter.h" | 25 #include "net/url_request/url_request_context_getter.h" |
| 26 #include "net/url_request/url_request_status.h" | 26 #include "net/url_request/url_request_status.h" |
| 27 | 27 |
| 28 using content::BrowserThread; | 28 using content::BrowserThread; |
| 29 | 29 |
| 30 namespace { |
| 31 static const int64 kDownloadRequestTimeoutMs = 3000; |
| 32 } // namespace |
| 33 |
| 30 namespace safe_browsing { | 34 namespace safe_browsing { |
| 31 | 35 |
| 32 const char DownloadProtectionService::kDownloadRequestUrl[] = | 36 const char DownloadProtectionService::kDownloadRequestUrl[] = |
| 33 "https://sb-ssl.google.com/safebrowsing/clientreport/download"; | 37 "https://sb-ssl.google.com/safebrowsing/clientreport/download"; |
| 34 | 38 |
| 35 namespace { | 39 namespace { |
| 36 bool IsBinaryFile(const FilePath& file) { | 40 bool IsBinaryFile(const FilePath& file) { |
| 37 return (file.MatchesExtension(FILE_PATH_LITERAL(".exe")) || | 41 return (file.MatchesExtension(FILE_PATH_LITERAL(".exe")) || |
| 38 file.MatchesExtension(FILE_PATH_LITERAL(".cab")) || | 42 file.MatchesExtension(FILE_PATH_LITERAL(".cab")) || |
| 39 file.MatchesExtension(FILE_PATH_LITERAL(".msi"))); | 43 file.MatchesExtension(FILE_PATH_LITERAL(".msi"))); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 | 141 |
| 138 // static | 142 // static |
| 139 DownloadProtectionService::DownloadInfo | 143 DownloadProtectionService::DownloadInfo |
| 140 DownloadProtectionService::DownloadInfo::FromDownloadItem( | 144 DownloadProtectionService::DownloadInfo::FromDownloadItem( |
| 141 const DownloadItem& item) { | 145 const DownloadItem& item) { |
| 142 DownloadInfo download_info; | 146 DownloadInfo download_info; |
| 143 download_info.local_file = item.full_path(); | 147 download_info.local_file = item.full_path(); |
| 144 download_info.target_file = item.GetTargetFilePath(); | 148 download_info.target_file = item.GetTargetFilePath(); |
| 145 download_info.download_url_chain = item.url_chain(); | 149 download_info.download_url_chain = item.url_chain(); |
| 146 download_info.referrer_url = item.referrer_url(); | 150 download_info.referrer_url = item.referrer_url(); |
| 147 // TODO(bryner): Fill in the hash (we shouldn't compute it again) | 151 download_info.sha256_hash = item.hash(); |
| 148 download_info.total_bytes = item.total_bytes(); | 152 download_info.total_bytes = item.total_bytes(); |
| 149 // TODO(bryner): Populate user_initiated | 153 // TODO(bryner): Populate user_initiated |
| 150 return download_info; | 154 return download_info; |
| 151 } | 155 } |
| 152 | 156 |
| 153 // Parent SafeBrowsing::Client class used to lookup the bad binary | 157 // Parent SafeBrowsing::Client class used to lookup the bad binary |
| 154 // URL and digest list. There are two sub-classes (one for each list). | 158 // URL and digest list. There are two sub-classes (one for each list). |
| 155 class DownloadSBClient | 159 class DownloadSBClient |
| 156 : public SafeBrowsingService::Client, | 160 : public SafeBrowsingService::Client, |
| 157 public base::RefCountedThreadSafe<DownloadSBClient> { | 161 public base::RefCountedThreadSafe<DownloadSBClient> { |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 325 CheckClientDownloadRequest(const DownloadInfo& info, | 329 CheckClientDownloadRequest(const DownloadInfo& info, |
| 326 const CheckDownloadCallback& callback, | 330 const CheckDownloadCallback& callback, |
| 327 DownloadProtectionService* service, | 331 DownloadProtectionService* service, |
| 328 SafeBrowsingService* sb_service, | 332 SafeBrowsingService* sb_service, |
| 329 SignatureUtil* signature_util) | 333 SignatureUtil* signature_util) |
| 330 : info_(info), | 334 : info_(info), |
| 331 callback_(callback), | 335 callback_(callback), |
| 332 service_(service), | 336 service_(service), |
| 333 signature_util_(signature_util), | 337 signature_util_(signature_util), |
| 334 sb_service_(sb_service), | 338 sb_service_(sb_service), |
| 335 pingback_enabled_(service_->enabled()) { | 339 pingback_enabled_(service_->enabled()), |
| 340 finished_(false) { |
| 336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 341 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 337 } | 342 } |
| 338 | 343 |
| 339 void Start() { | 344 void Start() { |
| 340 VLOG(2) << "Starting SafeBrowsing download check for: " | 345 VLOG(2) << "Starting SafeBrowsing download check for: " |
| 341 << info_.DebugString(); | 346 << info_.DebugString(); |
| 342 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 347 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 343 // TODO(noelutz): implement some cache to make sure we don't issue the same | 348 // TODO(noelutz): implement some cache to make sure we don't issue the same |
| 344 // request over and over again if a user downloads the same binary multiple | 349 // request over and over again if a user downloads the same binary multiple |
| 345 // times. | 350 // times. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 359 if (final_url.SchemeIs("https") || !IsBinaryFile(info_.target_file)) { | 364 if (final_url.SchemeIs("https") || !IsBinaryFile(info_.target_file)) { |
| 360 RecordImprovedProtectionStats(final_url.SchemeIs("https") ? | 365 RecordImprovedProtectionStats(final_url.SchemeIs("https") ? |
| 361 REASON_HTTPS_URL : REASON_NOT_BINARY_FILE); | 366 REASON_HTTPS_URL : REASON_NOT_BINARY_FILE); |
| 362 BrowserThread::PostTask( | 367 BrowserThread::PostTask( |
| 363 BrowserThread::IO, | 368 BrowserThread::IO, |
| 364 FROM_HERE, | 369 FROM_HERE, |
| 365 base::Bind(&CheckClientDownloadRequest::CheckDigestList, this)); | 370 base::Bind(&CheckClientDownloadRequest::CheckDigestList, this)); |
| 366 return; | 371 return; |
| 367 } | 372 } |
| 368 | 373 |
| 374 // If the request takes too long we cancel it. |
| 375 BrowserThread::PostDelayedTask( |
| 376 BrowserThread::UI, |
| 377 FROM_HERE, |
| 378 base::Bind(&CheckClientDownloadRequest::Cancel, this), |
| 379 service_->download_request_timeout_ms()); |
| 380 |
| 369 // Compute features from the file contents. Note that we record histograms | 381 // Compute features from the file contents. Note that we record histograms |
| 370 // based on the result, so this runs regardless of whether the pingbacks | 382 // based on the result, so this runs regardless of whether the pingbacks |
| 371 // are enabled. Since we do blocking I/O, this happens on the file thread. | 383 // are enabled. Since we do blocking I/O, this happens on the file thread. |
| 372 BrowserThread::PostTask( | 384 BrowserThread::PostTask( |
| 373 BrowserThread::FILE, | 385 BrowserThread::FILE, |
| 374 FROM_HERE, | 386 FROM_HERE, |
| 375 base::Bind(&CheckClientDownloadRequest::ExtractFileFeatures, this)); | 387 base::Bind(&CheckClientDownloadRequest::ExtractFileFeatures, this)); |
| 376 } | 388 } |
| 377 | 389 |
| 378 // Canceling a request will cause us to always report the result as SAFE. | 390 // Canceling a request will cause us to always report the result as SAFE |
| 379 // In addition, the DownloadProtectionService will not be notified when the | 391 // unless a pending request is about to call FinishRequest. |
| 380 // request finishes, so it must drop its reference after calling Cancel. | |
| 381 void Cancel() { | 392 void Cancel() { |
| 382 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 393 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 383 service_ = NULL; | |
| 384 if (fetcher_.get()) { | 394 if (fetcher_.get()) { |
| 385 // The DownloadProtectionService is going to release its reference, so we | 395 // The DownloadProtectionService is going to release its reference, so we |
| 386 // might be destroyed before the URLFetcher completes. Cancel the | 396 // might be destroyed before the URLFetcher completes. Cancel the |
| 387 // fetcher so it does not try to invoke OnURLFetchComplete. | 397 // fetcher so it does not try to invoke OnURLFetchComplete. |
| 388 FinishRequest(SAFE); | 398 FinishRequest(SAFE); |
| 389 fetcher_.reset(); | 399 fetcher_.reset(); |
| 390 } | 400 } |
| 391 // Note: If there is no fetcher, then some callback is still holding a | 401 // Note: If there is no fetcher, then some callback is still holding a |
| 392 // reference to this object. We'll eventually wind up in some method on | 402 // reference to this object. We'll eventually wind up in some method on |
| 393 // the UI thread that will call FinishRequest() and run the callback. | 403 // the UI thread that will call FinishRequest() and run the callback. |
| 404 service_ = NULL; |
| 394 } | 405 } |
| 395 | 406 |
| 396 // From the content::URLFetcherDelegate interface. | 407 // From the content::URLFetcherDelegate interface. |
| 397 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE { | 408 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE { |
| 398 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 409 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 399 DCHECK_EQ(source, fetcher_.get()); | 410 DCHECK_EQ(source, fetcher_.get()); |
| 400 VLOG(2) << "Received a response for URL: " | 411 VLOG(2) << "Received a response for URL: " |
| 401 << info_.download_url_chain.back() << ": success=" | 412 << info_.download_url_chain.back() << ": success=" |
| 402 << source->GetStatus().is_success() << " response_code=" | 413 << source->GetStatus().is_success() << " response_code=" |
| 403 << source->GetResponseCode(); | 414 << source->GetResponseCode(); |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 return; | 556 return; |
| 546 } | 557 } |
| 547 | 558 |
| 548 VLOG(2) << "Sending a request for URL: " | 559 VLOG(2) << "Sending a request for URL: " |
| 549 << info_.download_url_chain.back(); | 560 << info_.download_url_chain.back(); |
| 550 fetcher_.reset(content::URLFetcher::Create(0 /* ID used for testing */, | 561 fetcher_.reset(content::URLFetcher::Create(0 /* ID used for testing */, |
| 551 GURL(kDownloadRequestUrl), | 562 GURL(kDownloadRequestUrl), |
| 552 content::URLFetcher::POST, | 563 content::URLFetcher::POST, |
| 553 this)); | 564 this)); |
| 554 fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 565 fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 566 fetcher_->SetAutomaticallyRetryOn5xx(false); // Don't retry on error. |
| 555 fetcher_->SetRequestContext(service_->request_context_getter_.get()); | 567 fetcher_->SetRequestContext(service_->request_context_getter_.get()); |
| 556 fetcher_->SetUploadData("application/octet-stream", request_data); | 568 fetcher_->SetUploadData("application/octet-stream", request_data); |
| 557 fetcher_->Start(); | 569 fetcher_->Start(); |
| 558 } | 570 } |
| 559 | 571 |
| 560 void PostFinishTask(DownloadCheckResult result) { | 572 void PostFinishTask(DownloadCheckResult result) { |
| 561 BrowserThread::PostTask( | 573 BrowserThread::PostTask( |
| 562 BrowserThread::UI, | 574 BrowserThread::UI, |
| 563 FROM_HERE, | 575 FROM_HERE, |
| 564 base::Bind(&CheckClientDownloadRequest::FinishRequest, this, result)); | 576 base::Bind(&CheckClientDownloadRequest::FinishRequest, this, result)); |
| 565 } | 577 } |
| 566 | 578 |
| 567 void FinishRequest(DownloadCheckResult result) { | 579 void FinishRequest(DownloadCheckResult result) { |
| 568 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 580 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 581 if (finished_) { |
| 582 return; |
| 583 } |
| 584 finished_ = true; |
| 569 if (service_) { | 585 if (service_) { |
| 570 callback_.Run(result); | 586 callback_.Run(result); |
| 571 service_->RequestFinished(this); | 587 service_->RequestFinished(this); |
| 572 } else { | 588 } else { |
| 573 callback_.Run(SAFE); | 589 callback_.Run(SAFE); |
| 574 } | 590 } |
| 575 } | 591 } |
| 576 | 592 |
| 577 void RecordImprovedProtectionStats(DownloadCheckResultReason reason) { | 593 void RecordImprovedProtectionStats(DownloadCheckResultReason reason) { |
| 578 UMA_HISTOGRAM_ENUMERATION("SBClientDownload.CheckDownloadStats", | 594 UMA_HISTOGRAM_ENUMERATION("SBClientDownload.CheckDownloadStats", |
| 579 reason, | 595 reason, |
| 580 REASON_MAX); | 596 REASON_MAX); |
| 581 } | 597 } |
| 582 | 598 |
| 583 DownloadInfo info_; | 599 DownloadInfo info_; |
| 584 ClientDownloadRequest_SignatureInfo signature_info_; | 600 ClientDownloadRequest_SignatureInfo signature_info_; |
| 585 CheckDownloadCallback callback_; | 601 CheckDownloadCallback callback_; |
| 586 // Will be NULL if the request has been canceled. | 602 // Will be NULL if the request has been canceled. |
| 587 DownloadProtectionService* service_; | 603 DownloadProtectionService* service_; |
| 588 scoped_refptr<SignatureUtil> signature_util_; | 604 scoped_refptr<SignatureUtil> signature_util_; |
| 589 scoped_refptr<SafeBrowsingService> sb_service_; | 605 scoped_refptr<SafeBrowsingService> sb_service_; |
| 590 const bool pingback_enabled_; | 606 const bool pingback_enabled_; |
| 591 scoped_ptr<content::URLFetcher> fetcher_; | 607 scoped_ptr<content::URLFetcher> fetcher_; |
| 608 bool finished_; |
| 592 | 609 |
| 593 DISALLOW_COPY_AND_ASSIGN(CheckClientDownloadRequest); | 610 DISALLOW_COPY_AND_ASSIGN(CheckClientDownloadRequest); |
| 594 }; | 611 }; |
| 595 | 612 |
| 596 DownloadProtectionService::DownloadProtectionService( | 613 DownloadProtectionService::DownloadProtectionService( |
| 597 SafeBrowsingService* sb_service, | 614 SafeBrowsingService* sb_service, |
| 598 net::URLRequestContextGetter* request_context_getter) | 615 net::URLRequestContextGetter* request_context_getter) |
| 599 : sb_service_(sb_service), | 616 : sb_service_(sb_service), |
| 600 request_context_getter_(request_context_getter), | 617 request_context_getter_(request_context_getter), |
| 601 enabled_(false), | 618 enabled_(false), |
| 602 signature_util_(new SignatureUtil()) {} | 619 signature_util_(new SignatureUtil()), |
| 620 download_request_timeout_ms_(kDownloadRequestTimeoutMs) {} |
| 603 | 621 |
| 604 DownloadProtectionService::~DownloadProtectionService() { | 622 DownloadProtectionService::~DownloadProtectionService() { |
| 605 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 623 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 606 CancelPendingRequests(); | 624 CancelPendingRequests(); |
| 607 } | 625 } |
| 608 | 626 |
| 609 void DownloadProtectionService::SetEnabled(bool enabled) { | 627 void DownloadProtectionService::SetEnabled(bool enabled) { |
| 610 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 628 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 611 if (enabled == enabled_) { | 629 if (enabled == enabled_) { |
| 612 return; | 630 return; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 637 BrowserThread::PostTask( | 655 BrowserThread::PostTask( |
| 638 BrowserThread::IO, | 656 BrowserThread::IO, |
| 639 FROM_HERE, | 657 FROM_HERE, |
| 640 base::Bind(&DownloadUrlSBClient::StartCheck, client)); | 658 base::Bind(&DownloadUrlSBClient::StartCheck, client)); |
| 641 } | 659 } |
| 642 | 660 |
| 643 void DownloadProtectionService::CancelPendingRequests() { | 661 void DownloadProtectionService::CancelPendingRequests() { |
| 644 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 662 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 645 for (std::set<scoped_refptr<CheckClientDownloadRequest> >::iterator it = | 663 for (std::set<scoped_refptr<CheckClientDownloadRequest> >::iterator it = |
| 646 download_requests_.begin(); | 664 download_requests_.begin(); |
| 647 it != download_requests_.end(); ++it) { | 665 it != download_requests_.end();) { |
| 648 (*it)->Cancel(); | 666 // We need to advance the iterator before we cancel because canceling |
| 667 // the request will invalidate it when RequestFinished is called below. |
| 668 scoped_refptr<CheckClientDownloadRequest> tmp = *it++; |
| 669 tmp->Cancel(); |
| 649 } | 670 } |
| 650 download_requests_.clear(); | 671 DCHECK(download_requests_.empty()); |
| 651 } | 672 } |
| 652 | 673 |
| 653 void DownloadProtectionService::RequestFinished( | 674 void DownloadProtectionService::RequestFinished( |
| 654 CheckClientDownloadRequest* request) { | 675 CheckClientDownloadRequest* request) { |
| 655 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 676 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 656 std::set<scoped_refptr<CheckClientDownloadRequest> >::iterator it = | 677 std::set<scoped_refptr<CheckClientDownloadRequest> >::iterator it = |
| 657 download_requests_.find(request); | 678 download_requests_.find(request); |
| 658 DCHECK(it != download_requests_.end()); | 679 DCHECK(it != download_requests_.end()); |
| 659 download_requests_.erase(*it); | 680 download_requests_.erase(*it); |
| 660 } | 681 } |
| 661 } // namespace safe_browsing | 682 } // namespace safe_browsing |
| OLD | NEW |