OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Helper class which handles communication with the SafeBrowsing backends for | 5 // Helper class which handles communication with the SafeBrowsing backends for |
6 // client-side phishing detection. This class is used to fetch the client-side | 6 // client-side phishing detection. This class is used to fetch the client-side |
7 // model and send it to all renderers. This class is also used to send a ping | 7 // model and send it to all renderers. This class is also used to send a ping |
8 // back to Google to verify if a particular site is really phishing or not. | 8 // back to Google to verify if a particular site is really phishing or not. |
9 // | 9 // |
10 // This class is not thread-safe and expects all calls to be made on the UI | 10 // This class is not thread-safe and expects all calls to be made on the UI |
11 // thread. We also expect that the calling thread runs a message loop. | 11 // thread. We also expect that the calling thread runs a message loop. |
12 | 12 |
13 #ifndef CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ | 13 #ifndef CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ |
14 #define CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ | 14 #define CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ |
15 #pragma once | 15 #pragma once |
16 | 16 |
17 #include <map> | 17 #include <map> |
18 #include <queue> | 18 #include <queue> |
19 #include <set> | 19 #include <set> |
20 #include <string> | 20 #include <string> |
21 #include <utility> | 21 #include <utility> |
22 #include <vector> | 22 #include <vector> |
23 | 23 |
24 #include "base/basictypes.h" | 24 #include "base/basictypes.h" |
25 #include "base/callback_old.h" | 25 #include "base/callback_old.h" |
26 #include "base/gtest_prod_util.h" | 26 #include "base/gtest_prod_util.h" |
27 #include "base/hash_tables.h" | |
28 #include "base/memory/linked_ptr.h" | 27 #include "base/memory/linked_ptr.h" |
29 #include "base/memory/ref_counted.h" | 28 #include "base/memory/ref_counted.h" |
30 #include "base/memory/scoped_ptr.h" | 29 #include "base/memory/scoped_ptr.h" |
31 #include "base/task.h" | 30 #include "base/task.h" |
32 #include "base/time.h" | 31 #include "base/time.h" |
33 #include "content/common/notification_observer.h" | 32 #include "content/common/notification_observer.h" |
34 #include "content/common/notification_registrar.h" | 33 #include "content/common/notification_registrar.h" |
35 #include "content/common/url_fetcher.h" | 34 #include "content/common/url_fetcher.h" |
36 #include "googleurl/src/gurl.h" | 35 #include "googleurl/src/gurl.h" |
37 #include "net/base/net_util.h" | 36 #include "net/base/net_util.h" |
38 | 37 |
39 class RenderProcessHost; | 38 class RenderProcessHost; |
40 class SafeBrowsingService; | |
41 | 39 |
42 namespace base { | 40 namespace base { |
43 class TimeDelta; | 41 class TimeDelta; |
44 } | 42 } |
45 | 43 |
46 namespace net { | 44 namespace net { |
47 class URLRequestContextGetter; | 45 class URLRequestContextGetter; |
48 class URLRequestStatus; | 46 class URLRequestStatus; |
49 } // namespace net | 47 } // namespace net |
50 | 48 |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 virtual void EndFetchModel(ClientModelStatus status); // Virtual for testing. | 159 virtual void EndFetchModel(ClientModelStatus status); // Virtual for testing. |
162 | 160 |
163 private: | 161 private: |
164 friend class ClientSideDetectionServiceTest; | 162 friend class ClientSideDetectionServiceTest; |
165 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, FetchModelTest); | 163 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, FetchModelTest); |
166 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, SetBadSubnets); | 164 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, SetBadSubnets); |
167 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, SetEnabled); | 165 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, SetEnabled); |
168 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, IsBadIpAddress); | 166 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, IsBadIpAddress); |
169 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, | 167 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, |
170 ModelHasValidHashIds); | 168 ModelHasValidHashIds); |
171 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, | |
172 SanitizeRequestForPingback); | |
173 | 169 |
174 // CacheState holds all information necessary to respond to a caller without | 170 // CacheState holds all information necessary to respond to a caller without |
175 // actually making a HTTP request. | 171 // actually making a HTTP request. |
176 struct CacheState { | 172 struct CacheState { |
177 bool is_phishing; | 173 bool is_phishing; |
178 base::Time timestamp; | 174 base::Time timestamp; |
179 | 175 |
180 CacheState(bool phish, base::Time time); | 176 CacheState(bool phish, base::Time time); |
181 }; | 177 }; |
182 typedef std::map<GURL, linked_ptr<CacheState> > PhishingCache; | 178 typedef std::map<GURL, linked_ptr<CacheState> > PhishingCache; |
(...skipping 10 matching lines...) Expand all Loading... |
193 static const char kClientReportPhishingUrl[]; | 189 static const char kClientReportPhishingUrl[]; |
194 static const char kClientModelUrl[]; | 190 static const char kClientModelUrl[]; |
195 static const size_t kMaxModelSizeBytes; | 191 static const size_t kMaxModelSizeBytes; |
196 static const int kMaxReportsPerInterval; | 192 static const int kMaxReportsPerInterval; |
197 static const int kClientModelFetchIntervalMs; | 193 static const int kClientModelFetchIntervalMs; |
198 static const int kInitialClientModelFetchDelayMs; | 194 static const int kInitialClientModelFetchDelayMs; |
199 static const base::TimeDelta kReportsInterval; | 195 static const base::TimeDelta kReportsInterval; |
200 static const base::TimeDelta kNegativeCacheInterval; | 196 static const base::TimeDelta kNegativeCacheInterval; |
201 static const base::TimeDelta kPositiveCacheInterval; | 197 static const base::TimeDelta kPositiveCacheInterval; |
202 | 198 |
203 // Given a ClientSidePhishingRequest populated by the renderer and browser | |
204 // feature extractors, sanitizes it so that no data specifically identifying | |
205 // the URL or page content is included. This is used when sending a pingback | |
206 // if the user is not opted in to UMA. | |
207 void SanitizeRequestForPingback( | |
208 const ClientPhishingRequest& original_request, | |
209 ClientPhishingRequest* sanitized_request); | |
210 | |
211 // Starts sending the request to the client-side detection frontends. | 199 // Starts sending the request to the client-side detection frontends. |
212 // This method takes ownership of both pointers. | 200 // This method takes ownership of both pointers. |
213 void StartClientReportPhishingRequest( | 201 void StartClientReportPhishingRequest( |
214 ClientPhishingRequest* verdict, | 202 ClientPhishingRequest* verdict, |
215 ClientReportPhishingRequestCallback* callback); | 203 ClientReportPhishingRequestCallback* callback); |
216 | 204 |
217 // Called by OnURLFetchComplete to handle the response from fetching the | 205 // Called by OnURLFetchComplete to handle the response from fetching the |
218 // model. | 206 // model. |
219 void HandleModelResponse(const URLFetcher* source, | 207 void HandleModelResponse(const URLFetcher* source, |
220 const GURL& url, | 208 const GURL& url, |
(...skipping 14 matching lines...) Expand all Loading... |
235 // Invalidate cache results which are no longer useful. | 223 // Invalidate cache results which are no longer useful. |
236 void UpdateCache(); | 224 void UpdateCache(); |
237 | 225 |
238 // Get the number of phishing reports that we have sent over kReportsInterval | 226 // Get the number of phishing reports that we have sent over kReportsInterval |
239 int GetNumReports(); | 227 int GetNumReports(); |
240 | 228 |
241 // Initializes the |private_networks_| vector with the network blocks | 229 // Initializes the |private_networks_| vector with the network blocks |
242 // that we consider non-public IP addresses. Returns true on success. | 230 // that we consider non-public IP addresses. Returns true on success. |
243 bool InitializePrivateNetworks(); | 231 bool InitializePrivateNetworks(); |
244 | 232 |
245 // Initializes the |allowed_features_| hash_set with the features that | |
246 // can be sent in sanitized pingbacks. | |
247 void InitializeAllowedFeatures(); | |
248 | |
249 // Send the model to the given renderer. | 233 // Send the model to the given renderer. |
250 void SendModelToProcess(RenderProcessHost* process); | 234 void SendModelToProcess(RenderProcessHost* process); |
251 | 235 |
252 // Same as above but sends the model to all rendereres. | 236 // Same as above but sends the model to all rendereres. |
253 void SendModelToRenderers(); | 237 void SendModelToRenderers(); |
254 | 238 |
255 // Reads the bad subnets from the client model and inserts them into | 239 // Reads the bad subnets from the client model and inserts them into |
256 // |bad_subnets| for faster lookups. This method is static to simplify | 240 // |bad_subnets| for faster lookups. This method is static to simplify |
257 // testing. | 241 // testing. |
258 static void SetBadSubnets(const ClientSideModel& model, | 242 static void SetBadSubnets(const ClientSideModel& model, |
259 BadSubnetMap* bad_subnets); | 243 BadSubnetMap* bad_subnets); |
260 | 244 |
261 | 245 |
262 // Returns true iff all the hash id's in the client-side model point to | 246 // Returns true iff all the hash id's in the client-side model point to |
263 // valid hashes in the model. | 247 // valid hashes in the model. |
264 static bool ModelHasValidHashIds(const ClientSideModel& model); | 248 static bool ModelHasValidHashIds(const ClientSideModel& model); |
265 | 249 |
266 // Whether the service is running or not. When the service is not running, | 250 // Whether the service is running or not. When the service is not running, |
267 // it won't download the model nor report detected phishing URLs. | 251 // it won't download the model nor report detected phishing URLs. |
268 bool enabled_; | 252 bool enabled_; |
269 | 253 |
270 std::string model_str_; | 254 std::string model_str_; |
271 scoped_ptr<ClientSideModel> model_; | 255 scoped_ptr<ClientSideModel> model_; |
272 scoped_ptr<base::TimeDelta> model_max_age_; | 256 scoped_ptr<base::TimeDelta> model_max_age_; |
273 scoped_ptr<URLFetcher> model_fetcher_; | 257 scoped_ptr<URLFetcher> model_fetcher_; |
274 | 258 |
275 // This pointer may be NULL if SafeBrowsing is disabled. | |
276 scoped_refptr<SafeBrowsingService> sb_service_; | |
277 | |
278 // Map of client report phishing request to the corresponding callback that | 259 // Map of client report phishing request to the corresponding callback that |
279 // has to be invoked when the request is done. | 260 // has to be invoked when the request is done. |
280 struct ClientReportInfo; | 261 struct ClientReportInfo; |
281 std::map<const URLFetcher*, ClientReportInfo*> client_phishing_reports_; | 262 std::map<const URLFetcher*, ClientReportInfo*> client_phishing_reports_; |
282 | 263 |
283 // Cache of completed requests. Used to satisfy requests for the same urls | 264 // Cache of completed requests. Used to satisfy requests for the same urls |
284 // as long as the next request falls within our caching window (which is | 265 // as long as the next request falls within our caching window (which is |
285 // determined by kNegativeCacheInterval and kPositiveCacheInterval). The | 266 // determined by kNegativeCacheInterval and kPositiveCacheInterval). The |
286 // size of this cache is limited by kMaxReportsPerDay * | 267 // size of this cache is limited by kMaxReportsPerDay * |
287 // ceil(InDays(max(kNegativeCacheInterval, kPositiveCacheInterval))). | 268 // ceil(InDays(max(kNegativeCacheInterval, kPositiveCacheInterval))). |
288 // TODO(gcasto): Serialize this so that it doesn't reset on browser restart. | 269 // TODO(gcasto): Serialize this so that it doesn't reset on browser restart. |
289 PhishingCache cache_; | 270 PhishingCache cache_; |
290 | 271 |
291 // Timestamp of when we sent a phishing request. Used to limit the number | 272 // Timestamp of when we sent a phishing request. Used to limit the number |
292 // of phishing requests that we send in a day. | 273 // of phishing requests that we send in a day. |
293 // TODO(gcasto): Serialize this so that it doesn't reset on browser restart. | 274 // TODO(gcasto): Serialize this so that it doesn't reset on browser restart. |
294 std::queue<base::Time> phishing_report_times_; | 275 std::queue<base::Time> phishing_report_times_; |
295 | 276 |
296 // Used to asynchronously call the callbacks for | 277 // Used to asynchronously call the callbacks for |
297 // SendClientReportPhishingRequest. | 278 // SendClientReportPhishingRequest. |
298 ScopedRunnableMethodFactory<ClientSideDetectionService> method_factory_; | 279 ScopedRunnableMethodFactory<ClientSideDetectionService> method_factory_; |
299 | 280 |
300 // The context we use to issue network requests. | 281 // The context we use to issue network requests. |
301 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 282 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
302 | 283 |
303 // The network blocks that we consider private IP address ranges. | 284 // The network blocks that we consider private IP address ranges. |
304 std::vector<AddressRange> private_networks_; | 285 std::vector<AddressRange> private_networks_; |
305 | 286 |
306 // Features which are allowed to be sent in sanitized pingbacks. | |
307 base::hash_set<std::string> allowed_features_; | |
308 | |
309 // Map of bad subnets which are copied from the client model and put into | 287 // Map of bad subnets which are copied from the client model and put into |
310 // this map to speed up lookups. | 288 // this map to speed up lookups. |
311 BadSubnetMap bad_subnets_; | 289 BadSubnetMap bad_subnets_; |
312 | 290 |
313 NotificationRegistrar registrar_; | 291 NotificationRegistrar registrar_; |
314 | 292 |
315 DISALLOW_COPY_AND_ASSIGN(ClientSideDetectionService); | 293 DISALLOW_COPY_AND_ASSIGN(ClientSideDetectionService); |
316 }; | 294 }; |
317 } // namepsace safe_browsing | 295 } // namepsace safe_browsing |
318 | 296 |
319 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ | 297 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ |
OLD | NEW |