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" |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 186 | 187 |
| 187 bool SafeBrowsingProtocolManager::IsUpdateScheduled() const { | 188 bool SafeBrowsingProtocolManager::IsUpdateScheduled() const { |
| 188 return update_timer_.IsRunning(); | 189 return update_timer_.IsRunning(); |
| 189 } | 190 } |
| 190 | 191 |
| 191 SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() { | 192 SafeBrowsingProtocolManager::~SafeBrowsingProtocolManager() { |
| 192 // Delete in-progress SafeBrowsing requests. | 193 // Delete in-progress SafeBrowsing requests. |
| 193 STLDeleteContainerPairFirstPointers(hash_requests_.begin(), | 194 STLDeleteContainerPairFirstPointers(hash_requests_.begin(), |
| 194 hash_requests_.end()); | 195 hash_requests_.end()); |
| 195 hash_requests_.clear(); | 196 hash_requests_.clear(); |
| 197 | |
| 198 STLDeleteContainerPairFirstPointers(v4_hash_requests_.begin(), | |
| 199 v4_hash_requests_.end()); | |
| 200 v4_hash_requests_.clear(); | |
| 196 } | 201 } |
| 197 | 202 |
| 198 // We can only have one update or chunk request outstanding, but there may be | 203 // 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 | 204 // multiple GetHash requests pending since we don't want to serialize them and |
| 200 // slow down the user. | 205 // slow down the user. |
| 201 void SafeBrowsingProtocolManager::GetFullHash( | 206 void SafeBrowsingProtocolManager::GetFullHash( |
| 202 const std::vector<SBPrefix>& prefixes, | 207 const std::vector<SBPrefix>& prefixes, |
| 203 FullHashCallback callback, | 208 FullHashCallback callback, |
| 204 bool is_download, | 209 bool is_download, |
| 205 bool is_extended_reporting) { | 210 bool is_extended_reporting) { |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 221 hash_requests_[fetcher] = FullHashDetails(callback, is_download); | 226 hash_requests_[fetcher] = FullHashDetails(callback, is_download); |
| 222 | 227 |
| 223 const std::string get_hash = FormatGetHash(prefixes); | 228 const std::string get_hash = FormatGetHash(prefixes); |
| 224 | 229 |
| 225 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | 230 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); |
| 226 fetcher->SetRequestContext(request_context_getter_.get()); | 231 fetcher->SetRequestContext(request_context_getter_.get()); |
| 227 fetcher->SetUploadData("text/plain", get_hash); | 232 fetcher->SetUploadData("text/plain", get_hash); |
| 228 fetcher->Start(); | 233 fetcher->Start(); |
| 229 } | 234 } |
| 230 | 235 |
| 236 std::string SafeBrowsingProtocolManager::GetV4HashRequest( | |
| 237 const std::vector<SBPrefix>& prefixes, | |
| 238 ThreatType threat_type) { | |
| 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(threat_type); | |
| 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::GetV4FullHash( | |
|
awoz
2015/12/28 15:57:31
nit: This function name should reflect the fact th
kcarattini
2015/12/29 00:12:47
Done.
| |
| 261 const std::vector<SBPrefix>& prefixes, | |
| 262 ThreatType threat_type, | |
| 263 FullHashCallback callback) { | |
| 264 DCHECK(CalledOnValidThread()); | |
| 265 // TODO(kcarattini): Implement backoff behavior. | |
| 266 | |
| 267 std::string req_base64 = GetV4HashRequest(prefixes, threat_type); | |
| 268 GURL gethash_url = GetV4HashUrl(req_base64); | |
| 269 | |
| 270 net::URLFetcher* fetcher = | |
| 271 net::URLFetcher::Create(url_fetcher_id_++, gethash_url, | |
| 272 net::URLFetcher::POST, this) | |
| 273 .release(); | |
| 274 v4_hash_requests_[fetcher] = FullHashDetails(callback, | |
|
awoz
2015/12/28 15:57:32
Maybe a TODO to implement a new response processor
kcarattini
2015/12/29 00:12:47
Done.
| |
| 275 false /* is_download */); | |
|
awoz
2015/12/28 15:57:32
nit: indent off
kcarattini
2015/12/29 00:12:47
Done.
| |
| 276 | |
| 277 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | |
| 278 fetcher->SetRequestContext(request_context_getter_.get()); | |
| 279 fetcher->Start(); | |
| 280 } | |
| 281 | |
| 282 void SafeBrowsingProtocolManager::GetFullHashWithApis( | |
| 283 const std::vector<SBPrefix>& prefixes, | |
| 284 FullHashCallback callback) { | |
| 285 GetV4FullHash(prefixes, API_ABUSE, callback); | |
| 286 } | |
| 287 | |
| 231 void SafeBrowsingProtocolManager::GetNextUpdate() { | 288 void SafeBrowsingProtocolManager::GetNextUpdate() { |
| 232 DCHECK(CalledOnValidThread()); | 289 DCHECK(CalledOnValidThread()); |
| 233 if (request_.get() || request_type_ != NO_REQUEST) | 290 if (request_.get() || request_type_ != NO_REQUEST) |
| 234 return; | 291 return; |
| 235 | 292 |
| 236 IssueUpdateRequest(); | 293 IssueUpdateRequest(); |
| 237 } | 294 } |
| 238 | 295 |
| 239 // net::URLFetcherDelegate implementation ---------------------------------- | 296 // net::URLFetcherDelegate implementation ---------------------------------- |
| 240 | 297 |
| (...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 751 return GURL(url); | 808 return GURL(url); |
| 752 } | 809 } |
| 753 | 810 |
| 754 GURL SafeBrowsingProtocolManager::GetHashUrl(bool is_extended_reporting) const { | 811 GURL SafeBrowsingProtocolManager::GetHashUrl(bool is_extended_reporting) const { |
| 755 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( | 812 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( |
| 756 url_prefix_, "gethash", client_name_, version_, additional_query_, | 813 url_prefix_, "gethash", client_name_, version_, additional_query_, |
| 757 is_extended_reporting); | 814 is_extended_reporting); |
| 758 return GURL(url); | 815 return GURL(url); |
| 759 } | 816 } |
| 760 | 817 |
| 818 // The API hash call uses the pver4 Safe Browsing server. | |
| 819 GURL SafeBrowsingProtocolManager::GetV4HashUrl( | |
| 820 const std::string& request_base64) const { | |
| 821 std::string url = SafeBrowsingProtocolManagerHelper::ComposePver4Url( | |
| 822 "https://safebrowsing.googleapis.com/v4", "encodedFullHashes", | |
| 823 request_base64, client_name_, version_); | |
| 824 return GURL(url); | |
| 825 } | |
| 826 | |
| 761 GURL SafeBrowsingProtocolManager::NextChunkUrl(const std::string& url) const { | 827 GURL SafeBrowsingProtocolManager::NextChunkUrl(const std::string& url) const { |
| 762 DCHECK(CalledOnValidThread()); | 828 DCHECK(CalledOnValidThread()); |
| 763 std::string next_url; | 829 std::string next_url; |
| 764 if (!base::StartsWith(url, "http://", base::CompareCase::INSENSITIVE_ASCII) && | 830 if (!base::StartsWith(url, "http://", base::CompareCase::INSENSITIVE_ASCII) && |
| 765 !base::StartsWith(url, "https://", | 831 !base::StartsWith(url, "https://", |
| 766 base::CompareCase::INSENSITIVE_ASCII)) { | 832 base::CompareCase::INSENSITIVE_ASCII)) { |
| 767 // Use https if we updated via https, otherwise http (useful for testing). | 833 // Use https if we updated via https, otherwise http (useful for testing). |
| 768 if (base::StartsWith(url_prefix_, "https://", | 834 if (base::StartsWith(url_prefix_, "https://", |
| 769 base::CompareCase::INSENSITIVE_ASCII)) | 835 base::CompareCase::INSENSITIVE_ASCII)) |
| 770 next_url.append("https://"); | 836 next_url.append("https://"); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 791 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails( | 857 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails( |
| 792 FullHashCallback callback, | 858 FullHashCallback callback, |
| 793 bool is_download) | 859 bool is_download) |
| 794 : callback(callback), is_download(is_download) {} | 860 : callback(callback), is_download(is_download) {} |
| 795 | 861 |
| 796 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {} | 862 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {} |
| 797 | 863 |
| 798 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {} | 864 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {} |
| 799 | 865 |
| 800 } // namespace safe_browsing | 866 } // namespace safe_browsing |
| OLD | NEW |