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