Chromium Code Reviews| 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/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util_proxy.h" | 9 #include "base/file_util_proxy.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/metrics/histogram.h" | |
| 12 #include "base/platform_file.h" | 13 #include "base/platform_file.h" |
| 13 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
| 14 #include "base/stl_util-inl.h" | 15 #include "base/stl_util-inl.h" |
| 15 #include "base/task.h" | 16 #include "base/task.h" |
| 17 #include "chrome/browser/browser_process.h" | |
| 16 #include "chrome/browser/browser_thread.h" | 18 #include "chrome/browser/browser_thread.h" |
| 19 #include "chrome/browser/renderer_host/resource_dispatcher_host.h" | |
| 17 #include "chrome/browser/safe_browsing/csd.pb.h" | 20 #include "chrome/browser/safe_browsing/csd.pb.h" |
| 21 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | |
| 22 #include "chrome/browser/tab_contents/tab_contents.h" | |
| 23 #include "chrome/browser/tab_contents/tab_util.h" | |
| 24 #include "chrome/common/chrome_switches.h" | |
| 18 #include "chrome/common/net/http_return.h" | 25 #include "chrome/common/net/http_return.h" |
| 19 #include "chrome/common/net/url_fetcher.h" | 26 #include "chrome/common/net/url_fetcher.h" |
| 20 #include "chrome/common/net/url_request_context_getter.h" | 27 #include "chrome/common/net/url_request_context_getter.h" |
| 21 #include "googleurl/src/gurl.h" | 28 #include "googleurl/src/gurl.h" |
| 22 #include "net/base/load_flags.h" | 29 #include "net/base/load_flags.h" |
| 23 #include "net/url_request/url_request_status.h" | 30 #include "net/url_request/url_request_status.h" |
| 31 #include "webkit/glue/resource_type.h" | |
| 24 | 32 |
| 25 namespace safe_browsing { | 33 namespace safe_browsing { |
| 26 | 34 |
| 27 const char ClientSideDetectionService::kClientReportPhishingUrl[] = | 35 const char ClientSideDetectionService::kClientReportPhishingUrl[] = |
| 28 "https://sb-ssl.google.com/safebrowsing/clientreport/phishing"; | 36 "https://sb-ssl.google.com/safebrowsing/clientreport/phishing"; |
| 29 const char ClientSideDetectionService::kClientModelUrl[] = | 37 const char ClientSideDetectionService::kClientModelUrl[] = |
| 30 "https://ssl.gstatic.com/safebrowsing/csd/client_model_v0.pb"; | 38 "https://ssl.gstatic.com/safebrowsing/csd/client_model_v0.pb"; |
| 31 | 39 |
| 40 CsdClient::CsdClient(int render_process_id, int render_view_id) | |
| 41 : render_process_id_(render_process_id), | |
| 42 render_view_id_(render_view_id), | |
| 43 navigated_away_(false) {} | |
| 44 | |
| 45 CsdClient::~CsdClient() {} | |
| 46 | |
| 47 void CsdClient::OnUrlCheckResult(const GURL& url, | |
|
lzheng
2011/01/25 00:43:43
I don't think this is actually used? The interface
noelutz
2011/02/10 01:16:23
Removed.
| |
| 48 SafeBrowsingService::UrlCheckResult result) { | |
| 49 delete this; | |
|
Brian Ryner
2011/01/20 23:36:40
This looks a little odd at first glance, can you a
noelutz
2011/02/10 01:16:23
Done.
| |
| 50 } | |
| 51 | |
| 52 void CsdClient::OnBlockingPageComplete(bool proceed) { | |
| 53 delete this; | |
| 54 } | |
| 55 | |
| 56 void CsdClient::MaybeShowPhishingInterstitial(GURL phishing_url, | |
| 57 bool is_phishing) { | |
| 58 if (navigated_away_) { | |
| 59 // The user already navigated away from the phishing page. The server was | |
| 60 // too slow. | |
| 61 UMA_HISTOGRAM_COUNTS("SBClientPhishing.ServerVerdictTooLate", 1); | |
| 62 } | |
| 63 TabContents* tab = tab_util::GetTabContentsByID(render_process_id_, | |
| 64 render_view_id_); | |
| 65 if (tab) { | |
| 66 // No matter what we need to remove ourselves as a navigation observer if | |
| 67 // the tab still exists. | |
| 68 tab->RemoveNavigationObserver(this); | |
| 69 | |
| 70 if (is_phishing && | |
| 71 CommandLine::ForCurrentProcess()->HasSwitch( | |
| 72 switches::kEnableClientSidePhishingInterstitial)) { | |
| 73 // While we are on the UI thread we make sure that the user did not | |
| 74 // navigate away from the phishing page while we were busy checking | |
| 75 // whether or not the page is phishing. | |
| 76 // TODO(noelutz): this is not perfect. It's still possible that the | |
| 77 // user browses away before the interstitial is shown. Maybe we should | |
| 78 // stop all pending navigations? | |
| 79 ResourceDispatcherHost* resource = | |
| 80 g_browser_process->resource_dispatcher_host(); | |
| 81 if (!navigated_away_ && resource && resource->safe_browsing_service()) { | |
| 82 // TODO(noelutz): refactor this code so that we don't need to use | |
| 83 // the SafeBrowsing service class. | |
| 84 BrowserThread::PostTask( | |
| 85 BrowserThread::IO, | |
| 86 FROM_HERE, | |
| 87 NewRunnableMethod(resource->safe_browsing_service(), | |
| 88 &SafeBrowsingService::DisplayBlockingPage, | |
| 89 phishing_url, phishing_url, | |
| 90 // We only classify the main frame URL. | |
| 91 ResourceType::MAIN_FRAME, | |
| 92 // TODO(noelutz): create a separate threat type | |
| 93 // for client-side phishing detection. | |
| 94 SafeBrowsingService::URL_PHISHING, | |
| 95 this, | |
| 96 render_process_id_, | |
| 97 render_view_id_)); | |
| 98 return; // don't delete this object yet. | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 delete this; | |
| 103 } | |
| 104 | |
| 105 void CsdClient::DidNavigateMainFramePostCommit( | |
| 106 const NavigationController::LoadCommittedDetails& details, | |
| 107 const ViewHostMsg_FrameNavigate_Params& params) { | |
| 108 // If a sub-frame navigation happens we still consider the main-frame as | |
| 109 // phishing if both the client and the server agree that the main-frame | |
| 110 // is phishing. | |
| 111 navigated_away_ = details.is_main_frame; | |
| 112 } | |
| 113 | |
| 32 struct ClientSideDetectionService::ClientReportInfo { | 114 struct ClientSideDetectionService::ClientReportInfo { |
| 33 scoped_ptr<ClientReportPhishingRequestCallback> callback; | 115 scoped_ptr<ClientReportPhishingRequestCallback> callback; |
| 34 GURL phishing_url; | 116 GURL phishing_url; |
| 35 }; | 117 }; |
| 36 | 118 |
| 37 ClientSideDetectionService::ClientSideDetectionService( | 119 ClientSideDetectionService::ClientSideDetectionService( |
| 38 const FilePath& model_path, | 120 const FilePath& model_path, |
| 39 URLRequestContextGetter* request_context_getter) | 121 URLRequestContextGetter* request_context_getter) |
| 40 : model_path_(model_path), | 122 : model_path_(model_path), |
| 41 model_status_(UNKNOWN_STATUS), | 123 model_status_(UNKNOWN_STATUS), |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 // Remember which callback and URL correspond to the current fetcher object. | 328 // Remember which callback and URL correspond to the current fetcher object. |
| 247 ClientReportInfo* info = new ClientReportInfo; | 329 ClientReportInfo* info = new ClientReportInfo; |
| 248 info->callback.swap(cb); // takes ownership of the callback. | 330 info->callback.swap(cb); // takes ownership of the callback. |
| 249 info->phishing_url = phishing_url; | 331 info->phishing_url = phishing_url; |
| 250 client_phishing_reports_[fetcher] = info; | 332 client_phishing_reports_[fetcher] = info; |
| 251 | 333 |
| 252 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE); | 334 fetcher->set_load_flags(net::LOAD_DISABLE_CACHE); |
| 253 fetcher->set_request_context(request_context_getter_.get()); | 335 fetcher->set_request_context(request_context_getter_.get()); |
| 254 fetcher->set_upload_data("application/octet-stream", request_data); | 336 fetcher->set_upload_data("application/octet-stream", request_data); |
| 255 fetcher->Start(); | 337 fetcher->Start(); |
| 338 LOG(INFO) << "Start sending csd request"; | |
|
Brian Ryner
2011/01/20 23:36:40
Make this a VLOG?
noelutz
2011/02/10 01:16:23
Removed. Needed only for debugging.
| |
| 256 } | 339 } |
| 257 | 340 |
| 258 void ClientSideDetectionService::HandleModelResponse( | 341 void ClientSideDetectionService::HandleModelResponse( |
| 259 const URLFetcher* source, | 342 const URLFetcher* source, |
| 260 const GURL& url, | 343 const GURL& url, |
| 261 const net::URLRequestStatus& status, | 344 const net::URLRequestStatus& status, |
| 262 int response_code, | 345 int response_code, |
| 263 const ResponseCookies& cookies, | 346 const ResponseCookies& cookies, |
| 264 const std::string& data) { | 347 const std::string& data) { |
| 265 if (status.is_success() && RC_REQUEST_OK == response_code) { | 348 if (status.is_success() && RC_REQUEST_OK == response_code) { |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 286 } | 369 } |
| 287 } | 370 } |
| 288 | 371 |
| 289 void ClientSideDetectionService::HandlePhishingVerdict( | 372 void ClientSideDetectionService::HandlePhishingVerdict( |
| 290 const URLFetcher* source, | 373 const URLFetcher* source, |
| 291 const GURL& url, | 374 const GURL& url, |
| 292 const net::URLRequestStatus& status, | 375 const net::URLRequestStatus& status, |
| 293 int response_code, | 376 int response_code, |
| 294 const ResponseCookies& cookies, | 377 const ResponseCookies& cookies, |
| 295 const std::string& data) { | 378 const std::string& data) { |
| 379 LOG(INFO) << "Stop sending csd request"; | |
|
Brian Ryner
2011/01/20 23:36:40
Same here.
noelutz
2011/02/10 01:16:23
Removed. Needed only for debugging.
| |
| 296 ClientPhishingResponse response; | 380 ClientPhishingResponse response; |
| 297 scoped_ptr<ClientReportInfo> info(client_phishing_reports_[source]); | 381 scoped_ptr<ClientReportInfo> info(client_phishing_reports_[source]); |
| 298 if (status.is_success() && RC_REQUEST_OK == response_code && | 382 if (status.is_success() && RC_REQUEST_OK == response_code && |
| 299 response.ParseFromString(data)) { | 383 response.ParseFromString(data)) { |
| 300 info->callback->Run(info->phishing_url, response.phishy()); | 384 info->callback->Run(info->phishing_url, response.phishy()); |
| 301 } else { | 385 } else { |
| 302 DLOG(ERROR) << "Unable to get the server verdict for URL: " | 386 DLOG(ERROR) << "Unable to get the server verdict for URL: " |
| 303 << info->phishing_url; | 387 << info->phishing_url << " status: " << status.status() << " " |
| 388 << "response_code:" << response_code; | |
| 304 info->callback->Run(info->phishing_url, false); | 389 info->callback->Run(info->phishing_url, false); |
| 305 } | 390 } |
| 306 client_phishing_reports_.erase(source); | 391 client_phishing_reports_.erase(source); |
| 307 } | 392 } |
| 308 | 393 |
| 309 } // namespace safe_browsing | 394 } // namespace safe_browsing |
| OLD | NEW |