| 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_PROTOCOL_MANAGER_UTIL_H_ | 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ |
| 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ | 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ |
| 7 | 7 |
| 8 // A class that implements the stateless methods used by the GetHashUpdate and | 8 // A class that implements the stateless methods used by the GetHashUpdate and |
| 9 // GetFullHash stubby calls made by Chrome using the SafeBrowsing V4 protocol. | 9 // GetFullHash stubby calls made by Chrome using the SafeBrowsing V4 protocol. |
| 10 | 10 |
| 11 #include <ostream> | 11 #include <ostream> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/hash.h" | 15 #include "base/hash.h" |
| 16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
| 17 #include "components/safe_browsing_db/safebrowsing.pb.h" | 17 #include "components/safe_browsing_db/safebrowsing.pb.h" |
| 18 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
| 19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 20 | 20 |
| 21 namespace net { | 21 namespace net { |
| 22 class HttpRequestHeaders; | 22 class HttpRequestHeaders; |
| 23 } // namespace net | 23 } // namespace net |
| 24 | 24 |
| 25 namespace safe_browsing { | 25 namespace safe_browsing { |
| 26 | 26 |
| 27 // The size of the hash prefix, in bytes. It should be between 4 to 32 (full |
| 28 // hash). |
| 29 typedef size_t PrefixSize; |
| 30 |
| 31 // The minimum expected size (in bytes) of a hash-prefix. |
| 32 const PrefixSize kMinHashPrefixLength = 4; |
| 33 |
| 34 // The maximum expected size (in bytes) of a hash-prefix. This represents the |
| 35 // length of a SHA256 hash. |
| 36 const PrefixSize kMaxHashPrefixLength = 32; |
| 37 |
| 27 // A hash prefix sent by the SafeBrowsing PVer4 service. | 38 // A hash prefix sent by the SafeBrowsing PVer4 service. |
| 28 typedef std::string HashPrefix; | 39 typedef std::string HashPrefix; |
| 29 | 40 |
| 30 // A full SHA256 hash. | 41 // A full SHA256 hash. |
| 31 typedef HashPrefix FullHash; | 42 typedef HashPrefix FullHash; |
| 32 | 43 |
| 33 typedef FetchThreatListUpdatesRequest::ListUpdateRequest ListUpdateRequest; | 44 typedef FetchThreatListUpdatesRequest::ListUpdateRequest ListUpdateRequest; |
| 34 typedef FetchThreatListUpdatesResponse::ListUpdateResponse ListUpdateResponse; | 45 typedef FetchThreatListUpdatesResponse::ListUpdateResponse ListUpdateResponse; |
| 35 | 46 |
| 36 // Config passed to the constructor of a V4 protocol manager. | 47 // Config passed to the constructor of a V4 protocol manager. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 bool operator==(const UpdateListIdentifier& other) const; | 81 bool operator==(const UpdateListIdentifier& other) const; |
| 71 bool operator!=(const UpdateListIdentifier& other) const; | 82 bool operator!=(const UpdateListIdentifier& other) const; |
| 72 size_t hash() const; | 83 size_t hash() const; |
| 73 | 84 |
| 74 private: | 85 private: |
| 75 UpdateListIdentifier(); | 86 UpdateListIdentifier(); |
| 76 }; | 87 }; |
| 77 | 88 |
| 78 std::ostream& operator<<(std::ostream& os, const UpdateListIdentifier& id); | 89 std::ostream& operator<<(std::ostream& os, const UpdateListIdentifier& id); |
| 79 | 90 |
| 91 PlatformType GetCurrentPlatformType(); |
| 80 const UpdateListIdentifier GetUrlMalwareId(); | 92 const UpdateListIdentifier GetUrlMalwareId(); |
| 81 const UpdateListIdentifier GetUrlSocEngId(); | 93 const UpdateListIdentifier GetUrlSocEngId(); |
| 82 | 94 const UpdateListIdentifier GetChromeUrlApiId(); |
| 83 // The set of interesting lists and ASCII filenames for their hash prefix | |
| 84 // stores. The stores are created inside the user-data directory. | |
| 85 // For instance, the UpdateListIdentifier could be for URL expressions for UwS | |
| 86 // on Windows platform, and the corresponding file on disk could be named: | |
| 87 // "uws_win_url.store" | |
| 88 // TODO(vakh): Find the canonical place where these are defined and update the | |
| 89 // comment to point to that place. | |
| 90 typedef base::hash_map<UpdateListIdentifier, std::string> StoreFileNameMap; | |
| 91 | 95 |
| 92 // Represents the state of each store. | 96 // Represents the state of each store. |
| 93 typedef base::hash_map<UpdateListIdentifier, std::string> StoreStateMap; | 97 typedef base::hash_map<UpdateListIdentifier, std::string> StoreStateMap; |
| 94 | 98 |
| 95 // Sever response, parsed in vector form. | 99 // Sever response, parsed in vector form. |
| 96 typedef std::vector<std::unique_ptr<ListUpdateResponse>> ParsedServerResponse; | 100 typedef std::vector<std::unique_ptr<ListUpdateResponse>> ParsedServerResponse; |
| 97 | 101 |
| 102 // TODO(vakh): Consider using a std::pair for this. |
| 103 // Holds the hash prefix and the store that it matched in. |
| 104 struct StoreAndHashPrefix { |
| 105 public: |
| 106 UpdateListIdentifier list_id; |
| 107 HashPrefix hash_prefix; |
| 108 |
| 109 explicit StoreAndHashPrefix(UpdateListIdentifier, HashPrefix); |
| 110 ~StoreAndHashPrefix(); |
| 111 |
| 112 bool operator==(const StoreAndHashPrefix& other) const; |
| 113 bool operator!=(const StoreAndHashPrefix& other) const; |
| 114 size_t hash() const; |
| 115 |
| 116 private: |
| 117 StoreAndHashPrefix(); |
| 118 }; |
| 119 |
| 120 // Used to track the hash prefix and the store in which a full hash's prefix |
| 121 // matched. |
| 122 typedef std::vector<StoreAndHashPrefix> StoreAndHashPrefixes; |
| 123 |
| 98 // Enumerate failures for histogramming purposes. DO NOT CHANGE THE | 124 // Enumerate failures for histogramming purposes. DO NOT CHANGE THE |
| 99 // ORDERING OF THESE VALUES. | 125 // ORDERING OF THESE VALUES. |
| 100 enum V4OperationResult { | 126 enum V4OperationResult { |
| 101 // 200 response code means that the server recognized the request. | 127 // 200 response code means that the server recognized the request. |
| 102 STATUS_200 = 0, | 128 STATUS_200 = 0, |
| 103 | 129 |
| 104 // Subset of successful responses where the response body wasn't parsable. | 130 // Subset of successful responses where the response body wasn't parsable. |
| 105 PARSE_ERROR = 1, | 131 PARSE_ERROR = 1, |
| 106 | 132 |
| 107 // Operation request failed (network error). | 133 // Operation request failed (network error). |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 // error code against, |status| represents the status of the HTTP request, and | 202 // error code against, |status| represents the status of the HTTP request, and |
| 177 // |response code| represents the HTTP response code received from the server. | 203 // |response code| represents the HTTP response code received from the server. |
| 178 static void RecordHttpResponseOrErrorCode(const char* metric_name, | 204 static void RecordHttpResponseOrErrorCode(const char* metric_name, |
| 179 const net::URLRequestStatus& status, | 205 const net::URLRequestStatus& status, |
| 180 int response_code); | 206 int response_code); |
| 181 | 207 |
| 182 // Generate the set of FullHashes to check for |url|. | 208 // Generate the set of FullHashes to check for |url|. |
| 183 static void UrlToFullHashes(const GURL& url, | 209 static void UrlToFullHashes(const GURL& url, |
| 184 base::hash_set<FullHash>* full_hashes); | 210 base::hash_set<FullHash>* full_hashes); |
| 185 | 211 |
| 212 static bool FullHashToHashPrefix(const FullHash& full_hash, |
| 213 PrefixSize prefix_size, |
| 214 HashPrefix* hash_prefix); |
| 215 |
| 216 static bool FullHashToSmallestHashPrefix(const FullHash& full_hash, |
| 217 HashPrefix* hash_prefix); |
| 218 |
| 219 static bool FullHashMatchesHashPrefix(const FullHash& full_hash, |
| 220 const HashPrefix& hash_prefix); |
| 221 |
| 186 private: | 222 private: |
| 187 V4ProtocolManagerUtil(){}; | 223 V4ProtocolManagerUtil(){}; |
| 188 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, TestBackOffLogic); | 224 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, TestBackOffLogic); |
| 189 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, | 225 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, |
| 190 TestGetRequestUrlAndUpdateHeaders); | 226 TestGetRequestUrlAndUpdateHeaders); |
| 191 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, UrlParsing); | 227 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, UrlParsing); |
| 192 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, CanonicalizeUrl); | 228 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, CanonicalizeUrl); |
| 193 | 229 |
| 194 // Composes a URL using |prefix|, |method| (e.g.: encodedFullHashes). | 230 // Composes a URL using |prefix|, |method| (e.g.: encodedFullHashes). |
| 195 // |request_base64|, |client_id|, |version| and |key_param|. |prefix| | 231 // |request_base64|, |client_id|, |version| and |key_param|. |prefix| |
| (...skipping 18 matching lines...) Expand all Loading... |
| 214 static std::string RemoveConsecutiveChars(base::StringPiece str, | 250 static std::string RemoveConsecutiveChars(base::StringPiece str, |
| 215 const char c); | 251 const char c); |
| 216 | 252 |
| 217 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil); | 253 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil); |
| 218 }; | 254 }; |
| 219 | 255 |
| 220 } // namespace safe_browsing | 256 } // namespace safe_browsing |
| 221 | 257 |
| 222 namespace std { | 258 namespace std { |
| 223 template <> | 259 template <> |
| 260 struct hash<safe_browsing::PlatformType> { |
| 261 std::size_t operator()(const safe_browsing::PlatformType& p) const { |
| 262 return std::hash<unsigned int>()(p); |
| 263 } |
| 264 }; |
| 265 |
| 266 template <> |
| 267 struct hash<safe_browsing::ThreatEntryType> { |
| 268 std::size_t operator()(const safe_browsing::ThreatEntryType& tet) const { |
| 269 return std::hash<unsigned int>()(tet); |
| 270 } |
| 271 }; |
| 272 |
| 273 template <> |
| 274 struct hash<safe_browsing::ThreatType> { |
| 275 std::size_t operator()(const safe_browsing::ThreatType& tt) const { |
| 276 return std::hash<unsigned int>()(tt); |
| 277 } |
| 278 }; |
| 279 |
| 280 template <> |
| 224 struct hash<safe_browsing::UpdateListIdentifier> { | 281 struct hash<safe_browsing::UpdateListIdentifier> { |
| 225 std::size_t operator()(const safe_browsing::UpdateListIdentifier& s) const { | 282 std::size_t operator()(const safe_browsing::UpdateListIdentifier& id) const { |
| 226 return s.hash(); | 283 return id.hash(); |
| 227 } | 284 } |
| 228 }; | 285 }; |
| 229 } | 286 } |
| 230 | 287 |
| 231 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ | 288 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ |
| OLD | NEW |