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 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 // validating the model. | 146 // validating the model. |
145 virtual void EndFetchModel(ClientModelStatus status); // Virtual for testing. | 147 virtual void EndFetchModel(ClientModelStatus status); // Virtual for testing. |
146 | 148 |
147 private: | 149 private: |
148 friend class ClientSideDetectionServiceTest; | 150 friend class ClientSideDetectionServiceTest; |
149 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, FetchModelTest); | 151 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, FetchModelTest); |
150 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, SetBadSubnets); | 152 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, SetBadSubnets); |
151 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, IsBadIpAddress); | 153 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, IsBadIpAddress); |
152 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, | 154 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, |
153 ModelHasValidHashIds); | 155 ModelHasValidHashIds); |
| 156 FRIEND_TEST_ALL_PREFIXES(ClientSideDetectionServiceTest, |
| 157 SanitizeRequestForPingback); |
154 | 158 |
155 // CacheState holds all information necessary to respond to a caller without | 159 // CacheState holds all information necessary to respond to a caller without |
156 // actually making a HTTP request. | 160 // actually making a HTTP request. |
157 struct CacheState { | 161 struct CacheState { |
158 bool is_phishing; | 162 bool is_phishing; |
159 base::Time timestamp; | 163 base::Time timestamp; |
160 | 164 |
161 CacheState(bool phish, base::Time time); | 165 CacheState(bool phish, base::Time time); |
162 }; | 166 }; |
163 typedef std::map<GURL, linked_ptr<CacheState> > PhishingCache; | 167 typedef std::map<GURL, linked_ptr<CacheState> > PhishingCache; |
(...skipping 10 matching lines...) Expand all Loading... |
174 static const char kClientReportPhishingUrl[]; | 178 static const char kClientReportPhishingUrl[]; |
175 static const char kClientModelUrl[]; | 179 static const char kClientModelUrl[]; |
176 static const size_t kMaxModelSizeBytes; | 180 static const size_t kMaxModelSizeBytes; |
177 static const int kMaxReportsPerInterval; | 181 static const int kMaxReportsPerInterval; |
178 static const int kClientModelFetchIntervalMs; | 182 static const int kClientModelFetchIntervalMs; |
179 static const int kInitialClientModelFetchDelayMs; | 183 static const int kInitialClientModelFetchDelayMs; |
180 static const base::TimeDelta kReportsInterval; | 184 static const base::TimeDelta kReportsInterval; |
181 static const base::TimeDelta kNegativeCacheInterval; | 185 static const base::TimeDelta kNegativeCacheInterval; |
182 static const base::TimeDelta kPositiveCacheInterval; | 186 static const base::TimeDelta kPositiveCacheInterval; |
183 | 187 |
| 188 // Given a ClientSidePhishingRequest populated by the renderer and browser |
| 189 // feature extractors, sanitizes it so that no data specifically identifying |
| 190 // the URL or page content is included. This is used when sending a pingback |
| 191 // if the user is not opted in to UMA. |
| 192 void SanitizeRequestForPingback( |
| 193 const ClientPhishingRequest& original_request, |
| 194 ClientPhishingRequest* sanitized_request); |
| 195 |
184 // Starts sending the request to the client-side detection frontends. | 196 // Starts sending the request to the client-side detection frontends. |
185 // This method takes ownership of both pointers. | 197 // This method takes ownership of both pointers. |
186 void StartClientReportPhishingRequest( | 198 void StartClientReportPhishingRequest( |
187 ClientPhishingRequest* verdict, | 199 ClientPhishingRequest* verdict, |
188 ClientReportPhishingRequestCallback* callback); | 200 ClientReportPhishingRequestCallback* callback); |
189 | 201 |
190 // Called by OnURLFetchComplete to handle the response from fetching the | 202 // Called by OnURLFetchComplete to handle the response from fetching the |
191 // model. | 203 // model. |
192 void HandleModelResponse(const URLFetcher* source, | 204 void HandleModelResponse(const URLFetcher* source, |
193 const GURL& url, | 205 const GURL& url, |
(...skipping 14 matching lines...) Expand all Loading... |
208 // Invalidate cache results which are no longer useful. | 220 // Invalidate cache results which are no longer useful. |
209 void UpdateCache(); | 221 void UpdateCache(); |
210 | 222 |
211 // Get the number of phishing reports that we have sent over kReportsInterval | 223 // Get the number of phishing reports that we have sent over kReportsInterval |
212 int GetNumReports(); | 224 int GetNumReports(); |
213 | 225 |
214 // Initializes the |private_networks_| vector with the network blocks | 226 // Initializes the |private_networks_| vector with the network blocks |
215 // that we consider non-public IP addresses. Returns true on success. | 227 // that we consider non-public IP addresses. Returns true on success. |
216 bool InitializePrivateNetworks(); | 228 bool InitializePrivateNetworks(); |
217 | 229 |
| 230 // Initializes the |allowed_features_| hash_set with the features that |
| 231 // can be sent in sanitized pingbacks. |
| 232 void InitializeAllowedFeatures(); |
| 233 |
218 // Send the model to the given renderer. | 234 // Send the model to the given renderer. |
219 void SendModelToProcess(RenderProcessHost* process); | 235 void SendModelToProcess(RenderProcessHost* process); |
220 | 236 |
221 // Same as above but sends the model to all rendereres. | 237 // Same as above but sends the model to all rendereres. |
222 void SendModelToRenderers(); | 238 void SendModelToRenderers(); |
223 | 239 |
224 // Reads the bad subnets from the client model and inserts them into | 240 // Reads the bad subnets from the client model and inserts them into |
225 // |bad_subnets| for faster lookups. This method is static to simplify | 241 // |bad_subnets| for faster lookups. This method is static to simplify |
226 // testing. | 242 // testing. |
227 static void SetBadSubnets(const ClientSideModel& model, | 243 static void SetBadSubnets(const ClientSideModel& model, |
228 BadSubnetMap* bad_subnets); | 244 BadSubnetMap* bad_subnets); |
229 | 245 |
230 | 246 |
231 // Returns true iff all the hash id's in the client-side model point to | 247 // Returns true iff all the hash id's in the client-side model point to |
232 // valid hashes in the model. | 248 // valid hashes in the model. |
233 static bool ModelHasValidHashIds(const ClientSideModel& model); | 249 static bool ModelHasValidHashIds(const ClientSideModel& model); |
234 | 250 |
235 std::string model_str_; | 251 std::string model_str_; |
236 scoped_ptr<ClientSideModel> model_; | 252 scoped_ptr<ClientSideModel> model_; |
237 scoped_ptr<base::TimeDelta> model_max_age_; | 253 scoped_ptr<base::TimeDelta> model_max_age_; |
238 scoped_ptr<URLFetcher> model_fetcher_; | 254 scoped_ptr<URLFetcher> model_fetcher_; |
239 | 255 |
| 256 // This pointer may be NULL if SafeBrowsing is disabled. |
| 257 scoped_refptr<SafeBrowsingService> sb_service_; |
| 258 |
240 // Map of client report phishing request to the corresponding callback that | 259 // Map of client report phishing request to the corresponding callback that |
241 // has to be invoked when the request is done. | 260 // has to be invoked when the request is done. |
242 struct ClientReportInfo; | 261 struct ClientReportInfo; |
243 std::map<const URLFetcher*, ClientReportInfo*> client_phishing_reports_; | 262 std::map<const URLFetcher*, ClientReportInfo*> client_phishing_reports_; |
244 | 263 |
245 // 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 |
246 // 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 |
247 // determined by kNegativeCacheInterval and kPositiveCacheInterval). The | 266 // determined by kNegativeCacheInterval and kPositiveCacheInterval). The |
248 // size of this cache is limited by kMaxReportsPerDay * | 267 // size of this cache is limited by kMaxReportsPerDay * |
249 // ceil(InDays(max(kNegativeCacheInterval, kPositiveCacheInterval))). | 268 // ceil(InDays(max(kNegativeCacheInterval, kPositiveCacheInterval))). |
250 // 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. |
251 PhishingCache cache_; | 270 PhishingCache cache_; |
252 | 271 |
253 // 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 |
254 // of phishing requests that we send in a day. | 273 // of phishing requests that we send in a day. |
255 // 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. |
256 std::queue<base::Time> phishing_report_times_; | 275 std::queue<base::Time> phishing_report_times_; |
257 | 276 |
258 // Used to asynchronously call the callbacks for | 277 // Used to asynchronously call the callbacks for |
259 // SendClientReportPhishingRequest. | 278 // SendClientReportPhishingRequest. |
260 ScopedRunnableMethodFactory<ClientSideDetectionService> method_factory_; | 279 ScopedRunnableMethodFactory<ClientSideDetectionService> method_factory_; |
261 | 280 |
262 // The context we use to issue network requests. | 281 // The context we use to issue network requests. |
263 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 282 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
264 | 283 |
265 // The network blocks that we consider private IP address ranges. | 284 // The network blocks that we consider private IP address ranges. |
266 std::vector<AddressRange> private_networks_; | 285 std::vector<AddressRange> private_networks_; |
267 | 286 |
| 287 // Features which are allowed to be sent in sanitized pingbacks. |
| 288 base::hash_set<std::string> allowed_features_; |
| 289 |
268 // Map of bad subnets which are copied from the client model and put into | 290 // Map of bad subnets which are copied from the client model and put into |
269 // this map to speed up lookups. | 291 // this map to speed up lookups. |
270 BadSubnetMap bad_subnets_; | 292 BadSubnetMap bad_subnets_; |
271 | 293 |
272 NotificationRegistrar registrar_; | 294 NotificationRegistrar registrar_; |
273 | 295 |
274 DISALLOW_COPY_AND_ASSIGN(ClientSideDetectionService); | 296 DISALLOW_COPY_AND_ASSIGN(ClientSideDetectionService); |
275 }; | 297 }; |
276 } // namepsace safe_browsing | 298 } // namepsace safe_browsing |
277 | 299 |
278 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ | 300 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ |
OLD | NEW |