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
3 // found in the LICENSE file.
5 #include "chrome/browser/safe_browsing/ui_manager.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/debug/leak_tracker.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string_util.h"
13 #include "base/threading/thread.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/safe_browsing/malware_details.h"
17 #include "chrome/browser/safe_browsing/metadata.pb.h"
18 #include "chrome/browser/safe_browsing/ping_manager.h"
19 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
20 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
21 #include "chrome/browser/tab_contents/tab_util.h"
22 #include "chrome/common/url_constants.h"
23 #include "components/metrics/metrics_service.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/navigation_entry.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/web_contents.h"
28 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
29 #include "net/url_request/url_request_context.h"
30 #include "net/url_request/url_request_context_getter.h"
32 using content::BrowserThread
;
33 using content::NavigationEntry
;
34 using content::WebContents
;
36 struct SafeBrowsingUIManager::WhiteListedEntry
{
37 int render_process_host_id
;
40 SBThreatType threat_type
;
43 SafeBrowsingUIManager::UnsafeResource::UnsafeResource()
44 : is_subresource(false),
45 threat_type(SB_THREAT_TYPE_SAFE
),
46 render_process_host_id(-1),
50 SafeBrowsingUIManager::UnsafeResource::~UnsafeResource() { }
52 SafeBrowsingUIManager::SafeBrowsingUIManager(
53 const scoped_refptr
<SafeBrowsingService
>& service
)
54 : sb_service_(service
) {
57 SafeBrowsingUIManager::~SafeBrowsingUIManager() { }
59 void SafeBrowsingUIManager::StopOnIOThread(bool shutdown
) {
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
66 void SafeBrowsingUIManager::LogPauseDelay(base::TimeDelta time
) {
67 UMA_HISTOGRAM_LONG_TIMES("SB2.Delay", time
);
70 // Only report SafeBrowsing related stats when UMA is enabled. User must also
71 // ensure that safe browsing is enabled from the calling profile.
72 bool SafeBrowsingUIManager::CanReportStats() const {
73 const metrics::MetricsService
* metrics
= g_browser_process
->metrics_service();
74 return metrics
&& metrics
->reporting_active();
77 void SafeBrowsingUIManager::OnBlockingPageDone(
78 const std::vector
<UnsafeResource
>& resources
,
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
81 for (std::vector
<UnsafeResource
>::const_iterator iter
= resources
.begin();
82 iter
!= resources
.end(); ++iter
) {
83 const UnsafeResource
& resource
= *iter
;
84 if (!resource
.callback
.is_null())
85 resource
.callback
.Run(proceed
);
88 BrowserThread::PostTask(
91 base::Bind(&SafeBrowsingUIManager::UpdateWhitelist
, this, resource
));
96 void SafeBrowsingUIManager::DisplayBlockingPage(
97 const UnsafeResource
& resource
) {
98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
99 if (resource
.is_subresource
&& !resource
.is_subframe
) {
100 // Sites tagged as serving Unwanted Software should only show a warning for
101 // main-frame or sub-frame resource. Similar warning restrictions should be
102 // applied to malware sites tagged as "landing sites" (see "Types of
103 // Malware sites" under
104 // https://developers.google.com/safe-browsing/developers_guide_v3#UserWarnings).
105 safe_browsing::MalwarePatternType proto
;
106 if (resource
.threat_type
== SB_THREAT_TYPE_URL_UNWANTED
||
107 (resource
.threat_type
== SB_THREAT_TYPE_URL_MALWARE
&&
108 !resource
.threat_metadata
.empty() &&
109 proto
.ParseFromString(resource
.threat_metadata
) &&
110 proto
.pattern_type() == safe_browsing::MalwarePatternType::LANDING
)) {
111 if (!resource
.callback
.is_null()) {
112 BrowserThread::PostTask(
113 BrowserThread::IO
, FROM_HERE
, base::Bind(resource
.callback
, true));
119 // For M40, the UwS warning may be gated to not show any UI.
120 const bool ping_only
= resource
.threat_type
== SB_THREAT_TYPE_URL_UNWANTED
121 && safe_browsing_util::GetUnwantedTrialGroup() < safe_browsing_util::UWS_ON
;
123 // Indicate to interested observers that the resource in question matched the
124 // SB filters, unless the UwS interstitial is in ping-only mode.
125 if (resource
.threat_type
!= SB_THREAT_TYPE_SAFE
&& !ping_only
) {
126 FOR_EACH_OBSERVER(Observer
, observer_list_
, OnSafeBrowsingMatch(resource
));
129 // Check if the user has already ignored our warning for this render_view
131 if (IsWhitelisted(resource
)) {
132 if (!resource
.callback
.is_null()) {
133 BrowserThread::PostTask(
134 BrowserThread::IO
, FROM_HERE
, base::Bind(resource
.callback
, true));
139 // The tab might have been closed.
140 WebContents
* web_contents
=
141 tab_util::GetWebContentsByID(resource
.render_process_host_id
,
142 resource
.render_view_id
);
145 // The tab is gone and we did not have a chance at showing the interstitial.
146 // Just act as if "Don't Proceed" were chosen.
147 std::vector
<UnsafeResource
> resources
;
148 resources
.push_back(resource
);
149 BrowserThread::PostTask(
150 BrowserThread::IO
, FROM_HERE
,
151 base::Bind(&SafeBrowsingUIManager::OnBlockingPageDone
,
152 this, resources
, false));
156 if (resource
.threat_type
!= SB_THREAT_TYPE_SAFE
&&
158 GURL page_url
= web_contents
->GetURL();
160 NavigationEntry
* entry
= web_contents
->GetController().GetActiveEntry();
162 referrer_url
= entry
->GetReferrer().url
;
164 // When the malicious url is on the main frame, and resource.original_url
165 // is not the same as the resource.url, that means we have a redirect from
166 // resource.original_url to resource.url.
167 // Also, at this point, page_url points to the _previous_ page that we
168 // were on. We replace page_url with resource.original_url and referrer
170 if (!resource
.is_subresource
&&
171 !resource
.original_url
.is_empty() &&
172 resource
.original_url
!= resource
.url
) {
173 referrer_url
= page_url
;
174 page_url
= resource
.original_url
;
176 ReportSafeBrowsingHit(resource
.url
, page_url
, referrer_url
,
177 resource
.is_subresource
, resource
.threat_type
,
178 std::string() /* post_data */);
181 // If UwS interstitials are turned off, return here before showing UI.
183 if (!resource
.callback
.is_null()) {
184 BrowserThread::PostTask(
185 BrowserThread::IO
, FROM_HERE
, base::Bind(resource
.callback
, true));
190 if (resource
.threat_type
!= SB_THREAT_TYPE_SAFE
) {
191 FOR_EACH_OBSERVER(Observer
, observer_list_
, OnSafeBrowsingHit(resource
));
193 SafeBrowsingBlockingPage::ShowBlockingPage(this, resource
);
196 // A safebrowsing hit is sent after a blocking page for malware/phishing
197 // or after the warning dialog for download urls, only for UMA users.
198 void SafeBrowsingUIManager::ReportSafeBrowsingHit(
199 const GURL
& malicious_url
,
200 const GURL
& page_url
,
201 const GURL
& referrer_url
,
203 SBThreatType threat_type
,
204 const std::string
& post_data
) {
205 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
206 if (!CanReportStats())
209 BrowserThread::PostTask(
210 BrowserThread::IO
, FROM_HERE
,
211 base::Bind(&SafeBrowsingUIManager::ReportSafeBrowsingHitOnIOThread
, this,
212 malicious_url
, page_url
, referrer_url
, is_subresource
,
213 threat_type
, post_data
));
216 void SafeBrowsingUIManager::AddObserver(Observer
* observer
) {
217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
218 observer_list_
.AddObserver(observer
);
221 void SafeBrowsingUIManager::RemoveObserver(Observer
* observer
) {
222 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
223 observer_list_
.RemoveObserver(observer
);
226 void SafeBrowsingUIManager::ReportSafeBrowsingHitOnIOThread(
227 const GURL
& malicious_url
,
228 const GURL
& page_url
,
229 const GURL
& referrer_url
,
231 SBThreatType threat_type
,
232 const std::string
& post_data
) {
233 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
235 // The service may delete the ping manager (i.e. when user disabling service,
236 // etc). This happens on the IO thread.
237 if (sb_service_
.get() == NULL
|| sb_service_
->ping_manager() == NULL
)
240 DVLOG(1) << "ReportSafeBrowsingHit: " << malicious_url
<< " " << page_url
241 << " " << referrer_url
<< " " << is_subresource
<< " "
243 sb_service_
->ping_manager()->ReportSafeBrowsingHit(
244 malicious_url
, page_url
,
245 referrer_url
, is_subresource
,
246 threat_type
, post_data
);
249 // If the user had opted-in to send MalwareDetails, this gets called
250 // when the report is ready.
251 void SafeBrowsingUIManager::SendSerializedMalwareDetails(
252 const std::string
& serialized
) {
253 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
255 // The service may delete the ping manager (i.e. when user disabling service,
256 // etc). This happens on the IO thread.
257 if (sb_service_
.get() == NULL
|| sb_service_
->ping_manager() == NULL
)
260 if (!serialized
.empty()) {
261 DVLOG(1) << "Sending serialized malware details.";
262 sb_service_
->ping_manager()->ReportMalwareDetails(serialized
);
266 void SafeBrowsingUIManager::UpdateWhitelist(const UnsafeResource
& resource
) {
267 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
268 // Whitelist this domain and warning type for the given tab.
269 WhiteListedEntry entry
;
270 entry
.render_process_host_id
= resource
.render_process_host_id
;
271 entry
.render_view_id
= resource
.render_view_id
;
272 entry
.domain
= net::registry_controlled_domains::GetDomainAndRegistry(
274 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
);
275 entry
.threat_type
= resource
.threat_type
;
276 white_listed_entries_
.push_back(entry
);
279 bool SafeBrowsingUIManager::IsWhitelisted(const UnsafeResource
& resource
) {
280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
281 // Check if the user has already ignored our warning for this render_view
283 for (size_t i
= 0; i
< white_listed_entries_
.size(); ++i
) {
284 const WhiteListedEntry
& entry
= white_listed_entries_
[i
];
285 if (entry
.render_process_host_id
== resource
.render_process_host_id
&&
286 entry
.render_view_id
== resource
.render_view_id
&&
287 // Threat type must be the same or they can either be client-side
288 // phishing/malware URL or a SafeBrowsing phishing/malware URL.
289 // If we show one type of phishing/malware warning we don't want to show
290 // a second phishing/malware warning.
291 (entry
.threat_type
== resource
.threat_type
||
292 (entry
.threat_type
== SB_THREAT_TYPE_URL_PHISHING
&&
293 resource
.threat_type
== SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL
) ||
294 (entry
.threat_type
== SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL
&&
295 resource
.threat_type
== SB_THREAT_TYPE_URL_PHISHING
) ||
296 (entry
.threat_type
== SB_THREAT_TYPE_URL_MALWARE
&&
297 resource
.threat_type
== SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL
) ||
298 (entry
.threat_type
== SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL
&&
299 resource
.threat_type
== SB_THREAT_TYPE_URL_MALWARE
))) {
300 return entry
.domain
==
301 net::registry_controlled_domains::GetDomainAndRegistry(
303 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
);