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 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/net/qwac_web_contents_observer.h"
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/test_future.h"
#include "chrome/browser/net/system_network_context_manager.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "chrome/test/base/platform_browser_test.h"
#include "components/security_state/content/security_state_tab_helper.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "mojo/public/cpp/base/proto_wrapper.h"
#include "net/base/features.h"
#include "net/cert/internal/trust_store_chrome.h"
#include "net/cert/x509_certificate.h"
#include "net/dns/mock_host_resolver.h"
#include "net/http/http_status_code.h"
#include "net/test/cert_builder.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "net/test/two_qwac_cert_binding_builder.h"
#include "services/cert_verifier/public/mojom/cert_verifier_service_factory.mojom.h"
namespace {
class ResponseHandlerBase {
public:
ResponseHandlerBase(net::EmbeddedTestServer& server, std::string_view path)
: expected_path_(path) {
server.RegisterRequestHandler(base::BindRepeating(
&ResponseHandlerBase::ServeResponse, base::Unretained(this)));
}
std::unique_ptr<net::test_server::HttpResponse> ServeResponse(
const net::test_server::HttpRequest& request) {
if (request.GetURL().path() != expected_path_) {
return nullptr;
}
++request_count_;
return ServeResponseImpl(request);
}
virtual std::unique_ptr<net::test_server::HttpResponse> ServeResponseImpl(
const net::test_server::HttpRequest& request) = 0;
int request_count() const { return request_count_; }
private:
std::atomic_int request_count_ = 0;
std::string expected_path_;
};
class ServePageWithQwacHeaderHandler : public ResponseHandlerBase {
public:
ServePageWithQwacHeaderHandler(net::EmbeddedTestServer& server,
std::string_view path,
std::string_view qwac_link_url)
: ResponseHandlerBase(server, path), qwac_link_url_(qwac_link_url) {}
std::unique_ptr<net::test_server::HttpResponse> ServeResponseImpl(
const net::test_server::HttpRequest& request) override {
auto http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_OK);
http_response->set_content_type("text/html");
http_response->set_content("");
http_response->AddCustomHeader(
"Link", base::StringPrintf("<%s>;rel=\"tls-certificate-binding\"",
qwac_link_url_));
http_response->AddCustomHeader("Cache-Control", "max-age=3600");
return std::move(http_response);
}
private:
std::string qwac_link_url_;
};
class ServeResponseHandler : public ResponseHandlerBase {
public:
ServeResponseHandler(net::EmbeddedTestServer& server,
std::string_view path,
std::string_view content,
std::string_view content_type,
net::HttpStatusCode status_code = net::HTTP_OK)
: ResponseHandlerBase(server, path),
content_(content),
content_type_(content_type),
status_code_(status_code) {}
std::unique_ptr<net::test_server::HttpResponse> ServeResponseImpl(
const net::test_server::HttpRequest& request) override {
auto http_response =
std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_content(content_);
http_response->set_content_type(content_type_);
http_response->set_code(status_code_);
http_response->AddCustomHeader("Cache-Control", "max-age=3600");
return http_response;
}
private:
std::string content_;
std::string content_type_;
net::HttpStatusCode status_code_;
};
std::unique_ptr<net::test_server::HttpResponse> ServeRedirectWithQwacHeader(
const std::string& expected_path,
const std::string& redirect_url,
const std::string& qwac_link_url,
const net::test_server::HttpRequest& request) {
if (request.GetURL().path() != expected_path) {
return nullptr;
}
std::unique_ptr<net::test_server::BasicHttpResponse> http_response(
new net::test_server::BasicHttpResponse);
http_response->set_code(net::HTTP_TEMPORARY_REDIRECT);
http_response->AddCustomHeader("Location", redirect_url);
http_response->AddCustomHeader(
"Link", base::StringPrintf("<%s>;rel=\"tls-certificate-binding\"",
qwac_link_url));
return std::move(http_response);
}
std::unique_ptr<net::test_server::HttpResponse> FailTestIfPathRequested(
const std::string& expected_path,
const net::test_server::HttpRequest& request) {
if (request.GetURL().path() != expected_path) {
return nullptr;
}
ADD_FAILURE() << "FailTestIfPathRequested " << request.relative_url;
auto http_response = std::make_unique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_BAD_REQUEST);
return http_response;
}
bool WaitForStatusFinished(QwacWebContentsObserver::QwacStatus* status) {
if (status->is_finished()) {
return true;
}
base::test::TestFuture<void> future;
auto subscription = status->RegisterCallback(future.GetCallback());
return future.Wait();
}
class QwacWebContentsObserverTestBase : public PlatformBrowserTest {
public:
void SetUpOnMainThread() override {
host_resolver()->AddRule("*", "127.0.0.1");
PlatformBrowserTest::SetUpOnMainThread();
}
content::WebContents* web_contents() {
return chrome_test_utils::GetActiveWebContents(this);
}
QwacWebContentsObserver::QwacStatus* GetCurrentPageQwacStatus() {
return QwacWebContentsObserver::QwacStatus::GetForPage(
web_contents()->GetPrimaryPage());
}
scoped_refptr<net::X509Certificate> VisibleSecurityStateTwoQwac() {
SecurityStateTabHelper* helper =
SecurityStateTabHelper::FromWebContents(web_contents());
if (!helper) {
ADD_FAILURE() << "Failed to load SecurityStateTabHelper";
return nullptr;
}
std::unique_ptr<security_state::VisibleSecurityState>
visible_security_state = helper->GetVisibleSecurityState();
if (!visible_security_state) {
ADD_FAILURE() << "SecurityStateTabHelper has no VisibleSecurityState";
return nullptr;
}
return visible_security_state->two_qwac;
}
void ExpectHistogramSample(
const base::HistogramTester& histograms,
QwacWebContentsObserver::QwacLinkProcessingResult result) {
histograms.ExpectUniqueSample("Net.CertVerifier.Qwac.2QwacLinkProcessing",
result, 1u);
}
void ExpectNoHistogramSample(const base::HistogramTester& histograms) {
histograms.ExpectTotalCount("Net.CertVerifier.Qwac.2QwacLinkProcessing", 0);
}
};
class QwacWebContentsObserverDisabledBrowserTest
: public QwacWebContentsObserverTestBase {
public:
QwacWebContentsObserverDisabledBrowserTest() {
scoped_feature_list_.InitAndDisableFeature(net::features::kVerifyQWACs);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(QwacWebContentsObserverDisabledBrowserTest,
TestFeatureDisabled) {
ServePageWithQwacHeaderHandler main_page_handler(embedded_https_test_server(),
"/main", "/qwac");
embedded_https_test_server().RegisterRequestHandler(
base::BindRepeating(&FailTestIfPathRequested, "/qwac"));
auto test_server_handle = embedded_https_test_server().StartAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
base::HistogramTester histograms;
EXPECT_TRUE(content::NavigateToURL(
web_contents(), embedded_https_test_server().GetURL("/main")));
EXPECT_FALSE(GetCurrentPageQwacStatus());
EXPECT_FALSE(VisibleSecurityStateTwoQwac());
ExpectNoHistogramSample(histograms);
}
class QwacWebContentsObserverBrowserTest
: public QwacWebContentsObserverTestBase {
public:
void SetUpInProcessBrowserTestFixture() override {
// Disable certificate transparency so that adding test roots as known
// roots works without having to add test CT setup as well.
SystemNetworkContextManager::SetEnableCertificateTransparencyForTesting(
false);
PlatformBrowserTest::SetUpInProcessBrowserTestFixture();
}
// Installs a CRS proto update that trusts `root_cert` for TLS and adds
// `eutl_cert` as an EUTL.
//
// Technically the TLS cert bound by a 2-QWAC doesn't need to be trusted
// by a root that is in the CRS, but the CRS update will fail if
// `trust_anchors()` is empty. To satisfy this we put the test root in the
// CRS anchors list. Because it doesn't matter for the test whether the root
// is trusted by the CRS or by TestRootCerts, we don't bother with creating
// a unique test root or clearing TestRootCerts.
void InstallCRSUpdate(net::X509Certificate* root_cert,
net::X509Certificate* eutl_cert) {
ASSERT_TRUE(root_cert);
ASSERT_TRUE(eutl_cert);
chrome_root_store::RootStore root_store_proto;
root_store_proto.set_version_major(++crs_version_);
auto* anchor = root_store_proto.add_trust_anchors();
anchor->set_der(base::as_string_view(root_cert->cert_span()));
auto* additional_cert = root_store_proto.add_additional_certs();
additional_cert->set_der(base::as_string_view(eutl_cert->cert_span()));
additional_cert->set_eutl(true);
base::RunLoop update_run_loop;
content::GetCertVerifierServiceFactory()->UpdateChromeRootStore(
mojo_base::ProtoWrapper(root_store_proto),
update_run_loop.QuitClosure());
update_run_loop.Run();
}
private:
base::test::ScopedFeatureList scoped_feature_list_{
net::features::kVerifyQWACs};
int64_t crs_version_ = net::CompiledChromeRootStoreVersion();
};
IN_PROC_BROWSER_TEST_F(QwacWebContentsObserverBrowserTest,
TestSuccessAndCachedGoBack) {
ASSERT_TRUE(embedded_https_test_server().InitializeAndListen());
net::TwoQwacCertBindingBuilder binding_builder;
auto tls_leaf = embedded_https_test_server().GetCertificate();
ASSERT_TRUE(tls_leaf);
binding_builder.SetBoundCerts(
{std::string(base::as_string_view(tls_leaf->cert_span()))});
ServeResponseHandler qwac_handler(embedded_https_test_server(), "/qwac",
binding_builder.GetJWS(),
"application/jose");
ServePageWithQwacHeaderHandler main_page_handler(embedded_https_test_server(),
"/main", "/qwac");
ServeResponseHandler nonqwac_page_handler(embedded_https_test_server(),
"/main_noqwac", "", "text/html");
auto test_server_handle =
embedded_https_test_server().StartAcceptingConnectionsAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
InstallCRSUpdate(
/*root_cert=*/embedded_https_test_server().GetRoot().get(),
/*eutl_cert=*/
binding_builder.GetRootBuilder()->GetX509Certificate().get());
{
base::HistogramTester histograms;
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_https_test_server().GetURL("www.example.com", "/main")));
auto* status = GetCurrentPageQwacStatus();
ASSERT_TRUE(status);
ASSERT_TRUE(WaitForStatusFinished(status));
ASSERT_TRUE(status->verified_2qwac_cert());
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
binding_builder.GetLeafBuilder()->GetX509CertificateFullChain().get()));
ASSERT_TRUE(status->tls_cert());
EXPECT_TRUE(status->tls_cert()->EqualsExcludingChain(tls_leaf.get()));
EXPECT_EQ(1, main_page_handler.request_count());
EXPECT_EQ(1, qwac_handler.request_count());
auto visible_security_state_two_qwac = VisibleSecurityStateTwoQwac();
EXPECT_TRUE(visible_security_state_two_qwac);
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
visible_security_state_two_qwac.get()));
ExpectHistogramSample(
histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::kValid2Qwac);
}
{
base::HistogramTester histograms;
// Navigate to a different page without a qwac.
EXPECT_TRUE(content::NavigateToURL(
web_contents(), embedded_https_test_server().GetURL("www.example.com",
"/main_noqwac")));
EXPECT_FALSE(GetCurrentPageQwacStatus());
EXPECT_FALSE(VisibleSecurityStateTwoQwac());
ExpectHistogramSample(
histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::kNoQwacLinkHeader);
}
{
base::HistogramTester histograms;
// Now go back to the previous page. If BFCache is enabled, qwac status
// should still be available and not need to be re-fetched, otherwise it
// should be re-fetched successfully.
ASSERT_TRUE(HistoryGoBack(web_contents()));
auto* status = GetCurrentPageQwacStatus();
ASSERT_TRUE(status);
if (base::FeatureList::IsEnabled(features::kBackForwardCache)) {
ASSERT_TRUE(status->is_finished());
ExpectHistogramSample(histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::
kQwacStatusAlreadyPresent);
} else {
ASSERT_TRUE(WaitForStatusFinished(status));
ExpectHistogramSample(
histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::kValid2Qwac);
}
ASSERT_TRUE(status->verified_2qwac_cert());
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
binding_builder.GetLeafBuilder()->GetX509CertificateFullChain().get()));
ASSERT_TRUE(status->tls_cert());
EXPECT_TRUE(status->tls_cert()->EqualsExcludingChain(tls_leaf.get()));
EXPECT_EQ(1, main_page_handler.request_count());
EXPECT_EQ(1, qwac_handler.request_count());
auto visible_security_state_two_qwac = VisibleSecurityStateTwoQwac();
EXPECT_TRUE(visible_security_state_two_qwac);
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
visible_security_state_two_qwac.get()));
}
}
IN_PROC_BROWSER_TEST_F(QwacWebContentsObserverBrowserTest,
TestSuccessAndCachedNavigateSamePage) {
ASSERT_TRUE(embedded_https_test_server().InitializeAndListen());
net::TwoQwacCertBindingBuilder binding_builder;
auto tls_leaf = embedded_https_test_server().GetCertificate();
ASSERT_TRUE(tls_leaf);
binding_builder.SetBoundCerts(
{std::string(base::as_string_view(tls_leaf->cert_span()))});
ServeResponseHandler qwac_handler(embedded_https_test_server(), "/qwac",
binding_builder.GetJWS(),
"application/jose");
ServePageWithQwacHeaderHandler main_page_handler(embedded_https_test_server(),
"/main", "/qwac");
ServeResponseHandler nonqwac_page_handler(embedded_https_test_server(),
"/main_noqwac", "", "text/html");
auto test_server_handle =
embedded_https_test_server().StartAcceptingConnectionsAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
InstallCRSUpdate(
/*root_cert=*/embedded_https_test_server().GetRoot().get(),
/*eutl_cert=*/
binding_builder.GetRootBuilder()->GetX509Certificate().get());
{
base::HistogramTester histograms;
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_https_test_server().GetURL("www.example.com", "/main")));
auto* status = GetCurrentPageQwacStatus();
ASSERT_TRUE(status);
ASSERT_TRUE(WaitForStatusFinished(status));
ASSERT_TRUE(status->verified_2qwac_cert());
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
binding_builder.GetLeafBuilder()->GetX509CertificateFullChain().get()));
ASSERT_TRUE(status->tls_cert());
EXPECT_TRUE(status->tls_cert()->EqualsExcludingChain(tls_leaf.get()));
EXPECT_EQ(1, main_page_handler.request_count());
EXPECT_EQ(1, qwac_handler.request_count());
auto visible_security_state_two_qwac = VisibleSecurityStateTwoQwac();
EXPECT_TRUE(visible_security_state_two_qwac);
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
visible_security_state_two_qwac.get()));
ExpectHistogramSample(
histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::kValid2Qwac);
}
// Navigate to a different page without a qwac.
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_https_test_server().GetURL("www.example.com", "/main_noqwac")));
EXPECT_FALSE(GetCurrentPageQwacStatus());
EXPECT_FALSE(VisibleSecurityStateTwoQwac());
{
base::HistogramTester histograms;
// Now navigate to the original page URL again.
// The page and the 2-QWAC should be in HTTP cache, so the page and qwac
// status should be loaded without reloading either from the http server.
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_https_test_server().GetURL("www.example.com", "/main")));
auto* status = GetCurrentPageQwacStatus();
ASSERT_TRUE(status);
ASSERT_TRUE(WaitForStatusFinished(status));
ASSERT_TRUE(status->verified_2qwac_cert());
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
binding_builder.GetLeafBuilder()->GetX509CertificateFullChain().get()));
ASSERT_TRUE(status->tls_cert());
EXPECT_TRUE(status->tls_cert()->EqualsExcludingChain(tls_leaf.get()));
EXPECT_EQ(1, main_page_handler.request_count());
EXPECT_EQ(1, qwac_handler.request_count());
auto visible_security_state_two_qwac = VisibleSecurityStateTwoQwac();
EXPECT_TRUE(visible_security_state_two_qwac);
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
visible_security_state_two_qwac.get()));
ExpectHistogramSample(
histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::kValid2Qwac);
}
}
IN_PROC_BROWSER_TEST_F(QwacWebContentsObserverBrowserTest,
TestReloadDifferentCertAndQwac) {
int port;
{
ASSERT_TRUE(embedded_https_test_server().InitializeAndListen());
net::TwoQwacCertBindingBuilder binding_builder;
auto tls_leaf = embedded_https_test_server().GetCertificate();
ASSERT_TRUE(tls_leaf);
binding_builder.SetBoundCerts(
{std::string(base::as_string_view(tls_leaf->cert_span()))});
ServeResponseHandler qwac_handler(embedded_https_test_server(), "/qwac",
binding_builder.GetJWS(),
"application/jose");
ServePageWithQwacHeaderHandler main_page_handler(
embedded_https_test_server(), "/main", "/qwac");
auto test_server_handle =
embedded_https_test_server().StartAcceptingConnectionsAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
InstallCRSUpdate(
/*root_cert=*/embedded_https_test_server().GetRoot().get(),
/*eutl_cert=*/
binding_builder.GetRootBuilder()->GetX509Certificate().get());
base::HistogramTester histograms;
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_https_test_server().GetURL("www.example.com", "/main")));
auto* status = GetCurrentPageQwacStatus();
ASSERT_TRUE(status);
ASSERT_TRUE(WaitForStatusFinished(status));
ASSERT_TRUE(status->verified_2qwac_cert());
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
binding_builder.GetLeafBuilder()->GetX509CertificateFullChain().get()));
ASSERT_TRUE(status->tls_cert());
EXPECT_TRUE(status->tls_cert()->EqualsExcludingChain(tls_leaf.get()));
auto visible_security_state_two_qwac = VisibleSecurityStateTwoQwac();
EXPECT_TRUE(visible_security_state_two_qwac);
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
visible_security_state_two_qwac.get()));
ExpectHistogramSample(
histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::kValid2Qwac);
// Save the port number and release the `test_server_handle`, shutting down
// the existing test server.
port = embedded_https_test_server().port();
}
// Start a new test server on the same port with a different certificate and
// different 2-QWAC.
// This might be flaky if the test can't open the same port again, but
// there's not really any other way to test this.
net::EmbeddedTestServer https_test_server_2(
net::test_server::EmbeddedTestServer::TYPE_HTTPS);
https_test_server_2.SetCertHostnames({"*.example.com"});
ASSERT_TRUE(https_test_server_2.InitializeAndListen(port));
net::TwoQwacCertBindingBuilder binding_builder_2;
auto tls_leaf_2 = https_test_server_2.GetCertificate();
ASSERT_TRUE(tls_leaf_2);
binding_builder_2.SetBoundCerts(
{std::string(base::as_string_view(tls_leaf_2->cert_span()))});
ServeResponseHandler qwac_handler(https_test_server_2, "/qwac2",
binding_builder_2.GetJWS(),
"application/jose");
ServePageWithQwacHeaderHandler main_page_handler(https_test_server_2, "/main",
"/qwac2");
auto test_server_handle =
https_test_server_2.StartAcceptingConnectionsAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
InstallCRSUpdate(
/*root_cert=*/https_test_server_2.GetRoot().get(),
/*eutl_cert=*/
binding_builder_2.GetRootBuilder()->GetX509Certificate().get());
{
base::HistogramTester histograms;
// Reload the page.
content::TestNavigationObserver reload_observer(
web_contents(),
/*expected_number_of_navigations=*/1);
web_contents()->GetController().Reload(content::ReloadType::BYPASSING_CACHE,
/*check_for_repost=*/false);
reload_observer.Wait();
auto* status = GetCurrentPageQwacStatus();
ASSERT_TRUE(status);
ASSERT_TRUE(WaitForStatusFinished(status));
// Should have gotten the new 2-QWAC response.
ASSERT_TRUE(status->verified_2qwac_cert());
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
binding_builder_2.GetLeafBuilder()
->GetX509CertificateFullChain()
.get()));
ASSERT_TRUE(status->tls_cert());
EXPECT_TRUE(status->tls_cert()->EqualsExcludingChain(tls_leaf_2.get()));
auto visible_security_state_two_qwac = VisibleSecurityStateTwoQwac();
EXPECT_TRUE(visible_security_state_two_qwac);
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
visible_security_state_two_qwac.get()));
ExpectHistogramSample(
histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::kValid2Qwac);
}
}
IN_PROC_BROWSER_TEST_F(QwacWebContentsObserverBrowserTest,
SameDocumentNavigation) {
ASSERT_TRUE(embedded_https_test_server().InitializeAndListen());
net::TwoQwacCertBindingBuilder binding_builder;
auto tls_leaf = embedded_https_test_server().GetCertificate();
ASSERT_TRUE(tls_leaf);
binding_builder.SetBoundCerts(
{std::string(base::as_string_view(tls_leaf->cert_span()))});
ServeResponseHandler qwac_handler(embedded_https_test_server(), "/qwac",
binding_builder.GetJWS(),
"application/jose");
ServePageWithQwacHeaderHandler main_page_handler(embedded_https_test_server(),
"/main", "/qwac");
ServeResponseHandler nonqwac_page_handler(embedded_https_test_server(),
"/main_noqwac", "", "text/html");
auto test_server_handle =
embedded_https_test_server().StartAcceptingConnectionsAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
InstallCRSUpdate(
/*root_cert=*/embedded_https_test_server().GetRoot().get(),
/*eutl_cert=*/
binding_builder.GetRootBuilder()->GetX509Certificate().get());
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_https_test_server().GetURL("www.example.com", "/main")));
{
auto* status = GetCurrentPageQwacStatus();
ASSERT_TRUE(status);
ASSERT_TRUE(WaitForStatusFinished(status));
ASSERT_TRUE(status->verified_2qwac_cert());
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
binding_builder.GetLeafBuilder()->GetX509CertificateFullChain().get()));
ASSERT_TRUE(status->tls_cert());
EXPECT_TRUE(status->tls_cert()->EqualsExcludingChain(tls_leaf.get()));
EXPECT_EQ(1, main_page_handler.request_count());
EXPECT_EQ(1, qwac_handler.request_count());
auto visible_security_state_two_qwac = VisibleSecurityStateTwoQwac();
EXPECT_TRUE(visible_security_state_two_qwac);
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
visible_security_state_two_qwac.get()));
}
{
base::HistogramTester histograms;
// Navigate to a different URL with the same page.
// Since the page didn't change, the qwac status should still be available
// and not need to be re-fetched.
EXPECT_TRUE(content::NavigateToURL(
web_contents(), embedded_https_test_server().GetURL("www.example.com",
"/main#anchor")));
auto* status = GetCurrentPageQwacStatus();
ASSERT_TRUE(status);
ASSERT_TRUE(status->is_finished());
ASSERT_TRUE(status->verified_2qwac_cert());
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
binding_builder.GetLeafBuilder()->GetX509CertificateFullChain().get()));
ASSERT_TRUE(status->tls_cert());
EXPECT_TRUE(status->tls_cert()->EqualsExcludingChain(tls_leaf.get()));
EXPECT_EQ(1, main_page_handler.request_count());
EXPECT_EQ(1, qwac_handler.request_count());
auto visible_security_state_two_qwac = VisibleSecurityStateTwoQwac();
EXPECT_TRUE(visible_security_state_two_qwac);
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
visible_security_state_two_qwac.get()));
// Same-page navigations are not histogrammed.
ExpectNoHistogramSample(histograms);
}
}
IN_PROC_BROWSER_TEST_F(QwacWebContentsObserverBrowserTest,
SameOriginRedirectAllowed) {
ASSERT_TRUE(embedded_https_test_server().InitializeAndListen());
net::TwoQwacCertBindingBuilder binding_builder;
auto tls_leaf = embedded_https_test_server().GetCertificate();
ASSERT_TRUE(tls_leaf);
binding_builder.SetBoundCerts(
{std::string(base::as_string_view(tls_leaf->cert_span()))});
ServeResponseHandler qwac_handler(embedded_https_test_server(), "/qwac",
binding_builder.GetJWS(),
"application/jose");
ServePageWithQwacHeaderHandler main_page_handler(
embedded_https_test_server(), "/main", "/server-redirect?/qwac");
auto test_server_handle =
embedded_https_test_server().StartAcceptingConnectionsAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
InstallCRSUpdate(
/*root_cert=*/embedded_https_test_server().GetRoot().get(),
/*eutl_cert=*/
binding_builder.GetRootBuilder()->GetX509Certificate().get());
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_https_test_server().GetURL("www.example.com", "/main")));
auto* status = GetCurrentPageQwacStatus();
ASSERT_TRUE(status);
ASSERT_TRUE(WaitForStatusFinished(status));
ASSERT_TRUE(status->verified_2qwac_cert());
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
binding_builder.GetLeafBuilder()->GetX509CertificateFullChain().get()));
ASSERT_TRUE(status->tls_cert());
EXPECT_TRUE(status->tls_cert()->EqualsExcludingChain(tls_leaf.get()));
auto visible_security_state_two_qwac = VisibleSecurityStateTwoQwac();
EXPECT_TRUE(visible_security_state_two_qwac);
EXPECT_TRUE(status->verified_2qwac_cert()->EqualsIncludingChain(
visible_security_state_two_qwac.get()));
}
IN_PROC_BROWSER_TEST_F(QwacWebContentsObserverBrowserTest,
LinkHeaderFromMainPageRedirectIgnored) {
embedded_https_test_server().RegisterRequestHandler(base::BindRepeating(
&ServeRedirectWithQwacHeader, "/main", /*redirect_url=*/"/realmain",
/*qwac_link_url=*/"/qwac"));
ServeResponseHandler page_handler(embedded_https_test_server(), "/realmain",
"", "text/html");
embedded_https_test_server().RegisterRequestHandler(
base::BindRepeating(&FailTestIfPathRequested, "/qwac"));
auto test_server_handle = embedded_https_test_server().StartAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
base::HistogramTester histograms;
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_https_test_server().GetURL("www.example.com", "/main"),
/*expected_commit_url=*/
embedded_https_test_server().GetURL("www.example.com", "/realmain")));
// The QWAC link header was present on a redirect, but not on the final page
// load, so no QwacStatus should be created.
EXPECT_FALSE(GetCurrentPageQwacStatus());
EXPECT_FALSE(VisibleSecurityStateTwoQwac());
ExpectHistogramSample(
histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::kNoQwacLinkHeader);
}
IN_PROC_BROWSER_TEST_F(QwacWebContentsObserverBrowserTest,
CrossOriginRedirectNotAllowed) {
net::EmbeddedTestServer other_https_test_server(
net::test_server::EmbeddedTestServer::TYPE_HTTPS);
other_https_test_server.RegisterRequestHandler(
base::BindRepeating(&FailTestIfPathRequested, "/qwac"));
auto other_test_server_handle =
other_https_test_server.StartAndReturnHandle();
ASSERT_TRUE(other_test_server_handle.is_valid());
ServePageWithQwacHeaderHandler main_page_handler(
embedded_https_test_server(), "/main",
"/server-redirect?" +
other_https_test_server.GetURL("foo.example.com", "/qwac").spec());
auto test_server_handle = embedded_https_test_server().StartAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
base::HistogramTester histograms;
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_https_test_server().GetURL("www.example.com", "/main")));
auto* status = GetCurrentPageQwacStatus();
// The initial QWAC url is same-origin so a fetch request should be started
// and a QwacStatus created, but the fetch should fail once it hits the
// redirect to a different origin.
ASSERT_TRUE(status);
ASSERT_TRUE(WaitForStatusFinished(status));
EXPECT_FALSE(status->verified_2qwac_cert());
EXPECT_FALSE(VisibleSecurityStateTwoQwac());
ExpectHistogramSample(
histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::kDownloadFailed);
}
IN_PROC_BROWSER_TEST_F(QwacWebContentsObserverBrowserTest,
CrossOriginFetchNotAllowed) {
net::EmbeddedTestServer other_https_test_server(
net::test_server::EmbeddedTestServer::TYPE_HTTPS);
other_https_test_server.RegisterRequestHandler(
base::BindRepeating(&FailTestIfPathRequested, "/qwac"));
auto other_test_server_handle =
other_https_test_server.StartAndReturnHandle();
ASSERT_TRUE(other_test_server_handle.is_valid());
ServePageWithQwacHeaderHandler main_page_handler(
embedded_https_test_server(), "/main",
other_https_test_server.GetURL("foo.example.com", "/qwac").spec());
auto test_server_handle = embedded_https_test_server().StartAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
base::HistogramTester histograms;
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_https_test_server().GetURL("www.example.com", "/main")));
// Since the initial QWAC url was cross-origin, the fetch is never started
// and no QwacStatus should be created.
EXPECT_FALSE(GetCurrentPageQwacStatus());
EXPECT_FALSE(VisibleSecurityStateTwoQwac());
EXPECT_EQ(1, main_page_handler.request_count());
ExpectHistogramSample(histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::
kNonrelativeQwacLinkUrl);
}
IN_PROC_BROWSER_TEST_F(QwacWebContentsObserverBrowserTest,
TestQwacLoadHttpError) {
ServePageWithQwacHeaderHandler main_page_handler(embedded_https_test_server(),
"/main", "/qwac");
ServeResponseHandler qwac_handler(embedded_https_test_server(), "/qwac",
"404 content", "text/html",
net::HTTP_NOT_FOUND);
auto test_server_handle = embedded_https_test_server().StartAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
base::HistogramTester histograms;
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_https_test_server().GetURL("www.example.com", "/main")));
auto* status = GetCurrentPageQwacStatus();
ASSERT_TRUE(status);
ASSERT_TRUE(WaitForStatusFinished(status));
EXPECT_FALSE(status->verified_2qwac_cert());
EXPECT_EQ(1, qwac_handler.request_count());
EXPECT_FALSE(VisibleSecurityStateTwoQwac());
ExpectHistogramSample(
histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::kDownloadFailed);
}
IN_PROC_BROWSER_TEST_F(QwacWebContentsObserverBrowserTest, NotFetchedForHttp) {
ServePageWithQwacHeaderHandler main_page_handler(*embedded_test_server(),
"/main", "/qwac");
embedded_test_server()->RegisterRequestHandler(
base::BindRepeating(&FailTestIfPathRequested, "/qwac"));
auto test_server_handle = embedded_test_server()->StartAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
base::HistogramTester histograms;
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_test_server()->GetURL("www.example.com", "/main")));
EXPECT_FALSE(GetCurrentPageQwacStatus());
EXPECT_FALSE(VisibleSecurityStateTwoQwac());
EXPECT_EQ(1, main_page_handler.request_count());
ExpectNoHistogramSample(histograms);
}
IN_PROC_BROWSER_TEST_F(QwacWebContentsObserverBrowserTest, TestInvalidBinding) {
ASSERT_TRUE(embedded_https_test_server().InitializeAndListen());
net::TwoQwacCertBindingBuilder binding_builder;
auto tls_leaf = embedded_https_test_server().GetCertificate();
ASSERT_TRUE(tls_leaf);
binding_builder.SetBoundCerts(
{std::string(base::as_string_view(tls_leaf->cert_span()))});
// Serve a 2-QWAC with an invalid signature.
ServeResponseHandler qwac_handler(
embedded_https_test_server(), "/qwac",
binding_builder.GetJWSWithInvalidSignature(), "application/jose");
ServePageWithQwacHeaderHandler main_page_handler(embedded_https_test_server(),
"/main", "/qwac");
auto test_server_handle =
embedded_https_test_server().StartAcceptingConnectionsAndReturnHandle();
ASSERT_TRUE(test_server_handle.is_valid());
InstallCRSUpdate(
/*root_cert=*/embedded_https_test_server().GetRoot().get(),
/*eutl_cert=*/
binding_builder.GetRootBuilder()->GetX509Certificate().get());
base::HistogramTester histograms;
EXPECT_TRUE(content::NavigateToURL(
web_contents(),
embedded_https_test_server().GetURL("www.example.com", "/main")));
auto* status = GetCurrentPageQwacStatus();
ASSERT_TRUE(status);
ASSERT_TRUE(WaitForStatusFinished(status));
EXPECT_FALSE(status->verified_2qwac_cert());
ASSERT_TRUE(status->tls_cert());
EXPECT_TRUE(status->tls_cert()->EqualsExcludingChain(tls_leaf.get()));
EXPECT_EQ(1, main_page_handler.request_count());
EXPECT_EQ(1, qwac_handler.request_count());
ExpectHistogramSample(histograms,
QwacWebContentsObserver::QwacLinkProcessingResult::
k2QwacVerificationFailed);
}
// TODO(crbug.com/392931069): Test that qwac is not fetched after clicking
// through HTTPS error.
// TODO(crbug.com/392931069): Test that qwac requests shows up in netlog?
} // namespace
|