| 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 // 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 #pragma once | 10 #pragma once |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 namespace net { | 25 namespace net { |
| 26 class URLRequestContextGetter; | 26 class URLRequestContextGetter; |
| 27 class URLRequestStatus; | 27 class URLRequestStatus; |
| 28 } // namespace net | 28 } // namespace net |
| 29 class SafeBrowsingService; | 29 class SafeBrowsingService; |
| 30 | 30 |
| 31 namespace safe_browsing { | 31 namespace safe_browsing { |
| 32 | 32 |
| 33 // This class provides an asynchronous API to check whether a particular | 33 // This class provides an asynchronous API to check whether a particular |
| 34 // client download is malicious or not. | 34 // client download is malicious or not. |
| 35 class DownloadProtectionService | 35 class DownloadProtectionService : public URLFetcher::Delegate { |
| 36 : public base::RefCountedThreadSafe<DownloadProtectionService>, | |
| 37 public URLFetcher::Delegate { | |
| 38 public: | 36 public: |
| 39 // TODO(noelutz): we're missing some fields here: filename to get | 37 // TODO(noelutz): we're missing some fields here: server IPs, |
| 40 // the signature, server IPs, tab URL redirect chain, ... | 38 // tab URL redirect chain, ... |
| 41 struct DownloadInfo { | 39 struct DownloadInfo { |
| 40 FilePath local_file; |
| 42 std::vector<GURL> download_url_chain; | 41 std::vector<GURL> download_url_chain; |
| 43 GURL referrer_url; | 42 GURL referrer_url; |
| 44 std::string sha256_hash; | 43 std::string sha256_hash; |
| 45 int64 total_bytes; | 44 int64 total_bytes; |
| 46 bool user_initiated; | 45 bool user_initiated; |
| 47 DownloadInfo(); | 46 DownloadInfo(); |
| 48 ~DownloadInfo(); | 47 ~DownloadInfo(); |
| 49 }; | 48 }; |
| 50 | 49 |
| 51 enum DownloadCheckResult { | 50 enum DownloadCheckResult { |
| 52 SAFE, | 51 SAFE, |
| 53 MALICIOUS, | 52 MALICIOUS, |
| 54 // In the future we may introduce a third category which corresponds to | 53 // In the future we may introduce a third category which corresponds to |
| 55 // suspicious downloads that are not known to be malicious. | 54 // suspicious downloads that are not known to be malicious. |
| 56 }; | 55 }; |
| 57 | 56 |
| 58 // Callback type which is invoked once the download request is done. | 57 // Callback type which is invoked once the download request is done. |
| 59 typedef base::Callback<void(DownloadCheckResult)> CheckDownloadCallback; | 58 typedef base::Callback<void(DownloadCheckResult)> CheckDownloadCallback; |
| 60 | 59 |
| 61 // Creates a download service. The service is initially disabled. You need | 60 // Creates a download service. The service is initially disabled. You need |
| 62 // to call SetEnabled() to start it. We keep scoped references to both of | 61 // to call SetEnabled() to start it. We keep scoped references to both of |
| 63 // these objects. | 62 // these objects. |
| 64 DownloadProtectionService( | 63 DownloadProtectionService( |
| 65 SafeBrowsingService* sb_service, | 64 SafeBrowsingService* sb_service, |
| 66 net::URLRequestContextGetter* request_context_getter); | 65 net::URLRequestContextGetter* request_context_getter); |
| 67 | 66 |
| 67 // Note: This class may _only_ be deleted on the IO thread! |
| 68 virtual ~DownloadProtectionService(); |
| 69 |
| 68 // From the URLFetcher::Delegate interface. | 70 // From the URLFetcher::Delegate interface. |
| 69 virtual void OnURLFetchComplete(const URLFetcher* source, | 71 virtual void OnURLFetchComplete(const URLFetcher* source, |
| 70 const GURL& url, | 72 const GURL& url, |
| 71 const net::URLRequestStatus& status, | 73 const net::URLRequestStatus& status, |
| 72 int response_code, | 74 int response_code, |
| 73 const net::ResponseCookies& cookies, | 75 const net::ResponseCookies& cookies, |
| 74 const std::string& data) OVERRIDE; | 76 const std::string& data) OVERRIDE; |
| 75 | 77 |
| 76 // Checks whether the given client download is likely to be | 78 // Checks whether the given client download is likely to be |
| 77 // malicious or not. If this method returns true it means the | 79 // malicious or not. If this method returns true it means the |
| (...skipping 22 matching lines...) Expand all Loading... |
| 100 // Enum to keep track why a particular download verdict was chosen. | 102 // Enum to keep track why a particular download verdict was chosen. |
| 101 // This is used to keep some stats around. | 103 // This is used to keep some stats around. |
| 102 enum DownloadCheckResultReason { | 104 enum DownloadCheckResultReason { |
| 103 REASON_INVALID_URL, | 105 REASON_INVALID_URL, |
| 104 REASON_SB_DISABLED, | 106 REASON_SB_DISABLED, |
| 105 REASON_WHITELISTED_URL, | 107 REASON_WHITELISTED_URL, |
| 106 REASON_WHITELISTED_REFERRER, | 108 REASON_WHITELISTED_REFERRER, |
| 107 REASON_INVALID_REQUEST_PROTO, | 109 REASON_INVALID_REQUEST_PROTO, |
| 108 REASON_SERVER_PING_FAILED, | 110 REASON_SERVER_PING_FAILED, |
| 109 REASON_INVALID_RESPONSE_PROTO, | 111 REASON_INVALID_RESPONSE_PROTO, |
| 112 REASON_NOT_BINARY_FILE, |
| 110 REASON_MAX // Always add new values before this one. | 113 REASON_MAX // Always add new values before this one. |
| 111 }; | 114 }; |
| 112 | 115 |
| 113 virtual ~DownloadProtectionService(); | |
| 114 | |
| 115 private: | 116 private: |
| 116 friend class base::RefCountedThreadSafe<DownloadProtectionService>; | 117 friend class base::RefCountedThreadSafe<DownloadProtectionService>; |
| 117 friend class DownloadProtectionServiceTest; | 118 friend class DownloadProtectionServiceTest; |
| 118 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 119 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| 119 CheckClientDownloadValidateRequest); | 120 CheckClientDownloadValidateRequest); |
| 120 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 121 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| 121 CheckClientDownloadSuccess); | 122 CheckClientDownloadSuccess); |
| 122 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 123 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| 123 CheckClientDownloadFetchFailed); | 124 CheckClientDownloadFetchFailed); |
| 124 | 125 |
| 125 static const char kDownloadRequestUrl[]; | 126 static const char kDownloadRequestUrl[]; |
| 126 | 127 |
| 128 // Extracts features from the downloaded file. Runs in the file thread. |
| 129 // When finished, invokes StartCheckClientDownload on the IO thread. |
| 130 // |pingback_enabled| contains the value of |enabled_| when |
| 131 // CheckClientDownload was called; this is because enabled_ can only be |
| 132 // accessed on the UI thread. |
| 133 void ExtractFileFeatures(const DownloadInfo& info, |
| 134 bool pingback_enabled, |
| 135 const CheckDownloadCallback& callback); |
| 136 |
| 127 // Same as above but this method is called on the IO thread after we have | 137 // Same as above but this method is called on the IO thread after we have |
| 128 // done some basic checks to see whether the download is definitely not | 138 // done some basic checks to see whether the download is definitely not |
| 129 // safe. | 139 // safe. |
| 130 void StartCheckClientDownload(const DownloadInfo& info, | 140 void StartCheckClientDownload(const DownloadInfo& info, |
| 141 bool pingback_enabled, |
| 131 const CheckDownloadCallback& callback); | 142 const CheckDownloadCallback& callback); |
| 132 | 143 |
| 133 // This function must run on the UI thread and will invoke the callback | 144 // This function must run on the UI thread and will invoke the callback |
| 134 // with the given result. | 145 // with the given result. |
| 135 void EndCheckClientDownload(DownloadCheckResult result, | 146 void EndCheckClientDownload(DownloadCheckResult result, |
| 136 DownloadCheckResultReason reason, | 147 DownloadCheckResultReason reason, |
| 137 const CheckDownloadCallback& callback); | 148 const CheckDownloadCallback& callback); |
| 138 | 149 |
| 139 void RecordStats(DownloadCheckResultReason reason); | 150 void RecordStats(DownloadCheckResultReason reason); |
| 140 | 151 |
| 141 // SetEnabled(bool) calls this method on the IO thread. | 152 // SetEnabled(bool) calls this method on the IO thread. |
| 142 void SetEnabledOnIOThread(bool enableed); | 153 void SetEnabledOnIOThread(bool enableed); |
| 143 | 154 |
| 144 // This pointer may be NULL if SafeBrowsing is disabled. | 155 // This pointer may be NULL if SafeBrowsing is disabled. |
| 145 scoped_refptr<SafeBrowsingService> sb_service_; | 156 // The SafeBrowsingService owns us. |
| 157 SafeBrowsingService* sb_service_; |
| 146 | 158 |
| 147 // The context we use to issue network requests. | 159 // The context we use to issue network requests. |
| 148 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 160 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 149 | 161 |
| 150 // Map of client download request to the corresponding callback that | 162 // Map of client download request to the corresponding callback that |
| 151 // has to be invoked when the request is done. This map contains all | 163 // has to be invoked when the request is done. This map contains all |
| 152 // pending server requests. | 164 // pending server requests. |
| 153 std::map<const URLFetcher*, CheckDownloadCallback> download_requests_; | 165 std::map<const URLFetcher*, CheckDownloadCallback> download_requests_; |
| 154 | 166 |
| 155 // Keeps track of the state of the service. | 167 // Keeps track of the state of the service. |
| 156 bool enabled_; | 168 bool enabled_; |
| 157 | 169 |
| 158 DISALLOW_COPY_AND_ASSIGN(DownloadProtectionService); | 170 DISALLOW_COPY_AND_ASSIGN(DownloadProtectionService); |
| 159 }; | 171 }; |
| 160 } // namespace safe_browsing | 172 } // namespace safe_browsing |
| 161 | 173 |
| 162 #endif // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ | 174 #endif // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ |
| OLD | NEW |