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