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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <vector>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_switches.h"
#include "base/barrier_callback.h"
#include "base/barrier_closure.h"
#include "base/check_deref.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/containers/enum_set.h"
#include "base/notreached.h"
#include "base/test/bind.h"
#include "base/test/run_until.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "chrome/browser/ash/floating_sso/cookie_sync_conversions.h"
#include "chrome/browser/ash/floating_sso/cookie_sync_test_util.h"
#include "chrome/browser/ash/floating_sso/floating_sso_service.h"
#include "chrome/browser/ash/floating_sso/floating_sso_service_factory.h"
#include "chrome/browser/ash/floating_sso/floating_sso_sync_bridge.h"
#include "chrome/browser/ash/floating_workspace/floating_workspace_util.h"
#include "chrome/browser/policy/policy_test_utils.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/core/dependency_graph.h"
#include "components/keyed_service/core/keyed_service_base_factory.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_service.h"
#include "components/sync/base/pref_names.h"
#include "components/sync/model/entity_change.h"
#include "components/sync/protocol/cookie_specifics.pb.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"
#include "net/cookies/cookie_constants.h"
#include "net/cookies/cookie_util.h"
#include "services/network/public/mojom/cookie_manager.mojom.h"
#include "services/network/public/mojom/network_context.mojom.h"
namespace ash::floating_sso {
namespace {
using testing::_;
// Cookie that passes the Floating SSO filters.
constexpr char kStandardCookieLine[] = "CookieName=CookieValue; max-age=3600";
constexpr char kCookieName[] = "CookieName";
// Unique key for standard cookie (kStandardCookieLine and kNonGoogleURL).
// Has cross-site ancestor (true), name (CookieName), domain + path
// (example.com/), kSecure scheme (2), port (8888).
constexpr char kCookieUniqueKey[] = "trueCookieNameexample.com/28888";
class CookieChangeListener : public network::mojom::CookieChangeListener {
public:
// Create a change listener for all cookies.
CookieChangeListener(
network::mojom::CookieManager* cookie_manager,
base::RepeatingCallback<void(const net::CookieChangeInfo&)> callback)
: callback_(std::move(callback)), receiver_(this) {
cookie_manager->AddGlobalChangeListener(
receiver_.BindNewPipeAndPassRemote());
}
// Create a change listener limited to a specific URL.
CookieChangeListener(
network::mojom::CookieManager* cookie_manager,
const GURL& url,
base::RepeatingCallback<void(const net::CookieChangeInfo&)> callback)
: callback_(std::move(callback)), receiver_(this) {
cookie_manager->AddCookieChangeListener(
url, std::nullopt, receiver_.BindNewPipeAndPassRemote());
}
// network::mojom::CookieChangeListener:
void OnCookieChange(const net::CookieChangeInfo& change) override {
callback_.Run(change);
}
private:
base::RepeatingCallback<void(const net::CookieChangeInfo&)> callback_;
mojo::Receiver<network::mojom::CookieChangeListener> receiver_;
};
std::optional<net::CanonicalCookie> GetCookie(
network::mojom::CookieManager* cookie_manager,
const std::string& name) {
base::test::TestFuture<const std::vector<net::CanonicalCookie>&> future;
cookie_manager->GetAllCookies(future.GetCallback());
for (const net::CanonicalCookie& cookie : future.Take()) {
if (cookie.Name() == name) {
return std::make_optional<net::CanonicalCookie>(cookie);
}
}
return std::nullopt;
}
// Returns if the cookie was added successfully.
bool SetCookie(network::mojom::CookieManager* cookie_manager,
const GURL& url,
const net::CanonicalCookie& cookie) {
base::test::TestFuture<net::CookieAccessResult> future;
cookie_manager->SetCanonicalCookie(cookie, url,
net::CookieOptions::MakeAllInclusive(),
future.GetCallback());
return future.Take().status.IsInclude();
}
// Returns if the cookie was added successfully.
bool SetCookie(network::mojom::CookieManager* cookie_manager,
const net::CanonicalCookie& cookie) {
GURL url = net::cookie_util::SimulatedCookieSource(cookie, "https");
return SetCookie(cookie_manager, url, cookie);
}
// Returns if the cookie was added successfully.
bool SetCookie(network::mojom::CookieManager* cookie_manager,
const GURL& url,
const std::string& cookie_line) {
auto cookie = net::CanonicalCookie::CreateForTesting(
url, cookie_line, base::Time::Now(),
/*server_time=*/std::nullopt,
/*cookie_partition_key=*/std::nullopt, net::CookieSourceType::kHTTP);
return SetCookie(cookie_manager, url, *cookie);
}
// Returns the number of deleted cookies.
uint32_t DeleteCookies(network::mojom::CookieManager* cookie_manager,
network::mojom::CookieDeletionFilter filter) {
base::test::TestFuture<uint32_t> future;
cookie_manager->DeleteCookies(
network::mojom::CookieDeletionFilter::New(filter), future.GetCallback());
return future.Get();
}
} // namespace
class FloatingSsoTest : public policy::PolicyTest {
public:
FloatingSsoTest() {
feature_list_.InitAndEnableFeature(ash::features::kFloatingSso);
}
~FloatingSsoTest() override = default;
void SetUpOnMainThread() override {
network::mojom::NetworkContext* network_context =
profile()->GetDefaultStoragePartition()->GetNetworkContext();
network_context->GetCookieManager(
cookie_manager_.BindNewPipeAndPassReceiver());
}
protected:
void SetFloatingSsoEnabledPolicy(bool policy_value) {
policy::PolicyTest::SetPolicy(&policies_, policy::key::kFloatingSsoEnabled,
base::Value(policy_value));
provider_.UpdateChromePolicy(policies_);
}
void SetSyncEverythingPref(bool pref_value) {
profile()->GetPrefs()->SetBoolean(
syncer::prefs::internal::kSyncKeepEverythingSynced, pref_value);
}
void SetSyncCookiesPref(bool pref_value) {
profile()->GetPrefs()->SetBoolean(syncer::prefs::internal::kSyncCookies,
pref_value);
}
void SetSyncDisabledPolicy(bool policy_value) {
policy::PolicyTest::SetPolicy(&policies_, policy::key::kSyncDisabled,
base::Value(policy_value));
provider_.UpdateChromePolicy(policies_);
}
void SetFloatingSsoDomainBlocklistPolicy(const std::string& domain) {
base::Value::List domains;
domains.Append(domain);
policy::PolicyTest::SetPolicy(&policies_,
policy::key::kFloatingSsoDomainBlocklist,
base::Value(std::move(domains)));
provider_.UpdateChromePolicy(policies_);
}
void SetFloatingSsoDomainBlocklistExceptionsPolicy(
const std::string& domain) {
base::Value::List domains;
if (!domain.empty()) {
domains.Append(domain);
}
policy::PolicyTest::SetPolicy(
&policies_, policy::key::kFloatingSsoDomainBlocklistExceptions,
base::Value(std::move(domains)));
provider_.UpdateChromePolicy(policies_);
}
void EnableAllFloatingSsoSettings() {
SetFloatingSsoEnabledPolicy(/*policy_value=*/true);
SetSyncCookiesPref(/*pref_value=*/true);
SetSyncDisabledPolicy(/*policy_value=*/false);
}
void EnableFloatingWorkspace() {
policy::PolicyTest::SetPolicy(&policies_,
policy::key::kFloatingWorkspaceV2Enabled,
base::Value(true));
provider_.UpdateChromePolicy(policies_);
ASSERT_TRUE(ash::floating_workspace_util::IsFloatingWorkspaceV2Enabled());
}
bool IsFloatingSsoServiceRegistered() {
std::vector<raw_ptr<DependencyNode, VectorExperimental>> nodes;
const bool success = BrowserContextDependencyManager::GetInstance()
->GetDependencyGraphForTesting()
.GetConstructionOrder(&nodes);
EXPECT_TRUE(success);
return base::Contains(
nodes, "FloatingSsoService",
[](const DependencyNode* node) -> std::string_view {
return static_cast<const KeyedServiceBaseFactory*>(node)->name();
});
}
Profile* profile() { return browser()->profile(); }
FloatingSsoService& floating_sso_service() {
return CHECK_DEREF(FloatingSsoServiceFactory::GetForProfile(profile()));
}
network::mojom::CookieManager* cookie_manager() {
return cookie_manager_.get();
}
const FloatingSsoSyncBridge::CookieSpecificsEntries& GetStoreEntries() {
return floating_sso_service()
.GetBridgeForTesting()
->CookieSpecificsInStore();
}
void AddCookieAndWaitForCommit(network::mojom::CookieManager* cookie_manager,
const GURL& url,
const std::string& cookie_line) {
// Used for waiting for the store commit to be finalized.
base::test::TestFuture<void> commit_future;
floating_sso_service()
.GetBridgeForTesting()
->SetOnStoreCommitCallbackForTest(commit_future.GetRepeatingCallback());
// Used for waiting for the cookie change event (INSERTED) to be dispatched.
base::test::TestFuture<const net::CookieChangeInfo&> cookie_change_future;
CookieChangeListener listener(cookie_manager, url,
cookie_change_future.GetRepeatingCallback());
// Add cookie.
ASSERT_TRUE(SetCookie(cookie_manager, url, cookie_line));
EXPECT_EQ(cookie_change_future.Take().cause,
net::CookieChangeCause::INSERTED);
commit_future.Get();
}
void DeleteCookieAndWaitForCommit(
network::mojom::CookieManager* cookie_manager,
const GURL& url,
const std::string& cookie_name) {
// Used for waiting for the store commit to be finalized.
base::test::TestFuture<void> commit_future;
floating_sso_service()
.GetBridgeForTesting()
->SetOnStoreCommitCallbackForTest(commit_future.GetRepeatingCallback());
// Used for waiting for the cookie change event (EXPLICIT) to be dispatched.
base::test::TestFuture<const net::CookieChangeInfo&> cookie_change_future;
CookieChangeListener listener(cookie_manager, url,
cookie_change_future.GetRepeatingCallback());
// Delete cookie.
network::mojom::CookieDeletionFilter filter;
filter.cookie_name = cookie_name;
ASSERT_EQ(DeleteCookies(cookie_manager, filter), 1u);
EXPECT_EQ(cookie_change_future.Take().cause,
net::CookieChangeCause::EXPLICIT);
commit_future.Get();
}
void UpdateCookieAndWaitForCommit(
network::mojom::CookieManager* cookie_manager,
const GURL& url,
const std::string& cookie_name) {
// Used for waiting for the two store commits to be finalized.
base::test::TestFuture<void> commit_future;
floating_sso_service()
.GetBridgeForTesting()
->SetOnStoreCommitCallbackForTest(base::BarrierClosure(
/*num_callbacks=*/2, commit_future.GetRepeatingCallback()));
// Used for waiting for the cookie change events (OVERWRITE, INSERTED) to be
// dispatched.
base::test::TestFuture<std::vector<net::CookieChangeInfo>>
cookie_change_future;
CookieChangeListener listener(
cookie_manager, url,
base::BarrierCallback<const net::CookieChangeInfo&>(
/*num_callbacks=*/2, cookie_change_future.GetRepeatingCallback()));
// Update cookie.
auto cookie = GetCookie(cookie_manager, cookie_name);
ASSERT_TRUE(cookie.has_value());
cookie.value().SetLastAccessDate(base::Time::Now());
cookie_manager->SetCanonicalCookie(*cookie, url,
net::CookieOptions::MakeAllInclusive(),
base::DoNothing());
// Updating an existing cookie is a two-phase delete + insert operation, so
// two cookie change events are triggered.
EXPECT_THAT(cookie_change_future.Take(),
testing::ElementsAre(
testing::Field("cause", &net::CookieChangeInfo::cause,
net::CookieChangeCause::OVERWRITE),
testing::Field(
"cause", &net::CookieChangeInfo::cause,
net::CookieChangeCause::INSERTED_NO_CHANGE_OVERWRITE)));
commit_future.Get();
}
mojo::Remote<network::mojom::CookieManager> cookie_manager_;
base::test::ScopedFeatureList feature_list_;
const GURL kNonGoogleURL = GURL("https://example.com:8888");
policy::PolicyMap policies_;
};
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, ServiceRegistered) {
ASSERT_TRUE(IsFloatingSsoServiceRegistered());
}
// FloatingSsoEnabled policy is disabled, cookies are not added to the store.
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, FloatingSsoPolicyDisabled) {
auto& service = floating_sso_service();
SetFloatingSsoEnabledPolicy(/*policy_value=*/false);
SetSyncCookiesPref(/*pref_value=*/true);
SetSyncDisabledPolicy(/*policy_value=*/false);
ASSERT_FALSE(service.IsBoundToCookieManagerForTesting());
ASSERT_TRUE(SetCookie(cookie_manager(), kNonGoogleURL, kStandardCookieLine));
// Cookie is not added to store because the FloatingSsoEnabled policy is
// disabled.
auto store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 0u);
}
// SyncCookies pref (user toggle) is disabled, cookies are not added to the
// store.
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, SyncCookiesPrefDisabled) {
auto& service = floating_sso_service();
SetFloatingSsoEnabledPolicy(/*policy_value=*/true);
SetSyncEverythingPref(/*pref_value=*/false);
SetSyncCookiesPref(/*pref_value=*/false);
SetSyncDisabledPolicy(/*policy_value=*/false);
ASSERT_FALSE(service.IsBoundToCookieManagerForTesting());
ASSERT_TRUE(SetCookie(cookie_manager(), kNonGoogleURL, kStandardCookieLine));
// Cookie is not added to store because the SyncCookies pref is disabled.
auto store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 0u);
}
// SyncDisabled policy is enabled, cookies are not added to the store.
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, SyncDisabled) {
auto& service = floating_sso_service();
SetFloatingSsoEnabledPolicy(/*policy_value=*/true);
SetSyncCookiesPref(/*pref_value=*/true);
SetSyncDisabledPolicy(/*policy_value=*/true);
ASSERT_FALSE(service.IsBoundToCookieManagerForTesting());
ASSERT_TRUE(SetCookie(cookie_manager(), kNonGoogleURL, kStandardCookieLine));
// Cookie is not added to store because the SyncCookies pref is disabled.
auto store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 0u);
}
// Most Floating SSO tests rely on SyncCookies pref (user toggle) to enable the
// feature, this test separately checks that we also respect the option to sync
// everything.
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, FloatingSsoRespectsSyncEverythingPref) {
auto& service = floating_sso_service();
SetFloatingSsoEnabledPolicy(/*policy_value=*/true);
SetSyncEverythingPref(/*pref_value=*/true);
// When user selects to sync everything, the value of per-type prefs shouldn't
// matter.
SetSyncCookiesPref(/*pref_value=*/false);
SetSyncDisabledPolicy(/*policy_value=*/false);
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
AddCookieAndWaitForCommit(cookie_manager(), kNonGoogleURL,
kStandardCookieLine);
// Cookie is added to store.
const auto& store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 1u);
EXPECT_TRUE(store_entries.contains(kCookieUniqueKey));
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, FloatingSsoStopsListeningAndResumes) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
AddCookieAndWaitForCommit(cookie_manager(), kNonGoogleURL,
kStandardCookieLine);
// Cookie is added to store.
const auto& store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 1u);
EXPECT_TRUE(store_entries.contains(kCookieUniqueKey));
SetFloatingSsoEnabledPolicy(/*policy_value=*/false);
base::test::TestFuture<const net::CookieChangeInfo&> cookie_change_future;
CookieChangeListener listener(cookie_manager(), kNonGoogleURL,
cookie_change_future.GetRepeatingCallback());
// Add new cookie.
ASSERT_TRUE(SetCookie(cookie_manager(), kNonGoogleURL,
"CookieNameNew=CookieValueNew; max-age=3600"));
EXPECT_EQ(cookie_change_future.Take().cause,
net::CookieChangeCause::INSERTED);
EXPECT_EQ(store_entries.size(), 1u);
// Expect the new cookie to result in a commit once Floating SSO is enabled
// again.
base::test::TestFuture<void> commit_future;
floating_sso_service().GetBridgeForTesting()->SetOnStoreCommitCallbackForTest(
commit_future.GetRepeatingCallback());
// Re-enabling means that the cookies are fetched again and committed to the
// store.
SetFloatingSsoEnabledPolicy(/*policy_value=*/true);
commit_future.Get();
EXPECT_EQ(store_entries.size(), 2u);
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, FiltersOutGoogleCookies) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://google.com"),
kStandardCookieLine));
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://accounts.google.com"),
kStandardCookieLine));
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://youtube.com"),
kStandardCookieLine));
// Cookies are not added to store.
auto store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 0u);
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, FiltersOutSessionCookies) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
ASSERT_TRUE(
SetCookie(cookie_manager(), kNonGoogleURL, "CookieName=CookieValue"));
// Cookie is not added to store.
auto store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 0u);
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, FiltersOutCookiesWithNonHttpSource) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
const auto& store_entries = GetStoreEntries();
ASSERT_EQ(store_entries.size(), 0u);
// For every source type different from `net::CookieSourceType::kHTTP`, add a
// cookie with such source type to the browser, and verify that it's not being
// synced.
for (net::CookieSourceType source :
base::EnumSet<net::CookieSourceType, net::CookieSourceType::kUnknown,
net::CookieSourceType::kMaxValue>::All()) {
if (source == net::CookieSourceType::kHTTP) {
continue;
}
const GURL url("https://example.com");
std::unique_ptr<net::CanonicalCookie> cookie =
net::CanonicalCookie::CreateForTesting(
url, kStandardCookieLine, base::Time::Now(),
/*server_time=*/std::nullopt,
/*cookie_partition_key=*/std::nullopt, source);
ASSERT_TRUE(SetCookie(cookie_manager(), url, *cookie));
// Verify that nothing is added to Sync store.
EXPECT_EQ(store_entries.size(), 0u);
}
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest,
FiltersCookiesWithBlocklistBasicPattern) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
SetFloatingSsoDomainBlocklistPolicy("example.com");
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://example.com"),
kStandardCookieLine));
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("http://example.com"),
kStandardCookieLine));
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://www.example.com"),
kStandardCookieLine));
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://sub.www.example.com"),
kStandardCookieLine));
// Cookies are not added to store.
auto store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 0u);
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest,
FiltersCookiesWithBlocklistSubdomainPattern) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
SetFloatingSsoDomainBlocklistPolicy("mail.example.com");
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://mail.example.com"),
kStandardCookieLine));
// Cookie is not added to store.
const auto& store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 0u);
// Other subdomains are not filtered.
AddCookieAndWaitForCommit(cookie_manager(), GURL("http://example.com"),
kStandardCookieLine);
AddCookieAndWaitForCommit(cookie_manager(), GURL("https://www.example.com"),
kStandardCookieLine);
// Cookies are added to store.
EXPECT_EQ(store_entries.size(), 2u);
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, FiltersCookiesWithBlocklistDotPattern) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
SetFloatingSsoDomainBlocklistPolicy(".example.com");
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://example.com"),
kStandardCookieLine));
// Cookie is not added to store.
const auto& store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 0u);
// Subdomains are not filtered.
AddCookieAndWaitForCommit(cookie_manager(), GURL("https://mail.example.com"),
kStandardCookieLine);
// Cookie is added to store.
EXPECT_EQ(store_entries.size(), 1u);
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest,
AllowSpecificDomainsWithWildcardBlocking) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
SetFloatingSsoDomainBlocklistPolicy("*");
SetFloatingSsoDomainBlocklistExceptionsPolicy("example.com");
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://mail.com"),
kStandardCookieLine));
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://test.com"),
kStandardCookieLine));
// Cookies are not added to store.
const auto& store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 0u);
// Allows domains in exceptions.
AddCookieAndWaitForCommit(cookie_manager(), GURL("https://example.com"),
kStandardCookieLine);
// Cookie is added to store.
EXPECT_EQ(store_entries.size(), 1u);
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, ExceptionListTakesPrecedence) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
SetFloatingSsoDomainBlocklistPolicy("example.com");
SetFloatingSsoDomainBlocklistExceptionsPolicy("example.com");
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
AddCookieAndWaitForCommit(cookie_manager(), GURL("https://example.com"),
kStandardCookieLine);
// Cookie is added to store.
auto store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 1u);
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest,
FiltersOutGoogleCookiesDespiteException) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
SetFloatingSsoDomainBlocklistExceptionsPolicy("google.com");
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://google.com"),
kStandardCookieLine));
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://accounts.google.com"),
kStandardCookieLine));
// Cookies are not added to store.
auto store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 0u);
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, RespectsBlockAndExemptListUpdates) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
SetFloatingSsoDomainBlocklistPolicy("*");
SetFloatingSsoDomainBlocklistExceptionsPolicy("example.com");
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://mail.com"),
kStandardCookieLine));
AddCookieAndWaitForCommit(cookie_manager(), GURL("https://example.com"),
kStandardCookieLine);
// Only the example.com cookie is added to the store.
const auto& store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 1u);
constexpr char kExampleCookieUniqueKey[] = "trueCookieNameexample.com/2443";
EXPECT_TRUE(store_entries.contains(kExampleCookieUniqueKey));
// Update the block and exception list.
SetFloatingSsoDomainBlocklistPolicy("example.com");
SetFloatingSsoDomainBlocklistExceptionsPolicy("");
// Changing the cookie URL for mail.com to not trigger an update but an
// insert.
AddCookieAndWaitForCommit(cookie_manager(), GURL("https://sub.mail.com/"),
kStandardCookieLine);
ASSERT_TRUE(SetCookie(cookie_manager(), GURL("https://example.com"),
kStandardCookieLine));
// sub.mail.com cookie is added to store. The store still contains the
// example.com cookie.
EXPECT_EQ(store_entries.size(), 2u);
EXPECT_TRUE(store_entries.contains(kExampleCookieUniqueKey));
EXPECT_TRUE(store_entries.contains("trueCookieNamesub.mail.com/2443"));
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, CookiesFromSyncBlocked) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
SetFloatingSsoDomainBlocklistPolicy("example.com");
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
// Set up a listener for cookie change events, we expect to observe one change
// below.
base::test::TestFuture<const net::CookieChangeInfo&> cookie_change_future;
CookieChangeListener listener(cookie_manager(),
cookie_change_future.GetRepeatingCallback());
FloatingSsoSyncBridge& bridge =
CHECK_DEREF(floating_sso_service().GetBridgeForTesting());
// Create two sync changes.
syncer::EntityChangeList change_list;
// Addition of a new persistent cookie with the example.com domain. We expect
// it to not pass our filters because of the blocklist.
change_list.push_back(syncer::EntityChange::CreateAdd(
kUniqueKeysForTests[1],
CreateEntityDataForTest(CreatePredefinedCookieSpecificsForTest(
1, /*creation_time=*/base::Time::Now(), /*persistent=*/true))));
// Addition of a new persistent cookie with the test.com domain. We expect it
// to pass our filters since it does not match the blocklist.
constexpr char kTestCookieUniqueKey[] = "trueCookieNametest.com/2443";
change_list.push_back(syncer::EntityChange::CreateAdd(
kTestCookieUniqueKey,
CreateEntityDataForTest(
CreateCookieSpecificsForTest(kTestCookieUniqueKey, kCookieName,
/*creation_time=*/base::Time::Now(),
/*persistent=*/true, "www.test.com"))));
bridge.ApplyIncrementalSyncChanges(bridge.CreateMetadataChangeList(),
std::move(change_list));
// We expect one cookie to be added (`net::CookieChangeCause::INSERTED`). The
// other cookie doesn't trigger any event because it is filtered out based on
// the blocklist and doesn't get added to the cookie jar.
EXPECT_EQ(cookie_change_future.Take().cause,
net::CookieChangeCause::INSERTED);
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, KeepsThirdPartyCookies) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
AddCookieAndWaitForCommit(
cookie_manager(), kNonGoogleURL,
"CookieName=CookieValue; SameSite=None; Secure; max-age=3600");
// Cookie is added to store.
auto store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 1u);
EXPECT_TRUE(store_entries.contains(kCookieUniqueKey));
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, AddsAndDeletesCookiesToStore) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
// Add cookie.
AddCookieAndWaitForCommit(cookie_manager(), kNonGoogleURL,
kStandardCookieLine);
// Cookie is added to store.
const auto& store_entries = GetStoreEntries();
EXPECT_EQ(store_entries.size(), 1u);
EXPECT_TRUE(store_entries.contains(kCookieUniqueKey));
// Update cookie.
UpdateCookieAndWaitForCommit(cookie_manager(), kNonGoogleURL, kCookieName);
EXPECT_EQ(store_entries.size(), 1u);
// Delete cookie.
DeleteCookieAndWaitForCommit(cookie_manager(), kNonGoogleURL, kCookieName);
EXPECT_EQ(store_entries.size(), 0u);
}
// Verify that `FloatingSsoService` reacts to cookie changes arriving to
// `FloatingSsoSyncBridge` by applying corresponding changes to the browser.
IN_PROC_BROWSER_TEST_F(FloatingSsoTest, ApplyingChangesFromSync) {
// Populate cookie jar with a cookie. We create a cookie from predefined
// specifics just for convenience (easier to use its sync storage key when we
// need it below).
sync_pb::CookieSpecifics existing_local_cookie_specifics =
CreatePredefinedCookieSpecificsForTest(
0, /*creation_time=*/base::Time::Now(), /*persistent=*/true);
std::unique_ptr<net::CanonicalCookie> existing_local_cookie =
FromSyncProto(existing_local_cookie_specifics);
ASSERT_TRUE(existing_local_cookie);
ASSERT_TRUE(SetCookie(cookie_manager(), *existing_local_cookie));
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
// Set up a listener for cookie change events, we expect to observe two
// changes below.
base::test::TestFuture<std::vector<net::CookieChangeInfo>>
cookie_change_future;
CookieChangeListener listener(
cookie_manager(),
base::BarrierCallback<const net::CookieChangeInfo&>(
/*num_callbacks=*/2, cookie_change_future.GetRepeatingCallback()));
// This change list will only contain a Sync change which shouldn't result in
// any browser changes.
syncer::EntityChangeList no_op_change_list;
// Addition of a new session cookie: we expect that we will not add it
// to the cookie jar since session cookies are not synced by default. Note
// that they might still come from the sync server if in the past they were
// synced based on a different policy configuration.
no_op_change_list.push_back(syncer::EntityChange::CreateAdd(
kUniqueKeysForTests[2],
CreateEntityDataForTest(CreatePredefinedCookieSpecificsForTest(
2, /*creation_time=*/base::Time::Now(), /*persistent=*/false))));
FloatingSsoSyncBridge& bridge =
CHECK_DEREF(floating_sso_service().GetBridgeForTesting());
// Apply `no_op_change_list`. We do it before applying other changes to be
// sure that in case of a bug the cookie change from this call would appear by
// the time we check all observed cookie changes at the end of the test.
bridge.ApplyIncrementalSyncChanges(bridge.CreateMetadataChangeList(),
std::move(no_op_change_list));
// Create two sync changes which will affect the browser.
syncer::EntityChangeList change_list;
// Deletion of a cookie we added at the start of the test. We expect it to
// eventually generate a `net::CookieChangeCause::EXPLICIT` event.
change_list.push_back(syncer::EntityChange::CreateDelete(
existing_local_cookie_specifics.unique_key(),
CreateEntityDataForTest(sync_pb::CookieSpecifics())));
// Addition of a new persistent cookie: we expect it to pass our filters and
// eventually generate a `net::CookieChangeCause::INSERTED` event.
change_list.push_back(syncer::EntityChange::CreateAdd(
kUniqueKeysForTests[1],
CreateEntityDataForTest(CreatePredefinedCookieSpecificsForTest(
1, /*creation_time=*/base::Time::Now(), /*persistent=*/true))));
// Apply changes via the bridge - this should in turn notify
// `FloatingSsoService`, which is expected to notify the cookie manager.
bridge.ApplyIncrementalSyncChanges(bridge.CreateMetadataChangeList(),
std::move(change_list));
// We expect one cookie to be deleted (corresponds to
// `net::CookieChangeCause::EXPLICIT`) and one cookie to be added
// (`net::CookieChangeCause::INSERTED`).
EXPECT_THAT(cookie_change_future.Take(),
testing::UnorderedElementsAre(
testing::Field("cause", &net::CookieChangeInfo::cause,
net::CookieChangeCause::EXPLICIT),
testing::Field("cause", &net::CookieChangeInfo::cause,
net::CookieChangeCause::INSERTED)));
}
IN_PROC_BROWSER_TEST_F(FloatingSsoTest,
KeepsSessionCookiesIfFloatingWorkspaceEnabled) {
auto& service = floating_sso_service();
EnableAllFloatingSsoSettings();
// Note that we enable Floating Workspace only here - when browser is
// already initialized during fixture setup. As such, this test
// doesn't test any Floating Workspace functionality on session
// startup. This would require additional test setup because if we just enable
// Floating Workspace we will end up with uninitialized browser and
// InProcessBrowserTest::browser() will return nullptr resulting in crashes.
EnableFloatingWorkspace();
ASSERT_TRUE(service.IsBoundToCookieManagerForTesting());
const auto& store_entries = GetStoreEntries();
ASSERT_TRUE(store_entries.empty());
AddCookieAndWaitForCommit(cookie_manager(), kNonGoogleURL,
"CookieName=CookieValue");
EXPECT_EQ(store_entries.size(), 1u);
}
} // namespace ash::floating_sso
|