Chromium Code Reviews| 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 |
| 11 | 11 |
| 12 #include <map> | 12 #include <set> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
| 17 #include "base/callback.h" | 17 #include "base/callback.h" |
| 18 #include "base/file_path.h" | |
| 18 #include "base/gtest_prod_util.h" | 19 #include "base/gtest_prod_util.h" |
| 19 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 20 #include "base/memory/scoped_ptr.h" | |
| 21 #include "base/time.h" | |
| 22 #include "content/public/common/url_fetcher_delegate.h" | |
| 23 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
| 24 | 22 |
| 25 namespace net { | 23 namespace net { |
| 26 class URLRequestContextGetter; | 24 class URLRequestContextGetter; |
| 27 class URLRequestStatus; | 25 class URLRequestStatus; |
| 28 } // namespace net | 26 } // namespace net |
| 27 class DownloadItem; | |
| 29 class SafeBrowsingService; | 28 class SafeBrowsingService; |
| 30 | 29 |
| 31 namespace safe_browsing { | 30 namespace safe_browsing { |
| 32 | 31 |
| 33 // This class provides an asynchronous API to check whether a particular | 32 // This class provides an asynchronous API to check whether a particular |
| 34 // client download is malicious or not. | 33 // client download is malicious or not. |
| 35 class DownloadProtectionService | 34 class DownloadProtectionService { |
| 36 : public base::RefCountedThreadSafe<DownloadProtectionService>, | |
| 37 public content::URLFetcherDelegate { | |
| 38 public: | 35 public: |
| 39 // TODO(noelutz): we're missing some fields here: filename to get | 36 // TODO(noelutz): we're missing some fields here: server IPs, |
| 40 // the signature, server IPs, tab URL redirect chain, ... | 37 // tab URL redirect chain, ... |
| 41 struct DownloadInfo { | 38 struct DownloadInfo { |
| 39 FilePath local_file; | |
| 42 std::vector<GURL> download_url_chain; | 40 std::vector<GURL> download_url_chain; |
| 43 GURL referrer_url; | 41 GURL referrer_url; |
| 44 std::string sha256_hash; | 42 std::string sha256_hash; |
| 45 int64 total_bytes; | 43 int64 total_bytes; |
| 46 bool user_initiated; | 44 bool user_initiated; |
| 47 DownloadInfo(); | 45 DownloadInfo(); |
| 48 ~DownloadInfo(); | 46 ~DownloadInfo(); |
| 47 | |
| 48 // Creates a DownloadInfo from a DownloadItem object. | |
| 49 static DownloadInfo FromDownloadItem(const DownloadItem& item); | |
| 49 }; | 50 }; |
| 50 | 51 |
| 51 enum DownloadCheckResult { | 52 enum DownloadCheckResult { |
| 52 SAFE, | 53 SAFE, |
| 53 MALICIOUS, | 54 MALICIOUS, |
| 54 // In the future we may introduce a third category which corresponds to | 55 // In the future we may introduce a third category which corresponds to |
| 55 // suspicious downloads that are not known to be malicious. | 56 // suspicious downloads that are not known to be malicious. |
| 56 }; | 57 }; |
| 57 | 58 |
| 58 // Callback type which is invoked once the download request is done. | 59 // Callback type which is invoked once the download request is done. |
| 59 typedef base::Callback<void(DownloadCheckResult)> CheckDownloadCallback; | 60 typedef base::Callback<void(DownloadCheckResult)> CheckDownloadCallback; |
| 60 | 61 |
| 61 // Creates a download service. The service is initially disabled. You need | 62 // 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 | 63 // to call SetEnabled() to start it. |sb_service| owns this object; we |
| 63 // these objects. | 64 // keep a reference to |request_context_getter|. |
| 64 DownloadProtectionService( | 65 DownloadProtectionService( |
| 65 SafeBrowsingService* sb_service, | 66 SafeBrowsingService* sb_service, |
| 66 net::URLRequestContextGetter* request_context_getter); | 67 net::URLRequestContextGetter* request_context_getter); |
| 67 | 68 |
| 68 // From the content::URLFetcherDelegate interface. | 69 virtual ~DownloadProtectionService(); |
| 69 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | |
| 70 | 70 |
| 71 // Checks whether the given client download is likely to be | 71 // Checks whether the given client download is likely to be malicious or not. |
| 72 // malicious or not. If this method returns true it means the | 72 // The result is delivered asynchronously via the given callback. This |
| 73 // download is safe or we're unable to tell whether it is safe or | 73 // method must be called on the UI thread, and the callback will also be |
| 74 // not. In this case the callback will never be called. If this | 74 // invoked on the UI thread. |
| 75 // method returns false we will asynchronously check whether the | 75 virtual void CheckClientDownload(const DownloadInfo& info, |
|
noelutz
2011/10/25 21:43:50
It would be nice to keep the previous API. Eventu
Brian Ryner
2011/10/25 22:03:07
Is there a drawback to always returning false from
noelutz
2011/10/25 22:13:42
Randy might have some opinion on that. I don't kn
| |
| 76 // download is safe and call the callback when we have the response. | |
| 77 // This method should be called on the UI thread. The callback will | |
| 78 // be called on the UI thread and will always be called asynchronously. | |
| 79 virtual bool CheckClientDownload(const DownloadInfo& info, | |
| 80 const CheckDownloadCallback& callback); | 76 const CheckDownloadCallback& callback); |
| 81 | 77 |
| 82 // Enables or disables the service. This is usually called by the | 78 // Enables or disables the service. This is usually called by the |
| 83 // SafeBrowsingService, which tracks whether any profile uses these services | 79 // SafeBrowsingService, which tracks whether any profile uses these services |
| 84 // at all. Disabling cancels any pending requests; existing requests will | 80 // at all. Disabling causes any pending and future requests to have their |
| 85 // have their callbacks called with "SAFE" results. Note: SetEnabled() is | 81 // callbacks called with "SAFE" results. |
| 86 // asynchronous because this method is called on the UI thread but most | |
| 87 // everything else happens on the IO thread. | |
| 88 void SetEnabled(bool enabled); | 82 void SetEnabled(bool enabled); |
| 89 | 83 |
| 90 bool enabled() const { | 84 bool enabled() const { |
| 91 return enabled_; | 85 return enabled_; |
| 92 } | 86 } |
| 93 | 87 |
| 94 protected: | 88 protected: |
| 95 // Enum to keep track why a particular download verdict was chosen. | 89 // Enum to keep track why a particular download verdict was chosen. |
| 96 // This is used to keep some stats around. | 90 // This is used to keep some stats around. |
| 97 enum DownloadCheckResultReason { | 91 enum DownloadCheckResultReason { |
| 98 REASON_INVALID_URL, | 92 REASON_INVALID_URL, |
| 99 REASON_SB_DISABLED, | 93 REASON_SB_DISABLED, |
| 100 REASON_WHITELISTED_URL, | 94 REASON_WHITELISTED_URL, |
| 101 REASON_WHITELISTED_REFERRER, | 95 REASON_WHITELISTED_REFERRER, |
| 102 REASON_INVALID_REQUEST_PROTO, | 96 REASON_INVALID_REQUEST_PROTO, |
| 103 REASON_SERVER_PING_FAILED, | 97 REASON_SERVER_PING_FAILED, |
| 104 REASON_INVALID_RESPONSE_PROTO, | 98 REASON_INVALID_RESPONSE_PROTO, |
| 99 REASON_NOT_BINARY_FILE, | |
| 105 REASON_MAX // Always add new values before this one. | 100 REASON_MAX // Always add new values before this one. |
| 106 }; | 101 }; |
| 107 | 102 |
| 108 virtual ~DownloadProtectionService(); | |
| 109 | |
| 110 private: | 103 private: |
| 111 friend class base::RefCountedThreadSafe<DownloadProtectionService>; | 104 class CheckClientDownloadRequest; // Per-request state |
| 112 friend class DownloadProtectionServiceTest; | 105 friend class DownloadProtectionServiceTest; |
| 113 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 106 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| 114 CheckClientDownloadValidateRequest); | 107 CheckClientDownloadValidateRequest); |
| 115 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 108 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| 116 CheckClientDownloadSuccess); | 109 CheckClientDownloadSuccess); |
| 117 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 110 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
| 118 CheckClientDownloadFetchFailed); | 111 CheckClientDownloadFetchFailed); |
| 119 | 112 |
| 120 static const char kDownloadRequestUrl[]; | 113 static const char kDownloadRequestUrl[]; |
| 121 | 114 |
| 122 // Same as above but this method is called on the IO thread after we have | 115 // Called by a CheckClientDownloadRequest instance when it finishes, to |
| 123 // done some basic checks to see whether the download is definitely not | 116 // remove it from |download_requests_|. |
| 124 // safe. | 117 void RequestFinished(CheckClientDownloadRequest* request); |
| 125 void StartCheckClientDownload(const DownloadInfo& info, | |
| 126 const CheckDownloadCallback& callback); | |
| 127 | 118 |
| 128 // This function must run on the UI thread and will invoke the callback | 119 static void FillDownloadInfo(const DownloadItem& item, |
| 129 // with the given result. | 120 DownloadInfo* download_info); |
| 130 void EndCheckClientDownload(DownloadCheckResult result, | |
| 131 DownloadCheckResultReason reason, | |
| 132 const CheckDownloadCallback& callback); | |
| 133 | |
| 134 void RecordStats(DownloadCheckResultReason reason); | |
| 135 | |
| 136 // SetEnabled(bool) calls this method on the IO thread. | |
| 137 void SetEnabledOnIOThread(bool enableed); | |
| 138 | 121 |
| 139 // This pointer may be NULL if SafeBrowsing is disabled. | 122 // This pointer may be NULL if SafeBrowsing is disabled. |
| 140 scoped_refptr<SafeBrowsingService> sb_service_; | 123 // The SafeBrowsingService owns us. |
| 124 SafeBrowsingService* sb_service_; | |
| 141 | 125 |
| 142 // The context we use to issue network requests. | 126 // The context we use to issue network requests. |
| 143 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 127 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 144 | 128 |
| 145 // Map of client download request to the corresponding callback that | 129 // Map of client download request to the corresponding callback that |
| 146 // has to be invoked when the request is done. This map contains all | 130 // has to be invoked when the request is done. This map contains all |
| 147 // pending server requests. | 131 // pending server requests. |
| 148 std::map<const URLFetcher*, CheckDownloadCallback> download_requests_; | 132 std::set<scoped_refptr<CheckClientDownloadRequest> > download_requests_; |
| 149 | 133 |
| 150 // Keeps track of the state of the service. | 134 // Keeps track of the state of the service. |
| 151 bool enabled_; | 135 bool enabled_; |
| 152 | 136 |
| 153 DISALLOW_COPY_AND_ASSIGN(DownloadProtectionService); | 137 DISALLOW_COPY_AND_ASSIGN(DownloadProtectionService); |
| 154 }; | 138 }; |
| 155 } // namespace safe_browsing | 139 } // namespace safe_browsing |
| 156 | 140 |
| 157 #endif // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ | 141 #endif // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ |
| OLD | NEW |