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 #ifndef CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_THROTTLE_H_ | 5 #ifndef CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_THROTTLE_H_ |
| 6 #define CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_THROTTLE_H_ | 6 #define CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_THROTTLE_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
| 14 #include "chrome/browser/safe_browsing/database_manager.h" | 14 #include "chrome/browser/safe_browsing/database_manager.h" |
| 15 #include "chrome/browser/safe_browsing/ui_manager.h" | 15 #include "chrome/browser/safe_browsing/ui_manager.h" |
| 16 #include "content/public/browser/resource_throttle.h" | 16 #include "content/public/browser/resource_throttle.h" |
| 17 #include "content/public/common/resource_type.h" | 17 #include "content/public/common/resource_type.h" |
| 18 | 18 |
| 19 class ResourceDispatcherHost; | 19 class ResourceDispatcherHost; |
| 20 | 20 |
| 21 namespace net { | 21 namespace net { |
| 22 class URLRequest; | 22 class URLRequest; |
| 23 } | 23 } |
| 24 | 24 |
| 25 // SafeBrowsingResourceThrottle checks that URLs are "safe" before navigating | 25 // SafeBrowsingResourceThrottle checks that URLs are "safe" before |
| 26 // to them. To be considered "safe", a URL must not appear in the | 26 // navigating to them. To be considered "safe", a URL must not appear in the |
| 27 // malware/phishing blacklists (see SafeBrowsingService for details). | 27 // malware/phishing blacklists (see SafeBrowsingService for details). |
| 28 // | 28 // |
| 29 // On desktop (ifdef SAFE_BROWSING_DB_LOCAL) | |
| 30 // ----------------------------------------- | |
| 29 // This check is done before requesting the original URL, and additionally | 31 // This check is done before requesting the original URL, and additionally |
| 30 // before following any subsequent redirect. | 32 // before following any subsequent redirect. In the common case the check |
| 33 // completes synchronously (no match in the bloom filter), so the request's | |
|
mattm
2015/07/24 23:24:11
since we don't actually use a bloom filter these d
Scott Hess - ex-Googler
2015/07/24 23:55:18
Or "in-memory filter", since the implementation is
Nathan Parker
2015/07/28 17:42:12
Done.
| |
| 34 // flow is un-interrupted. However if the URL fails this quick check, it | |
| 35 // has the possibility of being on the blacklist. Now the request is | |
| 36 // deferred (prevented from starting), and a more expensive safe browsing | |
| 37 // check is begun (fetches the full hashes). | |
| 31 // | 38 // |
| 32 // In the common case, the check completes synchronously (no match in the bloom | 39 // On mobile (ifdef SAFE_BROWSING_DB_REMOTE): |
| 33 // filter), so the request's flow is un-interrupted. | 40 // ----------------------------------------- |
| 41 // The check is started and runs in parallel with the resource load. If the | |
| 42 // check is not complete by the time the headers are loaded, the request is | |
| 43 // suspended until the URL is classified. We let the headers load on mobile | |
| 44 // since the RemoteSafeBrowsingDatabase checks always have some non-zero | |
| 45 // latency -- there no synchronous pass. This parallelism helps | |
| 46 // performance. Redirects are handled the same way as desktop so they | |
| 47 // always defer. | |
| 34 // | 48 // |
| 35 // However if the URL fails this quick check, it has the possibility of being | |
| 36 // on the blacklist. Now the request is suspended (prevented from starting), | |
| 37 // and a more expensive safe browsing check is begun (fetches the full hashes). | |
| 38 // | 49 // |
| 39 // Note that the safe browsing check takes at most kCheckUrlTimeoutMs | 50 // Note that the safe browsing check takes at most kCheckUrlTimeoutMs |
| 40 // milliseconds. If it takes longer than this, then the system defaults to | 51 // milliseconds. If it takes longer than this, then the system defaults to |
| 41 // treating the URL as safe. | 52 // treating the URL as safe. |
| 42 // | 53 // |
| 43 // Once the safe browsing check has completed, if the URL was decided to be | 54 // If the URL is classified as dangerous, a warning page is thrown up and |
| 44 // dangerous, a warning page is thrown up and the request remains suspended. | 55 // the request remains suspended. If the user clicks "proceed" on warning |
| 45 // If on the other hand the URL was decided to be safe, the request is | 56 // page, we resume the request. |
| 46 // resumed. | 57 // |
| 58 // Note: The ResourceThrottle interface is called in this order: | |
| 59 // WillStartRequest once, WillRedirectRequest zero or more times, and then | |
| 60 // WillProcessReponse once. | |
| 47 class SafeBrowsingResourceThrottle | 61 class SafeBrowsingResourceThrottle |
| 48 : public content::ResourceThrottle, | 62 : public content::ResourceThrottle, |
| 49 public SafeBrowsingDatabaseManager::Client, | 63 public SafeBrowsingDatabaseManager::Client, |
| 50 public base::SupportsWeakPtr<SafeBrowsingResourceThrottle> { | 64 public base::SupportsWeakPtr<SafeBrowsingResourceThrottle> { |
| 51 public: | 65 public: |
| 52 SafeBrowsingResourceThrottle(const net::URLRequest* request, | 66 SafeBrowsingResourceThrottle(const net::URLRequest* request, |
| 53 content::ResourceType resource_type, | 67 content::ResourceType resource_type, |
| 54 SafeBrowsingService* safe_browsing); | 68 SafeBrowsingService* safe_browsing, |
| 69 bool defer_at_start); | |
| 55 | 70 |
| 56 // content::ResourceThrottle implementation (called on IO thread): | 71 // content::ResourceThrottle implementation (called on IO thread): |
| 57 void WillStartRequest(bool* defer) override; | 72 void WillStartRequest(bool* defer) override; |
| 58 void WillRedirectRequest(const net::RedirectInfo& redirect_info, | 73 void WillRedirectRequest(const net::RedirectInfo& redirect_info, |
| 59 bool* defer) override; | 74 bool* defer) override; |
| 75 void WillProcessResponse(bool* defer) override; | |
| 76 | |
| 60 const char* GetNameForLogging() const override; | 77 const char* GetNameForLogging() const override; |
| 61 | 78 |
| 62 // SafeBrowsingDabaseManager::Client implementation (called on IO thread): | 79 // SafeBrowsingDabaseManager::Client implementation (called on IO thread): |
| 63 void OnCheckBrowseUrlResult(const GURL& url, | 80 void OnCheckBrowseUrlResult(const GURL& url, |
| 64 SBThreatType result, | 81 SBThreatType result, |
| 65 const std::string& metadata) override; | 82 const std::string& metadata) override; |
| 66 | 83 |
| 67 private: | 84 private: |
| 68 // Describes what phase of the check a throttle is in. | 85 // Describes what phase of the check a throttle is in. |
| 69 enum State { | 86 enum State { |
| 87 // Haven't started checking or checking is complete. Not deferred. | |
| 70 STATE_NONE, | 88 STATE_NONE, |
| 89 // We have one outstanding URL-check. Could be deferred. | |
| 71 STATE_CHECKING_URL, | 90 STATE_CHECKING_URL, |
| 91 // We're displaying a blocking page. Could be deferred. | |
| 72 STATE_DISPLAYING_BLOCKING_PAGE, | 92 STATE_DISPLAYING_BLOCKING_PAGE, |
| 73 }; | 93 }; |
| 74 | 94 |
| 75 // Describes what stage of the request got paused by the check. | 95 // Describes what stage of the request got paused by the check. |
| 76 enum DeferState { | 96 enum DeferState { |
| 77 DEFERRED_NONE, | 97 DEFERRED_NONE, |
| 78 DEFERRED_START, | 98 DEFERRED_START, |
| 79 DEFERRED_REDIRECT, | 99 DEFERRED_REDIRECT, |
| 100 DEFERRED_UNCHECKED_REDIRECT, // unchecked_redirect_url_ is populated. | |
| 101 DEFERRED_PROCESSING, | |
| 80 }; | 102 }; |
| 81 | 103 |
| 82 ~SafeBrowsingResourceThrottle() override; | 104 ~SafeBrowsingResourceThrottle() override; |
| 83 | 105 |
| 84 // SafeBrowsingService::UrlCheckCallback implementation. | 106 // SafeBrowsingService::UrlCheckCallback implementation. |
| 85 void OnBlockingPageComplete(bool proceed); | 107 void OnBlockingPageComplete(bool proceed); |
| 86 | 108 |
| 87 // Starts running |url| through the safe browsing check. Returns true if the | 109 // Starts running |url| through the safe browsing check. Returns true if the |
| 88 // URL is safe to visit. Otherwise returns false and will call | 110 // URL is safe to visit. Otherwise returns false and will call |
| 89 // OnBrowseUrlResult() when the check has completed. | 111 // OnBrowseUrlResult() when the check has completed. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 101 const SafeBrowsingUIManager::UnsafeResource& resource); | 123 const SafeBrowsingUIManager::UnsafeResource& resource); |
| 102 | 124 |
| 103 // Called on the IO thread if the request turned out to be for a prerendered | 125 // Called on the IO thread if the request turned out to be for a prerendered |
| 104 // page. | 126 // page. |
| 105 void Cancel(); | 127 void Cancel(); |
| 106 | 128 |
| 107 // Resumes the request, by continuing the deferred action (either starting the | 129 // Resumes the request, by continuing the deferred action (either starting the |
| 108 // request, or following a redirect). | 130 // request, or following a redirect). |
| 109 void ResumeRequest(); | 131 void ResumeRequest(); |
| 110 | 132 |
| 133 // True if we want to block the starting of requests until they're | |
| 134 // deemed safe. Otherwise we let the resource partially load. | |
| 135 const bool defer_at_start_; | |
| 136 | |
| 111 State state_; | 137 State state_; |
| 112 DeferState defer_state_; | 138 DeferState defer_state_; |
| 113 | 139 |
| 114 // The result of the most recent safe browsing check. Only valid to read this | 140 // The result of the most recent safe browsing check. Only valid to read this |
| 115 // when state_ != STATE_CHECKING_URL. | 141 // when state_ != STATE_CHECKING_URL. |
| 116 SBThreatType threat_type_; | 142 SBThreatType threat_type_; |
| 117 | 143 |
| 118 // The time when the outstanding safe browsing check was started. | 144 // The time when we started deferring the request. |
| 119 base::TimeTicks url_check_start_time_; | 145 base::TimeTicks defer_start_time_; |
| 120 | 146 |
| 121 // Timer to abort the safe browsing check if it takes too long. | 147 // Timer to abort the safe browsing check if it takes too long. |
| 122 base::OneShotTimer<SafeBrowsingResourceThrottle> timer_; | 148 base::OneShotTimer<SafeBrowsingResourceThrottle> timer_; |
| 123 | 149 |
| 124 // The redirect chain for this resource | 150 // The redirect chain for this resource |
| 125 std::vector<GURL> redirect_urls_; | 151 std::vector<GURL> redirect_urls_; |
| 126 | 152 |
| 153 // If in DEFERRED_UNCHECKED_REDIRECT state, this is the | |
| 154 // URL we still need to check before resuming. | |
| 155 GURL unchecked_redirect_url_; | |
| 127 GURL url_being_checked_; | 156 GURL url_being_checked_; |
| 128 | 157 |
| 129 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; | 158 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; |
| 130 scoped_refptr<SafeBrowsingUIManager> ui_manager_; | 159 scoped_refptr<SafeBrowsingUIManager> ui_manager_; |
| 131 const net::URLRequest* request_; | 160 const net::URLRequest* request_; |
| 132 const bool is_subresource_; | 161 const bool is_subresource_; |
| 133 const bool is_subframe_; | 162 const bool is_subframe_; |
| 134 | 163 |
| 135 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingResourceThrottle); | 164 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingResourceThrottle); |
| 136 }; | 165 }; |
| 137 | |
| 138 | |
| 139 #endif // CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_THROTTLE_H_ | 166 #endif // CHROME_BROWSER_RENDERER_HOST_SAFE_BROWSING_RESOURCE_THROTTLE_H_ |
| OLD | NEW |