| 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/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/environment.h" | 10 #include "base/environment.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
| 13 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
| 17 #include "base/timer.h" | 17 #include "base/timer.h" |
| 18 #include "chrome/browser/safe_browsing/protocol_parser.h" | 18 #include "chrome/browser/safe_browsing/protocol_parser.h" |
| 19 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 19 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 20 #include "chrome/common/chrome_version_info.h" | 20 #include "chrome/common/chrome_version_info.h" |
| 21 #include "chrome/common/env_vars.h" | 21 #include "chrome/common/env_vars.h" |
| 22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/common/url_fetcher.h" | |
| 24 #include "net/base/escape.h" | 23 #include "net/base/escape.h" |
| 25 #include "net/base/load_flags.h" | 24 #include "net/base/load_flags.h" |
| 25 #include "net/url_request/url_fetcher.h" |
| 26 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
| 27 #include "net/url_request/url_request_status.h" | 27 #include "net/url_request/url_request_status.h" |
| 28 | 28 |
| 29 using base::Time; | 29 using base::Time; |
| 30 using base::TimeDelta; | 30 using base::TimeDelta; |
| 31 using content::BrowserThread; | 31 using content::BrowserThread; |
| 32 | 32 |
| 33 // Maximum time, in seconds, from start up before we must issue an update query. | 33 // Maximum time, in seconds, from start up before we must issue an update query. |
| 34 static const int kSbTimerStartIntervalSec = 5 * 60; | 34 static const int kSbTimerStartIntervalSec = 5 * 60; |
| 35 | 35 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 const std::vector<SBPrefix>& prefixes) { | 146 const std::vector<SBPrefix>& prefixes) { |
| 147 // If we are in GetHash backoff, we need to check if we're past the next | 147 // If we are in GetHash backoff, we need to check if we're past the next |
| 148 // allowed time. If we are, we can proceed with the request. If not, we are | 148 // allowed time. If we are, we can proceed with the request. If not, we are |
| 149 // required to return empty results (i.e. treat the page as safe). | 149 // required to return empty results (i.e. treat the page as safe). |
| 150 if (gethash_error_count_ && Time::Now() <= next_gethash_time_) { | 150 if (gethash_error_count_ && Time::Now() <= next_gethash_time_) { |
| 151 std::vector<SBFullHashResult> full_hashes; | 151 std::vector<SBFullHashResult> full_hashes; |
| 152 sb_service_->HandleGetHashResults(check, full_hashes, false); | 152 sb_service_->HandleGetHashResults(check, full_hashes, false); |
| 153 return; | 153 return; |
| 154 } | 154 } |
| 155 GURL gethash_url = GetHashUrl(); | 155 GURL gethash_url = GetHashUrl(); |
| 156 net::URLFetcher* fetcher = content::URLFetcher::Create( | 156 net::URLFetcher* fetcher = net::URLFetcher::Create( |
| 157 gethash_url, net::URLFetcher::POST, this); | 157 gethash_url, net::URLFetcher::POST, this); |
| 158 hash_requests_[fetcher] = check; | 158 hash_requests_[fetcher] = check; |
| 159 | 159 |
| 160 std::string get_hash; | 160 std::string get_hash; |
| 161 SafeBrowsingProtocolParser parser; | 161 SafeBrowsingProtocolParser parser; |
| 162 parser.FormatGetHash(prefixes, &get_hash); | 162 parser.FormatGetHash(prefixes, &get_hash); |
| 163 | 163 |
| 164 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 164 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 165 fetcher->SetRequestContext(request_context_getter_); | 165 fetcher->SetRequestContext(request_context_getter_); |
| 166 fetcher->SetUploadData("text/plain", get_hash); | 166 fetcher->SetUploadData("text/plain", get_hash); |
| (...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 // We are only allowed to have one request outstanding at any time. Also, | 490 // We are only allowed to have one request outstanding at any time. Also, |
| 491 // don't get the next url until the previous one has been written to disk so | 491 // don't get the next url until the previous one has been written to disk so |
| 492 // that we don't use too much memory. | 492 // that we don't use too much memory. |
| 493 if (request_.get() || chunk_request_urls_.empty() || chunk_pending_to_write_) | 493 if (request_.get() || chunk_request_urls_.empty() || chunk_pending_to_write_) |
| 494 return; | 494 return; |
| 495 | 495 |
| 496 ChunkUrl next_chunk = chunk_request_urls_.front(); | 496 ChunkUrl next_chunk = chunk_request_urls_.front(); |
| 497 DCHECK(!next_chunk.url.empty()); | 497 DCHECK(!next_chunk.url.empty()); |
| 498 GURL chunk_url = NextChunkUrl(next_chunk.url); | 498 GURL chunk_url = NextChunkUrl(next_chunk.url); |
| 499 request_type_ = CHUNK_REQUEST; | 499 request_type_ = CHUNK_REQUEST; |
| 500 request_.reset(content::URLFetcher::Create( | 500 request_.reset(net::URLFetcher::Create( |
| 501 chunk_url, net::URLFetcher::GET, this)); | 501 chunk_url, net::URLFetcher::GET, this)); |
| 502 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 502 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 503 request_->SetRequestContext(request_context_getter_); | 503 request_->SetRequestContext(request_context_getter_); |
| 504 chunk_request_start_ = base::Time::Now(); | 504 chunk_request_start_ = base::Time::Now(); |
| 505 request_->Start(); | 505 request_->Start(); |
| 506 } | 506 } |
| 507 | 507 |
| 508 void SafeBrowsingProtocolManager::OnGetChunksComplete( | 508 void SafeBrowsingProtocolManager::OnGetChunksComplete( |
| 509 const std::vector<SBListChunkRanges>& lists, bool database_error) { | 509 const std::vector<SBListChunkRanges>& lists, bool database_error) { |
| 510 DCHECK_EQ(request_type_, UPDATE_REQUEST); | 510 DCHECK_EQ(request_type_, UPDATE_REQUEST); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 531 // lists. | 531 // lists. |
| 532 if (!found_phishing) | 532 if (!found_phishing) |
| 533 list_data.append(FormatList( | 533 list_data.append(FormatList( |
| 534 SBListChunkRanges(safe_browsing_util::kPhishingList))); | 534 SBListChunkRanges(safe_browsing_util::kPhishingList))); |
| 535 | 535 |
| 536 if (!found_malware) | 536 if (!found_malware) |
| 537 list_data.append(FormatList( | 537 list_data.append(FormatList( |
| 538 SBListChunkRanges(safe_browsing_util::kMalwareList))); | 538 SBListChunkRanges(safe_browsing_util::kMalwareList))); |
| 539 | 539 |
| 540 GURL update_url = UpdateUrl(); | 540 GURL update_url = UpdateUrl(); |
| 541 request_.reset(content::URLFetcher::Create( | 541 request_.reset(net::URLFetcher::Create( |
| 542 update_url, net::URLFetcher::POST, this)); | 542 update_url, net::URLFetcher::POST, this)); |
| 543 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 543 request_->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 544 request_->SetRequestContext(request_context_getter_); | 544 request_->SetRequestContext(request_context_getter_); |
| 545 request_->SetUploadData("text/plain", list_data); | 545 request_->SetUploadData("text/plain", list_data); |
| 546 request_->Start(); | 546 request_->Start(); |
| 547 | 547 |
| 548 // Begin the update request timeout. | 548 // Begin the update request timeout. |
| 549 update_timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kSbMaxUpdateWaitSec), | 549 update_timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kSbMaxUpdateWaitSec), |
| 550 this, | 550 this, |
| 551 &SafeBrowsingProtocolManager::UpdateResponseTimeout); | 551 &SafeBrowsingProtocolManager::UpdateResponseTimeout); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 575 void SafeBrowsingProtocolManager::ReportSafeBrowsingHit( | 575 void SafeBrowsingProtocolManager::ReportSafeBrowsingHit( |
| 576 const GURL& malicious_url, | 576 const GURL& malicious_url, |
| 577 const GURL& page_url, | 577 const GURL& page_url, |
| 578 const GURL& referrer_url, | 578 const GURL& referrer_url, |
| 579 bool is_subresource, | 579 bool is_subresource, |
| 580 SafeBrowsingService::UrlCheckResult threat_type, | 580 SafeBrowsingService::UrlCheckResult threat_type, |
| 581 const std::string& post_data) { | 581 const std::string& post_data) { |
| 582 GURL report_url = SafeBrowsingHitUrl(malicious_url, page_url, | 582 GURL report_url = SafeBrowsingHitUrl(malicious_url, page_url, |
| 583 referrer_url, is_subresource, | 583 referrer_url, is_subresource, |
| 584 threat_type); | 584 threat_type); |
| 585 net::URLFetcher* report = content::URLFetcher::Create( | 585 net::URLFetcher* report = net::URLFetcher::Create( |
| 586 report_url, | 586 report_url, |
| 587 post_data.empty() ? net::URLFetcher::GET : net::URLFetcher::POST, | 587 post_data.empty() ? net::URLFetcher::GET : net::URLFetcher::POST, |
| 588 this); | 588 this); |
| 589 report->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 589 report->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 590 report->SetRequestContext(request_context_getter_); | 590 report->SetRequestContext(request_context_getter_); |
| 591 if (!post_data.empty()) | 591 if (!post_data.empty()) |
| 592 report->SetUploadData("text/plain", post_data); | 592 report->SetUploadData("text/plain", post_data); |
| 593 report->Start(); | 593 report->Start(); |
| 594 safebrowsing_reports_.insert(report); | 594 safebrowsing_reports_.insert(report); |
| 595 } | 595 } |
| 596 | 596 |
| 597 // Sends malware details for users who opt-in. | 597 // Sends malware details for users who opt-in. |
| 598 void SafeBrowsingProtocolManager::ReportMalwareDetails( | 598 void SafeBrowsingProtocolManager::ReportMalwareDetails( |
| 599 const std::string& report) { | 599 const std::string& report) { |
| 600 GURL report_url = MalwareDetailsUrl(); | 600 GURL report_url = MalwareDetailsUrl(); |
| 601 net::URLFetcher* fetcher = content::URLFetcher::Create( | 601 net::URLFetcher* fetcher = net::URLFetcher::Create( |
| 602 report_url, net::URLFetcher::POST, this); | 602 report_url, net::URLFetcher::POST, this); |
| 603 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 603 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 604 fetcher->SetRequestContext(request_context_getter_); | 604 fetcher->SetRequestContext(request_context_getter_); |
| 605 fetcher->SetUploadData("application/octet-stream", report); | 605 fetcher->SetUploadData("application/octet-stream", report); |
| 606 // Don't try too hard to send reports on failures. | 606 // Don't try too hard to send reports on failures. |
| 607 fetcher->SetAutomaticallyRetryOn5xx(false); | 607 fetcher->SetAutomaticallyRetryOn5xx(false); |
| 608 fetcher->Start(); | 608 fetcher->Start(); |
| 609 safebrowsing_reports_.insert(fetcher); | 609 safebrowsing_reports_.insert(fetcher); |
| 610 } | 610 } |
| 611 | 611 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 731 if (!additional_query_.empty()) { | 731 if (!additional_query_.empty()) { |
| 732 if (next_url.find("?") != std::string::npos) { | 732 if (next_url.find("?") != std::string::npos) { |
| 733 next_url.append("&"); | 733 next_url.append("&"); |
| 734 } else { | 734 } else { |
| 735 next_url.append("?"); | 735 next_url.append("?"); |
| 736 } | 736 } |
| 737 next_url.append(additional_query_); | 737 next_url.append(additional_query_); |
| 738 } | 738 } |
| 739 return GURL(next_url); | 739 return GURL(next_url); |
| 740 } | 740 } |
| OLD | NEW |