Commit 96f81d5c authored by jialiul's avatar jialiul Committed by Commit bot

Sample 1% url whitelisted PPAPI downloads to ping SB server

Similar to regular downloads, we want SB pings for requestors whose
URLs match the SB whitelist in flash downloads. To achieve that, we
sample 1% of whitelisted PPAPI downloads based on the
SafeBrowsingService::whitelist_sample_rate() if the user is in
extended reporting group and not in incognito mode.

Also refactored download_protection_service_unittest to elliminate
the use of MessageLoop (deprecated).

BUG=610924

Review-Url: https://codereview.chromium.org/2146703002
Cr-Commit-Position: refs/heads/master@{#406101}
parent 3f0f0534
...@@ -532,6 +532,7 @@ void FileSelectHelper::CheckDownloadRequestWithSafeBrowsing( ...@@ -532,6 +532,7 @@ void FileSelectHelper::CheckDownloadRequestWithSafeBrowsing(
GURL requestor_url = params->requestor; GURL requestor_url = params->requestor;
sb_service->download_protection_service()->CheckPPAPIDownloadRequest( sb_service->download_protection_service()->CheckPPAPIDownloadRequest(
requestor_url, default_file_path, alternate_extensions, requestor_url, default_file_path, alternate_extensions,
Profile::FromBrowserContext(web_contents_->GetBrowserContext()),
base::Bind(&InterpretSafeBrowsingVerdict, base::Bind(&InterpretSafeBrowsingVerdict,
base::Bind(&FileSelectHelper::ProceedWithSafeBrowsingVerdict, base::Bind(&FileSelectHelper::ProceedWithSafeBrowsingVerdict,
this, default_file_path, base::Passed(&params)))); this, default_file_path, base::Passed(&params))));
......
...@@ -73,9 +73,23 @@ ...@@ -73,9 +73,23 @@
using content::BrowserThread; using content::BrowserThread;
namespace { namespace {
static const int64_t kDownloadRequestTimeoutMs = 7000;
const int64_t kDownloadRequestTimeoutMs = 7000;
// We sample 1% of whitelisted downloads to still send out download pings. // We sample 1% of whitelisted downloads to still send out download pings.
static const double kWhitelistDownloadSampleRate = 0.01; const double kWhitelistDownloadSampleRate = 0.01;
enum WhitelistType {
NO_WHITELIST_MATCH,
URL_WHITELIST,
SIGNATURE_WHITELIST,
WHITELIST_TYPE_MAX
};
void RecordCountOfWhitelistedDownload(WhitelistType type) {
UMA_HISTOGRAM_ENUMERATION("SBClientDownload.CheckWhitelistResult", type,
WHITELIST_TYPE_MAX);
}
} // namespace } // namespace
namespace safe_browsing { namespace safe_browsing {
...@@ -760,20 +774,7 @@ class DownloadProtectionService::CheckClientDownloadRequest ...@@ -760,20 +774,7 @@ class DownloadProtectionService::CheckClientDownloadRequest
} }
#endif // defined(OS_MACOSX) #endif // defined(OS_MACOSX)
enum WhitelistType { bool ShouldSampleWhitelistedDownload() {
NO_WHITELIST_MATCH,
URL_WHITELIST,
SIGNATURE_WHITELIST,
WHITELIST_TYPE_MAX
};
static void RecordCountOfWhitelistedDownload(WhitelistType type) {
UMA_HISTOGRAM_ENUMERATION("SBClientDownload.CheckWhitelistResult",
type,
WHITELIST_TYPE_MAX);
}
virtual bool ShouldSampleWhitelistedDownload() {
// We currently sample 1% whitelisted downloads from users who opted // We currently sample 1% whitelisted downloads from users who opted
// in extended reporting and are not in incognito mode. // in extended reporting and are not in incognito mode.
return service_ && is_extended_reporting_ && !is_incognito_ && return service_ && is_extended_reporting_ && !is_incognito_ &&
...@@ -1200,6 +1201,7 @@ class DownloadProtectionService::PPAPIDownloadRequest ...@@ -1200,6 +1201,7 @@ class DownloadProtectionService::PPAPIDownloadRequest
const GURL& requestor_url, const GURL& requestor_url,
const base::FilePath& default_file_path, const base::FilePath& default_file_path,
const std::vector<base::FilePath::StringType>& alternate_extensions, const std::vector<base::FilePath::StringType>& alternate_extensions,
Profile* profile,
const CheckDownloadCallback& callback, const CheckDownloadCallback& callback,
DownloadProtectionService* service, DownloadProtectionService* service,
scoped_refptr<SafeBrowsingDatabaseManager> database_manager) scoped_refptr<SafeBrowsingDatabaseManager> database_manager)
...@@ -1212,7 +1214,13 @@ class DownloadProtectionService::PPAPIDownloadRequest ...@@ -1212,7 +1214,13 @@ class DownloadProtectionService::PPAPIDownloadRequest
start_time_(base::TimeTicks::Now()), start_time_(base::TimeTicks::Now()),
supported_path_( supported_path_(
GetSupportedFilePath(default_file_path, alternate_extensions)), GetSupportedFilePath(default_file_path, alternate_extensions)),
weakptr_factory_(this) {} sample_url_whitelist_(false),
weakptr_factory_(this) {
DCHECK(profile);
is_extended_reporting_ = profile->GetPrefs()->GetBoolean(
prefs::kSafeBrowsingExtendedReportingEnabled);
is_incognito_ = profile->IsOffTheRecord();
}
~PPAPIDownloadRequest() override { ~PPAPIDownloadRequest() override {
if (fetcher_ && !callback_.is_null()) if (fetcher_ && !callback_.is_null())
...@@ -1262,6 +1270,13 @@ class DownloadProtectionService::PPAPIDownloadRequest ...@@ -1262,6 +1270,13 @@ class DownloadProtectionService::PPAPIDownloadRequest
} }
private: private:
bool ShouldSampleWhitelistedDownload() {
// We currently sample 1% whitelisted downloads from users who opted
// in extended reporting and are not in incognito mode.
return service_ && !is_incognito_ && is_extended_reporting_ &&
base::RandDouble() < service_->whitelist_sample_rate();
}
// Whitelist checking needs to the done on the IO thread. // Whitelist checking needs to the done on the IO thread.
static void CheckWhitelistsOnIOThread( static void CheckWhitelistsOnIOThread(
const GURL& requestor_url, const GURL& requestor_url,
...@@ -1282,10 +1297,14 @@ class DownloadProtectionService::PPAPIDownloadRequest ...@@ -1282,10 +1297,14 @@ class DownloadProtectionService::PPAPIDownloadRequest
void WhitelistCheckComplete(bool was_on_whitelist) { void WhitelistCheckComplete(bool was_on_whitelist) {
DVLOG(2) << __FUNCTION__ << " was_on_whitelist:" << was_on_whitelist; DVLOG(2) << __FUNCTION__ << " was_on_whitelist:" << was_on_whitelist;
if (was_on_whitelist) { if (was_on_whitelist) {
// TODO(asanka): Should sample whitelisted downloads based on RecordCountOfWhitelistedDownload(URL_WHITELIST);
// service_->whitelist_sample_rate(). http://crbug.com/610924 if (!ShouldSampleWhitelistedDownload()) {
Finish(RequestOutcome::WHITELIST_HIT, SAFE); Finish(RequestOutcome::WHITELIST_HIT, SAFE);
return; return;
}
sample_url_whitelist_ = true;
} else {
RecordCountOfWhitelistedDownload(NO_WHITELIST_MATCH);
} }
// Not on whitelist, so we are going to check with the SafeBrowsing // Not on whitelist, so we are going to check with the SafeBrowsing
...@@ -1306,6 +1325,11 @@ class DownloadProtectionService::PPAPIDownloadRequest ...@@ -1306,6 +1325,11 @@ class DownloadProtectionService::PPAPIDownloadRequest
request.set_file_basename(supported_path_.BaseName().AsUTF8Unsafe()); request.set_file_basename(supported_path_.BaseName().AsUTF8Unsafe());
request.set_length(0); request.set_length(0);
request.mutable_digests()->set_md5(std::string()); request.mutable_digests()->set_md5(std::string());
request.set_skipped_url_whitelist(sample_url_whitelist_);
// Download protection does not check certificate whitelist for
// base::RunLoop()
// downloads.
request.set_skipped_certificate_whitelist(false);
for (const auto& alternate_extension : alternate_extensions_) { for (const auto& alternate_extension : alternate_extensions_) {
if (alternate_extension.empty()) if (alternate_extension.empty())
continue; continue;
...@@ -1368,12 +1392,13 @@ class DownloadProtectionService::PPAPIDownloadRequest ...@@ -1368,12 +1392,13 @@ class DownloadProtectionService::PPAPIDownloadRequest
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
DVLOG(2) << __FUNCTION__ << " response: " << response; DVLOG(2) << __FUNCTION__ << " response: " << response;
UMA_HISTOGRAM_SPARSE_SLOWLY( UMA_HISTOGRAM_SPARSE_SLOWLY(
"SBClientDownload.PPAPIDownloadRequest.RequestOutcome", "SBClientDownload.base::RunLoop()DownloadRequest.RequestOutcome",
static_cast<int>(reason)); static_cast<int>(reason));
UMA_HISTOGRAM_SPARSE_SLOWLY("SBClientDownload.PPAPIDownloadRequest.Result", UMA_HISTOGRAM_SPARSE_SLOWLY(
response); "SBClientDownload.base::RunLoop()DownloadRequest.Result", response);
UMA_HISTOGRAM_TIMES("SBClientDownload.PPAPIDownloadRequest.RequestDuration", UMA_HISTOGRAM_TIMES(
start_time_ - base::TimeTicks::Now()); "SBClientDownload.base::RunLoop()DownloadRequest.RequestDuration",
start_time_ - base::TimeTicks::Now());
if (!callback_.is_null()) if (!callback_.is_null())
base::ResetAndReturn(&callback_).Run(response); base::ResetAndReturn(&callback_).Run(response);
fetcher_.reset(); fetcher_.reset();
...@@ -1458,6 +1483,12 @@ class DownloadProtectionService::PPAPIDownloadRequest ...@@ -1458,6 +1483,12 @@ class DownloadProtectionService::PPAPIDownloadRequest
// ping. // ping.
const base::FilePath supported_path_; const base::FilePath supported_path_;
bool sample_url_whitelist_;
bool is_extended_reporting_;
bool is_incognito_;
base::WeakPtrFactory<PPAPIDownloadRequest> weakptr_factory_; base::WeakPtrFactory<PPAPIDownloadRequest> weakptr_factory_;
DISALLOW_COPY_AND_ASSIGN(PPAPIDownloadRequest); DISALLOW_COPY_AND_ASSIGN(PPAPIDownloadRequest);
...@@ -1563,12 +1594,13 @@ void DownloadProtectionService::CheckPPAPIDownloadRequest( ...@@ -1563,12 +1594,13 @@ void DownloadProtectionService::CheckPPAPIDownloadRequest(
const GURL& requestor_url, const GURL& requestor_url,
const base::FilePath& default_file_path, const base::FilePath& default_file_path,
const std::vector<base::FilePath::StringType>& alternate_extensions, const std::vector<base::FilePath::StringType>& alternate_extensions,
Profile* profile,
const CheckDownloadCallback& callback) { const CheckDownloadCallback& callback) {
DVLOG(1) << __FUNCTION__ << " url:" << requestor_url DVLOG(1) << __FUNCTION__ << " url:" << requestor_url
<< " default_file_path:" << default_file_path.value(); << " default_file_path:" << default_file_path.value();
std::unique_ptr<PPAPIDownloadRequest> request(new PPAPIDownloadRequest( std::unique_ptr<PPAPIDownloadRequest> request(new PPAPIDownloadRequest(
requestor_url, default_file_path, alternate_extensions, callback, this, requestor_url, default_file_path, alternate_extensions, profile, callback,
database_manager_)); this, database_manager_));
PPAPIDownloadRequest* request_copy = request.get(); PPAPIDownloadRequest* request_copy = request.get();
auto insertion_result = ppapi_download_requests_.insert( auto insertion_result = ppapi_download_requests_.insert(
std::make_pair(request_copy, std::move(request))); std::make_pair(request_copy, std::move(request)));
......
...@@ -28,7 +28,6 @@ ...@@ -28,7 +28,6 @@
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace content { namespace content {
class DownloadItem; class DownloadItem;
class PageNavigator; class PageNavigator;
...@@ -38,6 +37,8 @@ namespace net { ...@@ -38,6 +37,8 @@ namespace net {
class X509Certificate; class X509Certificate;
} // namespace net } // namespace net
class Profile;
namespace safe_browsing { namespace safe_browsing {
class BinaryFeatureExtractor; class BinaryFeatureExtractor;
class ClientDownloadRequest; class ClientDownloadRequest;
...@@ -115,6 +116,7 @@ class DownloadProtectionService { ...@@ -115,6 +116,7 @@ class DownloadProtectionService {
const GURL& requestor_url, const GURL& requestor_url,
const base::FilePath& default_file_path, const base::FilePath& default_file_path,
const std::vector<base::FilePath::StringType>& alternate_extensions, const std::vector<base::FilePath::StringType>& alternate_extensions,
Profile* profile,
const CheckDownloadCallback& callback); const CheckDownloadCallback& callback);
// Display more information to the user regarding the download specified by // Display more information to the user regarding the download specified by
......
...@@ -166,6 +166,7 @@ class FakeDownloadProtectionService : public DownloadProtectionService { ...@@ -166,6 +166,7 @@ class FakeDownloadProtectionService : public DownloadProtectionService {
const GURL& requestor_url, const GURL& requestor_url,
const base::FilePath& default_file_path, const base::FilePath& default_file_path,
const std::vector<base::FilePath::StringType>& alternate_extensions, const std::vector<base::FilePath::StringType>& alternate_extensions,
Profile* profile_unused,
const CheckDownloadCallback& callback) override { const CheckDownloadCallback& callback) override {
const auto iter = const auto iter =
test_configuration_->result_map.find(default_file_path.Extension()); test_configuration_->result_map.find(default_file_path.Extension());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment