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