| 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" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after 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(). | 56 // TODO(mirandac): remove GetDefaultProfile altogether. |
| 57 Profile* GetDefaultProfile() { | 57 Profile* GetDefaultProfile() { |
| 58 FilePath user_data_dir; | 58 FilePath user_data_dir; |
| 59 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); | 59 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 60 ProfileManager* profile_manager = g_browser_process->profile_manager(); | 60 ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| 61 return profile_manager->GetDefaultProfile(user_data_dir); | 61 return profile_manager->GetDefaultProfile(user_data_dir); |
| 62 } | 62 } |
| 63 | 63 |
| 64 // Records disposition information about the check. |hit| should be | 64 // Records disposition information about the check. |hit| should be |
| 65 // |true| if there were any prefix hits in |full_hashes|. | 65 // |true| if there were any prefix hits in |full_hashes|. |
| 66 void RecordGetHashCheckStatus( | 66 void RecordGetHashCheckStatus( |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 183 NewRunnableMethod(this, &SafeBrowsingService::OnIOShutdown)); | 183 NewRunnableMethod(this, &SafeBrowsingService::OnIOShutdown)); |
| 184 } | 184 } |
| 185 | 185 |
| 186 bool SafeBrowsingService::CanCheckUrl(const GURL& url) const { | 186 bool SafeBrowsingService::CanCheckUrl(const GURL& url) const { |
| 187 return url.SchemeIs(chrome::kFtpScheme) || | 187 return url.SchemeIs(chrome::kFtpScheme) || |
| 188 url.SchemeIs(chrome::kHttpScheme) || | 188 url.SchemeIs(chrome::kHttpScheme) || |
| 189 url.SchemeIs(chrome::kHttpsScheme); | 189 url.SchemeIs(chrome::kHttpsScheme); |
| 190 } | 190 } |
| 191 | 191 |
| 192 // Only report SafeBrowsing related stats when UMA is enabled and | 192 // Only report SafeBrowsing related stats when UMA is enabled and |
| 193 // safe browsing is enabled. | 193 // safe browsing is enabled. Do we really ever get here without |
| 194 // profile's kSafeBrowsingEnabled being true?? |
| 194 bool SafeBrowsingService::CanReportStats() const { | 195 bool SafeBrowsingService::CanReportStats() const { |
| 195 const MetricsService* metrics = g_browser_process->metrics_service(); | 196 const MetricsService* metrics = g_browser_process->metrics_service(); |
| 196 const PrefService* pref_service = GetDefaultProfile()->GetPrefs(); | 197 return metrics && metrics->reporting_active(); // && |
| 197 return metrics && metrics->reporting_active() && | |
| 198 pref_service && pref_service->GetBoolean(prefs::kSafeBrowsingEnabled); | |
| 199 } | 198 } |
| 200 | 199 |
| 201 // Binhash verification is only enabled for UMA users for now. | 200 // Binhash verification is only enabled for UMA users for now. |
| 202 bool SafeBrowsingService::DownloadBinHashNeeded() const { | 201 bool SafeBrowsingService::DownloadBinHashNeeded( |
| 203 return enable_download_protection_ && CanReportStats(); | 202 bool safe_browsing_enabled) const { |
| 203 return enable_download_protection_ && safe_browsing_enabled && |
| 204 CanReportStats(); |
| 204 } | 205 } |
| 205 | 206 |
| 206 bool SafeBrowsingService::CheckDownloadUrl(const std::vector<GURL>& url_chain, | 207 bool SafeBrowsingService::CheckDownloadUrl(const std::vector<GURL>& url_chain, |
| 207 Client* client) { | 208 Client* client) { |
| 208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 209 if (!enabled_ || !enable_download_protection_) | 210 if (!enabled_ || !enable_download_protection_) |
| 210 return true; | 211 return true; |
| 211 | 212 |
| 212 // We need to check the database for url prefix, and later may fetch the url | 213 // We need to check the database for url prefix, and later may fetch the url |
| 213 // from the safebrowsing backends. These need to be asynchronous. | 214 // from the safebrowsing backends. These need to be asynchronous. |
| (...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 848 PrefService* local_state = g_browser_process->local_state(); | 849 PrefService* local_state = g_browser_process->local_state(); |
| 849 DCHECK(local_state); | 850 DCHECK(local_state); |
| 850 std::string client_key, wrapped_key; | 851 std::string client_key, wrapped_key; |
| 851 if (local_state) { | 852 if (local_state) { |
| 852 client_key = | 853 client_key = |
| 853 local_state->GetString(prefs::kSafeBrowsingClientKey); | 854 local_state->GetString(prefs::kSafeBrowsingClientKey); |
| 854 wrapped_key = | 855 wrapped_key = |
| 855 local_state->GetString(prefs::kSafeBrowsingWrappedKey); | 856 local_state->GetString(prefs::kSafeBrowsingWrappedKey); |
| 856 } | 857 } |
| 857 | 858 |
| 858 // We will issue network fetches using the default profile's request context. | 859 // We will issue network fetches using the system request context. |
| 859 scoped_refptr<net::URLRequestContextGetter> request_context_getter( | 860 scoped_refptr<net::URLRequestContextGetter> request_context_getter( |
| 860 GetDefaultProfile()->GetRequestContext()); | 861 g_browser_process->system_request_context()); |
| 861 | 862 |
| 862 CommandLine* cmdline = CommandLine::ForCurrentProcess(); | 863 CommandLine* cmdline = CommandLine::ForCurrentProcess(); |
| 863 enable_download_protection_ = | 864 enable_download_protection_ = |
| 864 !cmdline->HasSwitch(switches::kSbDisableDownloadProtection); | 865 !cmdline->HasSwitch(switches::kSbDisableDownloadProtection); |
| 865 | 866 |
| 866 // We only download the csd-whitelist if client-side phishing detection is | 867 // 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 | 868 // 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 | 869 // cannot check whether the metrics_service() object is created because it |
| 869 // may be initialized after this method is called. | 870 // may be initialized after this method is called. |
| 870 #ifdef OS_CHROMEOS | 871 #ifdef OS_CHROMEOS |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 975 if (IsWhitelisted(resource)) { | 976 if (IsWhitelisted(resource)) { |
| 976 BrowserThread::PostTask( | 977 BrowserThread::PostTask( |
| 977 BrowserThread::IO, FROM_HERE, | 978 BrowserThread::IO, FROM_HERE, |
| 978 NewRunnableMethod(this, | 979 NewRunnableMethod(this, |
| 979 &SafeBrowsingService::NotifyClientBlockingComplete, | 980 &SafeBrowsingService::NotifyClientBlockingComplete, |
| 980 resource.client, true)); | 981 resource.client, true)); |
| 981 return; | 982 return; |
| 982 } | 983 } |
| 983 | 984 |
| 984 // The tab might have been closed. | 985 // The tab might have been closed. |
| 985 TabContents* wc = | 986 TabContents* tab_contents = |
| 986 tab_util::GetTabContentsByID(resource.render_process_host_id, | 987 tab_util::GetTabContentsByID(resource.render_process_host_id, |
| 987 resource.render_view_id); | 988 resource.render_view_id); |
| 988 | 989 |
| 989 if (!wc) { | 990 if (!tab_contents) { |
| 990 // The tab is gone and we did not have a chance at showing the interstitial. | 991 // The tab is gone and we did not have a chance at showing the interstitial. |
| 991 // Just act as "Don't Proceed" was chosen. | 992 // Just act as if "Don't Proceed" were chosen. |
| 992 std::vector<UnsafeResource> resources; | 993 std::vector<UnsafeResource> resources; |
| 993 resources.push_back(resource); | 994 resources.push_back(resource); |
| 994 BrowserThread::PostTask( | 995 BrowserThread::PostTask( |
| 995 BrowserThread::IO, FROM_HERE, | 996 BrowserThread::IO, FROM_HERE, |
| 996 NewRunnableMethod( | 997 NewRunnableMethod( |
| 997 this, &SafeBrowsingService::OnBlockingPageDone, resources, false)); | 998 this, &SafeBrowsingService::OnBlockingPageDone, resources, false)); |
| 998 return; | 999 return; |
| 999 } | 1000 } |
| 1000 | 1001 |
| 1001 if (resource.threat_type != SafeBrowsingService::SAFE && CanReportStats()) { | 1002 const PrefService* pref_service = tab_contents->profile()->GetPrefs(); |
| 1002 GURL page_url = wc->GetURL(); | 1003 if (resource.threat_type != SafeBrowsingService::SAFE && |
| 1004 CanReportStats() && |
| 1005 pref_service && |
| 1006 pref_service->GetBoolean(prefs::kSafeBrowsingEnabled)) { |
| 1007 GURL page_url = tab_contents->GetURL(); |
| 1003 GURL referrer_url; | 1008 GURL referrer_url; |
| 1004 NavigationEntry* entry = wc->controller().GetActiveEntry(); | 1009 NavigationEntry* entry = tab_contents->controller().GetActiveEntry(); |
| 1005 if (entry) | 1010 if (entry) |
| 1006 referrer_url = entry->referrer(); | 1011 referrer_url = entry->referrer(); |
| 1007 | 1012 |
| 1008 // When the malicious url is on the main frame, and resource.original_url | 1013 // 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 | 1014 // is not the same as the resource.url, that means we have a redirect from |
| 1010 // resource.original_url to resource.url. | 1015 // resource.original_url to resource.url. |
| 1011 // Also, at this point, page_url points to the _previous_ page that we | 1016 // 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 | 1017 // were on. We replace page_url with resource.original_url and referrer |
| 1013 // with page_url. | 1018 // with page_url. |
| 1014 if (!resource.is_subresource && | 1019 if (!resource.is_subresource && |
| 1015 !resource.original_url.is_empty() && | 1020 !resource.original_url.is_empty() && |
| 1016 resource.original_url != resource.url) { | 1021 resource.original_url != resource.url) { |
| 1017 referrer_url = page_url; | 1022 referrer_url = page_url; |
| 1018 page_url = resource.original_url; | 1023 page_url = resource.original_url; |
| 1019 } | 1024 } |
| 1020 ReportSafeBrowsingHit(resource.url, page_url, referrer_url, | 1025 ReportSafeBrowsingHit(resource.url, page_url, referrer_url, |
| 1021 resource.is_subresource, resource.threat_type, | 1026 resource.is_subresource, resource.threat_type, |
| 1022 std::string() /* post_data */); | 1027 std::string() /* post_data */, |
| 1028 true /* safe browsing enabled */ ); |
| 1023 } | 1029 } |
| 1024 | 1030 |
| 1025 SafeBrowsingBlockingPage::ShowBlockingPage(this, resource); | 1031 SafeBrowsingBlockingPage::ShowBlockingPage(this, resource); |
| 1026 } | 1032 } |
| 1027 | 1033 |
| 1028 // A safebrowsing hit is sent after a blocking page for malware/phishing | 1034 // A safebrowsing hit is sent after a blocking page for malware/phishing |
| 1029 // or after the warning dialog for download urls, only for UMA users. | 1035 // or after the warning dialog for download urls, only for UMA users. |
| 1036 // pass in knowledge of can we report stats... |
| 1030 void SafeBrowsingService::ReportSafeBrowsingHit( | 1037 void SafeBrowsingService::ReportSafeBrowsingHit( |
| 1031 const GURL& malicious_url, | 1038 const GURL& malicious_url, |
| 1032 const GURL& page_url, | 1039 const GURL& page_url, |
| 1033 const GURL& referrer_url, | 1040 const GURL& referrer_url, |
| 1034 bool is_subresource, | 1041 bool is_subresource, |
| 1035 SafeBrowsingService::UrlCheckResult threat_type, | 1042 SafeBrowsingService::UrlCheckResult threat_type, |
| 1036 const std::string& post_data) { | 1043 const std::string& post_data, |
| 1044 bool safe_browsing_enabled) { |
| 1037 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 1045 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1038 if (!CanReportStats()) | 1046 if (!(CanReportStats() && safe_browsing_enabled)) |
| 1039 return; | 1047 return; |
| 1040 | 1048 |
| 1041 BrowserThread::PostTask( | 1049 BrowserThread::PostTask( |
| 1042 BrowserThread::IO, FROM_HERE, | 1050 BrowserThread::IO, FROM_HERE, |
| 1043 NewRunnableMethod( | 1051 NewRunnableMethod( |
| 1044 this, | 1052 this, |
| 1045 &SafeBrowsingService::ReportSafeBrowsingHitOnIOThread, | 1053 &SafeBrowsingService::ReportSafeBrowsingHitOnIOThread, |
| 1046 malicious_url, | 1054 malicious_url, |
| 1047 page_url, | 1055 page_url, |
| 1048 referrer_url, | 1056 referrer_url, |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1220 (entry.result == CLIENT_SIDE_PHISHING_URL && | 1228 (entry.result == CLIENT_SIDE_PHISHING_URL && |
| 1221 resource.threat_type == URL_PHISHING)) && | 1229 resource.threat_type == URL_PHISHING)) && |
| 1222 entry.domain == | 1230 entry.domain == |
| 1223 net::RegistryControlledDomainService::GetDomainAndRegistry( | 1231 net::RegistryControlledDomainService::GetDomainAndRegistry( |
| 1224 resource.url)) { | 1232 resource.url)) { |
| 1225 return true; | 1233 return true; |
| 1226 } | 1234 } |
| 1227 } | 1235 } |
| 1228 return false; | 1236 return false; |
| 1229 } | 1237 } |
| OLD | NEW |