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 const UpdateListIdentifier GetUrlMalwareId(); | |
81 const UpdateListIdentifier GetUrlSocengId(); | |
Nathan Parker
2016/08/08 20:40:49
nit: SocEng (capital e)
| |
82 | |
73 // The set of interesting lists and ASCII filenames for their hash prefix | 83 // The set of interesting lists and ASCII filenames for their hash prefix |
74 // stores. The stores are created inside the user-data directory. | 84 // stores. The stores are created inside the user-data directory. |
75 // For instance, the UpdateListIdentifier could be for URL expressions for UwS | 85 // For instance, the UpdateListIdentifier could be for URL expressions for UwS |
76 // on Windows platform, and the corresponding file on disk could be named: | 86 // on Windows platform, and the corresponding file on disk could be named: |
77 // "uws_win_url.store" | 87 // "uws_win_url.store" |
78 // TODO(vakh): Find the canonical place where these are defined and update the | 88 // TODO(vakh): Find the canonical place where these are defined and update the |
79 // comment to point to that place. | 89 // comment to point to that place. |
80 typedef base::hash_map<UpdateListIdentifier, std::string> StoreFileNameMap; | 90 typedef base::hash_map<UpdateListIdentifier, std::string> StoreFileNameMap; |
81 | 91 |
82 // Represents the state of each store. | 92 // Represents the state of each store. |
(...skipping 27 matching lines...) Expand all Loading... | |
110 ALREADY_PENDING_ERROR = 6, | 120 ALREADY_PENDING_ERROR = 6, |
111 | 121 |
112 // Memory space for histograms is determined by the max. ALWAYS | 122 // Memory space for histograms is determined by the max. ALWAYS |
113 // ADD NEW VALUES BEFORE THIS ONE. | 123 // ADD NEW VALUES BEFORE THIS ONE. |
114 OPERATION_RESULT_MAX = 7 | 124 OPERATION_RESULT_MAX = 7 |
115 }; | 125 }; |
116 | 126 |
117 // A class that provides static methods related to the Pver4 protocol. | 127 // A class that provides static methods related to the Pver4 protocol. |
118 class V4ProtocolManagerUtil { | 128 class V4ProtocolManagerUtil { |
119 public: | 129 public: |
120 // Record HTTP response code when there's no error in fetching an HTTP | 130 // Canonicalizes url as per Google Safe Browsing Specification. |
121 // request, and the error code, when there is. | 131 // 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 | 132 static void CanonicalizeUrl(const GURL& url, |
123 // error code against, |status| represents the status of the HTTP request, and | 133 std::string* canonicalized_hostname, |
124 // |response code| represents the HTTP response code received from the server. | 134 std::string* canonicalized_path, |
125 static void RecordHttpResponseOrErrorCode(const char* metric_name, | 135 std::string* canonicalized_query); |
126 const net::URLRequestStatus& status, | 136 |
127 int response_code); | 137 // This method returns the host suffix combinations from the hostname in the |
138 // URL, as described here: | |
139 // https://developers.google.com/safe-browsing/v4/urls-hashing | |
140 static void GenerateHostVariantsToCheck(const std::string& host, | |
141 std::vector<std::string>* hosts); | |
142 | |
143 // This method returns the path prefix combinations from the path in the | |
144 // URL, as described here: | |
145 // https://developers.google.com/safe-browsing/v4/urls-hashing | |
146 static void GeneratePathVariantsToCheck(const std::string& path, | |
147 const std::string& query, | |
148 std::vector<std::string>* paths); | |
149 | |
150 // Given a URL, returns all the patterns we need to check. | |
151 static void GeneratePatternsToCheck(const GURL& url, | |
152 std::vector<std::string>* urls); | |
128 | 153 |
129 // Generates a Pver4 request URL and sets the appropriate header values. | 154 // Generates a Pver4 request URL and sets the appropriate header values. |
130 // |request_base64| is the serialized request protocol buffer encoded in | 155 // |request_base64| is the serialized request protocol buffer encoded in |
131 // base 64. | 156 // base 64. |
132 // |method_name| is the name of the method to call, as specified in the proto, | 157 // |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, | 158 // |config| is an instance of V4ProtocolConfig that stores the client config, |
134 // |gurl| is set to the value of the PVer4 request URL, | 159 // |gurl| is set to the value of the PVer4 request URL, |
135 // |headers| is populated with the appropriate header values. | 160 // |headers| is populated with the appropriate header values. |
136 static void GetRequestUrlAndHeaders(const std::string& request_base64, | 161 static void GetRequestUrlAndHeaders(const std::string& request_base64, |
137 const std::string& method_name, | 162 const std::string& method_name, |
138 const V4ProtocolConfig& config, | 163 const V4ProtocolConfig& config, |
139 GURL* gurl, | 164 GURL* gurl, |
140 net::HttpRequestHeaders* headers); | 165 net::HttpRequestHeaders* headers); |
141 | 166 |
142 // Worker function for calculating the backoff times. | 167 // Worker function for calculating the backoff times. |
143 // |multiplier| is doubled for each consecutive error after the | 168 // |multiplier| is doubled for each consecutive error after the |
144 // first, and |error_count| is incremented with each call. | 169 // first, and |error_count| is incremented with each call. |
145 static base::TimeDelta GetNextBackOffInterval(size_t* error_count, | 170 static base::TimeDelta GetNextBackOffInterval(size_t* error_count, |
146 size_t* multiplier); | 171 size_t* multiplier); |
147 | 172 |
173 // Record HTTP response code when there's no error in fetching an HTTP | |
174 // request, and the error code, when there is. | |
175 // |metric_name| is the name of the UMA metric to record the response code or | |
176 // error code against, |status| represents the status of the HTTP request, and | |
177 // |response code| represents the HTTP response code received from the server. | |
178 static void RecordHttpResponseOrErrorCode(const char* metric_name, | |
179 const net::URLRequestStatus& status, | |
180 int response_code); | |
181 | |
182 // Generate the set of FullHashes to check for |url|. | |
183 static void UrlToFullHashes(const GURL& url, | |
184 base::hash_set<FullHash>* full_hashes); | |
185 | |
148 private: | 186 private: |
149 V4ProtocolManagerUtil(){}; | 187 V4ProtocolManagerUtil(){}; |
150 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4ProtocolManagerUtilTest, | 188 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, TestBackOffLogic); |
151 TestBackOffLogic); | 189 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, |
152 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4ProtocolManagerUtilTest, | |
153 TestGetRequestUrlAndUpdateHeaders); | 190 TestGetRequestUrlAndUpdateHeaders); |
191 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, UrlParsing); | |
192 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, CanonicalizeUrl); | |
154 | 193 |
155 // Composes a URL using |prefix|, |method| (e.g.: encodedFullHashes). | 194 // Composes a URL using |prefix|, |method| (e.g.: encodedFullHashes). |
156 // |request_base64|, |client_id|, |version| and |key_param|. |prefix| | 195 // |request_base64|, |client_id|, |version| and |key_param|. |prefix| |
157 // should contain the entire url prefix including scheme, host and path. | 196 // should contain the entire url prefix including scheme, host and path. |
158 static std::string ComposeUrl(const std::string& prefix, | 197 static std::string ComposeUrl(const std::string& prefix, |
159 const std::string& method, | 198 const std::string& method, |
160 const std::string& request_base64, | 199 const std::string& request_base64, |
161 const std::string& key_param); | 200 const std::string& key_param); |
162 | 201 |
163 // Sets the HTTP headers expected by a standard PVer4 request. | 202 // Sets the HTTP headers expected by a standard PVer4 request. |
164 static void UpdateHeaders(net::HttpRequestHeaders* headers); | 203 static void UpdateHeaders(net::HttpRequestHeaders* headers); |
165 | 204 |
205 // Given a URL, returns all the hosts we need to check. They are returned | |
206 // in order of size (i.e. b.c is first, then a.b.c). | |
207 static void GenerateHostsToCheck(const GURL& url, | |
208 std::vector<std::string>* hosts); | |
209 | |
210 // Given a URL, returns all the paths we need to check. | |
211 static void GeneratePathsToCheck(const GURL& url, | |
212 std::vector<std::string>* paths); | |
213 | |
214 static std::string RemoveConsecutiveChars(base::StringPiece str, | |
215 const char c); | |
216 | |
166 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil); | 217 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil); |
167 }; | 218 }; |
168 | 219 |
169 } // namespace safe_browsing | 220 } // namespace safe_browsing |
170 | 221 |
171 namespace std { | 222 namespace std { |
172 template <> | 223 template <> |
173 struct hash<safe_browsing::UpdateListIdentifier> { | 224 struct hash<safe_browsing::UpdateListIdentifier> { |
174 std::size_t operator()(const safe_browsing::UpdateListIdentifier& s) const { | 225 std::size_t operator()(const safe_browsing::UpdateListIdentifier& s) const { |
175 return s.hash(); | 226 return s.hash(); |
176 } | 227 } |
177 }; | 228 }; |
178 } | 229 } |
179 | 230 |
180 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ | 231 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ |
OLD | NEW |