Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(286)

Side by Side Diff: chrome/browser/safe_browsing/download_protection_service.cc

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

Powered by Google App Engine
This is Rietveld 408576698