| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ | 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ |
| 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ | 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ |
| 7 | 7 |
| 8 // A class that implements Chrome's interface with the SafeBrowsing V4 protocol. | 8 // A class that implements Chrome's interface with the SafeBrowsing V4 protocol. |
| 9 // | 9 // |
| 10 // The V4GetHashProtocolManager handles formatting and making requests of, and | 10 // The V4GetHashProtocolManager handles formatting and making requests of, and |
| 11 // handling responses from, Google's SafeBrowsing servers. The purpose of this | 11 // handling responses from, Google's SafeBrowsing servers. The purpose of this |
| 12 // class is to get full hash matches from the SB server for the given set of | 12 // class is to get full hash matches from the SB server for the given set of |
| 13 // hash prefixes. | 13 // hash prefixes. |
| 14 | 14 |
| 15 #include <string> | 15 #include <string> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "base/containers/hash_tables.h" | |
| 19 #include "base/gtest_prod_util.h" | 18 #include "base/gtest_prod_util.h" |
| 20 #include "base/macros.h" | 19 #include "base/macros.h" |
| 21 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 22 #include "base/threading/non_thread_safe.h" | 21 #include "base/threading/non_thread_safe.h" |
| 23 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 24 #include "base/timer/timer.h" | 23 #include "base/timer/timer.h" |
| 25 #include "components/safe_browsing_db/safebrowsing.pb.h" | 24 #include "components/safe_browsing_db/safebrowsing.pb.h" |
| 26 #include "components/safe_browsing_db/util.h" | 25 #include "components/safe_browsing_db/util.h" |
| 26 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
| 27 #include "net/url_request/url_fetcher_delegate.h" | 27 #include "net/url_request/url_fetcher_delegate.h" |
| 28 #include "net/url_request/url_request_status.h" | |
| 29 #include "url/gurl.h" | 28 #include "url/gurl.h" |
| 30 | 29 |
| 31 namespace net { | 30 namespace net { |
| 32 class URLFetcher; | 31 class URLFetcher; |
| 33 class URLRequestContextGetter; | 32 class URLRequestContextGetter; |
| 34 } // namespace net | 33 } // namespace net |
| 35 | 34 |
| 36 namespace safe_browsing { | 35 namespace safe_browsing { |
| 37 | 36 |
| 38 // Config passed to the constructor of a V4GetHashProtocolManager. | |
| 39 struct V4GetHashProtocolConfig { | |
| 40 std::string client_name; | |
| 41 std::string version; | |
| 42 std::string key_param; | |
| 43 }; | |
| 44 | |
| 45 class V4GetHashProtocolManagerFactory; | 37 class V4GetHashProtocolManagerFactory; |
| 46 | 38 |
| 47 class V4GetHashProtocolManager : public net::URLFetcherDelegate, | 39 class V4GetHashProtocolManager : public net::URLFetcherDelegate, |
| 48 public base::NonThreadSafe { | 40 public base::NonThreadSafe { |
| 49 public: | 41 public: |
| 50 // FullHashCallback is invoked when GetFullHashes completes. | 42 // FullHashCallback is invoked when GetFullHashes completes. |
| 51 // Parameters: | 43 // Parameters: |
| 52 // - The vector of full hash results. If empty, indicates that there | 44 // - The vector of full hash results. If empty, indicates that there |
| 53 // were no matches, and that the resource is safe. | 45 // were no matches, and that the resource is safe. |
| 54 // - The negative cache duration of the result. | 46 // - The negative cache duration of the result. |
| 55 typedef base::Callback<void(const std::vector<SBFullHashResult>&, | 47 typedef base::Callback<void(const std::vector<SBFullHashResult>&, |
| 56 const base::TimeDelta&)> | 48 const base::TimeDelta&)> |
| 57 FullHashCallback; | 49 FullHashCallback; |
| 58 | 50 |
| 59 ~V4GetHashProtocolManager() override; | 51 ~V4GetHashProtocolManager() override; |
| 60 | 52 |
| 61 // Makes the passed |factory| the factory used to instantiate | 53 // Makes the passed |factory| the factory used to instantiate |
| 62 // a V4GetHashProtocolManager. Useful for tests. | 54 // a V4GetHashProtocolManager. Useful for tests. |
| 63 static void RegisterFactory(V4GetHashProtocolManagerFactory* factory) { | 55 static void RegisterFactory(V4GetHashProtocolManagerFactory* factory) { |
| 64 factory_ = factory; | 56 factory_ = factory; |
| 65 } | 57 } |
| 66 | 58 |
| 67 // Create an instance of the safe browsing v4 protocol manager. | 59 // Create an instance of the safe browsing v4 protocol manager. |
| 68 static V4GetHashProtocolManager* Create( | 60 static V4GetHashProtocolManager* Create( |
| 69 net::URLRequestContextGetter* request_context_getter, | 61 net::URLRequestContextGetter* request_context_getter, |
| 70 const V4GetHashProtocolConfig& config); | 62 const V4ProtocolConfig& config); |
| 71 | 63 |
| 72 // net::URLFetcherDelegate interface. | 64 // net::URLFetcherDelegate interface. |
| 73 void OnURLFetchComplete(const net::URLFetcher* source) override; | 65 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 74 | 66 |
| 75 // Retrieve the full hash for a set of prefixes, and invoke the callback | 67 // Retrieve the full hash for a set of prefixes, and invoke the callback |
| 76 // argument when the results are retrieved. The callback may be invoked | 68 // argument when the results are retrieved. The callback may be invoked |
| 77 // synchronously. | 69 // synchronously. |
| 78 virtual void GetFullHashes(const std::vector<SBPrefix>& prefixes, | 70 virtual void GetFullHashes(const std::vector<SBPrefix>& prefixes, |
| 79 const std::vector<PlatformType>& platforms, | 71 const std::vector<PlatformType>& platforms, |
| 80 ThreatType threat_type, | 72 ThreatType threat_type, |
| (...skipping 28 matching lines...) Expand all Loading... |
| 109 GET_HASH_MIN_WAIT_DURATION_ERROR = 5, | 101 GET_HASH_MIN_WAIT_DURATION_ERROR = 5, |
| 110 | 102 |
| 111 // Memory space for histograms is determined by the max. ALWAYS | 103 // Memory space for histograms is determined by the max. ALWAYS |
| 112 // ADD NEW VALUES BEFORE THIS ONE. | 104 // ADD NEW VALUES BEFORE THIS ONE. |
| 113 GET_HASH_RESULT_MAX = 6 | 105 GET_HASH_RESULT_MAX = 6 |
| 114 }; | 106 }; |
| 115 | 107 |
| 116 // Record a GetHash result. | 108 // Record a GetHash result. |
| 117 static void RecordGetHashResult(ResultType result_type); | 109 static void RecordGetHashResult(ResultType result_type); |
| 118 | 110 |
| 119 // Record HTTP response code when there's no error in fetching an HTTP | |
| 120 // request, and the error code, when there is. | |
| 121 // |metric_name| is the name of the UMA metric to record the response code or | |
| 122 // error code against, |status| represents the status of the HTTP request, and | |
| 123 // |response code| represents the HTTP response code received from the server. | |
| 124 static void RecordHttpResponseOrErrorCode(const char* metric_name, | |
| 125 const net::URLRequestStatus& status, | |
| 126 int response_code); | |
| 127 | |
| 128 protected: | 111 protected: |
| 129 // Constructs a V4GetHashProtocolManager that issues | 112 // Constructs a V4GetHashProtocolManager that issues |
| 130 // network requests using |request_context_getter|. | 113 // network requests using |request_context_getter|. |
| 131 V4GetHashProtocolManager(net::URLRequestContextGetter* request_context_getter, | 114 V4GetHashProtocolManager( |
| 132 const V4GetHashProtocolConfig& config); | 115 net::URLRequestContextGetter* request_context_getter, |
| 116 const V4ProtocolConfig& config); |
| 133 | 117 |
| 134 private: | 118 private: |
| 135 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 119 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, |
| 136 TestGetHashUrl); | |
| 137 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | |
| 138 TestGetHashRequest); | 120 TestGetHashRequest); |
| 139 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 121 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, |
| 140 TestParseHashResponse); | 122 TestParseHashResponse); |
| 141 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 123 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, |
| 142 TestParseHashResponseWrongThreatEntryType); | 124 TestParseHashResponseWrongThreatEntryType); |
| 143 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 125 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, |
| 144 TestParseHashResponseSocialEngineeringThreatType); | 126 TestParseHashResponseSocialEngineeringThreatType); |
| 145 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 127 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, |
| 146 TestParseHashResponseNonPermissionMetadata); | 128 TestParseHashResponseNonPermissionMetadata); |
| 147 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 129 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, |
| 148 TestParseHashResponseInconsistentThreatTypes); | 130 TestParseHashResponseInconsistentThreatTypes); |
| 149 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 131 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, |
| 150 TestGetHashBackOffTimes); | |
| 151 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | |
| 152 TestGetHashErrorHandlingOK); | 132 TestGetHashErrorHandlingOK); |
| 153 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 133 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, |
| 154 TestGetHashErrorHandlingNetwork); | 134 TestGetHashErrorHandlingNetwork); |
| 155 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, | 135 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4GetHashProtocolManagerTest, |
| 156 TestGetHashErrorHandlingResponseCode); | 136 TestGetHashErrorHandlingResponseCode); |
| 157 friend class V4GetHashProtocolManagerFactoryImpl; | 137 friend class V4GetHashProtocolManagerFactoryImpl; |
| 158 | 138 |
| 159 // Generates GetHashWithApis Pver4 request URL for retrieving full hashes. | |
| 160 // |request_base64| is the serialized FindFullHashesRequest protocol buffer | |
| 161 // encoded in base 64. | |
| 162 GURL GetHashUrl(const std::string& request_base64) const; | 139 GURL GetHashUrl(const std::string& request_base64) const; |
| 163 | 140 |
| 164 // Fills a FindFullHashesRequest protocol buffer for a request. | 141 // Fills a FindFullHashesRequest protocol buffer for a request. |
| 165 // Returns the serialized and base 64 encoded request as a string. | 142 // Returns the serialized and base 64 encoded request as a string. |
| 166 std::string GetHashRequest(const std::vector<SBPrefix>& prefixes, | 143 std::string GetHashRequest(const std::vector<SBPrefix>& prefixes, |
| 167 const std::vector<PlatformType>& platforms, | 144 const std::vector<PlatformType>& platforms, |
| 168 ThreatType threat_type); | 145 ThreatType threat_type); |
| 169 | 146 |
| 170 // Composes a URL using |prefix|, |method| (e.g.: encodedFullHashes). | |
| 171 // |request_base64|, |client_id|, |version| and |key_param|. |prefix| | |
| 172 // should contain the entire url prefix including scheme, host and path. | |
| 173 static std::string ComposePver4Url(const std::string& prefix, | |
| 174 const std::string& method, | |
| 175 const std::string& request_base64, | |
| 176 const std::string& client_id, | |
| 177 const std::string& version, | |
| 178 const std::string& key_param); | |
| 179 | |
| 180 // Parses a FindFullHashesResponse protocol buffer and fills the results in | 147 // Parses a FindFullHashesResponse protocol buffer and fills the results in |
| 181 // |full_hashes| and |negative_cache_duration|. |data| is a serialized | 148 // |full_hashes| and |negative_cache_duration|. |data| is a serialized |
| 182 // FindFullHashes protocol buffer. |negative_cache_duration| is the duration | 149 // FindFullHashes protocol buffer. |negative_cache_duration| is the duration |
| 183 // to cache the response for entities that did not match the threat list. | 150 // to cache the response for entities that did not match the threat list. |
| 184 // Returns true if parsing is successful, false otherwise. | 151 // Returns true if parsing is successful, false otherwise. |
| 185 bool ParseHashResponse(const std::string& data_base64, | 152 bool ParseHashResponse(const std::string& data_base64, |
| 186 std::vector<SBFullHashResult>* full_hashes, | 153 std::vector<SBFullHashResult>* full_hashes, |
| 187 base::TimeDelta* negative_cache_duration); | 154 base::TimeDelta* negative_cache_duration); |
| 188 | 155 |
| 189 // Worker function for calculating the GetHash backoff times. | |
| 190 // |multiplier| is doubled for each consecutive error after the | |
| 191 // first, and |error_count| is incremented with each call. | |
| 192 static base::TimeDelta GetNextBackOffInterval(size_t* error_count, | |
| 193 size_t* multiplier); | |
| 194 | |
| 195 // Resets the gethash error counter and multiplier. | 156 // Resets the gethash error counter and multiplier. |
| 196 void ResetGetHashErrors(); | 157 void ResetGetHashErrors(); |
| 197 | 158 |
| 198 // Updates internal state for each GetHash response error, assuming that | 159 // Updates internal state for each GetHash response error, assuming that |
| 199 // the current time is |now|. | 160 // the current time is |now|. |
| 200 void HandleGetHashError(const base::Time& now); | 161 void HandleGetHashError(const base::Time& now); |
| 201 | 162 |
| 202 private: | 163 private: |
| 203 // Map of GetHash requests to parameters which created it. | 164 // Map of GetHash requests to parameters which created it. |
| 204 typedef base::hash_map<const net::URLFetcher*, FullHashCallback> HashRequests; | 165 typedef base::hash_map<const net::URLFetcher*, FullHashCallback> HashRequests; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 219 // Multiplier for the backoff error after the second. | 180 // Multiplier for the backoff error after the second. |
| 220 size_t gethash_back_off_mult_; | 181 size_t gethash_back_off_mult_; |
| 221 | 182 |
| 222 HashRequests hash_requests_; | 183 HashRequests hash_requests_; |
| 223 | 184 |
| 224 // For v4, the next gethash time is set to the backoff time is the last | 185 // For v4, the next gethash time is set to the backoff time is the last |
| 225 // response was an error, or the minimum wait time if the last response was | 186 // response was an error, or the minimum wait time if the last response was |
| 226 // successful. | 187 // successful. |
| 227 base::Time next_gethash_time_; | 188 base::Time next_gethash_time_; |
| 228 | 189 |
| 229 // Current product version sent in each request. | 190 // The config of the client making Pver4 requests. |
| 230 std::string version_; | 191 const V4ProtocolConfig config_; |
| 231 | |
| 232 // The safe browsing client name sent in each request. | |
| 233 std::string client_name_; | |
| 234 | |
| 235 // The Google API key. | |
| 236 std::string key_param_; | |
| 237 | 192 |
| 238 // The context we use to issue network requests. | 193 // The context we use to issue network requests. |
| 239 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 194 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 240 | 195 |
| 241 // ID for URLFetchers for testing. | 196 // ID for URLFetchers for testing. |
| 242 int url_fetcher_id_; | 197 int url_fetcher_id_; |
| 243 | 198 |
| 244 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManager); | 199 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManager); |
| 245 }; | 200 }; |
| 246 | 201 |
| 247 // Interface of a factory to create V4GetHashProtocolManager. Useful for tests. | 202 // Interface of a factory to create V4GetHashProtocolManager. Useful for tests. |
| 248 class V4GetHashProtocolManagerFactory { | 203 class V4GetHashProtocolManagerFactory { |
| 249 public: | 204 public: |
| 250 V4GetHashProtocolManagerFactory() {} | 205 V4GetHashProtocolManagerFactory() {} |
| 251 virtual ~V4GetHashProtocolManagerFactory() {} | 206 virtual ~V4GetHashProtocolManagerFactory() {} |
| 252 virtual V4GetHashProtocolManager* CreateProtocolManager( | 207 virtual V4GetHashProtocolManager* CreateProtocolManager( |
| 253 net::URLRequestContextGetter* request_context_getter, | 208 net::URLRequestContextGetter* request_context_getter, |
| 254 const V4GetHashProtocolConfig& config) = 0; | 209 const V4ProtocolConfig& config) = 0; |
| 255 | 210 |
| 256 private: | 211 private: |
| 257 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManagerFactory); | 212 DISALLOW_COPY_AND_ASSIGN(V4GetHashProtocolManagerFactory); |
| 258 }; | 213 }; |
| 259 | 214 |
| 260 } // namespace safe_browsing | 215 } // namespace safe_browsing |
| 261 | 216 |
| 262 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ | 217 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_GET_HASH_PROTOCOL_MANAGER_H_ |
| OLD | NEW |