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
3 // found in the LICENSE file.
5 // Helper class which handles communication with the SafeBrowsing servers for
6 // improved binary download protection.
8 #ifndef CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_
9 #define CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_
15 #include "base/basictypes.h"
16 #include "base/callback.h"
17 #include "base/callback_list.h"
18 #include "base/files/file_path.h"
19 #include "base/gtest_prod_util.h"
20 #include "base/memory/ref_counted.h"
21 #include "base/memory/scoped_ptr.h"
22 #include "chrome/browser/safe_browsing/database_manager.h"
23 #include "chrome/browser/safe_browsing/ui_manager.h"
33 class URLRequestContextGetter
;
34 class X509Certificate
;
37 namespace safe_browsing
{
38 class BinaryFeatureExtractor
;
39 class ClientDownloadRequest
;
40 class DownloadFeedbackService
;
42 // This class provides an asynchronous API to check whether a particular
43 // client download is malicious or not.
44 class DownloadProtectionService
{
46 enum DownloadCheckResult
{
55 // Callback type which is invoked once the download request is done.
56 typedef base::Callback
<void(DownloadCheckResult
)> CheckDownloadCallback
;
58 // A type of callback run on the main thread when a ClientDownloadRequest has
59 // been formed for a download, or when one has not been formed for a supported
61 typedef base::Callback
<void(content::DownloadItem
*,
62 const ClientDownloadRequest
*)>
63 ClientDownloadRequestCallback
;
65 // A list of ClientDownloadRequest callbacks.
66 typedef base::CallbackList
<void(content::DownloadItem
*,
67 const ClientDownloadRequest
*)>
68 ClientDownloadRequestCallbackList
;
70 // A subscription to a registered ClientDownloadRequest callback.
71 typedef scoped_ptr
<ClientDownloadRequestCallbackList::Subscription
>
72 ClientDownloadRequestSubscription
;
74 // Creates a download service. The service is initially disabled. You need
75 // to call SetEnabled() to start it. |sb_service| owns this object; we
76 // keep a reference to |request_context_getter|.
77 DownloadProtectionService(
78 SafeBrowsingService
* sb_service
,
79 net::URLRequestContextGetter
* request_context_getter
);
81 virtual ~DownloadProtectionService();
83 // Checks whether the given client download is likely to be malicious or not.
84 // The result is delivered asynchronously via the given callback. This
85 // method must be called on the UI thread, and the callback will also be
86 // invoked on the UI thread. This method must be called once the download
87 // is finished and written to disk.
88 virtual void CheckClientDownload(content::DownloadItem
* item
,
89 const CheckDownloadCallback
& callback
);
91 // Checks whether any of the URLs in the redirect chain of the
92 // download match the SafeBrowsing bad binary URL list. The result is
93 // delivered asynchronously via the given callback. This method must be
94 // called on the UI thread, and the callback will also be invoked on the UI
95 // thread. Pre-condition: !info.download_url_chain.empty().
96 virtual void CheckDownloadUrl(const content::DownloadItem
& item
,
97 const CheckDownloadCallback
& callback
);
99 // Returns true iff the download specified by |info| should be scanned by
100 // CheckClientDownload() for malicious content.
101 virtual bool IsSupportedDownload(const content::DownloadItem
& item
,
102 const base::FilePath
& target_path
) const;
104 // Display more information to the user regarding the download specified by
105 // |info|. This method is invoked when the user requests more information
106 // about a download that was marked as malicious.
107 void ShowDetailsForDownload(const content::DownloadItem
& item
,
108 content::PageNavigator
* navigator
);
110 // Enables or disables the service. This is usually called by the
111 // SafeBrowsingService, which tracks whether any profile uses these services
112 // at all. Disabling causes any pending and future requests to have their
113 // callbacks called with "UNKNOWN" results.
114 void SetEnabled(bool enabled
);
116 bool enabled() const {
120 // Returns the timeout that is used by CheckClientDownload().
121 int64
download_request_timeout_ms() const {
122 return download_request_timeout_ms_
;
125 DownloadFeedbackService
* feedback_service() {
126 return feedback_service_
.get();
129 // Registers a callback that will be run when a ClientDownloadRequest has
131 ClientDownloadRequestSubscription
RegisterClientDownloadRequestCallback(
132 const ClientDownloadRequestCallback
& callback
);
135 // Enum to keep track why a particular download verdict was chosen.
136 // This is used to keep some stats around.
137 enum DownloadCheckResultReason
{
140 REASON_WHITELISTED_URL
,
141 REASON_WHITELISTED_REFERRER
,
142 REASON_INVALID_REQUEST_PROTO
,
143 REASON_SERVER_PING_FAILED
,
144 REASON_INVALID_RESPONSE_PROTO
,
145 REASON_NOT_BINARY_FILE
,
146 REASON_REQUEST_CANCELED
,
147 REASON_DOWNLOAD_DANGEROUS
,
148 REASON_DOWNLOAD_SAFE
,
149 REASON_EMPTY_URL_CHAIN
,
150 DEPRECATED_REASON_HTTPS_URL
,
151 REASON_PING_DISABLED
,
152 REASON_TRUSTED_EXECUTABLE
,
153 REASON_OS_NOT_SUPPORTED
,
154 REASON_DOWNLOAD_UNCOMMON
,
155 REASON_DOWNLOAD_NOT_SUPPORTED
,
156 REASON_INVALID_RESPONSE_VERDICT
,
157 REASON_ARCHIVE_WITHOUT_BINARIES
,
158 REASON_DOWNLOAD_DANGEROUS_HOST
,
159 REASON_DOWNLOAD_POTENTIALLY_UNWANTED
,
160 REASON_UNSUPPORTED_URL_SCHEME
,
161 REASON_MAX
// Always add new values before this one.
165 class CheckClientDownloadRequest
; // Per-request state
166 friend class DownloadProtectionServiceTest
;
167 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest
,
168 CheckClientDownloadWhitelistedUrl
);
169 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest
,
170 CheckClientDownloadValidateRequest
);
171 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest
,
172 CheckClientDownloadSuccess
);
173 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest
,
174 CheckClientDownloadHTTPS
);
175 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest
,
176 CheckClientDownloadBlob
);
177 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest
,
178 CheckClientDownloadData
);
179 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest
,
180 CheckClientDownloadZip
);
181 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest
,
182 CheckClientDownloadFetchFailed
);
183 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest
,
184 TestDownloadRequestTimeout
);
185 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest
,
186 CheckClientCrxDownloadSuccess
);
187 static const char kDownloadRequestUrl
[];
189 // Cancels all requests in |download_requests_|, and empties it, releasing
190 // the references to the requests.
191 void CancelPendingRequests();
193 // Called by a CheckClientDownloadRequest instance when it finishes, to
194 // remove it from |download_requests_|.
195 void RequestFinished(CheckClientDownloadRequest
* request
);
197 // Given a certificate and its immediate issuer certificate, generates the
198 // list of strings that need to be checked against the download whitelist to
199 // determine whether the certificate is whitelisted.
200 static void GetCertificateWhitelistStrings(
201 const net::X509Certificate
& certificate
,
202 const net::X509Certificate
& issuer
,
203 std::vector
<std::string
>* whitelist_strings
);
205 // Returns the URL that will be used for download requests.
206 static GURL
GetDownloadRequestUrl();
208 // These pointers may be NULL if SafeBrowsing is disabled.
209 scoped_refptr
<SafeBrowsingUIManager
> ui_manager_
;
210 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager_
;
212 // The context we use to issue network requests.
213 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
215 // Map of client download request to the corresponding callback that
216 // has to be invoked when the request is done. This map contains all
217 // pending server requests.
218 std::set
<scoped_refptr
<CheckClientDownloadRequest
> > download_requests_
;
220 // Keeps track of the state of the service.
223 // BinaryFeatureExtractor object, may be overridden for testing.
224 scoped_refptr
<BinaryFeatureExtractor
> binary_feature_extractor_
;
226 int64 download_request_timeout_ms_
;
228 scoped_ptr
<DownloadFeedbackService
> feedback_service_
;
230 // A list of callbacks to be run on the main thread when a
231 // ClientDownloadRequest has been formed.
232 ClientDownloadRequestCallbackList client_download_request_callbacks_
;
234 DISALLOW_COPY_AND_ASSIGN(DownloadProtectionService
);
236 } // namespace safe_browsing
238 #endif // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_