1 // Copyright 2014 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/ui/webui/interstitials/interstitial_ui.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/string_util.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/safe_browsing/safe_browsing_blocking_page.h"
12 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
13 #include "chrome/browser/ssl/bad_clock_blocking_page.h"
14 #include "chrome/browser/ssl/ssl_blocking_page.h"
15 #include "chrome/common/url_constants.h"
16 #include "chrome/grit/browser_resources.h"
17 #include "content/public/browser/interstitial_page_delegate.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/browser/web_ui.h"
20 #include "content/public/browser/web_ui_controller.h"
21 #include "content/public/browser/web_ui_data_source.h"
22 #include "net/base/net_errors.h"
23 #include "net/base/url_util.h"
24 #include "net/cert/x509_certificate.h"
25 #include "net/ssl/ssl_info.h"
26 #include "ui/base/resource/resource_bundle.h"
28 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
29 #include "chrome/browser/ssl/captive_portal_blocking_page.h"
34 class InterstitialHTMLSource
: public content::URLDataSource
{
36 explicit InterstitialHTMLSource(content::WebContents
* web_contents
);
37 ~InterstitialHTMLSource() override
;
39 // content::URLDataSource:
40 std::string
GetMimeType(const std::string
& mime_type
) const override
;
41 std::string
GetSource() const override
;
42 bool ShouldAddContentSecurityPolicy() const override
;
43 void StartDataRequest(
44 const std::string
& path
,
45 int render_process_id
,
47 const content::URLDataSource::GotDataCallback
& callback
) override
;
50 content::WebContents
* web_contents_
;
51 DISALLOW_COPY_AND_ASSIGN(InterstitialHTMLSource
);
54 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
55 // Provides fake connection information to the captive portal blocking page so
56 // that both Wi-Fi and non Wi-Fi blocking pages can be displayed.
57 class CaptivePortalBlockingPageWithNetInfo
: public CaptivePortalBlockingPage
{
59 CaptivePortalBlockingPageWithNetInfo(
60 content::WebContents
* web_contents
,
61 const GURL
& request_url
,
62 const GURL
& login_url
,
63 const net::SSLInfo
& ssl_info
,
64 const base::Callback
<void(bool)>& callback
,
66 const std::string
& wifi_ssid
)
67 : CaptivePortalBlockingPage(web_contents
,
74 wifi_ssid_(wifi_ssid
) {}
77 // CaptivePortalBlockingPage methods:
78 bool IsWifiConnection() const override
{ return is_wifi_
; }
79 std::string
GetWiFiSSID() const override
{ return wifi_ssid_
; }
82 const std::string wifi_ssid_
;
84 DISALLOW_COPY_AND_ASSIGN(CaptivePortalBlockingPageWithNetInfo
);
88 SSLBlockingPage
* CreateSSLBlockingPage(content::WebContents
* web_contents
) {
89 // Random parameters for SSL blocking page.
90 int cert_error
= net::ERR_CERT_CONTAINS_ERRORS
;
91 GURL
request_url("https://example.com");
92 bool overridable
= false;
93 bool strict_enforcement
= false;
94 base::Time time_triggered_
= base::Time::NowFromSystemTime();
95 std::string url_param
;
96 if (net::GetValueForKeyInQuery(web_contents
->GetURL(),
99 if (GURL(url_param
).is_valid())
100 request_url
= GURL(url_param
);
102 std::string overridable_param
;
103 if (net::GetValueForKeyInQuery(web_contents
->GetURL(),
105 &overridable_param
)) {
106 overridable
= overridable_param
== "1";
108 std::string strict_enforcement_param
;
109 if (net::GetValueForKeyInQuery(web_contents
->GetURL(),
110 "strict_enforcement",
111 &strict_enforcement_param
)) {
112 strict_enforcement
= strict_enforcement_param
== "1";
114 net::SSLInfo ssl_info
;
115 ssl_info
.cert
= new net::X509Certificate(
116 request_url
.host(), "CA", base::Time::Max(), base::Time::Max());
117 // This delegate doesn't create an interstitial.
118 int options_mask
= 0;
120 options_mask
|= SSLBlockingPage::OVERRIDABLE
;
121 if (strict_enforcement
)
122 options_mask
|= SSLBlockingPage::STRICT_ENFORCEMENT
;
123 return new SSLBlockingPage(web_contents
, cert_error
, ssl_info
, request_url
,
124 options_mask
, time_triggered_
, nullptr,
125 base::Callback
<void(bool)>());
128 BadClockBlockingPage
* CreateBadClockBlockingPage(
129 content::WebContents
* web_contents
) {
130 // Set up a fake clock error.
131 int cert_error
= net::ERR_CERT_DATE_INVALID
;
132 GURL
request_url("https://example.com");
133 bool overridable
= false;
134 bool strict_enforcement
= false;
135 std::string url_param
;
136 if (net::GetValueForKeyInQuery(web_contents
->GetURL(), "url", &url_param
) &&
137 GURL(url_param
).is_valid()) {
138 request_url
= GURL(url_param
);
140 std::string overridable_param
;
141 if (net::GetValueForKeyInQuery(web_contents
->GetURL(), "overridable",
142 &overridable_param
)) {
143 overridable
= overridable_param
== "1";
145 std::string strict_enforcement_param
;
146 if (net::GetValueForKeyInQuery(web_contents
->GetURL(), "strict_enforcement",
147 &strict_enforcement_param
)) {
148 strict_enforcement
= strict_enforcement_param
== "1";
151 // Determine whether to change the clock to be ahead or behind.
152 base::Time time_triggered_
= base::Time::NowFromSystemTime();
153 std::string clock_manipulation_param
;
154 if (net::GetValueForKeyInQuery(web_contents
->GetURL(), "clock_manipulation",
155 &clock_manipulation_param
)) {
157 if (!base::StringToInt(clock_manipulation_param
, &time_offset
))
159 time_triggered_
+= base::TimeDelta::FromDays(365 * time_offset
);
162 net::SSLInfo ssl_info
;
163 ssl_info
.cert
= new net::X509Certificate(
164 request_url
.host(), "CA", base::Time::Max(), base::Time::Max());
165 // This delegate doesn't create an interstitial.
166 int options_mask
= 0;
168 options_mask
|= SSLBlockingPage::OVERRIDABLE
;
169 if (strict_enforcement
)
170 options_mask
|= SSLBlockingPage::STRICT_ENFORCEMENT
;
171 return new BadClockBlockingPage(web_contents
, cert_error
, ssl_info
,
172 request_url
, time_triggered_
,
173 base::Callback
<void(bool)>());
176 SafeBrowsingBlockingPage
* CreateSafeBrowsingBlockingPage(
177 content::WebContents
* web_contents
) {
178 SBThreatType threat_type
= SB_THREAT_TYPE_URL_MALWARE
;
179 GURL
request_url("http://example.com");
180 std::string url_param
;
181 if (net::GetValueForKeyInQuery(web_contents
->GetURL(),
184 if (GURL(url_param
).is_valid())
185 request_url
= GURL(url_param
);
187 std::string type_param
;
188 if (net::GetValueForKeyInQuery(web_contents
->GetURL(),
191 if (type_param
== "malware") {
192 threat_type
= SB_THREAT_TYPE_URL_MALWARE
;
193 } else if (type_param
== "phishing") {
194 threat_type
= SB_THREAT_TYPE_URL_PHISHING
;
195 } else if (type_param
== "clientside_malware") {
196 threat_type
= SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL
;
197 } else if (type_param
== "clientside_phishing") {
198 threat_type
= SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL
;
199 // Interstitials for client side phishing urls load after the page loads
200 // (see SafeBrowsingBlockingPage::IsMainPageLoadBlocked), so there should
201 // either be a new navigation entry, or there shouldn't be any pending
202 // entries. Clear any pending navigation entries.
203 content::NavigationController
* controller
=
204 &web_contents
->GetController();
205 controller
->DiscardNonCommittedEntries();
208 SafeBrowsingBlockingPage::UnsafeResource resource
;
209 resource
.url
= request_url
;
210 resource
.threat_type
= threat_type
;
211 // Create a blocking page without showing the interstitial.
212 return SafeBrowsingBlockingPage::CreateBlockingPage(
213 g_browser_process
->safe_browsing_service()->ui_manager().get(),
218 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
219 CaptivePortalBlockingPage
* CreateCaptivePortalBlockingPage(
220 content::WebContents
* web_contents
) {
221 bool is_wifi_connection
= false;
222 GURL
landing_url("https://captive.portal/login");
223 GURL
request_url("https://google.com");
224 // Not initialized to a default value, since non-empty wifi_ssid is
225 // considered a wifi connection, even if is_wifi_connection is false.
226 std::string wifi_ssid
;
228 std::string request_url_param
;
229 if (net::GetValueForKeyInQuery(web_contents
->GetURL(), "url",
230 &request_url_param
)) {
231 if (GURL(request_url_param
).is_valid())
232 request_url
= GURL(request_url_param
);
234 std::string landing_url_param
;
235 if (net::GetValueForKeyInQuery(web_contents
->GetURL(), "landing_page",
236 &landing_url_param
)) {
237 if (GURL(landing_url_param
).is_valid())
238 landing_url
= GURL(landing_url_param
);
240 std::string wifi_connection_param
;
241 if (net::GetValueForKeyInQuery(web_contents
->GetURL(), "is_wifi",
242 &wifi_connection_param
)) {
243 is_wifi_connection
= wifi_connection_param
== "1";
245 std::string wifi_ssid_param
;
246 if (net::GetValueForKeyInQuery(web_contents
->GetURL(), "wifi_name",
248 wifi_ssid
= wifi_ssid_param
;
250 net::SSLInfo ssl_info
;
251 ssl_info
.cert
= new net::X509Certificate(
252 request_url
.host(), "CA", base::Time::Max(), base::Time::Max());
253 CaptivePortalBlockingPage
* blocking_page
=
254 new CaptivePortalBlockingPageWithNetInfo(
255 web_contents
, request_url
, landing_url
, ssl_info
,
256 base::Callback
<void(bool)>(), is_wifi_connection
, wifi_ssid
);
257 return blocking_page
;
263 InterstitialUI::InterstitialUI(content::WebUI
* web_ui
)
264 : WebUIController(web_ui
) {
265 scoped_ptr
<InterstitialHTMLSource
> html_source(
266 new InterstitialHTMLSource(web_ui
->GetWebContents()));
267 Profile
* profile
= Profile::FromWebUI(web_ui
);
268 content::URLDataSource::Add(profile
, html_source
.release());
271 InterstitialUI::~InterstitialUI() {
274 // InterstitialHTMLSource
276 InterstitialHTMLSource::InterstitialHTMLSource(
277 content::WebContents
* web_contents
)
278 : web_contents_(web_contents
) {
281 InterstitialHTMLSource::~InterstitialHTMLSource() {
284 std::string
InterstitialHTMLSource::GetMimeType(
285 const std::string
& mime_type
) const {
289 std::string
InterstitialHTMLSource::GetSource() const {
290 return chrome::kChromeUIInterstitialHost
;
293 bool InterstitialHTMLSource::ShouldAddContentSecurityPolicy()
298 void InterstitialHTMLSource::StartDataRequest(
299 const std::string
& path
,
300 int render_process_id
,
302 const content::URLDataSource::GotDataCallback
& callback
) {
303 scoped_ptr
<content::InterstitialPageDelegate
> interstitial_delegate
;
304 if (base::StartsWith(path
, "ssl", base::CompareCase::SENSITIVE
)) {
305 interstitial_delegate
.reset(CreateSSLBlockingPage(web_contents_
));
306 } else if (base::StartsWith(path
, "safebrowsing",
307 base::CompareCase::SENSITIVE
)) {
308 interstitial_delegate
.reset(CreateSafeBrowsingBlockingPage(web_contents_
));
309 } else if (base::StartsWith(path
, "clock", base::CompareCase::SENSITIVE
)) {
310 interstitial_delegate
.reset(CreateBadClockBlockingPage(web_contents_
));
312 #if defined(ENABLE_CAPTIVE_PORTAL_DETECTION)
313 else if (base::StartsWith(path
, "captiveportal",
314 base::CompareCase::SENSITIVE
))
316 interstitial_delegate
.reset(CreateCaptivePortalBlockingPage(web_contents_
));
320 if (interstitial_delegate
.get()) {
321 html
= interstitial_delegate
.get()->GetHTMLContents();
323 html
= ResourceBundle::GetSharedInstance()
324 .GetRawDataResource(IDR_SECURITY_INTERSTITIAL_UI_HTML
)
327 scoped_refptr
<base::RefCountedString
> html_bytes
= new base::RefCountedString
;
328 html_bytes
->data().assign(html
.begin(), html
.end());
329 callback
.Run(html_bytes
.get());