| 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 "components/safe_browsing_db/safebrowsing.pb.h" | 17 #include "components/safe_browsing_db/safebrowsing.pb.h" |
| 17 #include "net/url_request/url_request_status.h" | 18 #include "net/url_request/url_request_status.h" |
| 18 #include "url/gurl.h" | 19 #include "url/gurl.h" |
| 19 | 20 |
| 20 namespace net { | 21 namespace net { |
| 21 class HttpRequestHeaders; | 22 class HttpRequestHeaders; |
| 22 } // namespace net | 23 } // namespace net |
| 23 | 24 |
| 24 namespace safe_browsing { | 25 namespace safe_browsing { |
| 25 | 26 |
| 27 // A hash prefix sent by the SafeBrowsing PVer4 service. |
| 28 typedef std::string HashPrefix; |
| 29 |
| 30 // A full SHA256 hash. |
| 31 typedef HashPrefix FullHash; |
| 32 |
| 26 typedef FetchThreatListUpdatesRequest::ListUpdateRequest ListUpdateRequest; | 33 typedef FetchThreatListUpdatesRequest::ListUpdateRequest ListUpdateRequest; |
| 27 typedef FetchThreatListUpdatesResponse::ListUpdateResponse ListUpdateResponse; | 34 typedef FetchThreatListUpdatesResponse::ListUpdateResponse ListUpdateResponse; |
| 28 | 35 |
| 29 // Config passed to the constructor of a V4 protocol manager. | 36 // Config passed to the constructor of a V4 protocol manager. |
| 30 struct V4ProtocolConfig { | 37 struct V4ProtocolConfig { |
| 31 // The safe browsing client name sent in each request. | 38 // The safe browsing client name sent in each request. |
| 32 std::string client_name; | 39 std::string client_name; |
| 33 | 40 |
| 34 // Current product version sent in each request. | 41 // Current product version sent in each request. |
| 35 std::string version; | 42 std::string version; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 63 bool operator==(const UpdateListIdentifier& other) const; | 70 bool operator==(const UpdateListIdentifier& other) const; |
| 64 bool operator!=(const UpdateListIdentifier& other) const; | 71 bool operator!=(const UpdateListIdentifier& other) const; |
| 65 size_t hash() const; | 72 size_t hash() const; |
| 66 | 73 |
| 67 private: | 74 private: |
| 68 UpdateListIdentifier(); | 75 UpdateListIdentifier(); |
| 69 }; | 76 }; |
| 70 | 77 |
| 71 std::ostream& operator<<(std::ostream& os, const UpdateListIdentifier& id); | 78 std::ostream& operator<<(std::ostream& os, const UpdateListIdentifier& id); |
| 72 | 79 |
| 80 #if defined(OS_WIN) |
| 81 #define PLATFORM_TYPE WINDOWS_PLATFORM |
| 82 #elif defined(OS_LINUX) |
| 83 #define PLATFORM_TYPE LINUX_PLATFORM |
| 84 #elif defined(OS_MACOSX) |
| 85 #define PLATFORM_TYPE OSX_PLATFORM |
| 86 #else |
| 87 // This should ideally never compile but it is getting compiled on Android. |
| 88 // See: https://bugs.chromium.org/p/chromium/issues/detail?id=621647 |
| 89 // TODO(vakh): Once that bug is fixed, this should be removed. If we leave |
| 90 // the platform_type empty, the server won't recognize the request and |
| 91 // return an error response which will pollute our UMA metrics. |
| 92 #define PLATFORM_TYPE LINUX_PLATFORM |
| 93 #endif |
| 94 |
| 95 const UpdateListIdentifier kUrlMalwareId(PLATFORM_TYPE, URL, MALWARE_THREAT); |
| 96 const UpdateListIdentifier kUrlSocengId(PLATFORM_TYPE, |
| 97 URL, |
| 98 SOCIAL_ENGINEERING_PUBLIC); |
| 99 |
| 73 // The set of interesting lists and ASCII filenames for their hash prefix | 100 // The set of interesting lists and ASCII filenames for their hash prefix |
| 74 // stores. The stores are created inside the user-data directory. | 101 // stores. The stores are created inside the user-data directory. |
| 75 // For instance, the UpdateListIdentifier could be for URL expressions for UwS | 102 // For instance, the UpdateListIdentifier could be for URL expressions for UwS |
| 76 // on Windows platform, and the corresponding file on disk could be named: | 103 // on Windows platform, and the corresponding file on disk could be named: |
| 77 // "uws_win_url.store" | 104 // "uws_win_url.store" |
| 78 // TODO(vakh): Find the canonical place where these are defined and update the | 105 // TODO(vakh): Find the canonical place where these are defined and update the |
| 79 // comment to point to that place. | 106 // comment to point to that place. |
| 80 typedef base::hash_map<UpdateListIdentifier, std::string> StoreFileNameMap; | 107 typedef base::hash_map<UpdateListIdentifier, std::string> StoreFileNameMap; |
| 81 | 108 |
| 82 // Represents the state of each store. | 109 // Represents the state of each store. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 110 ALREADY_PENDING_ERROR = 6, | 137 ALREADY_PENDING_ERROR = 6, |
| 111 | 138 |
| 112 // Memory space for histograms is determined by the max. ALWAYS | 139 // Memory space for histograms is determined by the max. ALWAYS |
| 113 // ADD NEW VALUES BEFORE THIS ONE. | 140 // ADD NEW VALUES BEFORE THIS ONE. |
| 114 OPERATION_RESULT_MAX = 7 | 141 OPERATION_RESULT_MAX = 7 |
| 115 }; | 142 }; |
| 116 | 143 |
| 117 // A class that provides static methods related to the Pver4 protocol. | 144 // A class that provides static methods related to the Pver4 protocol. |
| 118 class V4ProtocolManagerUtil { | 145 class V4ProtocolManagerUtil { |
| 119 public: | 146 public: |
| 120 // Record HTTP response code when there's no error in fetching an HTTP | 147 // Canonicalizes url as per Google Safe Browsing Specification. |
| 121 // request, and the error code, when there is. | 148 // See: https://developers.google.com/safe-browsing/v4/urls-hashing |
| 122 // |metric_name| is the name of the UMA metric to record the response code or | 149 static void CanonicalizeUrl(const GURL& url, |
| 123 // error code against, |status| represents the status of the HTTP request, and | 150 std::string* canonicalized_hostname, |
| 124 // |response code| represents the HTTP response code received from the server. | 151 std::string* canonicalized_path, |
| 125 static void RecordHttpResponseOrErrorCode(const char* metric_name, | 152 std::string* canonicalized_query); |
| 126 const net::URLRequestStatus& status, | 153 |
| 127 int response_code); | 154 // This method returns the host suffix combinations from the hostname in the |
| 155 // URL, as described here: |
| 156 // https://developers.google.com/safe-browsing/v4/urls-hashing |
| 157 static void GenerateHostVariantsToCheck(const std::string& host, |
| 158 std::vector<std::string>* hosts); |
| 159 |
| 160 // This method returns the path prefix combinations from the path in the |
| 161 // URL, as described here: |
| 162 // https://developers.google.com/safe-browsing/v4/urls-hashing |
| 163 static void GeneratePathVariantsToCheck(const std::string& path, |
| 164 const std::string& query, |
| 165 std::vector<std::string>* paths); |
| 166 |
| 167 // Given a URL, returns all the patterns we need to check. |
| 168 static void GeneratePatternsToCheck(const GURL& url, |
| 169 std::vector<std::string>* urls); |
| 128 | 170 |
| 129 // Generates a Pver4 request URL and sets the appropriate header values. | 171 // Generates a Pver4 request URL and sets the appropriate header values. |
| 130 // |request_base64| is the serialized request protocol buffer encoded in | 172 // |request_base64| is the serialized request protocol buffer encoded in |
| 131 // base 64. | 173 // base 64. |
| 132 // |method_name| is the name of the method to call, as specified in the proto, | 174 // |method_name| is the name of the method to call, as specified in the proto, |
| 133 // |config| is an instance of V4ProtocolConfig that stores the client config, | 175 // |config| is an instance of V4ProtocolConfig that stores the client config, |
| 134 // |gurl| is set to the value of the PVer4 request URL, | 176 // |gurl| is set to the value of the PVer4 request URL, |
| 135 // |headers| is populated with the appropriate header values. | 177 // |headers| is populated with the appropriate header values. |
| 136 static void GetRequestUrlAndHeaders(const std::string& request_base64, | 178 static void GetRequestUrlAndHeaders(const std::string& request_base64, |
| 137 const std::string& method_name, | 179 const std::string& method_name, |
| 138 const V4ProtocolConfig& config, | 180 const V4ProtocolConfig& config, |
| 139 GURL* gurl, | 181 GURL* gurl, |
| 140 net::HttpRequestHeaders* headers); | 182 net::HttpRequestHeaders* headers); |
| 141 | 183 |
| 142 // Worker function for calculating the backoff times. | 184 // Worker function for calculating the backoff times. |
| 143 // |multiplier| is doubled for each consecutive error after the | 185 // |multiplier| is doubled for each consecutive error after the |
| 144 // first, and |error_count| is incremented with each call. | 186 // first, and |error_count| is incremented with each call. |
| 145 static base::TimeDelta GetNextBackOffInterval(size_t* error_count, | 187 static base::TimeDelta GetNextBackOffInterval(size_t* error_count, |
| 146 size_t* multiplier); | 188 size_t* multiplier); |
| 147 | 189 |
| 190 // Record HTTP response code when there's no error in fetching an HTTP |
| 191 // request, and the error code, when there is. |
| 192 // |metric_name| is the name of the UMA metric to record the response code or |
| 193 // error code against, |status| represents the status of the HTTP request, and |
| 194 // |response code| represents the HTTP response code received from the server. |
| 195 static void RecordHttpResponseOrErrorCode(const char* metric_name, |
| 196 const net::URLRequestStatus& status, |
| 197 int response_code); |
| 198 |
| 199 // Generate the set of FullHashes to check for |url|. |
| 200 static void UrlToFullHashes(const GURL& url, |
| 201 base::hash_set<FullHash>* full_hashes); |
| 202 |
| 148 private: | 203 private: |
| 149 V4ProtocolManagerUtil(){}; | 204 V4ProtocolManagerUtil(){}; |
| 150 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4ProtocolManagerUtilTest, | 205 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, TestBackOffLogic); |
| 151 TestBackOffLogic); | 206 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, |
| 152 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4ProtocolManagerUtilTest, | |
| 153 TestGetRequestUrlAndUpdateHeaders); | 207 TestGetRequestUrlAndUpdateHeaders); |
| 208 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, UrlParsing); |
| 209 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, CanonicalizeUrl); |
| 154 | 210 |
| 155 // Composes a URL using |prefix|, |method| (e.g.: encodedFullHashes). | 211 // Composes a URL using |prefix|, |method| (e.g.: encodedFullHashes). |
| 156 // |request_base64|, |client_id|, |version| and |key_param|. |prefix| | 212 // |request_base64|, |client_id|, |version| and |key_param|. |prefix| |
| 157 // should contain the entire url prefix including scheme, host and path. | 213 // should contain the entire url prefix including scheme, host and path. |
| 158 static std::string ComposeUrl(const std::string& prefix, | 214 static std::string ComposeUrl(const std::string& prefix, |
| 159 const std::string& method, | 215 const std::string& method, |
| 160 const std::string& request_base64, | 216 const std::string& request_base64, |
| 161 const std::string& key_param); | 217 const std::string& key_param); |
| 162 | 218 |
| 163 // Sets the HTTP headers expected by a standard PVer4 request. | 219 // Sets the HTTP headers expected by a standard PVer4 request. |
| 164 static void UpdateHeaders(net::HttpRequestHeaders* headers); | 220 static void UpdateHeaders(net::HttpRequestHeaders* headers); |
| 165 | 221 |
| 222 // Given a URL, returns all the hosts we need to check. They are returned |
| 223 // in order of size (i.e. b.c is first, then a.b.c). |
| 224 static void GenerateHostsToCheck(const GURL& url, |
| 225 std::vector<std::string>* hosts); |
| 226 |
| 227 // Given a URL, returns all the paths we need to check. |
| 228 static void GeneratePathsToCheck(const GURL& url, |
| 229 std::vector<std::string>* paths); |
| 230 |
| 231 static std::string RemoveConsecutiveChars(base::StringPiece str, |
| 232 const char c); |
| 233 |
| 166 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil); | 234 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil); |
| 167 }; | 235 }; |
| 168 | 236 |
| 169 } // namespace safe_browsing | 237 } // namespace safe_browsing |
| 170 | 238 |
| 171 namespace std { | 239 namespace std { |
| 172 template <> | 240 template <> |
| 173 struct hash<safe_browsing::UpdateListIdentifier> { | 241 struct hash<safe_browsing::UpdateListIdentifier> { |
| 174 std::size_t operator()(const safe_browsing::UpdateListIdentifier& s) const { | 242 std::size_t operator()(const safe_browsing::UpdateListIdentifier& s) const { |
| 175 return s.hash(); | 243 return s.hash(); |
| 176 } | 244 } |
| 177 }; | 245 }; |
| 178 } | 246 } |
| 179 | 247 |
| 180 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ | 248 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ |
| OLD | NEW |