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

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

Powered by Google App Engine
This is Rietveld 408576698