| 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/protocol_manager.h" | 5 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 6 | 6 |
| 7 #ifndef NDEBUG | 7 #ifndef NDEBUG |
| 8 #include "base/base64.h" | 8 #include "base/base64.h" |
| 9 #endif | 9 #endif |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/environment.h" | 11 #include "base/environment.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
| 18 #include "base/timer.h" | 18 #include "base/timer.h" |
| 19 #include "chrome/browser/safe_browsing/protocol_parser.h" | 19 #include "chrome/browser/safe_browsing/protocol_parser.h" |
| 20 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 21 #include "chrome/common/chrome_version_info.h" | 21 #include "chrome/common/chrome_version_info.h" |
| 22 #include "chrome/common/env_vars.h" | 22 #include "chrome/common/env_vars.h" |
| 23 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/common/content_url_request_user_data.h" |
| 24 #include "content/public/common/url_fetcher.h" | 25 #include "content/public/common/url_fetcher.h" |
| 25 #include "net/base/escape.h" | 26 #include "net/base/escape.h" |
| 26 #include "net/base/load_flags.h" | 27 #include "net/base/load_flags.h" |
| 27 #include "net/url_request/url_request_context_getter.h" | 28 #include "net/url_request/url_request_context_getter.h" |
| 28 #include "net/url_request/url_request_status.h" | 29 #include "net/url_request/url_request_status.h" |
| 29 | 30 |
| 30 using base::Time; | 31 using base::Time; |
| 31 using base::TimeDelta; | 32 using base::TimeDelta; |
| 32 using content::BrowserThread; | 33 using content::BrowserThread; |
| 33 | 34 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 content::URLFetcher* fetcher = content::URLFetcher::Create( | 174 content::URLFetcher* fetcher = content::URLFetcher::Create( |
| 174 gethash_url, content::URLFetcher::POST, this); | 175 gethash_url, content::URLFetcher::POST, this); |
| 175 hash_requests_[fetcher] = check; | 176 hash_requests_[fetcher] = check; |
| 176 | 177 |
| 177 std::string get_hash; | 178 std::string get_hash; |
| 178 SafeBrowsingProtocolParser parser; | 179 SafeBrowsingProtocolParser parser; |
| 179 parser.FormatGetHash(prefixes, &get_hash); | 180 parser.FormatGetHash(prefixes, &get_hash); |
| 180 | 181 |
| 181 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 182 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 182 fetcher->SetRequestContext(request_context_getter_); | 183 fetcher->SetRequestContext(request_context_getter_); |
| 184 // TODO(jochen): Do cookie audit. |
| 185 fetcher->SetContentURLRequestUserData( |
| 186 new content::ContentURLRequestUserData()); |
| 183 fetcher->SetUploadData("text/plain", get_hash); | 187 fetcher->SetUploadData("text/plain", get_hash); |
| 184 fetcher->Start(); | 188 fetcher->Start(); |
| 185 } | 189 } |
| 186 | 190 |
| 187 void SafeBrowsingProtocolManager::GetNextUpdate() { | 191 void SafeBrowsingProtocolManager::GetNextUpdate() { |
| 188 if (initial_request_) { | 192 if (initial_request_) { |
| 189 if (client_key_.empty() || wrapped_key_.empty()) { | 193 if (client_key_.empty() || wrapped_key_.empty()) { |
| 190 IssueKeyRequest(); | 194 IssueKeyRequest(); |
| 191 return; | 195 return; |
| 192 } else { | 196 } else { |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 560 return; | 564 return; |
| 561 | 565 |
| 562 ChunkUrl next_chunk = chunk_request_urls_.front(); | 566 ChunkUrl next_chunk = chunk_request_urls_.front(); |
| 563 DCHECK(!next_chunk.url.empty()); | 567 DCHECK(!next_chunk.url.empty()); |
| 564 GURL chunk_url = NextChunkUrl(next_chunk.url); | 568 GURL chunk_url = NextChunkUrl(next_chunk.url); |
| 565 request_type_ = CHUNK_REQUEST; | 569 request_type_ = CHUNK_REQUEST; |
| 566 request_.reset(content::URLFetcher::Create( | 570 request_.reset(content::URLFetcher::Create( |
| 567 chunk_url, content::URLFetcher::GET, this)); | 571 chunk_url, content::URLFetcher::GET, this)); |
| 568 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 572 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 569 request_->SetRequestContext(request_context_getter_); | 573 request_->SetRequestContext(request_context_getter_); |
| 574 // TODO(jochen): Do cookie audit. |
| 575 request_->SetContentURLRequestUserData( |
| 576 new content::ContentURLRequestUserData()); |
| 570 chunk_request_start_ = base::Time::Now(); | 577 chunk_request_start_ = base::Time::Now(); |
| 571 request_->Start(); | 578 request_->Start(); |
| 572 } | 579 } |
| 573 | 580 |
| 574 void SafeBrowsingProtocolManager::IssueKeyRequest() { | 581 void SafeBrowsingProtocolManager::IssueKeyRequest() { |
| 575 GURL key_url = MacKeyUrl(); | 582 GURL key_url = MacKeyUrl(); |
| 576 request_type_ = GETKEY_REQUEST; | 583 request_type_ = GETKEY_REQUEST; |
| 577 request_.reset(content::URLFetcher::Create( | 584 request_.reset(content::URLFetcher::Create( |
| 578 key_url, content::URLFetcher::GET, this)); | 585 key_url, content::URLFetcher::GET, this)); |
| 579 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 586 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 580 request_->SetRequestContext(request_context_getter_); | 587 request_->SetRequestContext(request_context_getter_); |
| 588 // TODO(jochen): Do cookie audit. |
| 589 request_->SetContentURLRequestUserData( |
| 590 new content::ContentURLRequestUserData()); |
| 581 request_->Start(); | 591 request_->Start(); |
| 582 } | 592 } |
| 583 | 593 |
| 584 void SafeBrowsingProtocolManager::OnGetChunksComplete( | 594 void SafeBrowsingProtocolManager::OnGetChunksComplete( |
| 585 const std::vector<SBListChunkRanges>& lists, bool database_error) { | 595 const std::vector<SBListChunkRanges>& lists, bool database_error) { |
| 586 DCHECK_EQ(request_type_, UPDATE_REQUEST); | 596 DCHECK_EQ(request_type_, UPDATE_REQUEST); |
| 587 if (database_error) { | 597 if (database_error) { |
| 588 UpdateFinished(false); | 598 UpdateFinished(false); |
| 589 ScheduleNextUpdate(false); | 599 ScheduleNextUpdate(false); |
| 590 return; | 600 return; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 613 | 623 |
| 614 if (!found_malware) | 624 if (!found_malware) |
| 615 list_data.append(FormatList( | 625 list_data.append(FormatList( |
| 616 SBListChunkRanges(safe_browsing_util::kMalwareList), use_mac)); | 626 SBListChunkRanges(safe_browsing_util::kMalwareList), use_mac)); |
| 617 | 627 |
| 618 GURL update_url = UpdateUrl(use_mac); | 628 GURL update_url = UpdateUrl(use_mac); |
| 619 request_.reset(content::URLFetcher::Create( | 629 request_.reset(content::URLFetcher::Create( |
| 620 update_url, content::URLFetcher::POST, this)); | 630 update_url, content::URLFetcher::POST, this)); |
| 621 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 631 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 622 request_->SetRequestContext(request_context_getter_); | 632 request_->SetRequestContext(request_context_getter_); |
| 633 // TODO(jochen): Do cookie audit. |
| 634 request_->SetContentURLRequestUserData( |
| 635 new content::ContentURLRequestUserData()); |
| 623 request_->SetUploadData("text/plain", list_data); | 636 request_->SetUploadData("text/plain", list_data); |
| 624 request_->Start(); | 637 request_->Start(); |
| 625 | 638 |
| 626 // Begin the update request timeout. | 639 // Begin the update request timeout. |
| 627 update_timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kSbMaxUpdateWaitSec), | 640 update_timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kSbMaxUpdateWaitSec), |
| 628 this, | 641 this, |
| 629 &SafeBrowsingProtocolManager::UpdateResponseTimeout); | 642 &SafeBrowsingProtocolManager::UpdateResponseTimeout); |
| 630 } | 643 } |
| 631 | 644 |
| 632 // If we haven't heard back from the server with an update response, this method | 645 // If we haven't heard back from the server with an update response, this method |
| (...skipping 26 matching lines...) Expand all Loading... |
| 659 const std::string& post_data) { | 672 const std::string& post_data) { |
| 660 GURL report_url = SafeBrowsingHitUrl(malicious_url, page_url, | 673 GURL report_url = SafeBrowsingHitUrl(malicious_url, page_url, |
| 661 referrer_url, is_subresource, | 674 referrer_url, is_subresource, |
| 662 threat_type); | 675 threat_type); |
| 663 content::URLFetcher* report = content::URLFetcher::Create( | 676 content::URLFetcher* report = content::URLFetcher::Create( |
| 664 report_url, | 677 report_url, |
| 665 post_data.empty() ? content::URLFetcher::GET : content::URLFetcher::POST, | 678 post_data.empty() ? content::URLFetcher::GET : content::URLFetcher::POST, |
| 666 this); | 679 this); |
| 667 report->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 680 report->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 668 report->SetRequestContext(request_context_getter_); | 681 report->SetRequestContext(request_context_getter_); |
| 682 // TODO(jochen): Do cookie audit. |
| 683 report->SetContentURLRequestUserData( |
| 684 new content::ContentURLRequestUserData()); |
| 669 if (!post_data.empty()) | 685 if (!post_data.empty()) |
| 670 report->SetUploadData("text/plain", post_data); | 686 report->SetUploadData("text/plain", post_data); |
| 671 report->Start(); | 687 report->Start(); |
| 672 safebrowsing_reports_.insert(report); | 688 safebrowsing_reports_.insert(report); |
| 673 } | 689 } |
| 674 | 690 |
| 675 // Sends malware details for users who opt-in. | 691 // Sends malware details for users who opt-in. |
| 676 void SafeBrowsingProtocolManager::ReportMalwareDetails( | 692 void SafeBrowsingProtocolManager::ReportMalwareDetails( |
| 677 const std::string& report) { | 693 const std::string& report) { |
| 678 GURL report_url = MalwareDetailsUrl(); | 694 GURL report_url = MalwareDetailsUrl(); |
| 679 content::URLFetcher* fetcher = content::URLFetcher::Create( | 695 content::URLFetcher* fetcher = content::URLFetcher::Create( |
| 680 report_url, content::URLFetcher::POST, this); | 696 report_url, content::URLFetcher::POST, this); |
| 681 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 697 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 682 fetcher->SetRequestContext(request_context_getter_); | 698 fetcher->SetRequestContext(request_context_getter_); |
| 699 // TODO(jochen): Do cookie audit. |
| 700 fetcher->SetContentURLRequestUserData( |
| 701 new content::ContentURLRequestUserData()); |
| 683 fetcher->SetUploadData("application/octet-stream", report); | 702 fetcher->SetUploadData("application/octet-stream", report); |
| 684 // Don't try too hard to send reports on failures. | 703 // Don't try too hard to send reports on failures. |
| 685 fetcher->SetAutomaticallyRetryOn5xx(false); | 704 fetcher->SetAutomaticallyRetryOn5xx(false); |
| 686 fetcher->Start(); | 705 fetcher->Start(); |
| 687 safebrowsing_reports_.insert(fetcher); | 706 safebrowsing_reports_.insert(fetcher); |
| 688 } | 707 } |
| 689 | 708 |
| 690 | 709 |
| 691 // static | 710 // static |
| 692 std::string SafeBrowsingProtocolManager::FormatList( | 711 std::string SafeBrowsingProtocolManager::FormatList( |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 832 if (!additional_query_.empty()) { | 851 if (!additional_query_.empty()) { |
| 833 if (next_url.find("?") != std::string::npos) { | 852 if (next_url.find("?") != std::string::npos) { |
| 834 next_url.append("&"); | 853 next_url.append("&"); |
| 835 } else { | 854 } else { |
| 836 next_url.append("?"); | 855 next_url.append("?"); |
| 837 } | 856 } |
| 838 next_url.append(additional_query_); | 857 next_url.append(additional_query_); |
| 839 } | 858 } |
| 840 return GURL(next_url); | 859 return GURL(next_url); |
| 841 } | 860 } |
| OLD | NEW |