Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: components/safe_browsing_db/v4_protocol_manager_util.h

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

Powered by Google App Engine
This is Rietveld 408576698