1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/security_interstitials/content/ssl_error_navigation_throttle.h"
#include <memory>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "components/security_interstitials/content/security_interstitial_controller_client.h"
#include "components/security_interstitials/content/security_interstitial_page.h"
#include "components/security_interstitials/content/settings_page_helper.h"
#include "components/security_interstitials/core/metrics_helper.h"
#include "components/security_interstitials/core/ssl_error_ui.h"
#include "content/public/browser/navigation_throttle.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/mock_navigation_throttle_registry.h"
#include "content/public/test/test_renderer_host.h"
#include "net/cert/cert_status_flags.h"
#include "net/test/cert_test_util.h"
#include "net/test/test_data_directory.h"
namespace {
// Constructs a MetricsHelper instance for usage in this context.
std::unique_ptr<security_interstitials::MetricsHelper>
CreateMetricsHelperForTest(const GURL& request_url) {
security_interstitials::MetricsHelper::ReportDetails report_details;
report_details.metric_prefix = "test";
return std::make_unique<security_interstitials::MetricsHelper>(
request_url, report_details, /*history_service=*/nullptr);
}
// A SecurityInterstitialPage implementation that does the minimum necessary
// to satisfy SSLErrorNavigationThrottle's expectations of the instance passed
// to its ShowInterstitial() method, in particular populates the data
// needed to instantiate the template HTML.
class FakeSSLBlockingPage
: public security_interstitials::SecurityInterstitialPage {
public:
FakeSSLBlockingPage(content::WebContents* web_contents,
int cert_error,
const net::SSLInfo& ssl_info,
const GURL& request_url)
: security_interstitials::SecurityInterstitialPage(
web_contents,
request_url,
std::make_unique<
security_interstitials::SecurityInterstitialControllerClient>(
web_contents,
CreateMetricsHelperForTest(request_url),
/*prefs=*/nullptr,
"en_US",
GURL("about:blank"),
/* settings_page_helper */ nullptr)),
ssl_error_ui_(request_url,
cert_error,
ssl_info,
/*options_mask=*/0,
base::Time::NowFromSystemTime(),
/*support_url=*/GURL(),
controller()) {}
~FakeSSLBlockingPage() override = default;
// SecurityInterstitialPage:
void OnInterstitialClosing() override {}
void PopulateInterstitialStrings(base::Value::Dict& load_time_data) override {
ssl_error_ui_.PopulateStringsForHTML(load_time_data);
}
private:
security_interstitials::SSLErrorUI ssl_error_ui_;
};
// Replacement for SSLErrorHandler::HandleSSLError that calls
// |blocking_page_ready_callback|. |async| specifies whether this call should be
// done synchronously or using PostTask().
void MockHandleSSLError(
bool async,
content::WebContents* web_contents,
int cert_error,
const net::SSLInfo& ssl_info,
const GURL& request_url,
base::OnceCallback<
void(std::unique_ptr<security_interstitials::SecurityInterstitialPage>)>
blocking_page_ready_callback) {
auto blocking_page = std::make_unique<FakeSSLBlockingPage>(
web_contents, cert_error, ssl_info, request_url);
if (async) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(std::move(blocking_page_ready_callback),
std::move(blocking_page)));
} else {
std::move(blocking_page_ready_callback).Run(std::move(blocking_page));
}
}
bool IsInHostedApp(content::WebContents* web_contents) {
return false;
}
bool ShouldIgnoreInterstitialBecauseNavigationDefaultedToHttps(
content::NavigationHandle* handle) {
return false;
}
class TestSSLErrorNavigationThrottle : public SSLErrorNavigationThrottle {
public:
TestSSLErrorNavigationThrottle(
content::NavigationThrottleRegistry& registry,
bool async_handle_ssl_error,
base::OnceCallback<void(content::NavigationThrottle::ThrottleCheckResult)>
on_cancel_deferred_navigation)
: SSLErrorNavigationThrottle(
registry,
base::BindOnce(&MockHandleSSLError, async_handle_ssl_error),
base::BindOnce(&IsInHostedApp),
base::BindOnce(
&ShouldIgnoreInterstitialBecauseNavigationDefaultedToHttps)),
on_cancel_deferred_navigation_(
std::move(on_cancel_deferred_navigation)) {}
TestSSLErrorNavigationThrottle(const TestSSLErrorNavigationThrottle&) =
delete;
TestSSLErrorNavigationThrottle& operator=(
const TestSSLErrorNavigationThrottle&) = delete;
// NavigationThrottle:
void CancelDeferredNavigation(
content::NavigationThrottle::ThrottleCheckResult result) override {
std::move(on_cancel_deferred_navigation_).Run(result);
}
private:
base::OnceCallback<void(content::NavigationThrottle::ThrottleCheckResult)>
on_cancel_deferred_navigation_;
};
class SSLErrorNavigationThrottleTest
: public content::RenderViewHostTestHarness,
public testing::WithParamInterface<bool> {
public:
SSLErrorNavigationThrottleTest() = default;
SSLErrorNavigationThrottleTest(const SSLErrorNavigationThrottleTest&) =
delete;
SSLErrorNavigationThrottleTest& operator=(
const SSLErrorNavigationThrottleTest&) = delete;
void SetUp() override {
content::RenderViewHostTestHarness::SetUp();
handle_ = std::make_unique<content::MockNavigationHandle>(web_contents());
handle_->set_has_committed(true);
async_ = GetParam();
registry_ = std::make_unique<content::MockNavigationThrottleRegistry>(
handle_.get(),
content::MockNavigationThrottleRegistry::RegistrationMode::kHold);
throttle_ = std::make_unique<TestSSLErrorNavigationThrottle>(
*registry_.get(), async_,
base::BindOnce(&SSLErrorNavigationThrottleTest::RecordDeferredResult,
base::Unretained(this)));
}
void RecordDeferredResult(
content::NavigationThrottle::ThrottleCheckResult result) {
deferred_result_ = result;
}
protected:
bool async_ = false;
std::unique_ptr<content::MockNavigationHandle> handle_;
std::unique_ptr<content::MockNavigationThrottleRegistry> registry_;
std::unique_ptr<TestSSLErrorNavigationThrottle> throttle_;
content::NavigationThrottle::ThrottleCheckResult deferred_result_ =
content::NavigationThrottle::DEFER;
};
// Tests that the throttle ignores a request with a non SSL related network
// error code.
TEST_P(SSLErrorNavigationThrottleTest, NoSSLError) {
SCOPED_TRACE(::testing::Message()
<< "Asynchronous MockHandleSSLError: " << async_);
handle_->set_net_error_code(net::ERR_BLOCKED_BY_CLIENT);
content::NavigationThrottle::ThrottleCheckResult result =
throttle_->WillFailRequest();
EXPECT_EQ(content::NavigationThrottle::PROCEED, result);
}
// Tests that the throttle defers and cancels a request with a net error that
// is a cert error.
TEST_P(SSLErrorNavigationThrottleTest, SSLInfoWithCertError) {
SCOPED_TRACE(::testing::Message()
<< "Asynchronous MockHandleSSLError: " << async_);
net::SSLInfo ssl_info;
ssl_info.cert =
net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem");
ssl_info.cert_status = net::CERT_STATUS_COMMON_NAME_INVALID;
handle_->set_net_error_code(net::ERR_CERT_COMMON_NAME_INVALID);
handle_->set_ssl_info(ssl_info);
content::NavigationThrottle::ThrottleCheckResult synchronous_result =
throttle_->WillFailRequest();
EXPECT_EQ(content::NavigationThrottle::DEFER, synchronous_result.action());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(content::NavigationThrottle::CANCEL, deferred_result_.action());
EXPECT_EQ(net::ERR_CERT_COMMON_NAME_INVALID,
deferred_result_.net_error_code());
EXPECT_TRUE(deferred_result_.error_page_content().has_value());
}
INSTANTIATE_TEST_SUITE_P(All,
SSLErrorNavigationThrottleTest,
::testing::Values(false, true));
} // namespace
|