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 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
|
// Copyright 2021 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/navigation_policy_container_builder.h"
#include <iosfwd>
#include <utility>
#include "base/files/file_path.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/scoped_feature_list.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/mock_navigation_handle.h"
#include "content/public/test/navigation_simulator.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/network/public/mojom/content_security_policy.mojom.h"
#include "services/network/public/mojom/referrer_policy.mojom-shared.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/url_constants.h"
namespace content {
namespace {
using ::testing::ByRef;
using ::testing::Eq;
using ::testing::IsNull;
using ::testing::NotNull;
using ::testing::Pointee;
using ::testing::SizeIs;
network::mojom::ContentSecurityPolicyPtr MakeTestCSP() {
auto csp = network::mojom::ContentSecurityPolicy::New();
csp->header = network::mojom::ContentSecurityPolicyHeader::New();
csp->header->header_value = "some-directive some-value";
return csp;
}
// Returns non-default policies for use in tests.
PolicyContainerPolicies MakeTestPolicies() {
std::vector<network::mojom::ContentSecurityPolicyPtr> csp_list;
csp_list.push_back(MakeTestCSP());
return PolicyContainerPolicies(
network::mojom::ReferrerPolicy::kAlways,
network::mojom::IPAddressSpace::kPublic,
/*is_web_secure_context=*/true, std::move(csp_list),
network::CrossOriginOpenerPolicy(), network::CrossOriginEmbedderPolicy(),
network::DocumentIsolationPolicy(), network::IntegrityPolicy(),
network::IntegrityPolicy(), network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false,
/*can_navigate_top_without_user_gesture=*/true,
/*cross_origin_isolation_enabled_by_dip=*/false);
}
// Shorthand.
scoped_refptr<PolicyContainerHost> NewHost(PolicyContainerPolicies policies) {
return base::MakeRefCounted<PolicyContainerHost>(std::move(policies));
}
GURL AboutBlankUrl() {
return GURL(url::kAboutBlankURL);
}
GURL AboutSrcdocUrl() {
return GURL(url::kAboutSrcdocURL);
}
// RenderViewHostImplTestHarness allows interacting with RenderFrameHosts in the
// form of TestRenderFrameHosts. This allows us to easily set policies on frames
// for testing. It also instantiates a BrowserTaskEnvironment so that tests are
// executed "on the UI thread".
//
// This test fixture is moderately expensive to set up (~100ms overhead per
// test), but still an order of magnitude faster than browser tests.
class NavigationPolicyContainerBuilderTest
: public RenderViewHostImplTestHarness {
protected:
void SetUp() override {
RenderViewHostImplTestHarness::SetUp();
contents()->GetPrimaryMainFrame()->InitializeRenderFrameIfNeeded();
}
};
// Verifies that the initial delivered policies are default-constructed.
TEST_F(NavigationPolicyContainerBuilderTest, DefaultDeliveredPolicies) {
EXPECT_EQ(
NavigationPolicyContainerBuilder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr)
.DeliveredPoliciesForTesting(),
PolicyContainerPolicies());
}
// Verifies that SetIPAddressSpace sets the address space in the builder's
// delivered policies.
TEST_F(NavigationPolicyContainerBuilderTest, SetIPAddressSpace) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIPAddressSpace(network::mojom::IPAddressSpace::kPublic);
PolicyContainerPolicies expected_policies;
expected_policies.ip_address_space = network::mojom::IPAddressSpace::kPublic;
EXPECT_EQ(builder.DeliveredPoliciesForTesting(), expected_policies);
}
// Verifies that SetIsOriginPotentiallyTrustworthy sets the secure context bit
// in the builder's delivered policies.
TEST_F(NavigationPolicyContainerBuilderTest,
SetIsOriginPotentiallyTrustworthy) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIsOriginPotentiallyTrustworthy(true);
PolicyContainerPolicies expected_policies;
expected_policies.is_web_secure_context = true;
EXPECT_EQ(builder.DeliveredPoliciesForTesting(), expected_policies);
builder.SetIsOriginPotentiallyTrustworthy(false);
expected_policies.is_web_secure_context = false;
EXPECT_EQ(builder.DeliveredPoliciesForTesting(), expected_policies);
}
// Verifies that SetCrossOriginOpenerPolicy sets the cross-origin-opener-policy
// in the builder's delivered policies.
TEST_F(NavigationPolicyContainerBuilderTest, SetCrossOriginOpenerPolicy) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
network::CrossOriginOpenerPolicy coop;
coop.value = network::mojom::CrossOriginOpenerPolicyValue::kSameOrigin;
coop.report_only_value =
network::mojom::CrossOriginOpenerPolicyValue::kSameOriginAllowPopups;
coop.reporting_endpoint = "A";
coop.report_only_reporting_endpoint = "B";
builder.SetCrossOriginOpenerPolicy(coop);
PolicyContainerPolicies expected_policies;
expected_policies.cross_origin_opener_policy = coop;
EXPECT_EQ(builder.DeliveredPoliciesForTesting(), expected_policies);
}
// Verifies that SetDocumentIsolationPolicy sets the document-isolation-policy
// in the builder's delivered policies.
TEST_F(NavigationPolicyContainerBuilderTest, SetDocumentIsolationPolicy) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
network::DocumentIsolationPolicy dip;
dip.value =
network::mojom::DocumentIsolationPolicyValue::kIsolateAndRequireCorp;
dip.report_only_value =
network::mojom::DocumentIsolationPolicyValue::kIsolateAndCredentialless;
dip.reporting_endpoint = "A";
dip.report_only_reporting_endpoint = "B";
builder.SetDocumentIsolationPolicy(dip);
PolicyContainerPolicies expected_policies;
expected_policies.document_isolation_policy = dip;
EXPECT_EQ(builder.DeliveredPoliciesForTesting(), expected_policies);
}
// Verifies that the default final policies of a builder are
// default-constructed, and are equal to the policies of the builder's policy
// container host.
TEST_F(NavigationPolicyContainerBuilderTest, DefaultFinalPolicies) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
MockNavigationHandle navigation_handle(GURL(), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
PolicyContainerPolicies expected_policies;
EXPECT_EQ(builder.FinalPolicies(), expected_policies);
scoped_refptr<PolicyContainerHost> cloned_host =
builder.GetPolicyContainerHost();
ASSERT_THAT(cloned_host, NotNull());
EXPECT_EQ(cloned_host->policies(), expected_policies);
scoped_refptr<PolicyContainerHost> host =
std::move(builder).TakePolicyContainerHost();
ASSERT_THAT(host, NotNull());
EXPECT_EQ(host->policies(), expected_policies);
ASSERT_THAT(cloned_host, NotNull());
}
// Verifies that when the URL of the document to commit does not have a local
// scheme, then the final policies are copied from the delivered policies.
TEST_F(NavigationPolicyContainerBuilderTest, FinalPoliciesNormalUrl) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIPAddressSpace(network::mojom::IPAddressSpace::kPublic);
builder.AddContentSecurityPolicy(MakeTestCSP());
PolicyContainerPolicies delivered_policies =
builder.DeliveredPoliciesForTesting().Clone();
MockNavigationHandle navigation_handle(GURL("https://foo.test"), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies(), delivered_policies);
}
// Verifies the final policies when the URL of the document to commit is
// `about:blank` but there is no initiator.
TEST_F(NavigationPolicyContainerBuilderTest,
FinalPoliciesAboutBlankWithoutInitiator) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIPAddressSpace(network::mojom::IPAddressSpace::kPublic);
PolicyContainerPolicies delivered_policies =
builder.DeliveredPoliciesForTesting().Clone();
MockNavigationHandle navigation_handle(AboutBlankUrl(), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies(), delivered_policies);
}
TEST_F(NavigationPolicyContainerBuilderTest, MHTMLSandboxFlags) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIPAddressSpace(network::mojom::IPAddressSpace::kLocal);
MockNavigationHandle navigation_handle(GURL("file:///my/page.mhtml"),
nullptr);
builder.ComputePolicies(&navigation_handle,
/*is_inside_mhtml=*/true,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies().sandbox_flags,
// MHTML archives receive all sandbox flags except these:
~(network::mojom::WebSandboxFlags::kPopups |
network::mojom::WebSandboxFlags::
kPropagatesToAuxiliaryBrowsingContexts));
}
// When kMHTML_Improvements is enabled, in mhtml, scripts are allowed.
TEST_F(NavigationPolicyContainerBuilderTest,
MHTMLSandboxFlagsWithMHTMLImprovementsLocalFile) {
base::test::ScopedFeatureList features(blink::features::kMHTML_Improvements);
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIPAddressSpace(network::mojom::IPAddressSpace::kLocal);
MockNavigationHandle navigation_handle(GURL("file:///my/page.mhtml"),
nullptr);
builder.ComputePolicies(&navigation_handle,
/*is_inside_mhtml=*/true,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies().sandbox_flags,
// When kMHTML_Improvements is enabled, MHTML archives receive all
// sandbox flags except these:
~(network::mojom::WebSandboxFlags::kPopups |
network::mojom::WebSandboxFlags::
kPropagatesToAuxiliaryBrowsingContexts |
network::mojom::WebSandboxFlags::kScripts));
}
// Verifies the final policies when the URL of the document to commit is
// `about:blank` but there is no initiator, and we have some additional CSPs.
TEST_F(NavigationPolicyContainerBuilderTest,
FinalPoliciesAboutBlankWithoutInitiatorAdditionalCSP) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIPAddressSpace(network::mojom::IPAddressSpace::kPublic);
builder.AddContentSecurityPolicy(MakeTestCSP());
PolicyContainerPolicies delivered_policies =
builder.DeliveredPoliciesForTesting().Clone();
MockNavigationHandle navigation_handle(AboutBlankUrl(), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies(), delivered_policies);
}
// This test verifies the default final policies on error pages.
TEST_F(NavigationPolicyContainerBuilderTest, DefaultFinalPoliciesForErrorPage) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.ComputePoliciesForError();
// Error pages commit with default policies, mostly ignoring the delivered
// policies and the document's URL.
EXPECT_EQ(builder.FinalPolicies(), PolicyContainerPolicies());
}
// This test verifies that error pages commit in the same IP address space as
// the underlying page would have, had it not failed to load.
TEST_F(NavigationPolicyContainerBuilderTest, ErrorPageIPAddressSpace) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIPAddressSpace(network::mojom::IPAddressSpace::kPublic);
builder.ComputePoliciesForError();
PolicyContainerPolicies expected_policies;
expected_policies.ip_address_space = network::mojom::IPAddressSpace::kPublic;
EXPECT_EQ(builder.FinalPolicies(), expected_policies);
}
// Variation of: NavigationPolicyContainerBuilderTest.ErrorPageIPAddressSpace
// The decision to commit an error happens after receiving the response.
TEST_F(NavigationPolicyContainerBuilderTest,
ErrorPageIPAddressSpaceAfterResponse) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIPAddressSpace(network::mojom::IPAddressSpace::kPrivate);
PolicyContainerPolicies expected_policies;
expected_policies.ip_address_space = network::mojom::IPAddressSpace::kPrivate;
MockNavigationHandle navigation_handle(GURL("https://foo.test"), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies(), expected_policies);
builder.ComputePoliciesForError();
EXPECT_EQ(builder.FinalPolicies(), expected_policies);
}
// CSP delivered by the HTTP response are ignored for error document.
TEST_F(NavigationPolicyContainerBuilderTest,
DeliveredCSPIgnoredForErrorDocument) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.AddContentSecurityPolicy(
network::mojom::ContentSecurityPolicy::New());
MockNavigationHandle navigation_handle(GURL("https://foo.test"), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_THAT(builder.FinalPolicies().content_security_policies, SizeIs(1));
builder.ComputePoliciesForError();
EXPECT_THAT(builder.FinalPolicies().content_security_policies, SizeIs(0));
}
// Verifies that InitiatorPolicies() returns nullptr in the absence of an
// initiator frame token.
TEST_F(NavigationPolicyContainerBuilderTest,
InitiatorPoliciesWithoutInitiator) {
EXPECT_THAT(
NavigationPolicyContainerBuilder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr)
.InitiatorPolicies(),
IsNull());
}
// Verifies that ParentPolicies returns nullptr in the absence of a parent.
TEST_F(NavigationPolicyContainerBuilderTest, ParentPoliciesWithoutParent) {
EXPECT_THAT(
NavigationPolicyContainerBuilder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr)
.ParentPolicies(),
IsNull());
}
// Verifies that ParentPolicies returns a pointer to a copy of the parent's
// policies.
TEST_F(NavigationPolicyContainerBuilderTest, ParentPoliciesWithParent) {
PolicyContainerPolicies parent_policies = MakeTestPolicies();
TestRenderFrameHost* parent = contents()->GetPrimaryMainFrame();
parent->SetPolicyContainerHost(NewHost(parent_policies.Clone()));
NavigationPolicyContainerBuilder builder(
parent, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
EXPECT_THAT(builder.ParentPolicies(), Pointee(Eq(ByRef(parent_policies))));
}
// Verifies that when the the URL of the document to commit is `about:srcdoc`,
// the builder's final policies are copied from the parent.
TEST_F(NavigationPolicyContainerBuilderTest,
FinalPoliciesAboutSrcdocWithParent) {
PolicyContainerPolicies parent_policies = MakeTestPolicies();
TestRenderFrameHost* parent = contents()->GetPrimaryMainFrame();
parent->SetPolicyContainerHost(NewHost(parent_policies.Clone()));
NavigationPolicyContainerBuilder builder(
parent, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
MockNavigationHandle navigation_handle(AboutSrcdocUrl(), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies(), parent_policies);
}
// Verifies that when a document has a potentially-trustworthy origin and no
// parent, then it is a secure context.
TEST_F(NavigationPolicyContainerBuilderTest,
IsWebSecureContextTrustworthyOriginNoParent) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIsOriginPotentiallyTrustworthy(true);
PolicyContainerPolicies delivered_policies =
builder.DeliveredPoliciesForTesting().Clone();
EXPECT_TRUE(delivered_policies.is_web_secure_context);
MockNavigationHandle navigation_handle(GURL(), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies(), delivered_policies);
}
// Verifies that when a document has a non-potentially-trustworthy origin and no
// parent, then it is not a secure context.
TEST_F(NavigationPolicyContainerBuilderTest,
IsWebSecureContextNonTrustworthyOriginNoParent) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIsOriginPotentiallyTrustworthy(false);
PolicyContainerPolicies delivered_policies =
builder.DeliveredPoliciesForTesting().Clone();
EXPECT_FALSE(delivered_policies.is_web_secure_context);
MockNavigationHandle navigation_handle(GURL(), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies(), delivered_policies);
}
// Verifies that when a document has a potentially-trustworthy origin and a
// parent that is not a secure context, then it is not a secure context.
TEST_F(NavigationPolicyContainerBuilderTest,
IsWebSecureContextTrustworthyOriginNonSecureParent) {
PolicyContainerPolicies parent_policies = MakeTestPolicies();
parent_policies.is_web_secure_context = false;
TestRenderFrameHost* parent = contents()->GetPrimaryMainFrame();
parent->SetPolicyContainerHost(NewHost(std::move(parent_policies)));
NavigationPolicyContainerBuilder builder(
parent, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIsOriginPotentiallyTrustworthy(true);
MockNavigationHandle navigation_handle(GURL("https://foo.test"), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_FALSE(builder.FinalPolicies().is_web_secure_context);
}
// Verifies that when a document has a non-potentially-trustworthy origin and a
// parent that is a secure context, then it is not a secure context.
TEST_F(NavigationPolicyContainerBuilderTest,
IsWebSecureContextNonTrustworthyOriginSecureParent) {
PolicyContainerPolicies parent_policies = MakeTestPolicies();
parent_policies.is_web_secure_context = true;
TestRenderFrameHost* parent = contents()->GetPrimaryMainFrame();
parent->SetPolicyContainerHost(NewHost(std::move(parent_policies)));
NavigationPolicyContainerBuilder builder(
parent, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIsOriginPotentiallyTrustworthy(false);
PolicyContainerPolicies delivered_policies =
builder.DeliveredPoliciesForTesting().Clone();
EXPECT_FALSE(delivered_policies.is_web_secure_context);
MockNavigationHandle navigation_handle(GURL("http://foo.test"), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies(), delivered_policies);
}
// Verifies that when a document has a potentially-trustworthy origin and a
// parent that is a secure context, then it is a secure context.
TEST_F(NavigationPolicyContainerBuilderTest,
IsWebSecureContextTrustworthyOriginSecureParent) {
PolicyContainerPolicies parent_policies = MakeTestPolicies();
parent_policies.is_web_secure_context = true;
TestRenderFrameHost* parent = contents()->GetPrimaryMainFrame();
parent->SetPolicyContainerHost(NewHost(std::move(parent_policies)));
NavigationPolicyContainerBuilder builder(
parent, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
builder.SetIsOriginPotentiallyTrustworthy(true);
PolicyContainerPolicies delivered_policies =
builder.DeliveredPoliciesForTesting().Clone();
EXPECT_TRUE(delivered_policies.is_web_secure_context);
MockNavigationHandle navigation_handle(GURL("https://foo.test"), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies(), delivered_policies);
}
// Verifies that when the the URL of the document to commit is `about:srcdoc`,
// the builder's final policies are copied from the parent, and additional
// delivered policies are merged.
TEST_F(NavigationPolicyContainerBuilderTest,
FinalPoliciesAboutSrcdocWithParentAndAdditionalCSP) {
PolicyContainerPolicies parent_policies = MakeTestPolicies();
TestRenderFrameHost* parent = contents()->GetPrimaryMainFrame();
parent->SetPolicyContainerHost(NewHost(parent_policies.Clone()));
NavigationPolicyContainerBuilder builder(
parent, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
// Add some CSP.
network::mojom::ContentSecurityPolicyPtr test_csp = MakeTestCSP();
builder.AddContentSecurityPolicy(test_csp.Clone());
MockNavigationHandle navigation_handle(AboutSrcdocUrl(), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
parent_policies.content_security_policies.push_back(std::move(test_csp));
EXPECT_EQ(builder.FinalPolicies(), parent_policies);
}
// Calling ComputePolicies() twice triggers a DCHECK.
TEST_F(NavigationPolicyContainerBuilderTest, ComputePoliciesTwiceDCHECK) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
MockNavigationHandle navigation_handle(GURL("https://foo.test"), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_DCHECK_DEATH(builder.ComputePolicies(
&navigation_handle, false, network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false));
}
// Calling ComputePolicies() followed by ComputePoliciesForError() is supported.
TEST_F(NavigationPolicyContainerBuilderTest, ComputePoliciesThenError) {
NavigationPolicyContainerBuilder builder(
nullptr, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
MockNavigationHandle navigation_handle(GURL("https://foo.test"), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
builder.ComputePoliciesForError();
}
// After ComputePolicies() or ComputePoliciesForError(), the parent
// policies are still accessible.
TEST_F(NavigationPolicyContainerBuilderTest,
AccessParentAfterComputingPolicies) {
PolicyContainerPolicies parent_policies = MakeTestPolicies();
TestRenderFrameHost* parent = contents()->GetPrimaryMainFrame();
parent->SetPolicyContainerHost(NewHost(parent_policies.Clone()));
NavigationPolicyContainerBuilder builder(
parent, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
EXPECT_THAT(builder.ParentPolicies(), Pointee(Eq(ByRef(parent_policies))));
MockNavigationHandle navigation_handle(GURL("https://foo.test"), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_THAT(builder.ParentPolicies(), Pointee(Eq(ByRef(parent_policies))));
builder.ComputePoliciesForError();
EXPECT_THAT(builder.ParentPolicies(), Pointee(Eq(ByRef(parent_policies))));
}
// Verifies that the parent policies are preserved on
// ResetForCrossDocumentRestart.
TEST_F(NavigationPolicyContainerBuilderTest,
ResetForCrossDocumentRestartParentPolicies) {
PolicyContainerPolicies parent_policies = MakeTestPolicies();
TestRenderFrameHost* parent = contents()->GetPrimaryMainFrame();
parent->SetPolicyContainerHost(NewHost(parent_policies.Clone()));
NavigationPolicyContainerBuilder builder(
parent, nullptr, kInvalidChildProcessUniqueId, nullptr, nullptr);
MockNavigationHandle navigation_handle(GURL("https://foo.test"), nullptr);
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies(), PolicyContainerPolicies());
builder.ResetForCrossDocumentRestart();
EXPECT_THAT(builder.ParentPolicies(), Pointee(Eq(ByRef(parent_policies))));
navigation_handle.set_url(AboutSrcdocUrl());
builder.ComputePolicies(&navigation_handle, false,
network::mojom::WebSandboxFlags::kNone,
/*is_credentialless=*/false);
EXPECT_EQ(builder.FinalPolicies(), parent_policies);
}
} // namespace
} // namespace content
|