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" | |
17 #include "components/safe_browsing_db/safebrowsing.pb.h" | 16 #include "components/safe_browsing_db/safebrowsing.pb.h" |
18 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
19 #include "url/gurl.h" | 18 #include "url/gurl.h" |
20 | 19 |
21 namespace net { | 20 namespace net { |
22 class HttpRequestHeaders; | 21 class HttpRequestHeaders; |
23 } // namespace net | 22 } // namespace net |
24 | 23 |
25 namespace safe_browsing { | 24 namespace safe_browsing { |
26 | 25 |
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 | |
33 typedef FetchThreatListUpdatesRequest::ListUpdateRequest ListUpdateRequest; | 26 typedef FetchThreatListUpdatesRequest::ListUpdateRequest ListUpdateRequest; |
34 typedef FetchThreatListUpdatesResponse::ListUpdateResponse ListUpdateResponse; | 27 typedef FetchThreatListUpdatesResponse::ListUpdateResponse ListUpdateResponse; |
35 | 28 |
36 // Config passed to the constructor of a V4 protocol manager. | 29 // Config passed to the constructor of a V4 protocol manager. |
37 struct V4ProtocolConfig { | 30 struct V4ProtocolConfig { |
38 // The safe browsing client name sent in each request. | 31 // The safe browsing client name sent in each request. |
39 std::string client_name; | 32 std::string client_name; |
40 | 33 |
41 // Current product version sent in each request. | 34 // Current product version sent in each request. |
42 std::string version; | 35 std::string version; |
(...skipping 27 matching lines...) Expand all Loading... |
70 bool operator==(const UpdateListIdentifier& other) const; | 63 bool operator==(const UpdateListIdentifier& other) const; |
71 bool operator!=(const UpdateListIdentifier& other) const; | 64 bool operator!=(const UpdateListIdentifier& other) const; |
72 size_t hash() const; | 65 size_t hash() const; |
73 | 66 |
74 private: | 67 private: |
75 UpdateListIdentifier(); | 68 UpdateListIdentifier(); |
76 }; | 69 }; |
77 | 70 |
78 std::ostream& operator<<(std::ostream& os, const UpdateListIdentifier& id); | 71 std::ostream& operator<<(std::ostream& os, const UpdateListIdentifier& id); |
79 | 72 |
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 | |
100 // The set of interesting lists and ASCII filenames for their hash prefix | 73 // The set of interesting lists and ASCII filenames for their hash prefix |
101 // stores. The stores are created inside the user-data directory. | 74 // stores. The stores are created inside the user-data directory. |
102 // For instance, the UpdateListIdentifier could be for URL expressions for UwS | 75 // For instance, the UpdateListIdentifier could be for URL expressions for UwS |
103 // on Windows platform, and the corresponding file on disk could be named: | 76 // on Windows platform, and the corresponding file on disk could be named: |
104 // "uws_win_url.store" | 77 // "uws_win_url.store" |
105 // TODO(vakh): Find the canonical place where these are defined and update the | 78 // TODO(vakh): Find the canonical place where these are defined and update the |
106 // comment to point to that place. | 79 // comment to point to that place. |
107 typedef base::hash_map<UpdateListIdentifier, std::string> StoreFileNameMap; | 80 typedef base::hash_map<UpdateListIdentifier, std::string> StoreFileNameMap; |
108 | 81 |
109 // Represents the state of each store. | 82 // Represents the state of each store. |
(...skipping 27 matching lines...) Expand all Loading... |
137 ALREADY_PENDING_ERROR = 6, | 110 ALREADY_PENDING_ERROR = 6, |
138 | 111 |
139 // Memory space for histograms is determined by the max. ALWAYS | 112 // Memory space for histograms is determined by the max. ALWAYS |
140 // ADD NEW VALUES BEFORE THIS ONE. | 113 // ADD NEW VALUES BEFORE THIS ONE. |
141 OPERATION_RESULT_MAX = 7 | 114 OPERATION_RESULT_MAX = 7 |
142 }; | 115 }; |
143 | 116 |
144 // A class that provides static methods related to the Pver4 protocol. | 117 // A class that provides static methods related to the Pver4 protocol. |
145 class V4ProtocolManagerUtil { | 118 class V4ProtocolManagerUtil { |
146 public: | 119 public: |
147 // Canonicalizes url as per Google Safe Browsing Specification. | 120 // Record HTTP response code when there's no error in fetching an HTTP |
148 // See: https://developers.google.com/safe-browsing/v4/urls-hashing | 121 // request, and the error code, when there is. |
149 static void CanonicalizeUrl(const GURL& url, | 122 // |metric_name| is the name of the UMA metric to record the response code or |
150 std::string* canonicalized_hostname, | 123 // error code against, |status| represents the status of the HTTP request, and |
151 std::string* canonicalized_path, | 124 // |response code| represents the HTTP response code received from the server. |
152 std::string* canonicalized_query); | 125 static void RecordHttpResponseOrErrorCode(const char* metric_name, |
153 | 126 const net::URLRequestStatus& status, |
154 // This method returns the host suffix combinations from the hostname in the | 127 int response_code); |
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); | |
170 | 128 |
171 // Generates a Pver4 request URL and sets the appropriate header values. | 129 // Generates a Pver4 request URL and sets the appropriate header values. |
172 // |request_base64| is the serialized request protocol buffer encoded in | 130 // |request_base64| is the serialized request protocol buffer encoded in |
173 // base 64. | 131 // base 64. |
174 // |method_name| is the name of the method to call, as specified in the proto, | 132 // |method_name| is the name of the method to call, as specified in the proto, |
175 // |config| is an instance of V4ProtocolConfig that stores the client config, | 133 // |config| is an instance of V4ProtocolConfig that stores the client config, |
176 // |gurl| is set to the value of the PVer4 request URL, | 134 // |gurl| is set to the value of the PVer4 request URL, |
177 // |headers| is populated with the appropriate header values. | 135 // |headers| is populated with the appropriate header values. |
178 static void GetRequestUrlAndHeaders(const std::string& request_base64, | 136 static void GetRequestUrlAndHeaders(const std::string& request_base64, |
179 const std::string& method_name, | 137 const std::string& method_name, |
180 const V4ProtocolConfig& config, | 138 const V4ProtocolConfig& config, |
181 GURL* gurl, | 139 GURL* gurl, |
182 net::HttpRequestHeaders* headers); | 140 net::HttpRequestHeaders* headers); |
183 | 141 |
184 // Worker function for calculating the backoff times. | 142 // Worker function for calculating the backoff times. |
185 // |multiplier| is doubled for each consecutive error after the | 143 // |multiplier| is doubled for each consecutive error after the |
186 // first, and |error_count| is incremented with each call. | 144 // first, and |error_count| is incremented with each call. |
187 static base::TimeDelta GetNextBackOffInterval(size_t* error_count, | 145 static base::TimeDelta GetNextBackOffInterval(size_t* error_count, |
188 size_t* multiplier); | 146 size_t* multiplier); |
189 | 147 |
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 | |
203 private: | 148 private: |
204 V4ProtocolManagerUtil(){}; | 149 V4ProtocolManagerUtil(){}; |
205 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, TestBackOffLogic); | 150 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4ProtocolManagerUtilTest, |
206 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, | 151 TestBackOffLogic); |
| 152 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4ProtocolManagerUtilTest, |
207 TestGetRequestUrlAndUpdateHeaders); | 153 TestGetRequestUrlAndUpdateHeaders); |
208 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, UrlParsing); | |
209 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, CanonicalizeUrl); | |
210 | 154 |
211 // Composes a URL using |prefix|, |method| (e.g.: encodedFullHashes). | 155 // Composes a URL using |prefix|, |method| (e.g.: encodedFullHashes). |
212 // |request_base64|, |client_id|, |version| and |key_param|. |prefix| | 156 // |request_base64|, |client_id|, |version| and |key_param|. |prefix| |
213 // should contain the entire url prefix including scheme, host and path. | 157 // should contain the entire url prefix including scheme, host and path. |
214 static std::string ComposeUrl(const std::string& prefix, | 158 static std::string ComposeUrl(const std::string& prefix, |
215 const std::string& method, | 159 const std::string& method, |
216 const std::string& request_base64, | 160 const std::string& request_base64, |
217 const std::string& key_param); | 161 const std::string& key_param); |
218 | 162 |
219 // Sets the HTTP headers expected by a standard PVer4 request. | 163 // Sets the HTTP headers expected by a standard PVer4 request. |
220 static void UpdateHeaders(net::HttpRequestHeaders* headers); | 164 static void UpdateHeaders(net::HttpRequestHeaders* headers); |
221 | 165 |
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 | |
234 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil); | 166 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil); |
235 }; | 167 }; |
236 | 168 |
237 } // namespace safe_browsing | 169 } // namespace safe_browsing |
238 | 170 |
239 namespace std { | 171 namespace std { |
240 template <> | 172 template <> |
241 struct hash<safe_browsing::UpdateListIdentifier> { | 173 struct hash<safe_browsing::UpdateListIdentifier> { |
242 std::size_t operator()(const safe_browsing::UpdateListIdentifier& s) const { | 174 std::size_t operator()(const safe_browsing::UpdateListIdentifier& s) const { |
243 return s.hash(); | 175 return s.hash(); |
244 } | 176 } |
245 }; | 177 }; |
246 } | 178 } |
247 | 179 |
248 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ | 180 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ |
OLD | NEW |