| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/unverified_download_policy.h" | 5 #include "chrome/browser/safe_browsing/unverified_download_policy.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/callback.h" | 10 #include "base/callback.h" |
| 9 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 10 #include "base/metrics/sparse_histogram.h" | 12 #include "base/metrics/sparse_histogram.h" |
| 11 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 14 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 13 #include "chrome/browser/safe_browsing/unverified_download_field_trial.h" | 15 #include "chrome/browser/safe_browsing/unverified_download_field_trial.h" |
| 14 #include "chrome/common/safe_browsing/download_protection_util.h" | 16 #include "chrome/common/safe_browsing/download_protection_util.h" |
| 15 #include "components/rappor/rappor_service.h" | 17 #include "components/rappor/rappor_service.h" |
| 16 #include "components/rappor/rappor_utils.h" | 18 #include "components/rappor/rappor_utils.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 int uma_file_type = | 55 int uma_file_type = |
| 54 download_protection_util::GetSBClientDownloadExtensionValueForUMA(file); | 56 download_protection_util::GetSBClientDownloadExtensionValueForUMA(file); |
| 55 if (policy == UnverifiedDownloadPolicy::ALLOWED) { | 57 if (policy == UnverifiedDownloadPolicy::ALLOWED) { |
| 56 RecordPolicyMetric("SafeBrowsing.UnverifiedDownloads.Allowed", | 58 RecordPolicyMetric("SafeBrowsing.UnverifiedDownloads.Allowed", |
| 57 uma_file_type, requestor); | 59 uma_file_type, requestor); |
| 58 } else { | 60 } else { |
| 59 RecordPolicyMetric("SafeBrowsing.UnverifiedDownloads.Blocked", | 61 RecordPolicyMetric("SafeBrowsing.UnverifiedDownloads.Blocked", |
| 60 uma_file_type, requestor); | 62 uma_file_type, requestor); |
| 61 } | 63 } |
| 62 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 64 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 63 base::Bind(callback, policy)); | 65 base::Bind(callback, policy)); |
| 64 } | 66 } |
| 65 | 67 |
| 66 void CheckFieldTrialOnAnyThread( | 68 void CheckFieldTrialOnAnyThread( |
| 67 const base::FilePath& file, | 69 const base::FilePath& file, |
| 70 const std::vector<base::FilePath::StringType>& alternate_extensions, |
| 68 const GURL& requestor, | 71 const GURL& requestor, |
| 69 const UnverifiedDownloadCheckCompletionCallback& callback) { | 72 const UnverifiedDownloadCheckCompletionCallback& callback) { |
| 70 bool is_allowed = IsUnverifiedDownloadAllowedByFieldTrial(file); | 73 if (!IsUnverifiedDownloadAllowedByFieldTrial(file)) { |
| 71 RespondWithPolicy(file, callback, requestor, is_allowed | 74 RespondWithPolicy(file, callback, requestor, |
| 72 ? UnverifiedDownloadPolicy::ALLOWED | 75 UnverifiedDownloadPolicy::DISALLOWED); |
| 73 : UnverifiedDownloadPolicy::DISALLOWED); | 76 return; |
| 77 } |
| 78 |
| 79 for (const auto& extension : alternate_extensions) { |
| 80 base::FilePath alternate_filename = file.AddExtension(extension); |
| 81 if (!IsUnverifiedDownloadAllowedByFieldTrial(alternate_filename)) { |
| 82 RespondWithPolicy(alternate_filename, callback, requestor, |
| 83 UnverifiedDownloadPolicy::DISALLOWED); |
| 84 return; |
| 85 } |
| 86 } |
| 87 |
| 88 RespondWithPolicy(file, callback, requestor, |
| 89 UnverifiedDownloadPolicy::ALLOWED); |
| 74 } | 90 } |
| 75 | 91 |
| 76 void CheckWhitelistOnIOThread( | 92 void CheckWhitelistOnIOThread( |
| 77 scoped_refptr<SafeBrowsingService> service, | 93 scoped_refptr<SafeBrowsingService> service, |
| 78 const GURL& requestor, | 94 const GURL& requestor, |
| 79 const base::FilePath& file, | 95 const base::FilePath& file, |
| 96 const std::vector<base::FilePath::StringType>& alternate_extensions, |
| 80 const UnverifiedDownloadCheckCompletionCallback& callback) { | 97 const UnverifiedDownloadCheckCompletionCallback& callback) { |
| 81 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 98 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 82 int uma_file_type = | 99 int uma_file_type = |
| 83 download_protection_util::GetSBClientDownloadExtensionValueForUMA(file); | 100 download_protection_util::GetSBClientDownloadExtensionValueForUMA(file); |
| 84 | 101 |
| 85 if (!service || !service->enabled()) { | 102 if (!service || !service->enabled()) { |
| 86 // If the SafeBrowsing service was disabled, don't try to check against the | 103 // If the SafeBrowsing service was disabled, don't try to check against the |
| 87 // field trial list. Instead allow the download. We are assuming that if the | 104 // field trial list. Instead allow the download. We are assuming that if the |
| 88 // SafeBrowsing service was disabled for this user, then we shouldn't | 105 // SafeBrowsing service was disabled for this user, then we shouldn't |
| 89 // interefere with unverified downloads. | 106 // interefere with unverified downloads. |
| 90 RecordPolicyMetric( | 107 RecordPolicyMetric( |
| 91 "SafeBrowsing.UnverifiedDownloads.AllowedDueToDisabledService", | 108 "SafeBrowsing.UnverifiedDownloads.AllowedDueToDisabledService", |
| 92 uma_file_type, requestor); | 109 uma_file_type, requestor); |
| 93 RespondWithPolicy(file, callback, requestor, | 110 RespondWithPolicy(file, callback, requestor, |
| 94 UnverifiedDownloadPolicy::ALLOWED); | 111 UnverifiedDownloadPolicy::ALLOWED); |
| 95 return; | 112 return; |
| 96 } | 113 } |
| 97 | 114 |
| 98 if (service->database_manager() && | 115 if (service->database_manager() && |
| 99 service->database_manager()->MatchDownloadWhitelistUrl(requestor)) { | 116 service->database_manager()->MatchDownloadWhitelistUrl(requestor)) { |
| 100 RecordPolicyMetric("SafeBrowsing.UnverifiedDownloads.AllowedByWhitelist", | 117 RecordPolicyMetric("SafeBrowsing.UnverifiedDownloads.AllowedByWhitelist", |
| 101 uma_file_type, requestor); | 118 uma_file_type, requestor); |
| 102 RespondWithPolicy(file, callback, requestor, | 119 RespondWithPolicy(file, callback, requestor, |
| 103 UnverifiedDownloadPolicy::ALLOWED); | 120 UnverifiedDownloadPolicy::ALLOWED); |
| 104 return; | 121 return; |
| 105 } | 122 } |
| 106 | 123 |
| 107 CheckFieldTrialOnAnyThread(file, requestor, callback); | 124 CheckFieldTrialOnAnyThread(file, alternate_extensions, requestor, callback); |
| 108 } | 125 } |
| 109 | 126 |
| 110 } // namespace | 127 } // namespace |
| 111 | 128 |
| 112 void CheckUnverifiedDownloadPolicy( | 129 void CheckUnverifiedDownloadPolicy( |
| 113 const GURL& requestor, | 130 const GURL& requestor, |
| 114 const base::FilePath& file, | 131 const base::FilePath& file, |
| 132 const std::vector<base::FilePath::StringType>& alternate_extensions, |
| 115 const UnverifiedDownloadCheckCompletionCallback& callback) { | 133 const UnverifiedDownloadCheckCompletionCallback& callback) { |
| 134 // Record a count of alternate extensions. The count is capped so that it |
| 135 // won't be unbounded. In practice, it's expected that numbers above 3 are |
| 136 // rare. |
| 137 int alternate_extension_count = |
| 138 std::min<int>(alternate_extensions.size(), 16); |
| 139 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 140 "SafeBrowsing.UnverifiedDownloads.AlternateExtensionCount", |
| 141 alternate_extension_count); |
| 116 if (requestor.is_valid()) { | 142 if (requestor.is_valid()) { |
| 117 scoped_refptr<SafeBrowsingService> service = | 143 scoped_refptr<SafeBrowsingService> service = |
| 118 g_browser_process->safe_browsing_service(); | 144 g_browser_process->safe_browsing_service(); |
| 119 BrowserThread::PostTask( | 145 BrowserThread::PostTask( |
| 120 BrowserThread::IO, FROM_HERE, | 146 BrowserThread::IO, FROM_HERE, |
| 121 base::Bind(&CheckWhitelistOnIOThread, service, requestor, file, | 147 base::Bind(&CheckWhitelistOnIOThread, service, requestor, file, |
| 122 callback)); | 148 alternate_extensions, callback)); |
| 123 return; | 149 return; |
| 124 } | 150 } |
| 125 | 151 |
| 126 CheckFieldTrialOnAnyThread(file, GURL(), callback); | 152 CheckFieldTrialOnAnyThread(file, alternate_extensions, GURL(), callback); |
| 127 } | 153 } |
| 128 | 154 |
| 129 } // namespace safe_browsing | 155 } // namespace safe_browsing |
| OLD | NEW |