| 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 #include "chrome/browser/safe_browsing/download_protection_service.h" | 5 #include "chrome/browser/safe_browsing/download_protection_service.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 database_manager_(database_manager), | 194 database_manager_(database_manager), |
| 195 download_item_observer_(this) { | 195 download_item_observer_(this) { |
| 196 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 196 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 197 DCHECK(item_); | 197 DCHECK(item_); |
| 198 DCHECK(service_); | 198 DCHECK(service_); |
| 199 download_item_observer_.Add(item_); | 199 download_item_observer_.Add(item_); |
| 200 Profile* profile = Profile::FromBrowserContext(item_->GetBrowserContext()); | 200 Profile* profile = Profile::FromBrowserContext(item_->GetBrowserContext()); |
| 201 extended_reporting_level_ = | 201 extended_reporting_level_ = |
| 202 profile ? GetExtendedReportingLevel(*profile->GetPrefs()) | 202 profile ? GetExtendedReportingLevel(*profile->GetPrefs()) |
| 203 : SBER_LEVEL_OFF; | 203 : SBER_LEVEL_OFF; |
| 204 download_attribution_enabled_ = service_->navigation_observer_manager() && | |
| 205 base::FeatureList::IsEnabled( | |
| 206 SafeBrowsingNavigationObserverManager::kDownloadAttribution); | |
| 207 } | 204 } |
| 208 | 205 |
| 209 // Implements DownloadItem::Observer. | 206 // Implements DownloadItem::Observer. |
| 210 void OnDownloadDestroyed(content::DownloadItem* download) override { | 207 void OnDownloadDestroyed(content::DownloadItem* download) override { |
| 211 download_item_observer_.Remove(item_); | 208 download_item_observer_.Remove(item_); |
| 212 item_ = nullptr; | 209 item_ = nullptr; |
| 213 } | 210 } |
| 214 | 211 |
| 215 void StartCheck() { | 212 void StartCheck() { |
| 216 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 213 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 249 void CheckDone(SBThreatType threat_type) { | 246 void CheckDone(SBThreatType threat_type) { |
| 250 DownloadProtectionService::DownloadCheckResult result = | 247 DownloadProtectionService::DownloadCheckResult result = |
| 251 IsDangerous(threat_type) ? | 248 IsDangerous(threat_type) ? |
| 252 DownloadProtectionService::DANGEROUS : | 249 DownloadProtectionService::DANGEROUS : |
| 253 DownloadProtectionService::SAFE; | 250 DownloadProtectionService::SAFE; |
| 254 UpdateDownloadCheckStats(total_type_); | 251 UpdateDownloadCheckStats(total_type_); |
| 255 if (threat_type != SB_THREAT_TYPE_SAFE) { | 252 if (threat_type != SB_THREAT_TYPE_SAFE) { |
| 256 UpdateDownloadCheckStats(dangerous_type_); | 253 UpdateDownloadCheckStats(dangerous_type_); |
| 257 BrowserThread::PostTask( | 254 BrowserThread::PostTask( |
| 258 BrowserThread::UI, FROM_HERE, | 255 BrowserThread::UI, FROM_HERE, |
| 259 base::BindOnce(&DownloadUrlSBClient::ReportMalware, this, | 256 base::Bind(&DownloadUrlSBClient::ReportMalware, this, threat_type)); |
| 260 threat_type)); | 257 } else { |
| 261 } else if (download_attribution_enabled_) { | 258 // Identify download referrer chain, which will be used in |
| 262 // Identify download referrer chain, which will be used in | 259 // ClientDownloadRequest. |
| 263 // ClientDownloadRequest. | 260 BrowserThread::PostTask( |
| 264 BrowserThread::PostTask( | 261 BrowserThread::UI, FROM_HERE, |
| 265 BrowserThread::UI, FROM_HERE, | 262 base::Bind(&DownloadUrlSBClient::IdentifyReferrerChain, this)); |
| 266 base::BindOnce(&DownloadUrlSBClient::IdentifyReferrerChain, this)); | |
| 267 } | 263 } |
| 268 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 264 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 269 base::BindOnce(callback_, result)); | 265 base::BindOnce(callback_, result)); |
| 270 } | 266 } |
| 271 | 267 |
| 272 void ReportMalware(SBThreatType threat_type) { | 268 void ReportMalware(SBThreatType threat_type) { |
| 273 std::string post_data; | 269 std::string post_data; |
| 274 if (!sha256_hash_.empty()) { | 270 if (!sha256_hash_.empty()) { |
| 275 post_data += base::HexEncode(sha256_hash_.data(), | 271 post_data += base::HexEncode(sha256_hash_.data(), |
| 276 sha256_hash_.size()) + "\n"; | 272 sha256_hash_.size()) + "\n"; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 // The DownloadItem we are checking. Must be accessed only on UI thread. | 312 // The DownloadItem we are checking. Must be accessed only on UI thread. |
| 317 content::DownloadItem* item_; | 313 content::DownloadItem* item_; |
| 318 // Copies of data from |item_| for access on other threads. | 314 // Copies of data from |item_| for access on other threads. |
| 319 std::string sha256_hash_; | 315 std::string sha256_hash_; |
| 320 std::vector<GURL> url_chain_; | 316 std::vector<GURL> url_chain_; |
| 321 GURL referrer_url_; | 317 GURL referrer_url_; |
| 322 DownloadProtectionService* service_; | 318 DownloadProtectionService* service_; |
| 323 DownloadProtectionService::CheckDownloadCallback callback_; | 319 DownloadProtectionService::CheckDownloadCallback callback_; |
| 324 scoped_refptr<SafeBrowsingUIManager> ui_manager_; | 320 scoped_refptr<SafeBrowsingUIManager> ui_manager_; |
| 325 base::TimeTicks start_time_; | 321 base::TimeTicks start_time_; |
| 326 bool download_attribution_enabled_; | |
| 327 const SBStatsType total_type_; | 322 const SBStatsType total_type_; |
| 328 const SBStatsType dangerous_type_; | 323 const SBStatsType dangerous_type_; |
| 329 ExtendedReportingLevel extended_reporting_level_; | 324 ExtendedReportingLevel extended_reporting_level_; |
| 330 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; | 325 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; |
| 331 ScopedObserver<content::DownloadItem, | 326 ScopedObserver<content::DownloadItem, |
| 332 content::DownloadItem::Observer> download_item_observer_; | 327 content::DownloadItem::Observer> download_item_observer_; |
| 333 | 328 |
| 334 DISALLOW_COPY_AND_ASSIGN(DownloadUrlSBClient); | 329 DISALLOW_COPY_AND_ASSIGN(DownloadUrlSBClient); |
| 335 }; | 330 }; |
| 336 | 331 |
| (...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1036 .AsUTF8Unsafe()); | 1031 .AsUTF8Unsafe()); |
| 1037 } else { | 1032 } else { |
| 1038 request.set_file_basename( | 1033 request.set_file_basename( |
| 1039 item_->GetTargetFilePath().BaseName().AsUTF8Unsafe()); | 1034 item_->GetTargetFilePath().BaseName().AsUTF8Unsafe()); |
| 1040 } | 1035 } |
| 1041 request.set_download_type(type_); | 1036 request.set_download_type(type_); |
| 1042 | 1037 |
| 1043 ReferrerChainData* referrer_chain_data = | 1038 ReferrerChainData* referrer_chain_data = |
| 1044 static_cast<ReferrerChainData*>( | 1039 static_cast<ReferrerChainData*>( |
| 1045 item_->GetUserData(kDownloadReferrerChainDataKey)); | 1040 item_->GetUserData(kDownloadReferrerChainDataKey)); |
| 1046 if (referrer_chain_data) { | 1041 if (referrer_chain_data && |
| 1047 request.set_download_attribution_finch_enabled(true); | 1042 !referrer_chain_data->GetReferrerChain()->empty()) { |
| 1048 if (!referrer_chain_data->GetReferrerChain()->empty()) { | 1043 request.mutable_referrer_chain()->Swap( |
| 1049 request.mutable_referrer_chain()->Swap( | 1044 referrer_chain_data->GetReferrerChain()); |
| 1050 referrer_chain_data->GetReferrerChain()); | |
| 1051 } | |
| 1052 } | 1045 } |
| 1053 | 1046 |
| 1054 if (archive_is_valid_ != ArchiveValid::UNSET) | 1047 if (archive_is_valid_ != ArchiveValid::UNSET) |
| 1055 request.set_archive_valid(archive_is_valid_ == ArchiveValid::VALID); | 1048 request.set_archive_valid(archive_is_valid_ == ArchiveValid::VALID); |
| 1056 request.mutable_signature()->CopyFrom(signature_info_); | 1049 request.mutable_signature()->CopyFrom(signature_info_); |
| 1057 if (image_headers_) | 1050 if (image_headers_) |
| 1058 request.set_allocated_image_headers(image_headers_.release()); | 1051 request.set_allocated_image_headers(image_headers_.release()); |
| 1059 if (archived_executable_) | 1052 if (archived_executable_) |
| 1060 request.mutable_archived_binary()->Swap(&archived_binary_); | 1053 request.mutable_archived_binary()->Swap(&archived_binary_); |
| 1061 if (!request.SerializeToString(&client_download_request_data_)) { | 1054 if (!request.SerializeToString(&client_download_request_data_)) { |
| (...skipping 597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1659 | 1652 |
| 1660 bool is_extended_reporting_; | 1653 bool is_extended_reporting_; |
| 1661 | 1654 |
| 1662 base::WeakPtrFactory<PPAPIDownloadRequest> weakptr_factory_; | 1655 base::WeakPtrFactory<PPAPIDownloadRequest> weakptr_factory_; |
| 1663 | 1656 |
| 1664 DISALLOW_COPY_AND_ASSIGN(PPAPIDownloadRequest); | 1657 DISALLOW_COPY_AND_ASSIGN(PPAPIDownloadRequest); |
| 1665 }; | 1658 }; |
| 1666 | 1659 |
| 1667 DownloadProtectionService::DownloadProtectionService( | 1660 DownloadProtectionService::DownloadProtectionService( |
| 1668 SafeBrowsingService* sb_service) | 1661 SafeBrowsingService* sb_service) |
| 1669 : request_context_getter_(sb_service ? sb_service->url_request_context() | 1662 : navigation_observer_manager_(nullptr), |
| 1663 request_context_getter_(sb_service ? sb_service->url_request_context() |
| 1670 : nullptr), | 1664 : nullptr), |
| 1671 enabled_(false), | 1665 enabled_(false), |
| 1672 binary_feature_extractor_(new BinaryFeatureExtractor()), | 1666 binary_feature_extractor_(new BinaryFeatureExtractor()), |
| 1673 download_request_timeout_ms_(kDownloadRequestTimeoutMs), | 1667 download_request_timeout_ms_(kDownloadRequestTimeoutMs), |
| 1674 feedback_service_( | 1668 feedback_service_( |
| 1675 new DownloadFeedbackService(request_context_getter_.get(), | 1669 new DownloadFeedbackService(request_context_getter_.get(), |
| 1676 BrowserThread::GetBlockingPool())), | 1670 BrowserThread::GetBlockingPool())), |
| 1677 whitelist_sample_rate_(kWhitelistDownloadSampleRate) { | 1671 whitelist_sample_rate_(kWhitelistDownloadSampleRate) { |
| 1678 if (sb_service) { | 1672 if (sb_service) { |
| 1679 ui_manager_ = sb_service->ui_manager(); | 1673 ui_manager_ = sb_service->ui_manager(); |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1942 std::string api_key = google_apis::GetAPIKey(); | 1936 std::string api_key = google_apis::GetAPIKey(); |
| 1943 if (!api_key.empty()) | 1937 if (!api_key.empty()) |
| 1944 url = url.Resolve("?key=" + net::EscapeQueryParamValue(api_key, true)); | 1938 url = url.Resolve("?key=" + net::EscapeQueryParamValue(api_key, true)); |
| 1945 | 1939 |
| 1946 return url; | 1940 return url; |
| 1947 } | 1941 } |
| 1948 | 1942 |
| 1949 std::unique_ptr<ReferrerChain> DownloadProtectionService::IdentifyReferrerChain( | 1943 std::unique_ptr<ReferrerChain> DownloadProtectionService::IdentifyReferrerChain( |
| 1950 const GURL& download_url, | 1944 const GURL& download_url, |
| 1951 content::WebContents* web_contents) { | 1945 content::WebContents* web_contents) { |
| 1946 // If navigation_observer_manager_ is null, return immediately. This could |
| 1947 // happen in tests. |
| 1948 if (!navigation_observer_manager_) |
| 1949 return nullptr; |
| 1950 |
| 1952 std::unique_ptr<ReferrerChain> referrer_chain = | 1951 std::unique_ptr<ReferrerChain> referrer_chain = |
| 1953 base::MakeUnique<ReferrerChain>(); | 1952 base::MakeUnique<ReferrerChain>(); |
| 1954 int download_tab_id = SessionTabHelper::IdForTab(web_contents); | 1953 int download_tab_id = SessionTabHelper::IdForTab(web_contents); |
| 1955 UMA_HISTOGRAM_BOOLEAN( | 1954 UMA_HISTOGRAM_BOOLEAN( |
| 1956 "SafeBrowsing.ReferrerHasInvalidTabID.DownloadAttribution", | 1955 "SafeBrowsing.ReferrerHasInvalidTabID.DownloadAttribution", |
| 1957 download_tab_id == -1); | 1956 download_tab_id == -1); |
| 1958 // We look for the referrer chain that leads to the download url first. | 1957 // We look for the referrer chain that leads to the download url first. |
| 1959 SafeBrowsingNavigationObserverManager::AttributionResult result = | 1958 SafeBrowsingNavigationObserverManager::AttributionResult result = |
| 1960 navigation_observer_manager_->IdentifyReferrerChainByEventURL( | 1959 navigation_observer_manager_->IdentifyReferrerChainByEventURL( |
| 1961 download_url, download_tab_id, kDownloadAttributionUserGestureLimit, | 1960 download_url, download_tab_id, kDownloadAttributionUserGestureLimit, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1980 SafeBrowsingNavigationObserverManager::ATTRIBUTION_FAILURE_TYPE_MAX); | 1979 SafeBrowsingNavigationObserverManager::ATTRIBUTION_FAILURE_TYPE_MAX); |
| 1981 return referrer_chain; | 1980 return referrer_chain; |
| 1982 } | 1981 } |
| 1983 | 1982 |
| 1984 void DownloadProtectionService::AddReferrerChainToPPAPIClientDownloadRequest( | 1983 void DownloadProtectionService::AddReferrerChainToPPAPIClientDownloadRequest( |
| 1985 const GURL& initiating_frame_url, | 1984 const GURL& initiating_frame_url, |
| 1986 const GURL& initiating_main_frame_url, | 1985 const GURL& initiating_main_frame_url, |
| 1987 int tab_id, | 1986 int tab_id, |
| 1988 bool has_user_gesture, | 1987 bool has_user_gesture, |
| 1989 ClientDownloadRequest* out_request) { | 1988 ClientDownloadRequest* out_request) { |
| 1990 if (!base::FeatureList::IsEnabled( | 1989 if (!navigation_observer_manager_) |
| 1991 SafeBrowsingNavigationObserverManager::kDownloadAttribution) || | |
| 1992 !navigation_observer_manager_) { | |
| 1993 return; | 1990 return; |
| 1994 } | |
| 1995 | 1991 |
| 1996 UMA_HISTOGRAM_BOOLEAN( | 1992 UMA_HISTOGRAM_BOOLEAN( |
| 1997 "SafeBrowsing.ReferrerHasInvalidTabID.DownloadAttribution", | 1993 "SafeBrowsing.ReferrerHasInvalidTabID.DownloadAttribution", |
| 1998 tab_id == -1); | 1994 tab_id == -1); |
| 1999 SafeBrowsingNavigationObserverManager::AttributionResult result = | 1995 SafeBrowsingNavigationObserverManager::AttributionResult result = |
| 2000 navigation_observer_manager_->IdentifyReferrerChainByHostingPage( | 1996 navigation_observer_manager_->IdentifyReferrerChainByHostingPage( |
| 2001 initiating_frame_url, initiating_main_frame_url, tab_id, | 1997 initiating_frame_url, initiating_main_frame_url, tab_id, |
| 2002 has_user_gesture, kDownloadAttributionUserGestureLimit, | 1998 has_user_gesture, kDownloadAttributionUserGestureLimit, |
| 2003 out_request->mutable_referrer_chain()); | 1999 out_request->mutable_referrer_chain()); |
| 2004 UMA_HISTOGRAM_COUNTS_100( | 2000 UMA_HISTOGRAM_COUNTS_100( |
| 2005 "SafeBrowsing.ReferrerURLChainSize.PPAPIDownloadAttribution", | 2001 "SafeBrowsing.ReferrerURLChainSize.PPAPIDownloadAttribution", |
| 2006 out_request->referrer_chain_size()); | 2002 out_request->referrer_chain_size()); |
| 2007 UMA_HISTOGRAM_ENUMERATION( | 2003 UMA_HISTOGRAM_ENUMERATION( |
| 2008 "SafeBrowsing.ReferrerAttributionResult.PPAPIDownloadAttribution", result, | 2004 "SafeBrowsing.ReferrerAttributionResult.PPAPIDownloadAttribution", result, |
| 2009 SafeBrowsingNavigationObserverManager::ATTRIBUTION_FAILURE_TYPE_MAX); | 2005 SafeBrowsingNavigationObserverManager::ATTRIBUTION_FAILURE_TYPE_MAX); |
| 2010 out_request->set_download_attribution_finch_enabled(true); | |
| 2011 } | 2006 } |
| 2012 | 2007 |
| 2013 } // namespace safe_browsing | 2008 } // namespace safe_browsing |
| OLD | NEW |