Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 // Helper class which handles communication with the SafeBrowsing servers for | 5 // Helper class which handles communication with the SafeBrowsing servers for |
| 6 // improved binary download protection. | 6 // improved binary download protection. |
| 7 | 7 |
| 8 #ifndef CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ | 8 #ifndef CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ |
| 9 #define CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ | 9 #define CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ |
| 10 | 10 |
| 11 #include <stdint.h> | 11 #include <stdint.h> |
| 12 | 12 |
| 13 #include <memory> | 13 #include <memory> |
| 14 #include <set> | 14 #include <set> |
| 15 #include <string> | 15 #include <string> |
| 16 #include <unordered_map> | 16 #include <unordered_map> |
| 17 #include <vector> | 17 #include <vector> |
| 18 | 18 |
| 19 #include "base/callback.h" | 19 #include "base/callback.h" |
| 20 #include "base/callback_list.h" | 20 #include "base/callback_list.h" |
| 21 #include "base/files/file_path.h" | 21 #include "base/files/file_path.h" |
| 22 #include "base/gtest_prod_util.h" | 22 #include "base/gtest_prod_util.h" |
| 23 #include "base/macros.h" | 23 #include "base/macros.h" |
| 24 #include "base/memory/ref_counted.h" | 24 #include "base/memory/ref_counted.h" |
| 25 #include "base/supports_user_data.h" | |
| 25 #include "chrome/browser/safe_browsing/ui_manager.h" | 26 #include "chrome/browser/safe_browsing/ui_manager.h" |
| 26 #include "components/safe_browsing_db/database_manager.h" | 27 #include "components/safe_browsing_db/database_manager.h" |
| 27 #include "net/url_request/url_request_context_getter.h" | 28 #include "net/url_request/url_request_context_getter.h" |
| 28 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 29 | 30 |
| 30 | 31 |
| 31 namespace content { | 32 namespace content { |
| 32 class DownloadItem; | 33 class DownloadItem; |
| 33 class PageNavigator; | 34 class PageNavigator; |
| 34 } | 35 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 virtual void ParseManualBlacklistFlag(); | 86 virtual void ParseManualBlacklistFlag(); |
| 86 | 87 |
| 87 // Return true if this hash value is blacklisted via flag (for testing). | 88 // Return true if this hash value is blacklisted via flag (for testing). |
| 88 virtual bool IsHashManuallyBlacklisted(const std::string& sha256_hash) const; | 89 virtual bool IsHashManuallyBlacklisted(const std::string& sha256_hash) const; |
| 89 | 90 |
| 90 // Checks whether the given client download is likely to be malicious or not. | 91 // Checks whether the given client download is likely to be malicious or not. |
| 91 // The result is delivered asynchronously via the given callback. This | 92 // The result is delivered asynchronously via the given callback. This |
| 92 // method must be called on the UI thread, and the callback will also be | 93 // method must be called on the UI thread, and the callback will also be |
| 93 // invoked on the UI thread. This method must be called once the download | 94 // invoked on the UI thread. This method must be called once the download |
| 94 // is finished and written to disk. | 95 // is finished and written to disk. |
| 95 virtual void CheckClientDownload(content::DownloadItem* item, | 96 virtual void CheckClientDownload( |
| 96 const CheckDownloadCallback& callback); | 97 content::DownloadItem* item, |
| 98 const CheckDownloadCallback& callback); | |
| 97 | 99 |
| 98 // Checks whether any of the URLs in the redirect chain of the | 100 // Checks whether any of the URLs in the redirect chain of the |
| 99 // download match the SafeBrowsing bad binary URL list. The result is | 101 // download match the SafeBrowsing bad binary URL list. The result is |
| 100 // delivered asynchronously via the given callback. This method must be | 102 // delivered asynchronously via the given callback. This method must be |
| 101 // called on the UI thread, and the callback will also be invoked on the UI | 103 // called on the UI thread, and the callback will also be invoked on the UI |
| 102 // thread. Pre-condition: !info.download_url_chain.empty(). | 104 // thread. Pre-condition: !info.download_url_chain.empty(). |
| 103 virtual void CheckDownloadUrl(const content::DownloadItem& item, | 105 virtual void CheckDownloadUrl(const content::DownloadItem& item, |
| 104 const CheckDownloadCallback& callback); | 106 const CheckDownloadCallback& callback); |
| 105 | 107 |
| 106 // Returns true iff the download specified by |info| should be scanned by | 108 // Returns true iff the download specified by |info| should be scanned by |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 | 143 |
| 142 // Registers a callback that will be run when a ClientDownloadRequest has | 144 // Registers a callback that will be run when a ClientDownloadRequest has |
| 143 // been formed. | 145 // been formed. |
| 144 ClientDownloadRequestSubscription RegisterClientDownloadRequestCallback( | 146 ClientDownloadRequestSubscription RegisterClientDownloadRequestCallback( |
| 145 const ClientDownloadRequestCallback& callback); | 147 const ClientDownloadRequestCallback& callback); |
| 146 | 148 |
| 147 double whitelist_sample_rate() const { | 149 double whitelist_sample_rate() const { |
| 148 return whitelist_sample_rate_; | 150 return whitelist_sample_rate_; |
| 149 } | 151 } |
| 150 | 152 |
| 153 static void SetDownloadPingToken(content::DownloadItem* item, | |
| 154 const std::string& token); | |
| 155 | |
| 156 static std::string GetDownloadPingToken(const content::DownloadItem* item); | |
| 157 | |
| 151 protected: | 158 protected: |
| 152 // Enum to keep track why a particular download verdict was chosen. | 159 // Enum to keep track why a particular download verdict was chosen. |
| 153 // This is used to keep some stats around. | 160 // This is used to keep some stats around. |
| 154 enum DownloadCheckResultReason { | 161 enum DownloadCheckResultReason { |
| 155 REASON_INVALID_URL, | 162 REASON_INVALID_URL, |
| 156 REASON_SB_DISABLED, | 163 REASON_SB_DISABLED, |
| 157 REASON_WHITELISTED_URL, | 164 REASON_WHITELISTED_URL, |
| 158 REASON_WHITELISTED_REFERRER, | 165 REASON_WHITELISTED_REFERRER, |
| 159 REASON_INVALID_REQUEST_PROTO, | 166 REASON_INVALID_REQUEST_PROTO, |
| 160 REASON_SERVER_PING_FAILED, | 167 REASON_SERVER_PING_FAILED, |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 176 REASON_DOWNLOAD_POTENTIALLY_UNWANTED, | 183 REASON_DOWNLOAD_POTENTIALLY_UNWANTED, |
| 177 REASON_UNSUPPORTED_URL_SCHEME, | 184 REASON_UNSUPPORTED_URL_SCHEME, |
| 178 REASON_MANUAL_BLACKLIST, | 185 REASON_MANUAL_BLACKLIST, |
| 179 REASON_MAX // Always add new values before this one. | 186 REASON_MAX // Always add new values before this one. |
| 180 }; | 187 }; |
| 181 | 188 |
| 182 private: | 189 private: |
| 183 class CheckClientDownloadRequest; | 190 class CheckClientDownloadRequest; |
| 184 class PPAPIDownloadRequest; | 191 class PPAPIDownloadRequest; |
| 185 friend class DownloadProtectionServiceTest; | 192 friend class DownloadProtectionServiceTest; |
| 193 friend class DownloadDangerPromptTest; | |
| 186 | 194 |
| 187 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 195 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| 188 CheckClientDownloadWhitelistedUrlWithoutSampling); | 196 CheckClientDownloadWhitelistedUrlWithoutSampling); |
| 189 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 197 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| 190 CheckClientDownloadWhitelistedUrlWithSampling); | 198 CheckClientDownloadWhitelistedUrlWithSampling); |
| 191 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 199 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| 192 CheckClientDownloadValidateRequest); | 200 CheckClientDownloadValidateRequest); |
| 193 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 201 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| 194 CheckClientDownloadSuccess); | 202 CheckClientDownloadSuccess); |
| 195 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 203 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 208 CheckClientCrxDownloadSuccess); | 216 CheckClientCrxDownloadSuccess); |
| 209 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 217 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| 210 PPAPIDownloadRequest_InvalidResponse); | 218 PPAPIDownloadRequest_InvalidResponse); |
| 211 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 219 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| 212 PPAPIDownloadRequest_Timeout); | 220 PPAPIDownloadRequest_Timeout); |
| 213 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceFlagTest, | 221 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceFlagTest, |
| 214 CheckClientDownloadOverridenByFlag); | 222 CheckClientDownloadOverridenByFlag); |
| 215 | 223 |
| 216 static const char kDownloadRequestUrl[]; | 224 static const char kDownloadRequestUrl[]; |
| 217 | 225 |
| 226 static const void* const kDownloadPingTokenKey; | |
|
Nathan Parker
2016/06/07 23:00:53
Could all of this be in an anonymous namespace in
Jialiu Lin
2016/06/08 00:06:16
Yes, I'm using it in download_danger_prompt_browse
| |
| 227 | |
| 228 // Helper class for easy setting and getting token string. | |
| 229 class DownloadPingToken : public base::SupportsUserData::Data { | |
| 230 public: | |
| 231 explicit DownloadPingToken(const std::string& token) | |
| 232 : token_string_(token) {} | |
| 233 | |
| 234 std::string token_string() { | |
| 235 return token_string_; | |
| 236 } | |
| 237 private: | |
| 238 std::string token_string_; | |
| 239 | |
| 240 DISALLOW_COPY_AND_ASSIGN(DownloadPingToken); | |
| 241 }; | |
| 242 | |
| 218 // Cancels all requests in |download_requests_|, and empties it, releasing | 243 // Cancels all requests in |download_requests_|, and empties it, releasing |
| 219 // the references to the requests. | 244 // the references to the requests. |
| 220 void CancelPendingRequests(); | 245 void CancelPendingRequests(); |
| 221 | 246 |
| 222 // Called by a CheckClientDownloadRequest instance when it finishes, to | 247 // Called by a CheckClientDownloadRequest instance when it finishes, to |
| 223 // remove it from |download_requests_|. | 248 // remove it from |download_requests_|. |
| 224 void RequestFinished(CheckClientDownloadRequest* request); | 249 void RequestFinished(CheckClientDownloadRequest* request); |
| 225 | 250 |
| 226 void PPAPIDownloadCheckRequestFinished(PPAPIDownloadRequest* request); | 251 void PPAPIDownloadCheckRequestFinished(PPAPIDownloadRequest* request); |
| 227 | 252 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 271 std::set<std::string> manual_blacklist_hashes_; | 296 std::set<std::string> manual_blacklist_hashes_; |
| 272 | 297 |
| 273 // Rate of whitelisted downloads we sample to send out download ping. | 298 // Rate of whitelisted downloads we sample to send out download ping. |
| 274 double whitelist_sample_rate_; | 299 double whitelist_sample_rate_; |
| 275 | 300 |
| 276 DISALLOW_COPY_AND_ASSIGN(DownloadProtectionService); | 301 DISALLOW_COPY_AND_ASSIGN(DownloadProtectionService); |
| 277 }; | 302 }; |
| 278 } // namespace safe_browsing | 303 } // namespace safe_browsing |
| 279 | 304 |
| 280 #endif // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ | 305 #endif // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ |
| OLD | NEW |