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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/mixed_content_checker.h"
#include <memory>
#include <ostream>
#include <tuple>
#include <vector>
#include "content/public/browser/web_contents.h"
#include "content/public/test/fake_local_frame.h"
#include "content/test/navigation_simulator_impl.h"
#include "content/test/test_render_frame_host.h"
#include "content/test/test_render_view_host.h"
#include "services/network/public/mojom/source_location.mojom-forward.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom.h"
#include "third_party/blink/public/mojom/loader/mixed_content.mojom.h"
#include "third_party/blink/public/mojom/security_context/insecure_request_policy.mojom.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom-shared.h"
#include "url/gurl.h"
namespace content {
namespace {
using ::testing::Eq;
using ::testing::FieldsAre;
using ::testing::IsEmpty;
using ::testing::Optional;
using ::testing::UnorderedElementsAre;
// Intercepts the mojo calls of `MixedContentFound()` and
// `ReportBlinkFeatureUsage()` from `rfh` to a LocalFrame in renderer.
class LocalFrameInterceptor : public FakeLocalFrame {
public:
explicit LocalFrameInterceptor(RenderFrameHost* rfh)
: rfh_(static_cast<TestRenderFrameHost*>(rfh)) {
Init(rfh_->GetRemoteAssociatedInterfaces());
}
// FakeLocalFrame overrides:
struct MixedContentResult {
GURL main_resource_url;
GURL mixed_content_url;
bool was_allowed;
bool had_redirect;
};
void MixedContentFound(
const GURL& main_resource_url,
const GURL& mixed_content_url,
blink::mojom::RequestContextType request_context,
bool was_allowed,
const GURL& url_before_redirects,
bool had_redirect,
network::mojom::SourceLocationPtr source_location) final {
mixed_content_result_ = MixedContentResult{
main_resource_url, mixed_content_url, was_allowed, had_redirect};
}
void ReportBlinkFeatureUsage(
const std::vector<blink::mojom::WebFeature>& web_features) final {
reported_web_features_ = web_features;
}
const absl::optional<MixedContentResult>& mixed_content_result() const {
return mixed_content_result_;
}
const std::vector<blink::mojom::WebFeature>& reported_web_features() const {
return reported_web_features_;
}
void FlushLocalFrameMessages() { rfh_->FlushLocalFrameMessages(); }
private:
raw_ptr<TestRenderFrameHost> rfh_;
std::vector<blink::mojom::WebFeature> reported_web_features_;
absl::optional<MixedContentResult> mixed_content_result_;
};
// Needed by GTest to display errors.
std::ostream& operator<<(std::ostream& out,
const LocalFrameInterceptor::MixedContentResult& m) {
return out << "\nMixedContentResult {\n"
<< " main_resource_url: " << m.main_resource_url << "\n"
<< " mixed_content_url: " << m.mixed_content_url << "\n"
<< " was_allowed: " << m.was_allowed << "\n"
<< " had_redirect: " << m.had_redirect << "\n"
<< "}";
}
} // namespace
// Tests that `content::MixedContentChecker` correctly detects or ignores many
// cases where there is or there is not mixed content, respectively.
// Note: Browser side version of
// `blink::MixedContentCheckerTest::IsMixedContent`.
// Must be kept in sync manually!
// LINT.IfChange
TEST(MixedContentCheckerTest, IsMixedContent) {
struct TestCase {
const char* origin;
const char* target;
const bool expected_mixed_content;
} cases[] = {
{"http://example.com/foo", "http://example.com/foo", false},
{"http://example.com/foo", "https://example.com/foo", false},
{"http://example.com/foo", "data:text/html,<p>Hi!</p>", false},
{"http://example.com/foo", "about:blank", false},
{"https://example.com/foo", "https://example.com/foo", false},
{"https://example.com/foo", "wss://example.com/foo", false},
{"https://example.com/foo", "data:text/html,<p>Hi!</p>", false},
{"https://example.com/foo", "blob:https://example.com/foo", false},
{"https://example.com/foo", "filesystem:https://example.com/foo", false},
{"https://example.com/foo", "http://127.0.0.1/", false},
{"https://example.com/foo", "http://[::1]/", false},
{"https://example.com/foo", "http://a.localhost/", false},
{"https://example.com/foo", "http://localhost/", false},
{"https://example.com/foo", "http://example.com/foo", true},
{"https://example.com/foo", "http://google.com/foo", true},
{"https://example.com/foo", "ws://example.com/foo", true},
{"https://example.com/foo", "ws://google.com/foo", true},
{"https://example.com/foo", "http://192.168.1.1/", true},
{"https://example.com/foo", "blob:http://example.com/foo", true},
{"https://example.com/foo", "blob:null/foo", true},
{"https://example.com/foo", "filesystem:http://example.com/foo", true},
{"https://example.com/foo", "filesystem:null/foo", true},
};
for (const auto& test : cases) {
SCOPED_TRACE(::testing::Message()
<< "Origin: " << test.origin << ", Target: " << test.target
<< ", Expectation: " << test.expected_mixed_content);
GURL origin_url(test.origin);
GURL target_url(test.target);
EXPECT_EQ(
test.expected_mixed_content,
MixedContentChecker::IsMixedContentForTesting(origin_url, target_url));
}
}
// LINT.ThenChange(third_party/blink/renderer/core/loader/mixed_content_checker_test.cc)
class MixedContentCheckerShouldBlockNavigationTestBase
: public RenderViewHostImplTestHarness,
public testing::WithParamInterface<bool> {
protected:
void SetUp() override {
RenderViewHostImplTestHarness::SetUp();
NavigateAndCommit(GURL("about:blank"));
}
bool for_redirect() const { return GetParam(); }
// Starts a navigation from `source_url` to `target_url`. `from_subframe`
// tells if the navigation is initiated from the main frame or sub frame of
// the page of `source_url`.
// Returns a tuple of:
// - a navigation simulator indicating a started navigation request.
// - a LocalFrame inspector that collects messages from the navigation
// initiator frame.
std::tuple<std::unique_ptr<NavigationSimulatorImpl>,
std::unique_ptr<LocalFrameInterceptor>>
StartNavigation(
const std::string& source_url,
const std::string& target_url,
bool from_subframe = false,
blink::mojom::InsecureRequestPolicy main_frame_insecure_request_policy =
blink::mojom::InsecureRequestPolicy::kLeaveInsecureRequestsAlone,
blink::mojom::MixedContentContextType mixed_content_context_type =
blink::mojom::MixedContentContextType::kBlockable) {
// Loads the page of `source_url` first.
NavigateAndCommit(GURL(source_url));
TestRenderFrameHost* rfh = main_test_rfh();
rfh->DidEnforceInsecureRequestPolicy(main_frame_insecure_request_policy);
if (from_subframe) {
// Navigation request is from a subframe of the page of `source_url`.
TestRenderFrameHost* subframe = rfh->AppendChild("subframe");
NavigationSimulator::NavigateAndCommitFromDocument(
GURL(source_url + "/subframe"), subframe);
rfh = subframe;
}
// `interceptor` must be constructed before initiating the navigation, such
// that the remote of `rfh` pointing to a LocalFrame can be replaced.
auto interceptor = std::make_unique<LocalFrameInterceptor>(rfh);
std::unique_ptr<NavigationSimulatorImpl> navigation =
NavigationSimulatorImpl::CreateRendererInitiated(GURL(target_url), rfh);
navigation->SetReferrer(blink::mojom::Referrer::New(
rfh->GetLastCommittedURL(),
network::mojom::ReferrerPolicy::kStrictOriginWhenCrossOrigin));
navigation->set_request_context_type(
blink::mojom::RequestContextType::INTERNAL);
navigation->set_mixed_content_context_type(mixed_content_context_type);
// This line will trigger `MixedContentNavigationThrottle`, which underlying
// also runs the test target `MixedContentChecker::ShouldBlockNavigation()`.
navigation->Start();
return std::make_tuple(std::move(navigation), std::move(interceptor));
}
};
using MixedContentCheckerShouldBlockNavigationTest =
MixedContentCheckerShouldBlockNavigationTestBase;
INSTANTIATE_TEST_SUITE_P(
All,
MixedContentCheckerShouldBlockNavigationTest,
::testing::Values(false, true),
[](const testing::TestParamInfo<
MixedContentCheckerShouldBlockNavigationTest::ParamType>& info) {
return info.param ? "ForRedirect" : "ForNonRedirect";
});
// Main frame navigations cannot be mixed content, no matter the source page is
// secure or not.
TEST_P(MixedContentCheckerShouldBlockNavigationTest,
ShouldNotBlockNavigationFromInsecureMainFrame) {
const auto [nav, inspector] =
StartNavigation("http://source.com", "http://target.com");
auto checker = MixedContentChecker();
EXPECT_FALSE(checker.ShouldBlockNavigation(*nav->GetNavigationHandle(),
for_redirect()));
inspector->FlushLocalFrameMessages();
EXPECT_THAT(inspector->mixed_content_result(), Eq(absl::nullopt));
EXPECT_THAT(inspector->reported_web_features(), IsEmpty());
}
// Main frame navigations cannot be mixed content, no matter the source page is
// secure or not.
TEST_P(MixedContentCheckerShouldBlockNavigationTest,
ShouldNotBlockNavigationFromSecureMainFrame) {
const auto [nav, inspector] =
StartNavigation("https://source.com", "http://target.com");
auto checker = MixedContentChecker();
EXPECT_FALSE(checker.ShouldBlockNavigation(*nav->GetNavigationHandle(),
for_redirect()));
inspector->FlushLocalFrameMessages();
EXPECT_THAT(inspector->mixed_content_result(), Eq(absl::nullopt));
EXPECT_THAT(inspector->reported_web_features(), IsEmpty());
}
// Navigates from insecure content is not mixed content.
TEST_P(MixedContentCheckerShouldBlockNavigationTest,
ShouldNotBlockNavigationFromInsecureSubFrame) {
const bool from_subframe = true;
const auto [nav, inspector] =
StartNavigation("http://source.com", "http://target.com", from_subframe);
auto checker = MixedContentChecker();
EXPECT_FALSE(checker.ShouldBlockNavigation(*nav->GetNavigationHandle(),
for_redirect()));
inspector->FlushLocalFrameMessages();
EXPECT_THAT(inspector->mixed_content_result(), Eq(absl::nullopt));
EXPECT_THAT(inspector->reported_web_features(), IsEmpty());
}
// Tests to cover MixedContentContextType = kBlockable.
class MixedContentCheckerShouldBlockNavigationWithBlockableContextTest
: public MixedContentCheckerShouldBlockNavigationTestBase {
protected:
blink::mojom::MixedContentContextType mixed_content_context_type() const {
return blink::mojom::MixedContentContextType::kBlockable;
}
};
INSTANTIATE_TEST_SUITE_P(
All,
MixedContentCheckerShouldBlockNavigationWithBlockableContextTest,
::testing::Values(false, true),
[](const testing::TestParamInfo<
MixedContentCheckerShouldBlockNavigationWithBlockableContextTest::
ParamType>& info) {
return info.param ? "ForRedirect" : "ForNonRedirect";
});
// ShouldBlockNavigation(subframe) => true
// - MixedContentContextType = kBlockable
// - main frame's InsecureRequestPolicy = kLeaveInsecureRequestsAlone
TEST_P(MixedContentCheckerShouldBlockNavigationWithBlockableContextTest,
ShouldBlockMixedContentNavigationWithPolicyLeaveInsecureRequestAlone) {
const auto main_frame_insecure_request_policy =
blink::mojom::InsecureRequestPolicy::kLeaveInsecureRequestsAlone;
const bool from_subframe = true;
const auto [nav, inspector] = StartNavigation(
"https://source.com", "http://target.com", from_subframe,
main_frame_insecure_request_policy, mixed_content_context_type());
auto checker = MixedContentChecker();
EXPECT_TRUE(checker.ShouldBlockNavigation(*nav->GetNavigationHandle(),
for_redirect()));
inspector->FlushLocalFrameMessages();
EXPECT_THAT(
inspector->mixed_content_result(),
Optional(FieldsAre(GURL("https://source.com"), GURL("http://target.com"),
/*was_allowed=*/false, for_redirect())));
EXPECT_THAT(
inspector->reported_web_features(),
UnorderedElementsAre(blink::mojom::WebFeature::kMixedContentPresent,
blink::mojom::WebFeature::kMixedContentBlockable));
}
// ShouldBlockNavigation(subframe) => true
// - MixedContentContextType = kBlockable
// - main frame's InsecureRequestPolicy = kBlockAllMixedContent
TEST_P(MixedContentCheckerShouldBlockNavigationWithBlockableContextTest,
ShouldBlockMixedContentNavigationWithPolicyBlockAll) {
const auto main_frame_insecure_request_policy =
blink::mojom::InsecureRequestPolicy::kBlockAllMixedContent;
const bool from_subframe = true;
const auto [nav, inspector] = StartNavigation(
"https://source.com", "http://target.com", from_subframe,
main_frame_insecure_request_policy, mixed_content_context_type());
auto checker = MixedContentChecker();
EXPECT_TRUE(checker.ShouldBlockNavigation(*nav->GetNavigationHandle(),
for_redirect()));
inspector->FlushLocalFrameMessages();
EXPECT_THAT(
inspector->mixed_content_result(),
Optional(FieldsAre(GURL("https://source.com"), GURL("http://target.com"),
/*was_allowed=*/false, for_redirect())));
EXPECT_THAT(
inspector->reported_web_features(),
UnorderedElementsAre(blink::mojom::WebFeature::kMixedContentPresent,
blink::mojom::WebFeature::kMixedContentBlockable));
}
// Tests to cover MixedContentContextType = kOptionallyBlockable.
class MixedContentCheckerShouldBlockNavigationWithOptionallyBlockableContextTest
: public MixedContentCheckerShouldBlockNavigationTestBase {
protected:
blink::mojom::MixedContentContextType mixed_content_context_type() const {
return blink::mojom::MixedContentContextType::kOptionallyBlockable;
}
};
INSTANTIATE_TEST_SUITE_P(
All,
MixedContentCheckerShouldBlockNavigationWithOptionallyBlockableContextTest,
::testing::Values(false, true),
[](const testing::TestParamInfo<
MixedContentCheckerShouldBlockNavigationWithOptionallyBlockableContextTest::
ParamType>& info) {
return info.param ? "ForRedirect" : "ForNonRedirect";
});
// ShouldBlockNavigation(subframe) => false
// - MixedContentContextType = kOptionallyBlockable
// - main frame's InsecureRequestPolicy = kLeaveInsecureRequestsAlone
TEST_P(
MixedContentCheckerShouldBlockNavigationWithOptionallyBlockableContextTest,
ShouldNotBlockMixedContentNavigationWithPolicyLeaveInsecureRequestAlone) {
const auto main_frame_insecure_request_policy =
blink::mojom::InsecureRequestPolicy::kLeaveInsecureRequestsAlone;
const bool from_subframe = true;
const auto [nav, inspector] = StartNavigation(
"https://source.com", "http://target.com", from_subframe,
main_frame_insecure_request_policy, mixed_content_context_type());
auto checker = MixedContentChecker();
EXPECT_FALSE(checker.ShouldBlockNavigation(*nav->GetNavigationHandle(),
for_redirect()));
inspector->FlushLocalFrameMessages();
EXPECT_THAT(
inspector->mixed_content_result(),
Optional(FieldsAre(GURL("https://source.com"), GURL("http://target.com"),
/*was_allowed=*/true, for_redirect())));
EXPECT_THAT(
inspector->reported_web_features(),
UnorderedElementsAre(blink::mojom::WebFeature::kMixedContentPresent,
blink::mojom::WebFeature::kMixedContentInternal));
}
// ShouldBlockNavigation(subframe) => true
// - MixedContentContextType = kOptionallyBlockable
// - main frame's InsecureRequestPolicy = kBlockAllMixedContent
TEST_P(
MixedContentCheckerShouldBlockNavigationWithOptionallyBlockableContextTest,
ShouldBlockMixedContentNavigationWithPolicyBlockAll) {
const auto main_frame_insecure_request_policy =
blink::mojom::InsecureRequestPolicy::kBlockAllMixedContent;
const bool from_subframe = true;
const auto [nav, inspector] = StartNavigation(
"https://source.com", "http://target.com", from_subframe,
main_frame_insecure_request_policy, mixed_content_context_type());
auto checker = MixedContentChecker();
EXPECT_TRUE(checker.ShouldBlockNavigation(*nav->GetNavigationHandle(),
for_redirect()));
inspector->FlushLocalFrameMessages();
EXPECT_THAT(
inspector->mixed_content_result(),
Optional(FieldsAre(GURL("https://source.com"), GURL("http://target.com"),
/*was_allowed=*/false, for_redirect())));
EXPECT_THAT(
inspector->reported_web_features(),
UnorderedElementsAre(blink::mojom::WebFeature::kMixedContentPresent,
blink::mojom::WebFeature::kMixedContentInternal));
}
// Tests to cover MixedContentContextType = kShouldBeBlockable.
class MixedContentCheckerShouldBlockNavigationWithShouldBeBlockableContextTest
: public MixedContentCheckerShouldBlockNavigationTestBase {
protected:
blink::mojom::MixedContentContextType mixed_content_context_type() const {
return blink::mojom::MixedContentContextType::kShouldBeBlockable;
}
};
INSTANTIATE_TEST_SUITE_P(
All,
MixedContentCheckerShouldBlockNavigationWithShouldBeBlockableContextTest,
::testing::Values(false, true),
[](const testing::TestParamInfo<
MixedContentCheckerShouldBlockNavigationWithShouldBeBlockableContextTest::
ParamType>& info) {
return info.param ? "ForRedirect" : "ForNonRedirect";
});
// ShouldBlockNavigation(subframe) => false
// - MixedContentContextType = kShouldBeBlockable
// - main frame's InsecureRequestPolicy = kLeaveInsecureRequestsAlone
TEST_P(
MixedContentCheckerShouldBlockNavigationWithShouldBeBlockableContextTest,
ShouldNotBlockMixedContentNavigationWithPolicyLeaveInsecureRequestAlone) {
const auto main_frame_insecure_request_policy =
blink::mojom::InsecureRequestPolicy::kLeaveInsecureRequestsAlone;
const bool from_subframe = true;
const auto [nav, inspector] = StartNavigation(
"https://source.com", "http://target.com", from_subframe,
main_frame_insecure_request_policy, mixed_content_context_type());
auto checker = MixedContentChecker();
EXPECT_FALSE(checker.ShouldBlockNavigation(*nav->GetNavigationHandle(),
for_redirect()));
inspector->FlushLocalFrameMessages();
EXPECT_THAT(
inspector->mixed_content_result(),
Optional(FieldsAre(GURL("https://source.com"), GURL("http://target.com"),
/*was_allowed=*/true, for_redirect())));
EXPECT_THAT(
inspector->reported_web_features(),
UnorderedElementsAre(blink::mojom::WebFeature::kMixedContentPresent,
blink::mojom::WebFeature::kMixedContentInternal));
}
// ShouldBlockNavigation(subframe) => true
// - MixedContentContextType = kShouldBeBlockable
// - main frame's InsecureRequestPolicy = kBlockAllMixedContent
TEST_P(MixedContentCheckerShouldBlockNavigationWithShouldBeBlockableContextTest,
ShouldBlockMixedContentNavigationWithPolicyBlockAll) {
const auto main_frame_insecure_request_policy =
blink::mojom::InsecureRequestPolicy::kBlockAllMixedContent;
const bool from_subframe = true;
const auto [nav, inspector] = StartNavigation(
"https://source.com", "http://target.com", from_subframe,
main_frame_insecure_request_policy, mixed_content_context_type());
auto checker = MixedContentChecker();
EXPECT_TRUE(checker.ShouldBlockNavigation(*nav->GetNavigationHandle(),
for_redirect()));
inspector->FlushLocalFrameMessages();
EXPECT_THAT(
inspector->mixed_content_result(),
Optional(FieldsAre(GURL("https://source.com"), GURL("http://target.com"),
/*was_allowed=*/false, for_redirect())));
EXPECT_THAT(
inspector->reported_web_features(),
UnorderedElementsAre(blink::mojom::WebFeature::kMixedContentPresent,
blink::mojom::WebFeature::kMixedContentInternal));
}
} // namespace content
|