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 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 5 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/time.h" | 9 #include "base/time.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/string_util.h" | |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
14 #include "base/task.h" | 15 #include "base/task.h" |
15 #include "base/time.h" | 16 #include "base/time.h" |
17 #include "chrome/browser/browser_process.h" | |
18 #include "chrome/browser/safe_browsing/browser_features.h" | |
19 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | |
16 #include "chrome/common/net/http_return.h" | 20 #include "chrome/common/net/http_return.h" |
17 #include "chrome/common/safe_browsing/client_model.pb.h" | 21 #include "chrome/common/safe_browsing/client_model.pb.h" |
18 #include "chrome/common/safe_browsing/csd.pb.h" | 22 #include "chrome/common/safe_browsing/csd.pb.h" |
19 #include "chrome/common/safe_browsing/safebrowsing_messages.h" | 23 #include "chrome/common/safe_browsing/safebrowsing_messages.h" |
24 #include "chrome/renderer/safe_browsing/features.h" | |
20 #include "content/browser/browser_thread.h" | 25 #include "content/browser/browser_thread.h" |
21 #include "content/browser/renderer_host/render_process_host.h" | 26 #include "content/browser/renderer_host/render_process_host.h" |
22 #include "content/common/notification_service.h" | 27 #include "content/common/notification_service.h" |
23 #include "content/common/url_fetcher.h" | 28 #include "content/common/url_fetcher.h" |
24 #include "crypto/sha2.h" | 29 #include "crypto/sha2.h" |
25 #include "googleurl/src/gurl.h" | 30 #include "googleurl/src/gurl.h" |
26 #include "net/base/load_flags.h" | 31 #include "net/base/load_flags.h" |
27 #include "net/http/http_response_headers.h" | 32 #include "net/http/http_response_headers.h" |
28 #include "net/url_request/url_request_context_getter.h" | 33 #include "net/url_request/url_request_context_getter.h" |
29 #include "net/url_request/url_request_status.h" | 34 #include "net/url_request/url_request_status.h" |
(...skipping 26 matching lines...) Expand all Loading... | |
56 scoped_ptr<ClientReportPhishingRequestCallback> callback; | 61 scoped_ptr<ClientReportPhishingRequestCallback> callback; |
57 GURL phishing_url; | 62 GURL phishing_url; |
58 }; | 63 }; |
59 | 64 |
60 ClientSideDetectionService::CacheState::CacheState(bool phish, base::Time time) | 65 ClientSideDetectionService::CacheState::CacheState(bool phish, base::Time time) |
61 : is_phishing(phish), | 66 : is_phishing(phish), |
62 timestamp(time) {} | 67 timestamp(time) {} |
63 | 68 |
64 ClientSideDetectionService::ClientSideDetectionService( | 69 ClientSideDetectionService::ClientSideDetectionService( |
65 net::URLRequestContextGetter* request_context_getter) | 70 net::URLRequestContextGetter* request_context_getter) |
66 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 71 : sb_service_(g_browser_process->safe_browsing_service()), |
72 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | |
67 request_context_getter_(request_context_getter) { | 73 request_context_getter_(request_context_getter) { |
68 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 74 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
69 NotificationService::AllSources()); | 75 NotificationService::AllSources()); |
70 } | 76 } |
71 | 77 |
72 ClientSideDetectionService::~ClientSideDetectionService() { | 78 ClientSideDetectionService::~ClientSideDetectionService() { |
73 method_factory_.RevokeAll(); | 79 method_factory_.RevokeAll(); |
74 STLDeleteContainerPairPointers(client_phishing_reports_.begin(), | 80 STLDeleteContainerPairPointers(client_phishing_reports_.begin(), |
75 client_phishing_reports_.end()); | 81 client_phishing_reports_.end()); |
76 client_phishing_reports_.clear(); | 82 client_phishing_reports_.clear(); |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
230 model_max_age_.reset(); | 236 model_max_age_.reset(); |
231 | 237 |
232 // Schedule the next model reload. | 238 // Schedule the next model reload. |
233 MessageLoop::current()->PostDelayedTask( | 239 MessageLoop::current()->PostDelayedTask( |
234 FROM_HERE, | 240 FROM_HERE, |
235 method_factory_.NewRunnableMethod( | 241 method_factory_.NewRunnableMethod( |
236 &ClientSideDetectionService::StartFetchModel), | 242 &ClientSideDetectionService::StartFetchModel), |
237 delay_ms); | 243 delay_ms); |
238 } | 244 } |
239 | 245 |
246 // static | |
247 void ClientSideDetectionService::SanitizeRequestForPingback( | |
248 const ClientPhishingRequest& full_request, | |
249 ClientPhishingRequest* sanitized_request) { | |
250 DCHECK(full_request.IsInitialized()); | |
251 sanitized_request->Clear(); | |
252 if (full_request.has_suffix_prefix_hash()) { | |
253 sanitized_request->set_suffix_prefix_hash( | |
254 full_request.suffix_prefix_hash()); | |
255 } | |
256 sanitized_request->set_client_score(full_request.client_score()); | |
257 if (full_request.has_is_phishing()) { | |
258 sanitized_request->set_is_phishing(full_request.is_phishing()); | |
259 } | |
260 | |
261 // Token and term features are not included in the pingback. | |
mattm
2011/08/12 03:00:28
Did you consider a white-listing approach to sanit
Brian Ryner
2011/08/12 04:52:24
That's a good point; I think whitelisting is proba
| |
262 static const char* const kFilteredModelFeatures[] = { | |
263 features::kUrlTldToken, | |
264 features::kUrlDomainToken, | |
265 features::kUrlOtherHostToken, | |
266 features::kUrlPathToken, | |
267 features::kPageLinkDomain, | |
268 features::kPageTerm, | |
269 }; | |
270 | |
271 for (int i = 0; i < full_request.feature_map_size(); ++i) { | |
272 const ClientPhishingRequest_Feature& feature = full_request.feature_map(i); | |
273 bool filtered = false; | |
274 for (size_t j = 0; j < arraysize(kFilteredModelFeatures); ++j) { | |
275 if (StartsWithASCII(feature.name(), kFilteredModelFeatures[j], | |
276 true /* case sensitive */)) { | |
277 filtered = true; | |
278 break; | |
279 } | |
280 } | |
281 if (!filtered) { | |
282 sanitized_request->add_feature_map()->CopyFrom(feature); | |
283 } | |
284 } | |
285 | |
286 if (full_request.has_model_version()) { | |
287 sanitized_request->set_model_version(full_request.model_version()); | |
288 } | |
289 | |
290 static const char* const kFilteredNonModelFeatures[] = { | |
291 features::kBadIpFetch, | |
292 features::kSafeBrowsingMaliciousUrl, | |
293 features::kSafeBrowsingOriginalUrl, | |
294 }; | |
295 | |
296 for (int i = 0; i < full_request.non_model_feature_map_size(); ++i) { | |
297 const ClientPhishingRequest_Feature& feature = | |
298 full_request.non_model_feature_map(i); | |
299 bool filtered = false; | |
300 for (size_t j = 0; j < arraysize(kFilteredNonModelFeatures); ++j) { | |
301 if (StartsWithASCII(feature.name(), kFilteredNonModelFeatures[j], | |
302 true /* case sensitive */)) { | |
303 filtered = true; | |
304 break; | |
305 } | |
306 } | |
307 // We look for referrer as a substring, since there are a few different | |
308 // permutations of the feature. | |
309 if (!filtered && | |
310 feature.name().find(std::string(features::kReferrer) + "=") == | |
311 std::string::npos) { | |
312 sanitized_request->add_non_model_feature_map()->CopyFrom(feature); | |
313 } | |
314 } | |
315 } | |
316 | |
240 void ClientSideDetectionService::StartClientReportPhishingRequest( | 317 void ClientSideDetectionService::StartClientReportPhishingRequest( |
241 ClientPhishingRequest* verdict, | 318 ClientPhishingRequest* verdict, |
242 ClientReportPhishingRequestCallback* callback) { | 319 ClientReportPhishingRequestCallback* callback) { |
243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
244 scoped_ptr<ClientPhishingRequest> request(verdict); | 321 scoped_ptr<ClientPhishingRequest> request(verdict); |
245 scoped_ptr<ClientReportPhishingRequestCallback> cb(callback); | 322 scoped_ptr<ClientReportPhishingRequestCallback> cb(callback); |
246 | 323 |
324 // Create the version of the request proto that we'll send over the network. | |
325 ClientPhishingRequest request_to_send; | |
326 if (sb_service_ && sb_service_->CanReportStats()) { | |
327 request_to_send.CopyFrom(*request); | |
328 } else { | |
329 SanitizeRequestForPingback(*request, &request_to_send); | |
330 } | |
331 | |
247 std::string request_data; | 332 std::string request_data; |
248 if (!request->SerializeToString(&request_data)) { | 333 if (!request_to_send.SerializeToString(&request_data)) { |
249 UMA_HISTOGRAM_COUNTS("SBClientPhishing.RequestNotSerialized", 1); | 334 UMA_HISTOGRAM_COUNTS("SBClientPhishing.RequestNotSerialized", 1); |
250 VLOG(1) << "Unable to serialize the CSD request. Proto file changed?"; | 335 VLOG(1) << "Unable to serialize the CSD request. Proto file changed?"; |
251 if (cb.get()) { | 336 if (cb.get()) { |
252 cb->Run(GURL(request->url()), false); | 337 cb->Run(GURL(request->url()), false); |
253 } | 338 } |
254 return; | 339 return; |
255 } | 340 } |
256 | 341 |
257 URLFetcher* fetcher = URLFetcher::Create(0 /* ID is not used */, | 342 URLFetcher* fetcher = URLFetcher::Create(0 /* ID is not used */, |
258 GURL(kClientReportPhishingUrl), | 343 GURL(kClientReportPhishingUrl), |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
480 } | 565 } |
481 } | 566 } |
482 for (int i = 0; i < model.page_word_size(); ++i) { | 567 for (int i = 0; i < model.page_word_size(); ++i) { |
483 if (model.page_word(i) < 0 || model.page_word(i) > max_index) { | 568 if (model.page_word(i) < 0 || model.page_word(i) > max_index) { |
484 return false; | 569 return false; |
485 } | 570 } |
486 } | 571 } |
487 return true; | 572 return true; |
488 } | 573 } |
489 } // namespace safe_browsing | 574 } // namespace safe_browsing |
OLD | NEW |