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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/permissions/site_permissions_helper.h"
#include <string_view>
#include "base/run_loop.h"
#include "chrome/browser/extensions/blocked_action_waiter.h"
#include "chrome/browser/extensions/extension_action_runner.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/permissions/permissions_test_util.h"
#include "chrome/browser/extensions/permissions/scripting_permissions_modifier.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/browsertest_util.h"
#include "extensions/browser/permissions_manager.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/manifest_handlers/content_scripts_handler.h"
#include "extensions/common/manifest_handlers/permissions_parser.h"
#include "extensions/common/mojom/run_location.mojom-shared.h"
#include "extensions/test/extension_test_message_listener.h"
#include "net/dns/mock_host_resolver.h"
#include "testing/gtest/include/gtest/gtest.h"
static_assert(BUILDFLAG(ENABLE_EXTENSIONS_CORE));
namespace extensions {
namespace {
using UserSiteAccess = PermissionsManager::UserSiteAccess;
} // namespace
class SitePermissionsHelperBrowserTest : public ExtensionBrowserTest {
public:
SitePermissionsHelperBrowserTest() = default;
void SetUpOnMainThread() override {
ExtensionBrowserTest::SetUpOnMainThread();
// Loads an extension that can run on every page at document start. Then
// loads a test page and confirm it is running on the page.
ASSERT_TRUE(embedded_test_server()->Start());
extension_ = LoadExtension(
test_data_dir_.AppendASCII("blocked_actions/content_scripts"));
ASSERT_TRUE(extension_);
// Navigate to a page where the extension wants to run.
original_url_ = embedded_test_server()->GetURL("/simple.html");
ASSERT_TRUE(NavigateToURL(original_url_));
ASSERT_TRUE(content::WaitForLoadStop(GetActiveWebContents()));
original_nav_id_ =
active_nav_controller().GetLastCommittedEntry()->GetUniqueID();
// The extension should want to run on the page, script should have
// injected, and should have "on all sites" access.
ASSERT_TRUE(active_action_runner());
ASSERT_FALSE(active_action_runner()->WantsToRun(extension_));
ASSERT_EQ(
ContentScriptsInfo::GetContentScripts(extension_)[0]->run_location(),
mojom::RunLocation::kDocumentStart);
ASSERT_TRUE(GetActiveWebContents());
ASSERT_TRUE(ContentScriptInjected());
permissions_helper_ = std::make_unique<SitePermissionsHelper>(profile());
permissions_manager_ = PermissionsManager::Get(profile());
ASSERT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnAllSites);
}
void TearDownOnMainThread() override {
ExtensionBrowserTest::TearDownOnMainThread();
// Extension is created as a scoped_refptr so no need to delete.
extension_ = nullptr;
// Avoid dangling pointer.
permissions_manager_ = nullptr;
// Avoid dangling pointer to profile.
permissions_helper_.reset(nullptr);
}
// The content script for the extension was successfully injected into the
// page.
bool ContentScriptInjected();
// Extension has blocked actions that are pending to run.
bool ExtensionWantsToRun();
bool ReloadPageAndWaitForLoad();
bool WaitForReloadToFinish();
content::NavigationController& active_nav_controller() {
return GetActiveWebContents()->GetController();
}
ExtensionActionRunner* active_action_runner() {
return ExtensionActionRunner::GetForWebContents(GetActiveWebContents());
}
protected:
int original_nav_id_{0};
GURL original_url_;
raw_ptr<const Extension> extension_;
std::unique_ptr<SitePermissionsHelper> permissions_helper_;
raw_ptr<PermissionsManager> permissions_manager_;
};
bool SitePermissionsHelperBrowserTest::ContentScriptInjected() {
return browsertest_util::DidChangeTitle(*GetActiveWebContents(),
/*original_title=*/u"OK",
/*changed_title=*/u"success");
}
bool SitePermissionsHelperBrowserTest::ExtensionWantsToRun() {
return active_action_runner()->WantsToRun(extension_);
}
bool SitePermissionsHelperBrowserTest::ReloadPageAndWaitForLoad() {
GetActiveWebContents()->GetController().Reload(content::ReloadType::NORMAL,
false);
return WaitForReloadToFinish();
}
bool SitePermissionsHelperBrowserTest::WaitForReloadToFinish() {
// This is needed in the instance where on site -> on-click revokes
// permissions. This is because when testing we run
// `ExtensionActionRunner::accept_bubble_for_testing(true)` which causes
// `ExtensionActionRunner::ShowReloadPageBubble(...)` to run the reload with
// a `base::SingleThreadTaskRunner` so we must wait for that to complete.
base::RunLoop().RunUntilIdle();
return content::WaitForLoadStop(GetActiveWebContents());
}
// TODO(crbug.com/40883928): Paramertize these test scenarios (and the setup as
// well). This would allow us to concisely describe the multiple state changes
// and expected end states without having an individual test case for each or
// (as below) have two large tests that rely on previous tests steps creating
// state to proceed successfully.
// Tests the various states of permission changes that can occur. When changes
// occur we automatically accept the reload bubble, confirm the content script
// for the extension is running/not running, and that we are still on the same
// page after changing permissions. User site access changes are expected to be
// immediate. There are many ASSERTS here because each test case is relying on
// the previous changes completing in order to properly test its scenario.
// Scenarios tested (in order):
//
// on all sites -> on site
// on site -> on-click (refresh needed due to revoking permissions)
// on click -> on site (refresh needed due to script wanting to load at start)
// on site -> on all sites
// on all sites -> on-click (refresh needed due to revoking permissions)
IN_PROC_BROWSER_TEST_F(SitePermissionsHelperBrowserTest,
UpdateSiteAccess_AcceptReloadBubble) {
// By default, test setup should set site access to be on all sites.
ASSERT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnAllSites);
active_action_runner()->accept_bubble_for_testing(true);
// on all sites -> on site
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnSite);
EXPECT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnSite);
// We assume that there is only ever one action that wants to run for the test
// extension used by these tests. Anything else is an unexpected change, bug,
// or a flaw in the test.
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
// on site -> on-click (refresh needed due to revoking permissions)
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnClick);
EXPECT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnClick);
ASSERT_TRUE(WaitForReloadToFinish());
ASSERT_FALSE(ContentScriptInjected());
ASSERT_TRUE(ExtensionWantsToRun());
// on click -> on site (refresh needed due to script wanting to load at
// start)
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnSite);
EXPECT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnSite);
ASSERT_TRUE(WaitForReloadToFinish());
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
// on site -> on all sites
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnAllSites);
EXPECT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnAllSites);
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
// on all sites -> on-click (refresh needed due to revoking permissions)
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnClick);
EXPECT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnClick);
EXPECT_TRUE(WaitForReloadToFinish());
EXPECT_FALSE(ContentScriptInjected());
EXPECT_TRUE(ExtensionWantsToRun());
}
// Tests the various states of permission changes that can occur. When changes
// occur we automatically dismiss the reload bubble, confirm the content script
// for the extension is running/not running, and that we are still on the same
// page after changing permissions. User site access changes are expected to be
// immediate, but a reload is expected so we instead simulate reloading via the
// "Reload this page" button. There are many ASSERTS here because each test case
// is relying on the previous changes completing in order to properly test its
// scenario. Scenarios tested (in order):
//
// on all sites -> on site
// on site -> on-click (refresh needed, and done manually, due to revoking
// permissions)
// on click -> on site (refresh needed, and done manually, due to
// script wanting to load at start)
// on site -> on all sites on all sites -> on-click (refresh needed, and done
// manually due to revoking permissions)
IN_PROC_BROWSER_TEST_F(
SitePermissionsHelperBrowserTest,
UpdateSiteAccess_DismissReloadBubble_ReloadPageManually) {
// By default, test setup should set site access to be on all sites.
ASSERT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnAllSites);
// Reload will not happen via the user reload bubble.
active_action_runner()->accept_bubble_for_testing(false);
// on all sites -> on site
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnSite);
EXPECT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnSite);
// We assume that there is only ever one action that wants to run for the test
// extension used by these tests. Anything else is an unexpected change, bug,
// or a flaw in the test.
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
// on site -> on-click (refresh needed due to revoking permissions)
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnClick);
EXPECT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnClick);
EXPECT_TRUE(ContentScriptInjected() && !ExtensionWantsToRun());
ASSERT_TRUE(ReloadPageAndWaitForLoad());
ASSERT_FALSE(ContentScriptInjected());
ASSERT_TRUE(ExtensionWantsToRun());
// on click -> on site (refresh needed due to script wanting to load at
// start)
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnSite);
EXPECT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnSite);
EXPECT_TRUE(!ContentScriptInjected() && ExtensionWantsToRun());
ASSERT_TRUE(ReloadPageAndWaitForLoad());
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
// on site -> on all sites
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnAllSites);
EXPECT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnAllSites);
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
// on all sites -> on-click (refresh needed due to revoking permissions)
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnClick);
EXPECT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnClick);
EXPECT_TRUE(ContentScriptInjected() && !ExtensionWantsToRun());
ASSERT_TRUE(ReloadPageAndWaitForLoad());
EXPECT_TRUE(!ContentScriptInjected() && ExtensionWantsToRun());
}
#if BUILDFLAG(ENABLE_EXTENSIONS)
// Provides test cases with an extension that executes a script programmatically
// on every site it visits.
// TODO(crbug.com/371432155): Port to desktop Android when the chrome.tabs API
// is supported. chrome.tabs is used by the test extension.
class SitePermissionsHelperExecuteSciptBrowserTest
: public SitePermissionsHelperBrowserTest {
public:
void SetUpOnMainThread() override {
host_resolver()->AddRule("a.com", "127.0.0.1");
host_resolver()->AddRule("b.com", "127.0.0.1");
ExtensionBrowserTest::SetUpOnMainThread();
// Loads an extension that executes a script on every page that is navigated
// to. Then loads a test page and confirms it is running on the page.
ASSERT_TRUE(embedded_test_server()->Start());
extension_ = LoadExtension(test_data_dir_.AppendASCII(
"blocked_actions/revoke_execute_script_on_click"));
ASSERT_TRUE(extension_);
// Navigate to a page where the extension can run.
original_url_ = embedded_test_server()->GetURL("/simple.html");
ExtensionTestMessageListener listener("injection succeeded");
ASSERT_TRUE(NavigateToURL(original_url_));
ASSERT_TRUE(GetActiveWebContents());
ASSERT_TRUE(content::WaitForLoadStop(GetActiveWebContents()));
permissions_manager_ = PermissionsManager::Get(profile());
ASSERT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnAllSites);
ASSERT_TRUE(listener.WaitUntilSatisfied());
ASSERT_TRUE(active_action_runner());
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
permissions_helper_ = std::make_unique<SitePermissionsHelper>(profile());
original_nav_id_ =
active_nav_controller().GetLastCommittedEntry()->GetUniqueID();
}
// Navigates to `host_name` with `relative_url`. `host_name` must be added as
// a rule in SetUpOnMainThread().
void NavigateTo(std::string_view host_name, std::string_view relative_url) {
GURL url = embedded_test_server()->GetURL(host_name, relative_url);
ASSERT_TRUE(NavigateToURL(url));
}
};
// Tests that active tab is cleared when we revoke site permissions of an
// extension that injects a script programmatically into the page. To fix
// crbug.com/1433399.
IN_PROC_BROWSER_TEST_F(
SitePermissionsHelperExecuteSciptBrowserTest,
UpdateSiteAccess_RevokingSitePermission_AlsoClearsActiveTab) {
// We want to control refreshes manually due to timing issues with permissions
// being updated across browser/renderer.
active_action_runner()->accept_bubble_for_testing(true);
{
// on all sites -> on click (revokes access)
BlockedActionWaiter blocked_action_waiter(active_action_runner());
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnClick);
ASSERT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnClick);
ASSERT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kWithheld);
ASSERT_TRUE(WaitForReloadToFinish());
blocked_action_waiter.Wait();
ASSERT_FALSE(ContentScriptInjected());
ASSERT_TRUE(ExtensionWantsToRun());
}
ExtensionTestMessageListener listener("injection succeeded");
// on click -> on site (grants site access and active tab permission)
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnSite);
ASSERT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnSite);
ASSERT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kGranted);
ASSERT_TRUE(listener.WaitUntilSatisfied());
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
{
// on site -> on-click (should remove site access and active tab
// permissions)
BlockedActionWaiter blocked_action_waiter(active_action_runner());
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnClick);
ASSERT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnClick);
ASSERT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kWithheld);
ASSERT_TRUE(WaitForReloadToFinish());
blocked_action_waiter.Wait();
EXPECT_FALSE(ContentScriptInjected());
EXPECT_TRUE(ExtensionWantsToRun());
}
{
// Confirm that unintended access isn't just waiting for a reload to allow
// it to run.
BlockedActionWaiter blocked_action_waiter(active_action_runner());
ASSERT_TRUE(ReloadPageAndWaitForLoad());
ASSERT_TRUE(WaitForReloadToFinish());
blocked_action_waiter.Wait();
EXPECT_FALSE(ContentScriptInjected());
EXPECT_TRUE(ExtensionWantsToRun());
}
}
// Tests that active tab is cleared when we revoke site permissions after
// granting active tab permissions of an extension that injects a script
// programmatically into the page. To fix crbug.com/1433399.
IN_PROC_BROWSER_TEST_F(
SitePermissionsHelperExecuteSciptBrowserTest,
UpdateSiteAccess_RevokingSitePermissionAfterGrantTab_AlsoClearsActiveTab) {
active_action_runner()->accept_bubble_for_testing(true);
{
// on all sites -> on click (revokes access)
BlockedActionWaiter blocked_action_waiter(active_action_runner());
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnClick);
ASSERT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnClick);
ASSERT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kWithheld);
ASSERT_TRUE(WaitForReloadToFinish());
blocked_action_waiter.Wait();
ASSERT_FALSE(ContentScriptInjected());
ASSERT_TRUE(ExtensionWantsToRun());
}
ExtensionTestMessageListener listener("injection succeeded");
// Grant active tab independently.
active_action_runner()->RunAction(extension_, /*grant_tab_permissions=*/true);
ASSERT_TRUE(listener.WaitUntilSatisfied());
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
// on click -> on site (grants site access and redundantly active tab
// permission)
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnSite);
ASSERT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnSite);
ASSERT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kGranted);
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
{
// on site -> on-click (should remove site access and active tab
// permissions)
BlockedActionWaiter blocked_action_waiter(active_action_runner());
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnClick);
ASSERT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnClick);
ASSERT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kWithheld);
ASSERT_TRUE(WaitForReloadToFinish());
blocked_action_waiter.Wait();
EXPECT_FALSE(ContentScriptInjected());
EXPECT_TRUE(ExtensionWantsToRun());
}
{
// Confirm that unintended access isn't just waiting for a reload to allow
// it to run.
BlockedActionWaiter blocked_action_waiter(active_action_runner());
ASSERT_TRUE(ReloadPageAndWaitForLoad());
ASSERT_TRUE(WaitForReloadToFinish());
blocked_action_waiter.Wait();
EXPECT_FALSE(ContentScriptInjected());
EXPECT_TRUE(ExtensionWantsToRun());
}
}
// Tests that active tab is cleared when we renavigate to a site previously
// granted one-time tab permission after a cross-origin navigation. Regression
// rest for b/324455951.
IN_PROC_BROWSER_TEST_F(SitePermissionsHelperExecuteSciptBrowserTest,
CrossOriginRenavigationClearsGrantedTabPermission) {
active_action_runner()->accept_bubble_for_testing(true);
// Withheld extension's site access.
ScriptingPermissionsModifier(profile(), extension_.get())
.SetWithholdHostPermissions(true);
{
// Navigate to a.com. Script is not injected since extension has withheld
// site access.
BlockedActionWaiter blocked_action_waiter(active_action_runner());
NavigateTo("a.com", "/simple.html");
blocked_action_waiter.Wait();
ASSERT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kWithheld);
EXPECT_FALSE(ContentScriptInjected());
EXPECT_TRUE(ExtensionWantsToRun());
}
{
// Grant tab permissions to a.com. Script is injected (one time).
ExtensionTestMessageListener script_injection_listener(
"injection succeeded");
active_action_runner()->GrantTabPermissions({extension_.get()});
ASSERT_TRUE(WaitForReloadToFinish());
ASSERT_TRUE(script_injection_listener.WaitUntilSatisfied());
EXPECT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kGranted);
EXPECT_TRUE(ContentScriptInjected());
EXPECT_FALSE(ExtensionWantsToRun());
}
{
// Navigate to b.com. Script is not injected since extension has withheld
// site access.
BlockedActionWaiter blocked_action_waiter(active_action_runner());
NavigateTo("b.com", "/simple.html");
blocked_action_waiter.Wait();
EXPECT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kWithheld);
EXPECT_FALSE(ContentScriptInjected());
EXPECT_TRUE(ExtensionWantsToRun());
}
{
// Navigate back to a.com. Since we navigated to another origin, and then
// back to a.com it should not have tab permissions anymore. Thus, the
// script is not injected.
BlockedActionWaiter blocked_action_waiter(active_action_runner());
NavigateTo("a.com", "/simple.html");
blocked_action_waiter.Wait();
EXPECT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kWithheld);
EXPECT_FALSE(ContentScriptInjected());
EXPECT_TRUE(ExtensionWantsToRun());
}
}
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
class SitePermissionsHelperContentScriptBrowserTest
: public SitePermissionsHelperBrowserTest {
void SetUpOnMainThread() override {
ExtensionBrowserTest::SetUpOnMainThread();
// Loads an extension that injects a content script on every page that is
// navigated to on document_end. Then loads a test page and confirms it is
// running on the page.
ASSERT_TRUE(embedded_test_server()->Start());
extension_ = LoadExtension(
test_data_dir_.AppendASCII("blocked_actions/content_script_at_end"));
ASSERT_TRUE(extension_);
// Navigate to a page where the extension can run.
original_url_ = embedded_test_server()->GetURL("/simple.html");
ExtensionTestMessageListener listener("injection succeeded");
ASSERT_TRUE(NavigateToURL(original_url_));
ASSERT_TRUE(GetActiveWebContents());
ASSERT_TRUE(content::WaitForLoadStop(GetActiveWebContents()));
permissions_manager_ = PermissionsManager::Get(profile());
ASSERT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnAllSites);
ASSERT_TRUE(listener.WaitUntilSatisfied());
ASSERT_TRUE(active_action_runner());
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
permissions_helper_ = std::make_unique<SitePermissionsHelper>(profile());
original_nav_id_ =
active_nav_controller().GetLastCommittedEntry()->GetUniqueID();
}
};
// Tests that active tab is cleared when we revoke site permissions of an
// extension that injects a content script. To fix crbug.com/1433399.
IN_PROC_BROWSER_TEST_F(
SitePermissionsHelperContentScriptBrowserTest,
UpdateSiteAccess_RevokingSitePermission_AlsoClearsActiveTab) {
// We want to control refreshes manually due to timing issues with permissions
// being updated across browser/renderer.
active_action_runner()->accept_bubble_for_testing(true);
// on all sites -> on click (revokes access)
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnClick);
ASSERT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnClick);
ASSERT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kWithheld);
ASSERT_TRUE(WaitForReloadToFinish());
ASSERT_FALSE(ContentScriptInjected());
ASSERT_TRUE(ExtensionWantsToRun());
ExtensionTestMessageListener listener("injection succeeded");
// on click -> on site (grants site access and active tab permission)
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnSite);
ASSERT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnSite);
ASSERT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kGranted);
ASSERT_TRUE(listener.WaitUntilSatisfied());
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
// on site -> on-click (should remove site access and active tab permissions)
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnClick);
EXPECT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnClick);
EXPECT_EQ(permissions_helper_->GetSiteInteraction(*extension_,
GetActiveWebContents()),
SitePermissionsHelper::SiteInteraction::kWithheld);
ASSERT_TRUE(WaitForReloadToFinish());
EXPECT_FALSE(ContentScriptInjected());
EXPECT_TRUE(ExtensionWantsToRun());
// Confirm that unintended access isn't just waiting for a reload to allow it
// to run.
ASSERT_TRUE(ReloadPageAndWaitForLoad());
EXPECT_FALSE(ContentScriptInjected());
EXPECT_TRUE(ExtensionWantsToRun());
}
#if BUILDFLAG(ENABLE_EXTENSIONS)
// TODO(crbug.com/371432155): Port to desktop Android when the chrome.tabs API
// is supported. chrome.tabs is used by the test extension.
class SitePermissionsHelperOptionalHostPermissions
: public SitePermissionsHelperBrowserTest {
public:
void SetUpOnMainThread() override {
ExtensionBrowserTest::SetUpOnMainThread();
// Loads an extension that, when granted the optional host permission,
// executes a script on every page that is navigated to. Then loads a test
// page and confirms it is running on the page.
ASSERT_TRUE(embedded_test_server()->Start());
extension_ = LoadExtension(test_data_dir_.AppendASCII(
"blocked_actions/optional_host_permissions"));
ASSERT_TRUE(extension_);
// Grant the optional host permissions.
permissions_manager_ = PermissionsManager::Get(profile());
permissions_test_util::GrantOptionalPermissionsAndWaitForCompletion(
profile(), *extension_,
PermissionsParser::GetOptionalPermissions(extension_));
// Navigate to a page where the extension can run.
original_url_ = embedded_test_server()->GetURL("/simple.html");
ExtensionTestMessageListener listener("success");
ASSERT_TRUE(NavigateToURL(original_url_));
ASSERT_TRUE(GetActiveWebContents());
ASSERT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnAllSites);
ASSERT_TRUE(listener.WaitUntilSatisfied());
ASSERT_TRUE(active_action_runner());
ASSERT_TRUE(ContentScriptInjected());
ASSERT_FALSE(ExtensionWantsToRun());
permissions_helper_ = std::make_unique<SitePermissionsHelper>(profile());
}
};
// Tests the behavior when granted optional host permissions are altered by
// updating site access. The scenarios tested here are
// on all sites -> on site
// on site -> on click
// on click -> on site
// on site -> on all sites
// on all sites -> on click
IN_PROC_BROWSER_TEST_F(SitePermissionsHelperOptionalHostPermissions,
UpdateSiteAccess) {
// By default, test setup should set site access to be on all sites.
ASSERT_EQ(permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnAllSites);
active_action_runner()->accept_bubble_for_testing(true);
{
// on all sites -> on site.
ExtensionTestMessageListener listener("success");
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnSite);
EXPECT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnSite);
// Changing the site access to on site should still allow script injection.
// Reloading the page to verify if the scripts were re-injected.
ASSERT_TRUE(ReloadPageAndWaitForLoad());
EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_TRUE(ContentScriptInjected());
EXPECT_FALSE(ExtensionWantsToRun());
}
{
// on site -> on-click (refresh needed due to revoking permissions).
BlockedActionWaiter blocked_action_waiter(active_action_runner());
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnClick);
EXPECT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnClick);
ASSERT_TRUE(WaitForReloadToFinish());
blocked_action_waiter.Wait();
EXPECT_FALSE(ContentScriptInjected());
EXPECT_TRUE(ExtensionWantsToRun());
}
{
// on click -> on site
ExtensionTestMessageListener listener("success");
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnSite);
EXPECT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnSite);
ASSERT_TRUE(WaitForReloadToFinish());
EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_TRUE(ContentScriptInjected());
EXPECT_FALSE(ExtensionWantsToRun());
}
{
// on site -> on all sites
ExtensionTestMessageListener listener("success");
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnAllSites);
EXPECT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnAllSites);
// Reloading the page to verify if the scripts were re-injected.
ASSERT_TRUE(ReloadPageAndWaitForLoad());
EXPECT_TRUE(listener.WaitUntilSatisfied());
EXPECT_TRUE(ContentScriptInjected());
EXPECT_FALSE(ExtensionWantsToRun());
}
{
// on all sites -> on-click
BlockedActionWaiter blocked_action_waiter(active_action_runner());
permissions_helper_->UpdateSiteAccess(*extension_, GetActiveWebContents(),
UserSiteAccess::kOnClick);
EXPECT_EQ(
permissions_manager_->GetUserSiteAccess(*extension_, original_url_),
UserSiteAccess::kOnClick);
ASSERT_TRUE(WaitForReloadToFinish());
blocked_action_waiter.Wait();
EXPECT_FALSE(ContentScriptInjected());
EXPECT_TRUE(ExtensionWantsToRun());
}
}
#endif // BUILDFLAG(ENABLE_EXTENSIONS)
} // namespace extensions
|