Chromium Code Reviews| 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 #include "base/base64.h" | |
| 7 #include "base/environment.h" | 8 #include "base/environment.h" |
| 8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 9 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/metrics/sparse_histogram.h" | 11 #include "base/metrics/sparse_histogram.h" |
| 11 #include "base/profiler/scoped_tracker.h" | 12 #include "base/profiler/scoped_tracker.h" |
| 12 #include "base/rand_util.h" | 13 #include "base/rand_util.h" |
| 13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 17 #include "base/timer/timer.h" | 18 #include "base/timer/timer.h" |
| 18 #include "chrome/browser/safe_browsing/protocol_parser.h" | 19 #include "chrome/browser/safe_browsing/protocol_parser.h" |
| 19 #include "chrome/common/env_vars.h" | 20 #include "chrome/common/env_vars.h" |
| 21 #include "components/safe_browsing_db/safebrowsing.pb.h" | |
| 20 #include "components/safe_browsing_db/util.h" | 22 #include "components/safe_browsing_db/util.h" |
| 21 #include "components/variations/variations_associated_data.h" | 23 #include "components/variations/variations_associated_data.h" |
| 22 #include "google_apis/google_api_keys.h" | 24 #include "google_apis/google_api_keys.h" |
| 23 #include "net/base/escape.h" | 25 #include "net/base/escape.h" |
| 24 #include "net/base/load_flags.h" | 26 #include "net/base/load_flags.h" |
| 25 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 26 #include "net/http/http_response_headers.h" | 28 #include "net/http/http_response_headers.h" |
| 27 #include "net/http/http_status_code.h" | 29 #include "net/http/http_status_code.h" |
| 28 #include "net/url_request/url_fetcher.h" | 30 #include "net/url_request/url_fetcher.h" |
| 29 #include "net/url_request/url_request_context_getter.h" | 31 #include "net/url_request/url_request_context_getter.h" |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 | 188 |
| 187 bool SafeBrowsingProtocolManager::IsUpdateScheduled() const { | 189 bool SafeBrowsingProtocolManager::IsUpdateScheduled() const { |
| 188 return update_timer_.IsRunning(); | 190 return update_timer_.IsRunning(); |
| 189 } | 191 } |
| 190 | 192 |
| 191 SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() { | 193 SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() { |
| 192 // Delete in-progress SafeBrowsing requests. | 194 // Delete in-progress SafeBrowsing requests. |
| 193 STLDeleteContainerPairFirstPointers(hash_requests_.begin(), | 195 STLDeleteContainerPairFirstPointers(hash_requests_.begin(), |
| 194 hash_requests_.end()); | 196 hash_requests_.end()); |
| 195 hash_requests_.clear(); | 197 hash_requests_.clear(); |
| 198 | |
| 199 STLDeleteContainerPairFirstPointers(hash_api_requests_.begin(), | |
| 200 hash_api_requests_.end()); | |
| 201 hash_api_requests_.clear(); | |
| 196 } | 202 } |
| 197 | 203 |
| 198 // We can only have one update or chunk request outstanding, but there may be | 204 // We can only have one update or chunk request outstanding, but there may be |
| 199 // multiple GetHash requests pending since we don't want to serialize them and | 205 // multiple GetHash requests pending since we don't want to serialize them and |
| 200 // slow down the user. | 206 // slow down the user. |
| 201 void SafeBrowsingProtocolManager::GetFullHash( | 207 void SafeBrowsingProtocolManager::GetFullHash( |
| 202 const std::vector<SBPrefix>& prefixes, | 208 const std::vector<SBPrefix>& prefixes, |
| 203 FullHashCallback callback, | 209 FullHashCallback callback, |
| 204 bool is_download, | 210 bool is_download, |
| 205 bool is_extended_reporting) { | 211 bool is_extended_reporting) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 221 hash_requests_[fetcher] = FullHashDetails(callback, is_download); | 227 hash_requests_[fetcher] = FullHashDetails(callback, is_download); |
| 222 | 228 |
| 223 const std::string get_hash = FormatGetHash(prefixes); | 229 const std::string get_hash = FormatGetHash(prefixes); |
| 224 | 230 |
| 225 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 231 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 226 fetcher->SetRequestContext(request_context_getter_.get()); | 232 fetcher->SetRequestContext(request_context_getter_.get()); |
| 227 fetcher->SetUploadData("text/plain", get_hash); | 233 fetcher->SetUploadData("text/plain", get_hash); |
| 228 fetcher->Start(); | 234 fetcher->Start(); |
| 229 } | 235 } |
| 230 | 236 |
| 237 std::string SafeBrowsingProtocolManager::GetHashWithApisRequest( | |
|
Nathan Parker
2015/12/27 01:10:17
This could be non-"Apis
-specific by passing the t
kcarattini
2015/12/28 00:05:46
Done.
| |
| 238 const std::vector<SBPrefix>& prefixes) { | |
| 239 // Build the request. Client info and client states are not added to the | |
| 240 // request protocol buffer. Client info is passed as params in the url. | |
| 241 FindFullHashesRequest req; | |
| 242 ThreatInfo* info = req.mutable_threat_info(); | |
| 243 info->add_threat_types(API_ABUSE); | |
| 244 info->add_platform_types(CHROME_PLATFORM); | |
| 245 info->add_threat_entry_types(URL_EXPRESSION); | |
| 246 for (const SBPrefix& prefix : prefixes) { | |
| 247 std::string hash; | |
| 248 hash.append(reinterpret_cast<const char*>(&prefix), sizeof(SBPrefix)); | |
| 249 info->add_threat_entries()->set_hash(hash); | |
| 250 } | |
| 251 | |
| 252 // Serialize and Base64 encode. | |
| 253 std::string req_data, req_base64; | |
| 254 req.SerializeToString(&req_data); | |
| 255 base::Base64Encode(req_data, &req_base64); | |
| 256 | |
| 257 return req_base64; | |
| 258 } | |
| 259 | |
| 260 void SafeBrowsingProtocolManager::GetFullHashWithApis( | |
|
Nathan Parker
2015/12/27 01:10:17
Same here. This function could call a generic Pve
kcarattini
2015/12/28 00:05:46
Done.
| |
| 261 const std::vector<SBPrefix>& prefixes, | |
| 262 FullHashCallback callback) { | |
| 263 DCHECK(CalledOnValidThread()); | |
| 264 // TODO(kcarattini): Implement backoff behavior. | |
| 265 | |
| 266 std::string req_base64 = GetHashWithApisRequest(prefixes); | |
| 267 GURL gethash_url = GetHashWithApisUrl(req_base64); | |
| 268 | |
| 269 net::URLFetcher* fetcher = | |
| 270 net::URLFetcher::Create(url_fetcher_id_++, gethash_url, | |
| 271 net::URLFetcher::POST, this) | |
| 272 .release(); | |
| 273 hash_api_requests_[fetcher] = FullHashDetails(callback, | |
| 274 false /* is_download */); | |
| 275 | |
| 276 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | |
| 277 fetcher->SetRequestContext(request_context_getter_.get()); | |
| 278 fetcher->Start(); | |
| 279 } | |
| 280 | |
| 231 void SafeBrowsingProtocolManager::GetNextUpdate() { | 281 void SafeBrowsingProtocolManager::GetNextUpdate() { |
| 232 DCHECK(CalledOnValidThread()); | 282 DCHECK(CalledOnValidThread()); |
| 233 if (request_.get() || request_type_ != NO_REQUEST) | 283 if (request_.get() || request_type_ != NO_REQUEST) |
| 234 return; | 284 return; |
| 235 | 285 |
| 236 IssueUpdateRequest(); | 286 IssueUpdateRequest(); |
| 237 } | 287 } |
| 238 | 288 |
| 239 // net::URLFetcherDelegate implementation ---------------------------------- | 289 // net::URLFetcherDelegate implementation ---------------------------------- |
| 240 | 290 |
| (...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 751 return GURL(url); | 801 return GURL(url); |
| 752 } | 802 } |
| 753 | 803 |
| 754 GURL SafeBrowsingProtocolManager::GetHashUrl(bool is_extended_reporting) const { | 804 GURL SafeBrowsingProtocolManager::GetHashUrl(bool is_extended_reporting) const { |
| 755 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( | 805 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( |
| 756 url_prefix_, "gethash", client_name_, version_, additional_query_, | 806 url_prefix_, "gethash", client_name_, version_, additional_query_, |
| 757 is_extended_reporting); | 807 is_extended_reporting); |
| 758 return GURL(url); | 808 return GURL(url); |
| 759 } | 809 } |
| 760 | 810 |
| 811 // The API hash call uses the pver4 Safe Browsing server. | |
| 812 GURL SafeBrowsingProtocolManager::GetHashWithApisUrl( | |
|
Nathan Parker
2015/12/27 01:10:17
This is not "Apis" specific -- we could call it Ge
kcarattini
2015/12/28 00:05:46
Done.
| |
| 813 const std::string& request_base64) const { | |
| 814 std::string url = SafeBrowsingProtocolManagerHelper::ComposePver4Url( | |
| 815 "https://safebrowsing.googleapis.com/v4", "encodedFullHashes", | |
| 816 request_base64, client_name_, version_); | |
| 817 return GURL(url); | |
| 818 } | |
| 819 | |
| 761 GURL SafeBrowsingProtocolManager::NextChunkUrl(const std::string& url) const { | 820 GURL SafeBrowsingProtocolManager::NextChunkUrl(const std::string& url) const { |
| 762 DCHECK(CalledOnValidThread()); | 821 DCHECK(CalledOnValidThread()); |
| 763 std::string next_url; | 822 std::string next_url; |
| 764 if (!base::StartsWith(url, "http://", base::CompareCase::INSENSITIVE_ASCII) && | 823 if (!base::StartsWith(url, "http://", base::CompareCase::INSENSITIVE_ASCII) && |
| 765 !base::StartsWith(url, "https://", | 824 !base::StartsWith(url, "https://", |
| 766 base::CompareCase::INSENSITIVE_ASCII)) { | 825 base::CompareCase::INSENSITIVE_ASCII)) { |
| 767 // Use https if we updated via https, otherwise http (useful for testing). | 826 // Use https if we updated via https, otherwise http (useful for testing). |
| 768 if (base::StartsWith(url_prefix_, "https://", | 827 if (base::StartsWith(url_prefix_, "https://", |
| 769 base::CompareCase::INSENSITIVE_ASCII)) | 828 base::CompareCase::INSENSITIVE_ASCII)) |
| 770 next_url.append("https://"); | 829 next_url.append("https://"); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 791 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails( | 850 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails( |
| 792 FullHashCallback callback, | 851 FullHashCallback callback, |
| 793 bool is_download) | 852 bool is_download) |
| 794 : callback(callback), is_download(is_download) {} | 853 : callback(callback), is_download(is_download) {} |
| 795 | 854 |
| 796 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {} | 855 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {} |
| 797 | 856 |
| 798 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {} | 857 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {} |
| 799 | 858 |
| 800 } // namespace safe_browsing | 859 } // namespace safe_browsing |
| OLD | NEW |