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::GetV4FullHashes( | |
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 // TODO(kcarattini): Implement a new response processor. | |
275 v4_hash_requests_[fetcher] = FullHashDetails(callback, | |
276 false /* is_download */); | |
277 | |
278 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | |
279 fetcher->SetRequestContext(request_context_getter_.get()); | |
280 fetcher->Start(); | |
281 } | |
282 | |
283 void SafeBrowsingProtocolManager::GetFullHashesWithApis( | |
284 const std::vector<SBPrefix>& prefixes, | |
285 FullHashCallback callback) { | |
286 GetV4FullHashes(prefixes, API_ABUSE, callback); | |
287 } | |
288 | |
231 void SafeBrowsingProtocolManager::GetNextUpdate() { | 289 void SafeBrowsingProtocolManager::GetNextUpdate() { |
232 DCHECK(CalledOnValidThread()); | 290 DCHECK(CalledOnValidThread()); |
233 if (request_.get() || request_type_ != NO_REQUEST) | 291 if (request_.get() || request_type_ != NO_REQUEST) |
234 return; | 292 return; |
235 | 293 |
236 IssueUpdateRequest(); | 294 IssueUpdateRequest(); |
237 } | 295 } |
238 | 296 |
239 // net::URLFetcherDelegate implementation ---------------------------------- | 297 // net::URLFetcherDelegate implementation ---------------------------------- |
240 | 298 |
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
751 return GURL(url); | 809 return GURL(url); |
752 } | 810 } |
753 | 811 |
754 GURL SafeBrowsingProtocolManager::GetHashUrl(bool is_extended_reporting) const { | 812 GURL SafeBrowsingProtocolManager::GetHashUrl(bool is_extended_reporting) const { |
755 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( | 813 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl( |
756 url_prefix_, "gethash", client_name_, version_, additional_query_, | 814 url_prefix_, "gethash", client_name_, version_, additional_query_, |
757 is_extended_reporting); | 815 is_extended_reporting); |
758 return GURL(url); | 816 return GURL(url); |
759 } | 817 } |
760 | 818 |
819 // The API hash call uses the pver4 Safe Browsing server. | |
820 GURL SafeBrowsingProtocolManager::GetV4HashUrl( | |
821 const std::string& request_base64) const { | |
822 std::string url = SafeBrowsingProtocolManagerHelper::ComposePver4Url( | |
823 "https://safebrowsing.googleapis.com/v4", "encodedFullHashes", | |
vakh (old account. dont use)
2015/12/29 20:53:33
This partial URL string should probably be in safe
kcarattini
2015/12/29 22:33:41
Defined as a constant in this file to avoid a depe
| |
824 request_base64, client_name_, version_); | |
825 return GURL(url); | |
826 } | |
827 | |
761 GURL SafeBrowsingProtocolManager::NextChunkUrl(const std::string& url) const { | 828 GURL SafeBrowsingProtocolManager::NextChunkUrl(const std::string& url) const { |
762 DCHECK(CalledOnValidThread()); | 829 DCHECK(CalledOnValidThread()); |
763 std::string next_url; | 830 std::string next_url; |
764 if (!base::StartsWith(url, "http://", base::CompareCase::INSENSITIVE_ASCII) && | 831 if (!base::StartsWith(url, "http://", base::CompareCase::INSENSITIVE_ASCII) && |
765 !base::StartsWith(url, "https://", | 832 !base::StartsWith(url, "https://", |
766 base::CompareCase::INSENSITIVE_ASCII)) { | 833 base::CompareCase::INSENSITIVE_ASCII)) { |
767 // Use https if we updated via https, otherwise http (useful for testing). | 834 // Use https if we updated via https, otherwise http (useful for testing). |
768 if (base::StartsWith(url_prefix_, "https://", | 835 if (base::StartsWith(url_prefix_, "https://", |
769 base::CompareCase::INSENSITIVE_ASCII)) | 836 base::CompareCase::INSENSITIVE_ASCII)) |
770 next_url.append("https://"); | 837 next_url.append("https://"); |
(...skipping 20 matching lines...) Expand all Loading... | |
791 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails( | 858 SafeBrowsingProtocolManager::FullHashDetails::FullHashDetails( |
792 FullHashCallback callback, | 859 FullHashCallback callback, |
793 bool is_download) | 860 bool is_download) |
794 : callback(callback), is_download(is_download) {} | 861 : callback(callback), is_download(is_download) {} |
795 | 862 |
796 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {} | 863 SafeBrowsingProtocolManager::FullHashDetails::~FullHashDetails() {} |
797 | 864 |
798 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {} | 865 SafeBrowsingProtocolManagerDelegate::~SafeBrowsingProtocolManagerDelegate() {} |
799 | 866 |
800 } // namespace safe_browsing | 867 } // namespace safe_browsing |
OLD | NEW |