Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: chrome/browser/safe_browsing/download_protection_service.cc

Issue 2146703002: Sample 1% url whitelisted PPAPI downloads to ping safe browsing server (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address asanka@'s comment Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/download_protection_service.h" 5 #include "chrome/browser/safe_browsing/download_protection_service.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 #include "net/url_request/url_fetcher.h" 65 #include "net/url_request/url_fetcher.h"
66 #include "net/url_request/url_fetcher_delegate.h" 66 #include "net/url_request/url_fetcher_delegate.h"
67 #include "net/url_request/url_request_status.h" 67 #include "net/url_request/url_request_status.h"
68 68
69 #if defined(OS_MACOSX) 69 #if defined(OS_MACOSX)
70 #include "chrome/browser/safe_browsing/sandboxed_dmg_analyzer_mac.h" 70 #include "chrome/browser/safe_browsing/sandboxed_dmg_analyzer_mac.h"
71 #endif 71 #endif
72 72
73 using content::BrowserThread; 73 using content::BrowserThread;
74 74
75 namespace { 75 namespace {
Lei Zhang 2016/07/14 23:56:11 Add some blank lines on the insides of the namespa
Jialiu Lin 2016/07/15 01:20:05 Done.
76 static const int64_t kDownloadRequestTimeoutMs = 7000; 76 static const int64_t kDownloadRequestTimeoutMs = 7000;
77 // We sample 1% of whitelisted downloads to still send out download pings. 77 // We sample 1% of whitelisted downloads to still send out download pings.
78 static const double kWhitelistDownloadSampleRate = 0.01; 78 static const double kWhitelistDownloadSampleRate = 0.01;
79
80 enum WhitelistType {
81 NO_WHITELIST_MATCH,
82 URL_WHITELIST,
83 SIGNATURE_WHITELIST,
84 WHITELIST_TYPE_MAX
85 };
86
87 static void RecordCountOfWhitelistedDownload(WhitelistType type) {
Lei Zhang 2016/07/14 23:56:11 static inside an anonymous namespace is redundant.
Jialiu Lin 2016/07/15 01:20:05 Done.
88 UMA_HISTOGRAM_ENUMERATION("SBClientDownload.CheckWhitelistResult", type,
89 WHITELIST_TYPE_MAX);
90 }
79 } // namespace 91 } // namespace
80 92
81 namespace safe_browsing { 93 namespace safe_browsing {
82 94
83 const char DownloadProtectionService::kDownloadRequestUrl[] = 95 const char DownloadProtectionService::kDownloadRequestUrl[] =
84 "https://sb-ssl.google.com/safebrowsing/clientreport/download"; 96 "https://sb-ssl.google.com/safebrowsing/clientreport/download";
85 97
86 const void* const DownloadProtectionService::kDownloadPingTokenKey 98 const void* const DownloadProtectionService::kDownloadPingTokenKey
87 = &kDownloadPingTokenKey; 99 = &kDownloadPingTokenKey;
88 100
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after
746 } else { 758 } else {
747 PostFinishTask(UNKNOWN, REASON_ARCHIVE_WITHOUT_BINARIES); 759 PostFinishTask(UNKNOWN, REASON_ARCHIVE_WITHOUT_BINARIES);
748 return; 760 return;
749 } 761 }
750 } 762 }
751 763
752 OnFileFeatureExtractionDone(); 764 OnFileFeatureExtractionDone();
753 } 765 }
754 #endif // defined(OS_MACOSX) 766 #endif // defined(OS_MACOSX)
755 767
756 enum WhitelistType { 768 bool ShouldSampleWhitelistedDownload() {
757 NO_WHITELIST_MATCH,
758 URL_WHITELIST,
759 SIGNATURE_WHITELIST,
760 WHITELIST_TYPE_MAX
761 };
762
763 static void RecordCountOfWhitelistedDownload(WhitelistType type) {
764 UMA_HISTOGRAM_ENUMERATION("SBClientDownload.CheckWhitelistResult",
765 type,
766 WHITELIST_TYPE_MAX);
767 }
768
769 virtual bool ShouldSampleWhitelistedDownload() {
770 // We currently sample 1% whitelisted downloads from users who opted 769 // We currently sample 1% whitelisted downloads from users who opted
771 // in extended reporting and are not in incognito mode. 770 // in extended reporting and are not in incognito mode.
772 return service_ && is_extended_reporting_ && !is_incognito_ && 771 return service_ && is_extended_reporting_ && !is_incognito_ &&
773 base::RandDouble() < service_->whitelist_sample_rate(); 772 base::RandDouble() < service_->whitelist_sample_rate();
774 } 773 }
775 774
776 void CheckWhitelists() { 775 void CheckWhitelists() {
777 DCHECK_CURRENTLY_ON(BrowserThread::IO); 776 DCHECK_CURRENTLY_ON(BrowserThread::IO);
778 777
779 if (!database_manager_.get()) { 778 if (!database_manager_.get()) {
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after
1186 REQUEST_MALFORMED, 1185 REQUEST_MALFORMED,
1187 FETCH_FAILED, 1186 FETCH_FAILED,
1188 RESPONSE_MALFORMED, 1187 RESPONSE_MALFORMED,
1189 SUCCEEDED 1188 SUCCEEDED
1190 }; 1189 };
1191 1190
1192 PPAPIDownloadRequest( 1191 PPAPIDownloadRequest(
1193 const GURL& requestor_url, 1192 const GURL& requestor_url,
1194 const base::FilePath& default_file_path, 1193 const base::FilePath& default_file_path,
1195 const std::vector<base::FilePath::StringType>& alternate_extensions, 1194 const std::vector<base::FilePath::StringType>& alternate_extensions,
1195 Profile* profile,
1196 const CheckDownloadCallback& callback, 1196 const CheckDownloadCallback& callback,
1197 DownloadProtectionService* service, 1197 DownloadProtectionService* service,
1198 scoped_refptr<SafeBrowsingDatabaseManager> database_manager) 1198 scoped_refptr<SafeBrowsingDatabaseManager> database_manager)
1199 : requestor_url_(requestor_url), 1199 : requestor_url_(requestor_url),
1200 default_file_path_(default_file_path), 1200 default_file_path_(default_file_path),
1201 alternate_extensions_(alternate_extensions), 1201 alternate_extensions_(alternate_extensions),
1202 callback_(callback), 1202 callback_(callback),
1203 service_(service), 1203 service_(service),
1204 database_manager_(database_manager), 1204 database_manager_(database_manager),
1205 start_time_(base::TimeTicks::Now()), 1205 start_time_(base::TimeTicks::Now()),
1206 supported_path_( 1206 supported_path_(
1207 GetSupportedFilePath(default_file_path, alternate_extensions)), 1207 GetSupportedFilePath(default_file_path, alternate_extensions)),
1208 weakptr_factory_(this) {} 1208 sample_url_whitelist_(false),
1209 weakptr_factory_(this) {
1210 is_extended_reporting_ = profile &&
1211 profile->GetPrefs()->GetBoolean(
1212 prefs::kSafeBrowsingExtendedReportingEnabled);
1213 is_incognito_ = profile && profile->IsOffTheRecord();
1214 }
1209 1215
1210 ~PPAPIDownloadRequest() override { 1216 ~PPAPIDownloadRequest() override {
1211 if (fetcher_ && !callback_.is_null()) 1217 if (fetcher_ && !callback_.is_null())
1212 Finish(RequestOutcome::REQUEST_DESTROYED, UNKNOWN); 1218 Finish(RequestOutcome::REQUEST_DESTROYED, UNKNOWN);
1213 } 1219 }
1214 1220
1215 // Start the process of checking the download request. The callback passed as 1221 // Start the process of checking the download request. The callback passed as
1216 // the |callback| parameter to the constructor will be invoked with the result 1222 // the |callback| parameter to the constructor will be invoked with the result
1217 // of the check at some point in the future. 1223 // of the check at some point in the future.
1218 // 1224 //
(...skipping 29 matching lines...) Expand all
1248 service_->download_request_timeout_ms())); 1254 service_->download_request_timeout_ms()));
1249 1255
1250 BrowserThread::PostTask( 1256 BrowserThread::PostTask(
1251 BrowserThread::IO, FROM_HERE, 1257 BrowserThread::IO, FROM_HERE,
1252 base::Bind(&PPAPIDownloadRequest::CheckWhitelistsOnIOThread, 1258 base::Bind(&PPAPIDownloadRequest::CheckWhitelistsOnIOThread,
1253 requestor_url_, database_manager_, 1259 requestor_url_, database_manager_,
1254 weakptr_factory_.GetWeakPtr())); 1260 weakptr_factory_.GetWeakPtr()));
1255 } 1261 }
1256 1262
1257 private: 1263 private:
1264 bool ShouldSampleWhitelistedDownload() {
1265 // We currently sample 1% whitelisted downloads from users who opted
1266 // in extended reporting and are not in incognito mode.
1267 return service_ && !is_incognito_ && is_extended_reporting_ &&
1268 base::RandDouble() < service_->whitelist_sample_rate();
1269 }
1270
1258 // Whitelist checking needs to the done on the IO thread. 1271 // Whitelist checking needs to the done on the IO thread.
1259 static void CheckWhitelistsOnIOThread( 1272 static void CheckWhitelistsOnIOThread(
1260 const GURL& requestor_url, 1273 const GURL& requestor_url,
1261 scoped_refptr<SafeBrowsingDatabaseManager> database_manager, 1274 scoped_refptr<SafeBrowsingDatabaseManager> database_manager,
1262 base::WeakPtr<PPAPIDownloadRequest> download_request) { 1275 base::WeakPtr<PPAPIDownloadRequest> download_request) {
1263 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1276 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1264 DVLOG(2) << " checking whitelists for requestor URL:" << requestor_url; 1277 DVLOG(2) << " checking whitelists for requestor URL:" << requestor_url;
1265 1278
1266 bool url_was_whitelisted = 1279 bool url_was_whitelisted =
1267 requestor_url.is_valid() && database_manager && 1280 requestor_url.is_valid() && database_manager &&
1268 database_manager->MatchDownloadWhitelistUrl(requestor_url); 1281 database_manager->MatchDownloadWhitelistUrl(requestor_url);
1269 BrowserThread::PostTask( 1282 BrowserThread::PostTask(
1270 BrowserThread::UI, FROM_HERE, 1283 BrowserThread::UI, FROM_HERE,
1271 base::Bind(&PPAPIDownloadRequest::WhitelistCheckComplete, 1284 base::Bind(&PPAPIDownloadRequest::WhitelistCheckComplete,
1272 download_request, url_was_whitelisted)); 1285 download_request, url_was_whitelisted));
1273 } 1286 }
1274 1287
1275 void WhitelistCheckComplete(bool was_on_whitelist) { 1288 void WhitelistCheckComplete(bool was_on_whitelist) {
1276 DVLOG(2) << __FUNCTION__ << " was_on_whitelist:" << was_on_whitelist; 1289 DVLOG(2) << __FUNCTION__ << " was_on_whitelist:" << was_on_whitelist;
1277 if (was_on_whitelist) { 1290 if (was_on_whitelist) {
1278 // TODO(asanka): Should sample whitelisted downloads based on 1291 RecordCountOfWhitelistedDownload(URL_WHITELIST);
1279 // service_->whitelist_sample_rate(). http://crbug.com/610924 1292 if (ShouldSampleWhitelistedDownload()) {
Lei Zhang 2016/07/14 23:56:11 Logic might flow more easily if written as: if (!
Jialiu Lin 2016/07/15 01:20:05 Done.
1280 Finish(RequestOutcome::WHITELIST_HIT, SAFE); 1293 sample_url_whitelist_ = true;
1281 return; 1294 } else {
1295 Finish(RequestOutcome::WHITELIST_HIT, SAFE);
1296 return;
1297 }
1298 } else {
1299 RecordCountOfWhitelistedDownload(NO_WHITELIST_MATCH);
1282 } 1300 }
1283 1301
1284 // Not on whitelist, so we are going to check with the SafeBrowsing 1302 // Not on whitelist, so we are going to check with the SafeBrowsing
1285 // backend. 1303 // backend.
1286 SendRequest(); 1304 SendRequest();
1287 } 1305 }
1288 1306
1289 void SendRequest() { 1307 void SendRequest() {
1290 DVLOG(2) << __FUNCTION__; 1308 DVLOG(2) << __FUNCTION__;
1291 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1309 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1292 1310
1293 ClientDownloadRequest request; 1311 ClientDownloadRequest request;
1294 request.set_download_type(ClientDownloadRequest::PPAPI_SAVE_REQUEST); 1312 request.set_download_type(ClientDownloadRequest::PPAPI_SAVE_REQUEST);
1295 ClientDownloadRequest::Resource* resource = request.add_resources(); 1313 ClientDownloadRequest::Resource* resource = request.add_resources();
1296 resource->set_type(ClientDownloadRequest::PPAPI_DOCUMENT); 1314 resource->set_type(ClientDownloadRequest::PPAPI_DOCUMENT);
1297 resource->set_url(requestor_url_.spec()); 1315 resource->set_url(requestor_url_.spec());
1298 request.set_url(requestor_url_.spec()); 1316 request.set_url(requestor_url_.spec());
1299 request.set_file_basename(supported_path_.BaseName().AsUTF8Unsafe()); 1317 request.set_file_basename(supported_path_.BaseName().AsUTF8Unsafe());
1300 request.set_length(0); 1318 request.set_length(0);
1301 request.mutable_digests()->set_md5(std::string()); 1319 request.mutable_digests()->set_md5(std::string());
1320 request.set_skipped_url_whitelist(sample_url_whitelist_);
1321 // Download protection does not check certificate whitelist for PPAPI
1322 // downloads.
1323 request.set_skipped_certificate_whitelist(false);
1302 for (const auto& alternate_extension : alternate_extensions_) { 1324 for (const auto& alternate_extension : alternate_extensions_) {
1303 if (alternate_extension.empty()) 1325 if (alternate_extension.empty())
1304 continue; 1326 continue;
1305 DCHECK_EQ(base::FilePath::kExtensionSeparator, alternate_extension[0]); 1327 DCHECK_EQ(base::FilePath::kExtensionSeparator, alternate_extension[0]);
1306 *(request.add_alternate_extensions()) = 1328 *(request.add_alternate_extensions()) =
1307 base::FilePath(alternate_extension).AsUTF8Unsafe(); 1329 base::FilePath(alternate_extension).AsUTF8Unsafe();
1308 } 1330 }
1309 if (supported_path_ != default_file_path_) { 1331 if (supported_path_ != default_file_path_) {
1310 *(request.add_alternate_extensions()) = 1332 *(request.add_alternate_extensions()) =
1311 base::FilePath(default_file_path_.FinalExtension()).AsUTF8Unsafe(); 1333 base::FilePath(default_file_path_.FinalExtension()).AsUTF8Unsafe();
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
1442 // Time request was started. 1464 // Time request was started.
1443 const base::TimeTicks start_time_; 1465 const base::TimeTicks start_time_;
1444 1466
1445 // A download path that is supported by SafeBrowsing. This is determined by 1467 // A download path that is supported by SafeBrowsing. This is determined by
1446 // invoking GetSupportedFilePath(). If non-empty, 1468 // invoking GetSupportedFilePath(). If non-empty,
1447 // IsCheckedBinaryFile(supported_path_) is always true. This 1469 // IsCheckedBinaryFile(supported_path_) is always true. This
1448 // path is therefore used as the download target when sending the SafeBrowsing 1470 // path is therefore used as the download target when sending the SafeBrowsing
1449 // ping. 1471 // ping.
1450 const base::FilePath supported_path_; 1472 const base::FilePath supported_path_;
1451 1473
1474 bool sample_url_whitelist_;
1475
1476 bool is_extended_reporting_;
1477
1478 bool is_incognito_;
1479
1452 base::WeakPtrFactory<PPAPIDownloadRequest> weakptr_factory_; 1480 base::WeakPtrFactory<PPAPIDownloadRequest> weakptr_factory_;
1453 1481
1454 DISALLOW_COPY_AND_ASSIGN(PPAPIDownloadRequest); 1482 DISALLOW_COPY_AND_ASSIGN(PPAPIDownloadRequest);
1455 }; 1483 };
1456 1484
1457 DownloadProtectionService::DownloadProtectionService( 1485 DownloadProtectionService::DownloadProtectionService(
1458 SafeBrowsingService* sb_service) 1486 SafeBrowsingService* sb_service)
1459 : request_context_getter_(sb_service ? sb_service->url_request_context() 1487 : request_context_getter_(sb_service ? sb_service->url_request_context()
1460 : nullptr), 1488 : nullptr),
1461 enabled_(false), 1489 enabled_(false),
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
1547 // UNKNOWN types properly. http://crbug.com/581044 1575 // UNKNOWN types properly. http://crbug.com/581044
1548 return (CheckClientDownloadRequest::IsSupportedDownload( 1576 return (CheckClientDownloadRequest::IsSupportedDownload(
1549 item, target_path, &reason, &type) && 1577 item, target_path, &reason, &type) &&
1550 (ClientDownloadRequest::CHROME_EXTENSION != type)); 1578 (ClientDownloadRequest::CHROME_EXTENSION != type));
1551 } 1579 }
1552 1580
1553 void DownloadProtectionService::CheckPPAPIDownloadRequest( 1581 void DownloadProtectionService::CheckPPAPIDownloadRequest(
1554 const GURL& requestor_url, 1582 const GURL& requestor_url,
1555 const base::FilePath& default_file_path, 1583 const base::FilePath& default_file_path,
1556 const std::vector<base::FilePath::StringType>& alternate_extensions, 1584 const std::vector<base::FilePath::StringType>& alternate_extensions,
1585 Profile* profile,
1557 const CheckDownloadCallback& callback) { 1586 const CheckDownloadCallback& callback) {
1558 DVLOG(1) << __FUNCTION__ << " url:" << requestor_url 1587 DVLOG(1) << __FUNCTION__ << " url:" << requestor_url
1559 << " default_file_path:" << default_file_path.value(); 1588 << " default_file_path:" << default_file_path.value();
1560 std::unique_ptr<PPAPIDownloadRequest> request(new PPAPIDownloadRequest( 1589 std::unique_ptr<PPAPIDownloadRequest> request(new PPAPIDownloadRequest(
1561 requestor_url, default_file_path, alternate_extensions, callback, this, 1590 requestor_url, default_file_path, alternate_extensions, profile, callback,
1562 database_manager_)); 1591 this, database_manager_));
1563 PPAPIDownloadRequest* request_copy = request.get(); 1592 PPAPIDownloadRequest* request_copy = request.get();
1564 auto insertion_result = ppapi_download_requests_.insert( 1593 auto insertion_result = ppapi_download_requests_.insert(
1565 std::make_pair(request_copy, std::move(request))); 1594 std::make_pair(request_copy, std::move(request)));
1566 DCHECK(insertion_result.second); 1595 DCHECK(insertion_result.second);
1567 insertion_result.first->second->Start(); 1596 insertion_result.first->second->Start();
1568 } 1597 }
1569 1598
1570 DownloadProtectionService::ClientDownloadRequestSubscription 1599 DownloadProtectionService::ClientDownloadRequestSubscription
1571 DownloadProtectionService::RegisterClientDownloadRequestCallback( 1600 DownloadProtectionService::RegisterClientDownloadRequestCallback(
1572 const ClientDownloadRequestCallback& callback) { 1601 const ClientDownloadRequestCallback& callback) {
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
1718 GURL DownloadProtectionService::GetDownloadRequestUrl() { 1747 GURL DownloadProtectionService::GetDownloadRequestUrl() {
1719 GURL url(kDownloadRequestUrl); 1748 GURL url(kDownloadRequestUrl);
1720 std::string api_key = google_apis::GetAPIKey(); 1749 std::string api_key = google_apis::GetAPIKey();
1721 if (!api_key.empty()) 1750 if (!api_key.empty())
1722 url = url.Resolve("?key=" + net::EscapeQueryParamValue(api_key, true)); 1751 url = url.Resolve("?key=" + net::EscapeQueryParamValue(api_key, true));
1723 1752
1724 return url; 1753 return url;
1725 } 1754 }
1726 1755
1727 } // namespace safe_browsing 1756 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698