| 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 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/stl_util-inl.h" | 11 #include "base/stl_util-inl.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
| 14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/metrics/metrics_service.h" | 15 #include "chrome/browser/metrics/metrics_service.h" |
| 16 #include "chrome/browser/prefs/pref_service.h" | 16 #include "chrome/browser/prefs/pref_service.h" |
| 17 #include "chrome/browser/profiles/profile_manager.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/safe_browsing/malware_details.h" | 18 #include "chrome/browser/safe_browsing/malware_details.h" |
| 19 #include "chrome/browser/safe_browsing/protocol_manager.h" | 19 #include "chrome/browser/safe_browsing/protocol_manager.h" |
| 20 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h" |
| 21 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 21 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
| 22 #include "chrome/browser/tab_contents/tab_util.h" | 22 #include "chrome/browser/tab_contents/tab_util.h" |
| 23 #include "chrome/common/chrome_constants.h" | 23 #include "chrome/common/chrome_constants.h" |
| 24 #include "chrome/common/chrome_paths.h" | 24 #include "chrome/common/chrome_paths.h" |
| 25 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
| 26 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 27 #include "chrome/common/url_constants.h" | 27 #include "chrome/common/url_constants.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 47 const char* const kSbDefaultMacKeyURLPrefix = | 47 const char* const kSbDefaultMacKeyURLPrefix = |
| 48 "https://sb-ssl.google.com/safebrowsing"; | 48 "https://sb-ssl.google.com/safebrowsing"; |
| 49 | 49 |
| 50 // When download url check takes this long, client's callback will be called | 50 // When download url check takes this long, client's callback will be called |
| 51 // without waiting for the result. | 51 // without waiting for the result. |
| 52 const int64 kDownloadUrlCheckTimeoutMs = 10000; | 52 const int64 kDownloadUrlCheckTimeoutMs = 10000; |
| 53 | 53 |
| 54 // Similar to kDownloadUrlCheckTimeoutMs, but for download hash checks. | 54 // Similar to kDownloadUrlCheckTimeoutMs, but for download hash checks. |
| 55 const int64 kDownloadHashCheckTimeoutMs = 10000; | 55 const int64 kDownloadHashCheckTimeoutMs = 10000; |
| 56 | 56 |
| 57 // TODO(lzheng): Replace this with Profile* ProfileManager::GetDefaultProfile(). | |
| 58 Profile* GetDefaultProfile() { | |
| 59 FilePath user_data_dir; | |
| 60 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | |
| 61 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 62 return profile_manager->GetDefaultProfile(user_data_dir); | |
| 63 } | |
| 64 | |
| 65 // Records disposition information about the check. |hit| should be | 57 // Records disposition information about the check. |hit| should be |
| 66 // |true| if there were any prefix hits in |full_hashes|. | 58 // |true| if there were any prefix hits in |full_hashes|. |
| 67 void RecordGetHashCheckStatus( | 59 void RecordGetHashCheckStatus( |
| 68 bool hit, | 60 bool hit, |
| 69 bool is_download, | 61 bool is_download, |
| 70 const std::vector<SBFullHashResult>& full_hashes) { | 62 const std::vector<SBFullHashResult>& full_hashes) { |
| 71 SafeBrowsingProtocolManager::ResultType result; | 63 SafeBrowsingProtocolManager::ResultType result; |
| 72 if (full_hashes.empty()) { | 64 if (full_hashes.empty()) { |
| 73 result = SafeBrowsingProtocolManager::GET_HASH_FULL_HASH_EMPTY; | 65 result = SafeBrowsingProtocolManager::GET_HASH_FULL_HASH_EMPTY; |
| 74 } else if (hit) { | 66 } else if (hit) { |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 enable_download_protection_(false), | 157 enable_download_protection_(false), |
| 166 enable_csd_whitelist_(false), | 158 enable_csd_whitelist_(false), |
| 167 update_in_progress_(false), | 159 update_in_progress_(false), |
| 168 database_update_in_progress_(false), | 160 database_update_in_progress_(false), |
| 169 closing_database_(false), | 161 closing_database_(false), |
| 170 download_urlcheck_timeout_ms_(kDownloadUrlCheckTimeoutMs), | 162 download_urlcheck_timeout_ms_(kDownloadUrlCheckTimeoutMs), |
| 171 download_hashcheck_timeout_ms_(kDownloadHashCheckTimeoutMs) { | 163 download_hashcheck_timeout_ms_(kDownloadHashCheckTimeoutMs) { |
| 172 } | 164 } |
| 173 | 165 |
| 174 void SafeBrowsingService::Initialize() { | 166 void SafeBrowsingService::Initialize() { |
| 175 // Get the profile's preference for SafeBrowsing. | 167 // Always initialize the safe browsing service. Each profile will decide |
| 176 PrefService* pref_service = GetDefaultProfile()->GetPrefs(); | 168 // whether to use it based on per-user preferences. TODO(mirandac): in |
| 177 if (pref_service->GetBoolean(prefs::kSafeBrowsingEnabled)) | 169 // follow-up CL, only initialize if a profile is launched for which safe |
| 178 Start(); | 170 // browsing is enabled. see http://crbug.com/88661 |
| 171 Start(); |
| 179 } | 172 } |
| 180 | 173 |
| 181 void SafeBrowsingService::ShutDown() { | 174 void SafeBrowsingService::ShutDown() { |
| 182 BrowserThread::PostTask( | 175 BrowserThread::PostTask( |
| 183 BrowserThread::IO, FROM_HERE, | 176 BrowserThread::IO, FROM_HERE, |
| 184 NewRunnableMethod(this, &SafeBrowsingService::OnIOShutdown)); | 177 NewRunnableMethod(this, &SafeBrowsingService::OnIOShutdown)); |
| 185 } | 178 } |
| 186 | 179 |
| 187 bool SafeBrowsingService::CanCheckUrl(const GURL& url) const { | 180 bool SafeBrowsingService::CanCheckUrl(const GURL& url) const { |
| 188 return url.SchemeIs(chrome::kFtpScheme) || | 181 return url.SchemeIs(chrome::kFtpScheme) || |
| 189 url.SchemeIs(chrome::kHttpScheme) || | 182 url.SchemeIs(chrome::kHttpScheme) || |
| 190 url.SchemeIs(chrome::kHttpsScheme); | 183 url.SchemeIs(chrome::kHttpsScheme); |
| 191 } | 184 } |
| 192 | 185 |
| 193 // Only report SafeBrowsing related stats when UMA is enabled and | 186 // Only report SafeBrowsing related stats when UMA is enabled. User must also |
| 194 // safe browsing is enabled. | 187 // ensure that safe browsing is enabled from the calling profile. |
| 195 bool SafeBrowsingService::CanReportStats() const { | 188 bool SafeBrowsingService::CanReportStats() const { |
| 196 const MetricsService* metrics = g_browser_process->metrics_service(); | 189 const MetricsService* metrics = g_browser_process->metrics_service(); |
| 197 const PrefService* pref_service = GetDefaultProfile()->GetPrefs(); | 190 return metrics && metrics->reporting_active(); |
| 198 return metrics && metrics->reporting_active() && | |
| 199 pref_service && pref_service->GetBoolean(prefs::kSafeBrowsingEnabled); | |
| 200 } | 191 } |
| 201 | 192 |
| 202 // Binhash verification is only enabled for UMA users for now. | 193 // Binhash verification is only enabled for UMA users for now. |
| 203 bool SafeBrowsingService::DownloadBinHashNeeded() const { | 194 bool SafeBrowsingService::DownloadBinHashNeeded() const { |
| 204 return enable_download_protection_ && CanReportStats(); | 195 return enable_download_protection_ && CanReportStats(); |
| 205 } | 196 } |
| 206 | 197 |
| 207 bool SafeBrowsingService::CheckDownloadUrl(const std::vector<GURL>& url_chain, | 198 bool SafeBrowsingService::CheckDownloadUrl(const std::vector<GURL>& url_chain, |
| 208 Client* client) { | 199 Client* client) { |
| 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| (...skipping 644 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 PrefService* local_state = g_browser_process->local_state(); | 845 PrefService* local_state = g_browser_process->local_state(); |
| 855 DCHECK(local_state); | 846 DCHECK(local_state); |
| 856 std::string client_key, wrapped_key; | 847 std::string client_key, wrapped_key; |
| 857 if (local_state) { | 848 if (local_state) { |
| 858 client_key = | 849 client_key = |
| 859 local_state->GetString(prefs::kSafeBrowsingClientKey); | 850 local_state->GetString(prefs::kSafeBrowsingClientKey); |
| 860 wrapped_key = | 851 wrapped_key = |
| 861 local_state->GetString(prefs::kSafeBrowsingWrappedKey); | 852 local_state->GetString(prefs::kSafeBrowsingWrappedKey); |
| 862 } | 853 } |
| 863 | 854 |
| 864 // We will issue network fetches using the default profile's request context. | 855 // We will issue network fetches using the system request context. |
| 865 scoped_refptr<net::URLRequestContextGetter> request_context_getter( | 856 scoped_refptr<net::URLRequestContextGetter> request_context_getter( |
| 866 GetDefaultProfile()->GetRequestContext()); | 857 g_browser_process->system_request_context()); |
| 867 | 858 |
| 868 CommandLine* cmdline = CommandLine::ForCurrentProcess(); | 859 CommandLine* cmdline = CommandLine::ForCurrentProcess(); |
| 869 enable_download_protection_ = | 860 enable_download_protection_ = |
| 870 !cmdline->HasSwitch(switches::kSbDisableDownloadProtection); | 861 !cmdline->HasSwitch(switches::kSbDisableDownloadProtection); |
| 871 | 862 |
| 872 // We only download the csd-whitelist if client-side phishing detection is | 863 // We only download the csd-whitelist if client-side phishing detection is |
| 873 // enabled and if the user has opted in with stats collection. Note: we | 864 // enabled and if the user has opted in with stats collection. Note: we |
| 874 // cannot check whether the metrics_service() object is created because it | 865 // cannot check whether the metrics_service() object is created because it |
| 875 // may be initialized after this method is called. | 866 // may be initialized after this method is called. |
| 876 #ifdef OS_CHROMEOS | 867 #ifdef OS_CHROMEOS |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 981 if (IsWhitelisted(resource)) { | 972 if (IsWhitelisted(resource)) { |
| 982 BrowserThread::PostTask( | 973 BrowserThread::PostTask( |
| 983 BrowserThread::IO, FROM_HERE, | 974 BrowserThread::IO, FROM_HERE, |
| 984 NewRunnableMethod(this, | 975 NewRunnableMethod(this, |
| 985 &SafeBrowsingService::NotifyClientBlockingComplete, | 976 &SafeBrowsingService::NotifyClientBlockingComplete, |
| 986 resource.client, true)); | 977 resource.client, true)); |
| 987 return; | 978 return; |
| 988 } | 979 } |
| 989 | 980 |
| 990 // The tab might have been closed. | 981 // The tab might have been closed. |
| 991 TabContents* wc = | 982 TabContents* tab_contents = |
| 992 tab_util::GetTabContentsByID(resource.render_process_host_id, | 983 tab_util::GetTabContentsByID(resource.render_process_host_id, |
| 993 resource.render_view_id); | 984 resource.render_view_id); |
| 994 | 985 |
| 995 if (!wc) { | 986 if (!tab_contents) { |
| 996 // The tab is gone and we did not have a chance at showing the interstitial. | 987 // The tab is gone and we did not have a chance at showing the interstitial. |
| 997 // Just act as "Don't Proceed" was chosen. | 988 // Just act as if "Don't Proceed" were chosen. |
| 998 std::vector<UnsafeResource> resources; | 989 std::vector<UnsafeResource> resources; |
| 999 resources.push_back(resource); | 990 resources.push_back(resource); |
| 1000 BrowserThread::PostTask( | 991 BrowserThread::PostTask( |
| 1001 BrowserThread::IO, FROM_HERE, | 992 BrowserThread::IO, FROM_HERE, |
| 1002 NewRunnableMethod( | 993 NewRunnableMethod( |
| 1003 this, &SafeBrowsingService::OnBlockingPageDone, resources, false)); | 994 this, &SafeBrowsingService::OnBlockingPageDone, resources, false)); |
| 1004 return; | 995 return; |
| 1005 } | 996 } |
| 1006 | 997 |
| 1007 if (resource.threat_type != SafeBrowsingService::SAFE && CanReportStats()) { | 998 if (resource.threat_type != SafeBrowsingService::SAFE && |
| 1008 GURL page_url = wc->GetURL(); | 999 CanReportStats()) { |
| 1000 GURL page_url = tab_contents->GetURL(); |
| 1009 GURL referrer_url; | 1001 GURL referrer_url; |
| 1010 NavigationEntry* entry = wc->controller().GetActiveEntry(); | 1002 NavigationEntry* entry = tab_contents->controller().GetActiveEntry(); |
| 1011 if (entry) | 1003 if (entry) |
| 1012 referrer_url = entry->referrer(); | 1004 referrer_url = entry->referrer(); |
| 1013 | 1005 |
| 1014 // When the malicious url is on the main frame, and resource.original_url | 1006 // When the malicious url is on the main frame, and resource.original_url |
| 1015 // is not the same as the resource.url, that means we have a redirect from | 1007 // is not the same as the resource.url, that means we have a redirect from |
| 1016 // resource.original_url to resource.url. | 1008 // resource.original_url to resource.url. |
| 1017 // Also, at this point, page_url points to the _previous_ page that we | 1009 // Also, at this point, page_url points to the _previous_ page that we |
| 1018 // were on. We replace page_url with resource.original_url and referrer | 1010 // were on. We replace page_url with resource.original_url and referrer |
| 1019 // with page_url. | 1011 // with page_url. |
| 1020 if (!resource.is_subresource && | 1012 if (!resource.is_subresource && |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1234 (entry.result == CLIENT_SIDE_PHISHING_URL && | 1226 (entry.result == CLIENT_SIDE_PHISHING_URL && |
| 1235 resource.threat_type == URL_PHISHING)) && | 1227 resource.threat_type == URL_PHISHING)) && |
| 1236 entry.domain == | 1228 entry.domain == |
| 1237 net::RegistryControlledDomainService::GetDomainAndRegistry( | 1229 net::RegistryControlledDomainService::GetDomainAndRegistry( |
| 1238 resource.url)) { | 1230 resource.url)) { |
| 1239 return true; | 1231 return true; |
| 1240 } | 1232 } |
| 1241 } | 1233 } |
| 1242 return false; | 1234 return false; |
| 1243 } | 1235 } |
| OLD | NEW |