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
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/browsertest_util.h"
#include "chrome/browser/extensions/chrome_content_verifier_delegate.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "extensions/browser/computed_hashes.h"
#include "extensions/browser/content_verifier/test_utils.h"
#include "extensions/browser/extension_file_task_runner.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/test_extension_registry_observer.h"
#include "extensions/common/file_util.h"
#include "services/network/public/cpp/features.h"
namespace extensions {
namespace {
// Specifies the content verification mode.
enum ContentVerificationMode {
// Uses ContentVerifierDelegate::ENFORCE mode.
kEnforce,
// Uses ContentVerifierDelegate::ENFORCE_STRICT mode.
kEnforceStrict
};
} // namespace
// Tests content verification's hash fetch behavior and its implication on
// verification failure in different verification modes (enforce and
// enforce_strict).
// TODO(lazyboy): Add assertions for checking verified_contents.json file's
// validity after running each test.
class ContentVerifierHashTest
: public ExtensionBrowserTest,
public testing::WithParamInterface<ContentVerificationMode> {
public:
ContentVerifierHashTest() = default;
ContentVerifierHashTest(const ContentVerifierHashTest&) = delete;
ContentVerifierHashTest& operator=(const ContentVerifierHashTest&) = delete;
~ContentVerifierHashTest() override = default;
enum TamperResourceType {
kTamperRequestedResource,
kTamperNotRequestedResource
};
// ExtensionBrowserTest:
bool ShouldEnableContentVerification() override { return true; }
void SetUp() override {
// Override content verification mode before ChromeExtensionSystem
// initializes ChromeContentVerifierDelegate.
ChromeContentVerifierDelegate::SetDefaultModeForTesting(
uses_enforce_strict_mode()
? ChromeContentVerifierDelegate::VerifyInfo::Mode::ENFORCE_STRICT
: ChromeContentVerifierDelegate::VerifyInfo::Mode::ENFORCE);
ExtensionBrowserTest::SetUp();
}
void TearDown() override {
ExtensionBrowserTest::TearDown();
ChromeContentVerifierDelegate::SetDefaultModeForTesting(std::nullopt);
}
void TearDownOnMainThread() override {
url_loader_interceptor_.reset();
ExtensionBrowserTest::TearDownOnMainThread();
}
bool uses_enforce_strict_mode() {
return GetParam() == ContentVerificationMode::kEnforceStrict;
}
bool uses_enforce_mode() {
return GetParam() == ContentVerificationMode::kEnforce;
}
void DisableHashFetching() { hash_fetching_disabled_ = true; }
testing::AssertionResult InstallDefaultResourceExtension() {
LOG(INFO) << "InstallDefaultResourceExtension";
return InstallExtension(kHasDefaultResource);
}
testing::AssertionResult InstallNoDefaultResourceExtension() {
LOG(INFO) << "InstallNoDefaultResourceExtension";
return InstallExtension(kDoesNotHaveDefaultResource);
}
void DisableExtension() { ExtensionBrowserTest::DisableExtension(id()); }
testing::AssertionResult DeleteVerifiedContents() {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath verified_contents_path =
file_util::GetVerifiedContentsPath(info_->extension_root);
if (!base::PathExists(verified_contents_path)) {
return testing::AssertionFailure()
<< "Could not find verified_contents.json.";
}
// Delete verified_contents.json:
if (!base::DeleteFile(verified_contents_path)) {
return testing::AssertionFailure()
<< "Could not delete verified_contents.json.";
}
return testing::AssertionSuccess();
}
bool HasComputedHashes() {
base::ScopedAllowBlockingForTesting allow_blocking;
return base::PathExists(
file_util::GetComputedHashesPath(info_->extension_root));
}
testing::AssertionResult DeleteComputedHashes() {
LOG(INFO) << "Deleting computed_hashes.json";
base::ScopedAllowBlockingForTesting allow_blocking;
if (!HasComputedHashes()) {
return testing::AssertionFailure()
<< "Could not find computed_hashes.json for deletion. "
<< "Make sure the previous steps created a "
<< "computed_hashes.json, otherwise tests might fail/flake";
}
base::FilePath computed_hashes_path =
file_util::GetComputedHashesPath(info_->extension_root);
if (!base::DeleteFile(computed_hashes_path)) {
return testing::AssertionFailure()
<< "Error deleting computed_hashes.json.";
}
return testing::AssertionSuccess();
}
testing::AssertionResult TamperComputedHashes() {
LOG(INFO) << "Tampering computed_hashes.json";
base::ScopedAllowBlockingForTesting allow_blocking;
if (!HasComputedHashes()) {
return testing::AssertionFailure()
<< "Could not find computed_hashes.json for tampering.";
}
base::FilePath computed_hashes_path =
file_util::GetComputedHashesPath(info_->extension_root);
std::string extra = R"({hello:"world"})";
if (!base::AppendToFile(computed_hashes_path, extra)) {
return testing::AssertionFailure()
<< "Could not tamper computed_hashes.json";
}
return testing::AssertionSuccess();
}
testing::AssertionResult TamperResource(TamperResourceType type) {
const std::string resource_to_tamper =
type == kTamperRequestedResource ? "background.js" : "script.js";
base::ScopedAllowBlockingForTesting allow_blocking;
// Modify content of a resource if this test requested that, time the
// extension loads, hash fetch will discover content verification failure
// due to hash mismatch.
std::string extra = "some_extra_function_call();";
base::FilePath real_path =
info_->extension_root.AppendASCII(resource_to_tamper);
if (!base::AppendToFile(real_path, extra)) {
return testing::AssertionFailure()
<< "Could not tamper " << resource_to_tamper << ".";
}
return testing::AssertionSuccess();
}
const ExtensionId& id() const { return info_->extension_id; }
void EnableExtensionAndWaitForCompletion(bool expect_disabled) {
LOG(INFO) << "EnableExtensionAndWaitForCompletion: expect_disabled = "
<< expect_disabled;
// Only observe ContentVerifyJob when necessary. This is because
// ContentVerifyJob's callback and ContentVerifyJob::OnExtensionLoad's
// callbacks can be race-y.
std::unique_ptr<TestContentVerifyJobObserver> job_observer;
const bool needs_to_observe_content_verify_job =
// If the test wouldn't disable the extension, extensions with
// default resource(s) will always see at at least one ContentVerifyJob
// to a default resource (background.js).
info_->type == kHasDefaultResource && !expect_disabled;
if (needs_to_observe_content_verify_job) {
LOG(INFO) << "Observing ContentVerifyJob";
job_observer = std::make_unique<TestContentVerifyJobObserver>();
using Result = TestContentVerifyJobObserver::Result;
job_observer->ExpectJobResult(
id(), base::FilePath(FILE_PATH_LITERAL("background.js")),
Result::SUCCESS);
}
TestExtensionRegistryObserver registry_observer(
ExtensionRegistry::Get(profile()), id());
VerifierObserver verifier_observer;
{
EnableExtension(id());
registry_observer.WaitForExtensionLoaded();
}
verifier_observer.EnsureFetchCompleted(id());
LOG(INFO) << "Verifier observer has seen FetchComplete";
if (job_observer) {
LOG(INFO) << "ContentVerifyJobObserver, wait for expected job";
job_observer->WaitForExpectedJobs();
LOG(INFO) << "ContentVerifyJobObserver, completed expected job";
}
}
bool ExtensionIsDisabledForCorruption() {
const Extension* extension =
ExtensionRegistry::Get(profile())->disabled_extensions().GetByID(id());
if (!extension)
return false;
ExtensionPrefs* prefs = ExtensionPrefs::Get(profile());
// Make sure the extension got disabled due to corruption (and only due to
// corruption).
return prefs->HasOnlyDisableReason(id(), disable_reason::DISABLE_CORRUPTED);
}
bool ExtensionIsEnabled() {
return ExtensionRegistry::Get(profile())->enabled_extensions().Contains(
id());
}
bool HasValidComputedHashes() {
base::ScopedAllowBlockingForTesting allow_blocking;
ComputedHashes::Status computed_hashes_status;
return ComputedHashes::CreateFromFile(
file_util::GetComputedHashesPath(info_->extension_root),
&computed_hashes_status) != std::nullopt;
}
bool HasValidVerifiedContents() {
base::ScopedAllowBlockingForTesting allow_blocking;
base::FilePath verified_contents_path =
file_util::GetVerifiedContentsPath(info_->extension_root);
if (!base::PathExists(verified_contents_path)) {
ADD_FAILURE() << "Could not find verified_contents.json.";
return false;
}
std::string contents;
if (!base::ReadFileToString(verified_contents_path, &contents)) {
ADD_FAILURE() << "Could not read verified_contents.json.";
return false;
}
return verified_contents_contents_ == contents;
}
private:
enum ExtensionType {
// An extension (has_default_resource.crx) that by default requests a
// resource in it during ExtensionLoad.
kHasDefaultResource,
// An extension (no_default_resources.crx) that doesn't request any
// resource during ExtensionLoad.
kDoesNotHaveDefaultResource
};
struct ExtensionInfo {
ExtensionId extension_id;
base::FilePath extension_root;
base::Version version;
ExtensionType type;
ExtensionInfo(const ExtensionId& extension_id,
const base::FilePath& extension_root,
const base::Version& version,
ExtensionType type)
: extension_id(extension_id),
extension_root(extension_root),
version(version),
type(type) {}
ExtensionInfo(const Extension* extension, ExtensionType type)
: ExtensionInfo(extension->id(),
extension->path(),
extension->version(),
type) {}
};
// Stores verified_contents.json into a temp file.
bool CopyVerifiedContents(base::FilePath* out_path) {
base::ScopedAllowBlockingForTesting allow_blocking;
if (!temp_dir_.CreateUniqueTempDir()) {
ADD_FAILURE() << "Could not create temp dir for test.";
return false;
}
base::FilePath verified_contents_path =
file_util::GetVerifiedContentsPath(info_->extension_root);
if (!base::PathExists(verified_contents_path)) {
ADD_FAILURE() << "Could not find verified_contents.json for copying.";
return false;
}
base::FilePath destination = temp_dir_.GetPath();
*out_path = destination.Append(FILE_PATH_LITERAL("copy.json"));
if (!base::CopyFile(verified_contents_path, *out_path)) {
ADD_FAILURE() << "Could not copy verified_contents.json to a temp dir.";
return false;
}
if (!base::ReadFileToString(verified_contents_path,
&verified_contents_contents_)) {
ADD_FAILURE() << "Could not read verified_contents.json.";
return false;
}
return true;
}
// Installs test extension that is copied from the webstore with actual
// signatures.
testing::AssertionResult InstallExtension(ExtensionType type) {
// This observer will make sure content hash read and computed_hashes.json
// writing is complete before we proceed.
VerifierObserver verifier_observer;
const std::string crx_relative_path =
type == kHasDefaultResource
? "content_verifier/has_default_resource.crx"
: "content_verifier/no_default_resources.crx";
// These test extension is copied from the webstore that has actual
// signatures.
const Extension* extension = InstallExtensionFromWebstore(
test_data_dir_.AppendASCII(crx_relative_path), 1);
if (!extension) {
return testing::AssertionFailure()
<< "Could not install extension: " << crx_relative_path;
}
const ExtensionId& extension_id = extension->id();
verifier_observer.EnsureFetchCompleted(extension_id);
info_ = std::make_unique<ExtensionInfo>(extension, type);
// Set up the interceptor functor and data needed by it.
if (!hash_fetching_disabled_ && !InstallInterceptor())
return testing::AssertionFailure() << "Failed to install interceptor.";
return testing::AssertionSuccess();
}
bool InstallInterceptor() {
if (url_loader_interceptor_) {
testing::AssertionFailure() << "Already created interceptor.";
return false;
}
SetUpInterceptorData();
auto interceptor_function =
[](GURL* fetch_url, base::FilePath* file_path,
content::URLLoaderInterceptor::RequestParams* params) {
GURL url = params->url_request.url;
if (url == *fetch_url) {
base::ScopedAllowBlockingForTesting allow_io;
std::string contents;
CHECK(base::ReadFileToString(*file_path, &contents));
content::URLLoaderInterceptor::WriteResponse(
std::string(), contents, params->client.get());
return true;
}
return false;
};
url_loader_interceptor_ = std::make_unique<content::URLLoaderInterceptor>(
base::BindRepeating(interceptor_function, &fetch_url_,
&copied_verified_contents_path_));
return true;
}
bool SetUpInterceptorData() {
// Use stored copy of verified_contents.json as hash fetch response.
if (!CopyVerifiedContents(&copied_verified_contents_path_)) {
ADD_FAILURE() << "Could not copy verified_contents.json.";
return false;
}
ExtensionSystem* system = ExtensionSystem::Get(profile());
fetch_url_ = system->content_verifier()->GetSignatureFetchUrlForTest(
id(), info_->version);
return true;
}
std::unique_ptr<content::URLLoaderInterceptor> url_loader_interceptor_;
base::ScopedTempDir temp_dir_;
base::FilePath copied_verified_contents_path_;
GURL fetch_url_;
// Information about the loaded extension.
std::unique_ptr<ExtensionInfo> info_;
// Contents of verified_contents.json (if available).
std::string verified_contents_contents_;
bool hash_fetching_disabled_ = false;
};
// Tests that corruption of a requested extension resource always disables the
// extension.
// Flaky test. See crbug.com/1276043.
IN_PROC_BROWSER_TEST_P(ContentVerifierHashTest,
DISABLED_TamperRequestedResourceKeepComputedHashes) {
ASSERT_TRUE(InstallDefaultResourceExtension());
DisableExtension();
// Delete verified_contents.json to force a hash fetch on next load.
EXPECT_TRUE(DeleteVerifiedContents());
// computed_hashes.json should remain valid.
EXPECT_TRUE(HasComputedHashes());
// Tamper an extension resource that will be requested on next load.
EXPECT_TRUE(TamperResource(kTamperRequestedResource));
TestExtensionRegistryObserver registry_observer(
ExtensionRegistry::Get(profile()), id());
// Since we tampered with the resource, content verification should
// disable the extension, both in "enforce_strict" and "enforce" mode.
EnableExtensionAndWaitForCompletion(true /* expect_disabled */);
EXPECT_TRUE(registry_observer.WaitForExtensionUnloaded());
EXPECT_TRUE(ExtensionIsDisabledForCorruption());
// Expect a valid verified_contents.json file at the end.
EXPECT_TRUE(HasValidVerifiedContents());
}
// Tests that tampering a resource that will be requested by the extension and
// deleting computed_hashes.json will always disable the extension.
IN_PROC_BROWSER_TEST_P(ContentVerifierHashTest,
TamperRequestedResourceDeleteComputedHashes) {
ASSERT_TRUE(InstallDefaultResourceExtension());
DisableExtension();
// Delete verified_contents.json to force a hash fetch on next load.
EXPECT_TRUE(DeleteVerifiedContents());
// Delete computed_hashes.json.
EXPECT_TRUE(DeleteComputedHashes());
// Tamper an extension resource that will be requested on next load.
EXPECT_TRUE(TamperResource(kTamperRequestedResource));
TestExtensionRegistryObserver registry_observer(
ExtensionRegistry::Get(profile()), id());
// Since we tampered with the resource, content verification should
// disable the extension, both in "enforce_strict" and "enforce" mode.
EnableExtensionAndWaitForCompletion(true /* expect_disabled */);
EXPECT_TRUE(registry_observer.WaitForExtensionUnloaded());
EXPECT_TRUE(ExtensionIsDisabledForCorruption());
// Expect a valid computed_hashes.json file at the end, the verification
// failure must have used this file to detect corruption.
EXPECT_TRUE(HasValidComputedHashes());
// Expect a valid verified_contents.json file at the end.
EXPECT_TRUE(HasValidVerifiedContents());
}
// Tests that tampering a resource that will be requested by the extension and
// tampering computed_hashes.json will always disable the extension.
// TODO(crbug.com/40810537): Flaky.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_TamperRequestedResourceTamperComputedHashes \
DISABLED_TamperRequestedResourceTamperComputedHashes
#else
#define MAYBE_TamperRequestedResourceTamperComputedHashes \
TamperRequestedResourceTamperComputedHashes
#endif
IN_PROC_BROWSER_TEST_P(ContentVerifierHashTest,
MAYBE_TamperRequestedResourceTamperComputedHashes) {
ASSERT_TRUE(InstallDefaultResourceExtension());
DisableExtension();
// Delete verified_contents.json to force a hash fetch on next load.
EXPECT_TRUE(DeleteVerifiedContents());
// Tamper computed_hashes.json.
EXPECT_TRUE(TamperComputedHashes());
// Tamper an extension resource that will be requested on next load.
EXPECT_TRUE(TamperResource(kTamperRequestedResource));
TestExtensionRegistryObserver registry_observer(
ExtensionRegistry::Get(profile()), id());
// Since we tampered with the resource, content verification should
// disable the extension, both in "enforce_strict" and "enforce" mode.
EnableExtensionAndWaitForCompletion(true /* expect_disabled */);
EXPECT_TRUE(registry_observer.WaitForExtensionUnloaded());
EXPECT_TRUE(ExtensionIsDisabledForCorruption());
// Expect a valid computed_hashes.json file at the end, the verification
// failure must have used this file to detect corruption.
EXPECT_TRUE(HasValidComputedHashes());
// Expect a valid verified_contents.json file at the end.
EXPECT_TRUE(HasValidVerifiedContents());
}
// Tests hash fetch failure scenario with an extension that requests resource(s)
// by default.
IN_PROC_BROWSER_TEST_P(ContentVerifierHashTest,
FetchFailureWithDefaultResourceExtension) {
// Do *not* install any hash fetch interceptor, so that hash fetch after
// reload fails.
DisableHashFetching();
ASSERT_TRUE(InstallDefaultResourceExtension());
DisableExtension();
// Delete verified_contents.json to force a hash fetch on next load.
EXPECT_TRUE(DeleteVerifiedContents());
// computed_hashes.json and extension resource(s) aren't touched since they do
// not matter as hash fetch will fail.
// In "enforce_strict" mode, hash fetch failures will cause the extension to
// be disabled. Implementation-wise, this happens because the requested
// resource's ContentVerifyJob will result in failure, not because the hash
// fetch failed. See https://crbug.com/819818 for details.
//
// In "enforce" mode, the extension won't be disabled. However, since we
// request a resource (background.js), its corresponding ContentVerifyJob will
// attempt to fetch hash and that job will also fail. In order to achieve
// determinism in this case, also observe a ContentVerifyJob that will fail.
const bool expect_disabled = uses_enforce_strict_mode();
// Similar to EnableExtensionAndWaitForCompletion, but also forces a
// ContentVerifyJob observer in "enforce" mode.
// Instead of generalizing this oddball expectation into
// EnableExtensionAndWaitForCompletion, provide the implementation right here
// in the test body.
{
LOG(INFO) << "EnableExtensionAndWaitForCompletion: expect_disabled = "
<< expect_disabled;
ExtensionId extension_id = id();
// Only observe ContentVerifyJob when necessary. This is because
// ContentVerifyJob's callback and ContentVerifyJob::OnExtensionLoad's
// callbacks can be race-y.
std::unique_ptr<TestContentVerifyJobObserver> job_observer;
if (uses_enforce_mode()) {
// In "enforce" mode, we expect to see a job completion (and its failure).
job_observer = std::make_unique<TestContentVerifyJobObserver>();
using Result = TestContentVerifyJobObserver::Result;
job_observer->ExpectJobResult(
extension_id,
// This extension has default resource (background.js), so it must
// request it.
base::FilePath(FILE_PATH_LITERAL("background.js")), Result::FAILURE);
}
TestExtensionRegistryObserver registry_observer(
ExtensionRegistry::Get(profile()), extension_id);
VerifierObserver verifier_observer;
{
EnableExtension(extension_id);
registry_observer.WaitForExtensionLoaded();
}
verifier_observer.EnsureFetchCompleted(extension_id);
LOG(INFO) << "Verifier observer has seen FetchComplete";
if (job_observer) {
LOG(INFO) << "ContentVerifyJobObserver, wait for expected job";
job_observer->WaitForExpectedJobs();
LOG(INFO) << "ContentVerifyJobObserver, completed expected job";
}
if (expect_disabled)
EXPECT_TRUE(registry_observer.WaitForExtensionUnloaded());
}
EXPECT_TRUE(expect_disabled ? ExtensionIsDisabledForCorruption()
: ExtensionIsEnabled());
}
// Tests that hash fetch failure for loading an extension that doesn't request
// any resource by default will not be disabled.
IN_PROC_BROWSER_TEST_P(ContentVerifierHashTest,
FetchFailureWithNoDefaultResourceDoesNotDisable) {
// Do *not* install any hash fetch interceptor, so that hash fetch after
// reload fails.
DisableHashFetching();
ASSERT_TRUE(InstallNoDefaultResourceExtension());
DisableExtension();
// Delete verified_contents.json to force a hash fetch on next load.
EXPECT_TRUE(DeleteVerifiedContents());
// computed_hashes.json and extension resource(s) aren't touched since they do
// not matter as hash fetch will fail.
// If the extension didn't explicitly request any resources, then there will
// not be any content verification failures.
const bool expect_disabled = false;
// TODO(lazyboy): https://crbug.com/819818: "enforce_strict" mode should
// disable the extension.
// const bool expect_disabled = uses_enforce_strict_mode();
EnableExtensionAndWaitForCompletion(expect_disabled);
EXPECT_TRUE(expect_disabled ? ExtensionIsDisabledForCorruption()
: ExtensionIsEnabled());
}
// Tests the behavior of tampering an extension resource that is not requested
// by default and without modifying computed_hashes.json.
IN_PROC_BROWSER_TEST_P(ContentVerifierHashTest,
TamperNotRequestedResourceKeepComputedHashes) {
ASSERT_TRUE(InstallDefaultResourceExtension());
DisableExtension();
// Delete verified_contents.json to force a hash fetch on next load.
EXPECT_TRUE(DeleteVerifiedContents());
// computed_hashes.json should remain valid.
EXPECT_TRUE(HasComputedHashes());
// Tamper an extension resource that will *not* be requested on next load.
EXPECT_TRUE(TamperResource(kTamperNotRequestedResource));
// We tampered a resource that is not requested by the extension. Keeping
// computed_hashes.json will not compute new compute computed_hashes.json, and
// we will not discover the tampered hash. So the extension won't be disabled.
//
// TODO(lazyboy): http://crbug.com/819832: We fetched a new
// verified_contents.json in this case. However, if we had recomputed
// computed_hashes.json we would have discovered the tampered resource's hash
// mismatch. Fix.
const bool expect_disabled = false;
EnableExtensionAndWaitForCompletion(expect_disabled);
EXPECT_TRUE(ExtensionIsEnabled());
// Expect a valid verified_contents.json file at the end.
EXPECT_TRUE(HasValidVerifiedContents());
}
// Tests the behavior of loading an extension without any default resource
// request and deleting its computed_hashes.json before fetching hashes.
IN_PROC_BROWSER_TEST_P(ContentVerifierHashTest,
TamperNoResourceExtensionDeleteComputedHashes) {
ASSERT_TRUE(InstallNoDefaultResourceExtension());
DisableExtension();
// Delete verified_contents.json to force a hash fetch on next load.
EXPECT_TRUE(DeleteVerifiedContents());
// Delete computed_hashes.json.
EXPECT_TRUE(DeleteComputedHashes());
// Tamper an extension resource that will *not* be requested on next load.
EXPECT_TRUE(TamperResource(kTamperNotRequestedResource));
TestExtensionRegistryObserver registry_observer(
ExtensionRegistry::Get(profile()), id());
// The deletion of computed_hashes.json forces its recomputation and disables
// the extension.
EnableExtensionAndWaitForCompletion(true /* expect_disabled */);
EXPECT_TRUE(registry_observer.WaitForExtensionUnloaded());
EXPECT_TRUE(ExtensionIsDisabledForCorruption());
// Expect a valid computed_hashes.json file at the end, the verification
// failure must have used this file to detect corruption.
EXPECT_TRUE(HasValidComputedHashes());
// Expect a valid verified_contents.json file at the end.
EXPECT_TRUE(HasValidVerifiedContents());
}
// Tests the behavior of loading an extension without any default resource
// request and keeping its computed_hashes.json.
IN_PROC_BROWSER_TEST_P(ContentVerifierHashTest,
TamperNoResourceExtensionKeepComputedHashes) {
ASSERT_TRUE(InstallNoDefaultResourceExtension());
DisableExtension();
// Delete verified_contents.json to force a hash fetch on next load.
EXPECT_TRUE(DeleteVerifiedContents());
// computed_hashes.json should remain valid.
EXPECT_TRUE(HasComputedHashes());
// Tamper an extension resource that will *not* be requested on next load.
EXPECT_TRUE(TamperResource(kTamperNotRequestedResource));
// Not modifying computed_hashes.json will not trigger any hash computation
// at OnExtensionLoad, so we won't discover any hash mismatches.
EnableExtensionAndWaitForCompletion(false /* expect_disabled */);
EXPECT_TRUE(ExtensionIsEnabled());
// Expect a valid verified_contents.json file at the end.
EXPECT_TRUE(HasValidVerifiedContents());
}
// Tests the behavior of loading an extension without any default resource
// request and tampering its computed_hashes.json.
IN_PROC_BROWSER_TEST_P(ContentVerifierHashTest,
TamperNoResourceExtensionTamperComputedHashes) {
ASSERT_TRUE(InstallNoDefaultResourceExtension());
DisableExtension();
// Delete verified_contents.json to force a hash fetch on next load.
EXPECT_TRUE(DeleteVerifiedContents());
// Tamper computed_hashes.json.
EXPECT_TRUE(TamperComputedHashes());
// Tamper an extension resource that will *not* be requested on next load.
EXPECT_TRUE(TamperResource(kTamperNotRequestedResource));
// Tampering computed_hashes.json will not trigger any hash computation
// at OnExtensionLoad, so we won't discover any hash mismatches.
// TODO(lazyboy): Consider fixing this, see http://crbug.com/819832 for
// details.
EnableExtensionAndWaitForCompletion(false /* expect_disabled */);
EXPECT_TRUE(ExtensionIsEnabled());
// Because we didn't do any hash computation, expect computed_hashes.json to
// still remain invalid.
EXPECT_FALSE(HasValidComputedHashes());
// Expect a valid verified_contents.json file at the end.
EXPECT_TRUE(HasValidVerifiedContents());
}
// Tests the behavior of loading a default resource extension with tampering
// an extension resource that is not requested by default and without modifying
// computed_hashes.json.
IN_PROC_BROWSER_TEST_P(
ContentVerifierHashTest,
DefaultRequestExtensionTamperNotRequestedResourceKeepComputedHashes) {
ASSERT_TRUE(InstallDefaultResourceExtension());
DisableExtension();
// Delete verified_contents.json to force a hash fetch on next load.
EXPECT_TRUE(DeleteVerifiedContents());
// computed_hashes.json should remain valid.
EXPECT_TRUE(HasComputedHashes());
// Tamper an extension resource that will *not* be requested on next load.
EXPECT_TRUE(TamperResource(kTamperNotRequestedResource));
// TODO(lazyboy): Not modifying the computed_hashes.json file doesn't prompt
// a hash recomputation and the requested (not-tampered) resource's
// corresponding ContentVerifyJob succeeds because that resource's hash
// remains fine. Therefore, the extension remains enabled. Consider disabling
// the extension in this case: https://crbug.com/819832.
EnableExtensionAndWaitForCompletion(false /* expect_disabled */);
EXPECT_TRUE(ExtensionIsEnabled());
// Expect a valid verified_contents.json file at the end.
EXPECT_TRUE(HasValidVerifiedContents());
}
// Tests the behavior of loading a default resource extension with tampering
// an extension resource that is not requested by default and tampering
// computed_hashes.json.
// TODO(crbug.com/40810776): Flaky.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_DefaultRequestExtensionTamperNotRequestedResourceTamperComputedHashes \
DISABLED_DefaultRequestExtensionTamperNotRequestedResourceTamperComputedHashes
#else
#define MAYBE_DefaultRequestExtensionTamperNotRequestedResourceTamperComputedHashes \
DefaultRequestExtensionTamperNotRequestedResourceTamperComputedHashes
#endif
IN_PROC_BROWSER_TEST_P(
ContentVerifierHashTest,
MAYBE_DefaultRequestExtensionTamperNotRequestedResourceTamperComputedHashes) {
ASSERT_TRUE(InstallDefaultResourceExtension());
DisableExtension();
// Delete verified_contents.json to force a hash fetch on next load.
EXPECT_TRUE(DeleteVerifiedContents());
// Tamper computed_hashes.json.
EXPECT_TRUE(TamperComputedHashes());
// Tamper an extension resource that will *not* be requested on next load.
EXPECT_TRUE(TamperResource(kTamperNotRequestedResource));
TestExtensionRegistryObserver registry_observer(
ExtensionRegistry::Get(profile()), id());
// Subtle: tampering computed_hashes.json (by itself) will not trigger any
// hash computation or failure during OnExtensionLoad. However, the default
// resource request (that isn't tampered) will prompt a hash read that will
// fail due to the tampered computed_hashes.json.
EnableExtensionAndWaitForCompletion(true /* expect_disabled */);
EXPECT_TRUE(registry_observer.WaitForExtensionUnloaded());
EXPECT_TRUE(ExtensionIsDisabledForCorruption());
// Expect a valid computed_hashes.json file at the end, the verification
// failure must have used this file to detect corruption.
EXPECT_TRUE(HasValidComputedHashes());
// Expect a valid verified_contents.json file at the end.
EXPECT_TRUE(HasValidVerifiedContents());
}
// Tests the behavior of loading a default resource extension with tampering
// an extension resource that is not requested by default and deleting
// computed_hashes.json.
IN_PROC_BROWSER_TEST_P(
ContentVerifierHashTest,
DefaultRequestExtensionTamperNotRequestedResourceDeleteComputedHashes) {
ASSERT_TRUE(InstallDefaultResourceExtension());
DisableExtension();
// Delete verified_contents.json to force a hash fetch on next load.
EXPECT_TRUE(DeleteVerifiedContents());
// Delete computed_hashes.json.
EXPECT_TRUE(DeleteComputedHashes());
// Tamper an extension resource that will *not* be requested on next load.
EXPECT_TRUE(TamperResource(kTamperNotRequestedResource));
TestExtensionRegistryObserver registry_observer(
ExtensionRegistry::Get(profile()), id());
// The deletion of computed_hashes.json will trigger hash recomputation and
// the file's regeneration. This will discover the resource tampering and
// disable the extension.
EnableExtensionAndWaitForCompletion(true /* expect_disabled */);
EXPECT_TRUE(registry_observer.WaitForExtensionUnloaded());
EXPECT_TRUE(ExtensionIsDisabledForCorruption());
// Expect a valid computed_hashes.json file at the end, the verification
// failure must have used this file to detect corruption.
EXPECT_TRUE(HasValidComputedHashes());
// Expect a valid verified_contents.json file at the end.
EXPECT_TRUE(HasValidVerifiedContents());
}
INSTANTIATE_TEST_SUITE_P(FetchBehaviorTests,
ContentVerifierHashTest,
testing::Values(kEnforce, kEnforceStrict));
} // namespace extensions
|