1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/privacy_sandbox/privacy_sandbox_settings_impl.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <vector>
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/json/values_util.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/observer_list.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/browsing_topics/common/common_types.h"
#include "components/browsing_topics/common/semantic_tree.h"
#include "components/content_settings/core/browser/cookie_settings.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/pref_names.h"
#include "components/metrics/dwa/dwa_builders.h"
#include "components/metrics/dwa/dwa_recorder.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/privacy_sandbox/canonical_topic.h"
#include "components/privacy_sandbox/privacy_sandbox_attestations/privacy_sandbox_attestations.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/privacy_sandbox/privacy_sandbox_prefs.h"
#include "components/privacy_sandbox/privacy_sandbox_settings.h"
#include "components/privacy_sandbox/tpcd_experiment_eligibility.h"
#include "components/privacy_sandbox/tracking_protection_settings.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/common/content_features.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/base/schemeful_site.h"
#include "net/cookies/site_for_cookies.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/mojom/devtools/console_message.mojom.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace privacy_sandbox {
namespace {
using ::content_settings::CookieControlsMode;
constexpr char kBlockedTopicsTopicKey[] = "topic";
constexpr char kBlockedTopicsBlockTimeKey[] = "blockedOn";
constexpr char kIsTopicsAllowedHistogram[] = "PrivacySandbox.IsTopicsAllowed";
constexpr char kIsTopicsAllowedForContextHistogram[] =
"PrivacySandbox.IsTopicsAllowedForContext";
constexpr char kIsAttributionReportingEverAllowedHistogram[] =
"PrivacySandbox.IsAttributionReportingEverAllowed";
constexpr char kIsAttributionReportingAllowedHistogram[] =
"PrivacySandbox.IsAttributionReportingAllowed";
constexpr char kMaySendAttributionReportHistogram[] =
"PrivacySandbox.MaySendAttributionReport";
constexpr char kIsFledgeJoinAllowedHistogram[] =
"PrivacySandbox.IsFledgeJoinAllowed";
constexpr char kIsFledgeLeaveAllowedHistogram[] =
"PrivacySandbox.IsFledgeLeaveAllowed";
constexpr char kIsFledgeUpdateAllowedHistogram[] =
"PrivacySandbox.IsFledgeUpdateAllowed";
constexpr char kIsFledgeSellAllowedHistogram[] =
"PrivacySandbox.IsFledgeSellAllowed";
constexpr char kIsFledgeBuyAllowedHistogram[] =
"PrivacySandbox.IsFledgeBuyAllowed";
constexpr char kIsFledgeReadAllowedHistogram[] =
"PrivacySandbox.IsFledgeReadAllowed";
constexpr char kIsPrivacySandboxReportingDestinationAttestedHistogram[] =
"PrivacySandbox.IsPrivacySandboxReportingDestinationAttested";
constexpr char kIsSharedStorageAllowedHistogram[] =
"PrivacySandbox.IsSharedStorageAllowed";
constexpr char kIsSharedStorageSelectURLAllowedHistogram[] =
"PrivacySandbox.IsSharedStorageSelectURLAllowed";
constexpr char kIsFencedStorageReadAllowedHistogram[] =
"PrivacySandbox.IsFencedStorageReadAllowed";
constexpr char kIsPrivateAggregationAllowedHistogram[] =
"PrivacySandbox.IsPrivateAggregationAllowed";
bool IsCookiesClearOnExitEnabled(HostContentSettingsMap* map) {
return map->GetDefaultContentSetting(ContentSettingsType::COOKIES) ==
ContentSetting::CONTENT_SETTING_SESSION_ONLY;
}
// Convert a stored FLEDGE block eTLD+1 into applicable content settings
// patterns. This ensures that if Public Suffix List membership changes, the
// stored item continues to match as when it was set. Multiple patterns are set
// to support IP address fallbacks, which do not support [*.] prefixes.
// TODO (crbug.com/1287153): This is somewhat hacky and can be removed when
// FLEDGE is controlled by a content setting directly.
std::vector<ContentSettingsPattern> FledgeBlockToContentSettingsPatterns(
const std::string& entry) {
return {ContentSettingsPattern::FromString("[*.]" + entry),
ContentSettingsPattern::FromString(entry)};
}
// Returns a base::Value::Dict for storage in prefs that represents |topic|
// blocked at the current time.
base::Value::Dict CreateBlockedTopicEntry(const CanonicalTopic& topic) {
return base::Value::Dict()
.Set(kBlockedTopicsTopicKey, topic.ToValue())
.Set(kBlockedTopicsBlockTimeKey, base::TimeToValue(base::Time::Now()));
}
std::set<browsing_topics::Topic> GetTopicsSetFromString(
std::string topics_string) {
if (topics_string.empty()) {
return {};
}
std::set<browsing_topics::Topic> result;
std::vector<std::string> tokens = base::SplitString(
topics_string, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
for (const std::string& token : tokens) {
int topic_id;
CHECK(base::StringToInt(token, &topic_id));
result.emplace(topic_id);
}
return result;
}
} // namespace
// static
bool PrivacySandboxSettingsImpl::IsAllowed(Status status) {
return false;
}
// static
void PrivacySandboxSettingsImpl::JoinHistogram(const char* name,
Status status) {
base::UmaHistogramEnumeration(name, status);
}
// static
void PrivacySandboxSettingsImpl::JoinFledgeHistogram(
content::InterestGroupApiOperation interest_group_api_operation,
Status status) {
switch (interest_group_api_operation) {
case content::InterestGroupApiOperation::kJoin:
JoinHistogram(kIsFledgeJoinAllowedHistogram, status);
break;
case content::InterestGroupApiOperation::kLeave:
JoinHistogram(kIsFledgeLeaveAllowedHistogram, status);
break;
case content::InterestGroupApiOperation::kUpdate:
JoinHistogram(kIsFledgeUpdateAllowedHistogram, status);
break;
case content::InterestGroupApiOperation::kSell:
JoinHistogram(kIsFledgeSellAllowedHistogram, status);
break;
case content::InterestGroupApiOperation::kBuy:
JoinHistogram(kIsFledgeBuyAllowedHistogram, status);
break;
case content::InterestGroupApiOperation::kRead:
JoinHistogram(kIsFledgeReadAllowedHistogram, status);
break;
}
}
PrivacySandboxSettingsImpl::PrivacySandboxSettingsImpl(
std::unique_ptr<Delegate> delegate,
HostContentSettingsMap* host_content_settings_map,
scoped_refptr<content_settings::CookieSettings> cookie_settings,
TrackingProtectionSettings* tracking_protection_settings,
PrefService* pref_service)
: delegate_(std::move(delegate)),
host_content_settings_map_(host_content_settings_map),
cookie_settings_(cookie_settings),
tracking_protection_settings_(tracking_protection_settings),
pref_service_(pref_service) {
CHECK(pref_service_);
CHECK(host_content_settings_map_);
CHECK(cookie_settings_);
CHECK(tracking_protection_settings_);
// "Clear on exit" causes a cookie deletion on shutdown. But for practical
// purposes, we're notifying the observers on startup (which should be
// equivalent, as no cookie operations could have happened while the profile
// was shut down).
if (IsCookiesClearOnExitEnabled(host_content_settings_map_)) {
OnCookiesCleared();
}
tracking_protection_settings_observation_.Observe(
tracking_protection_settings_);
pref_change_registrar_.Init(pref_service_);
pref_change_registrar_.Add(
prefs::kPrivacySandboxRelatedWebsiteSetsEnabled,
base::BindRepeating(
&PrivacySandboxSettingsImpl::OnRelatedWebsiteSetsEnabledPrefChanged,
base::Unretained(this)));
}
PrivacySandboxSettingsImpl::~PrivacySandboxSettingsImpl() = default;
void PrivacySandboxSettingsImpl::Shutdown() {
observers_.Clear();
delegate_.reset();
host_content_settings_map_ = nullptr;
cookie_settings_.reset();
tracking_protection_settings_ = nullptr;
pref_service_ = nullptr;
pref_change_registrar_.Reset();
tracking_protection_settings_observation_.Reset();
}
PrivacySandboxSettingsImpl::Status
PrivacySandboxSettingsImpl::GetM1TopicAllowedStatus() const {
auto control_status = GetM1PrivacySandboxApiEnabledStatus(
prefs::kPrivacySandboxM1TopicsEnabled);
auto has_appropriate_consent = delegate_->HasAppropriateTopicsConsent();
// If `control_status` indicates that Topics is allowed, then
// `has_appropriate_consent` should be true, as there is no pathway for a user
// to enable Topics controls without also granting consent. The inverse does
// not hold, as an extension or policy may disable Topics, without necessarily
// revoking user consent.
if (control_status == Status::kAllowed && !has_appropriate_consent) {
// This status will be recorded via UMA, and is indicative of an error.
return Status::kMismatchedConsent;
}
return control_status;
}
const std::set<browsing_topics::Topic>&
PrivacySandboxSettingsImpl::GetFinchDisabledTopics() {
if (finch_disabled_topics_.size() > 0) {
return finch_disabled_topics_;
}
std::string disabled_topics_string =
blink::features::kBrowsingTopicsDisabledTopicsList.Get();
finch_disabled_topics_ = GetTopicsSetFromString(disabled_topics_string);
return finch_disabled_topics_;
}
const std::set<browsing_topics::Topic>&
PrivacySandboxSettingsImpl::GetFinchPrioritizedTopics() {
if (finch_prioritized_topics_.size() > 0) {
return finch_prioritized_topics_;
}
std::string prioritized_topics_string =
blink::features::kBrowsingTopicsPrioritizedTopicsList.Get();
finch_prioritized_topics_ = GetTopicsSetFromString(prioritized_topics_string);
return finch_prioritized_topics_;
}
bool PrivacySandboxSettingsImpl::IsTopicsAllowed() const {
Status status = GetM1TopicAllowedStatus();
JoinHistogram(kIsTopicsAllowedHistogram, status);
return IsAllowed(status);
}
bool PrivacySandboxSettingsImpl::IsTopicsAllowedForContext(
const url::Origin& top_frame_origin,
const GURL& url,
content::RenderFrameHost* console_frame) const {
// Check for attestation on the calling context's site.
Status attestation_status =
PrivacySandboxAttestations::GetInstance()->IsSiteAttested(
net::SchemefulSite(url), PrivacySandboxAttestationsGatedAPI::kTopics);
if (!IsAllowed(attestation_status)) {
JoinHistogram(kIsTopicsAllowedForContextHistogram, attestation_status);
if (console_frame) {
console_frame->AddMessageToConsole(
blink::mojom::ConsoleMessageLevel::kError,
"Attestation check for Topics on " + url.spec() + " failed.");
}
return false;
}
Status status = GetM1TopicAllowedStatus();
if (IsAllowed(status)) {
status = GetSiteAccessAllowedStatus(top_frame_origin, url);
}
JoinHistogram(kIsTopicsAllowedForContextHistogram, status);
return IsAllowed(status);
}
bool PrivacySandboxSettingsImpl::IsTopicAllowed(const CanonicalTopic& topic) {
const auto& blocked_topics =
pref_service_->GetList(prefs::kPrivacySandboxBlockedTopics);
std::vector<browsing_topics::Topic> ancestor_topics =
browsing_topics::SemanticTree().GetAncestorTopics(topic.topic_id());
for (const base::Value& item : blocked_topics) {
auto blocked_topic =
CanonicalTopic::FromValue(*item.GetDict().Find(kBlockedTopicsTopicKey));
if (!blocked_topic) {
continue;
}
if ((topic.topic_id() == blocked_topic->topic_id()) ||
(base::Contains(ancestor_topics, blocked_topic->topic_id()))) {
return false;
}
}
for (browsing_topics::Topic blocked_topic_id : GetFinchDisabledTopics()) {
if ((topic.topic_id() == blocked_topic_id) ||
(base::Contains(ancestor_topics, blocked_topic_id))) {
return false;
}
}
return true;
}
void PrivacySandboxSettingsImpl::SetTopicAllowed(const CanonicalTopic& topic,
bool allowed) {
ScopedListPrefUpdate scoped_pref_update(pref_service_,
prefs::kPrivacySandboxBlockedTopics);
// Presence in the preference list indicates that a topic is blocked, as
// there is no concept of explicitly allowed topics. Thus, allowing a topic
// is the same as removing it, if it exists, from the blocklist. Blocking
// a topic is the same as adding it to the blocklist, but as duplicate entries
// are undesireable, removing any existing reference first is desireable.
// Thus, regardless of |allowed|, removing any existing reference is the
// first step.
scoped_pref_update->EraseIf([&](const base::Value& value) {
auto* blocked_topic_value = value.GetDict().Find(kBlockedTopicsTopicKey);
auto converted_topic = CanonicalTopic::FromValue(*blocked_topic_value);
return converted_topic && *converted_topic == topic;
});
// If the topic is being blocked, it can be (re)added to the blocklist. If the
// topic was removed from the blocklist above, this is equivalent to updating
// the modified time associated with the entry to the current time. As data
// deletions are typically from the current time backwards, this makes it
// more likely to be removed - a privacy improvement.
if (!allowed) {
scoped_pref_update->Append(CreateBlockedTopicEntry(topic));
}
}
bool PrivacySandboxSettingsImpl::IsTopicPrioritized(
const CanonicalTopic& topic) {
const std::set<browsing_topics::Topic>& prioritized_topics =
GetFinchPrioritizedTopics();
if (prioritized_topics.contains(topic.topic_id())) {
return true;
}
for (const browsing_topics::Topic& ancestor_topic :
browsing_topics::SemanticTree().GetAncestorTopics(topic.topic_id())) {
if (prioritized_topics.contains(ancestor_topic)) {
return true;
}
}
return false;
}
void PrivacySandboxSettingsImpl::ClearTopicSettings(base::Time start_time,
base::Time end_time) {
ScopedListPrefUpdate scoped_pref_update(pref_service_,
prefs::kPrivacySandboxBlockedTopics);
// Shortcut for maximum time range deletion.
if (start_time == base::Time() && end_time == base::Time::Max()) {
scoped_pref_update->clear();
return;
}
scoped_pref_update->EraseIf([&](const base::Value& value) {
auto blocked_time =
base::ValueToTime(value.GetDict().Find(kBlockedTopicsBlockTimeKey));
return start_time <= blocked_time && blocked_time <= end_time;
});
}
base::Time PrivacySandboxSettingsImpl::TopicsDataAccessibleSince() const {
return pref_service_->GetTime(
prefs::kPrivacySandboxTopicsDataAccessibleSince);
}
PrivacySandboxSettingsImpl::Status
PrivacySandboxSettingsImpl::GetM1AdMeasurementAllowedStatus(
const url::Origin& top_frame_origin,
const url::Origin& reporting_origin) const {
Status status = GetM1PrivacySandboxApiEnabledStatus(
prefs::kPrivacySandboxM1AdMeasurementEnabled);
if (!IsAllowed(status)) {
return status;
}
return GetSiteAccessAllowedStatus(top_frame_origin,
reporting_origin.GetURL());
}
bool PrivacySandboxSettingsImpl::IsAttributionReportingEverAllowed() const {
Status status = GetM1PrivacySandboxApiEnabledStatus(
prefs::kPrivacySandboxM1AdMeasurementEnabled);
JoinHistogram(kIsAttributionReportingEverAllowedHistogram, status);
return IsAllowed(status);
}
bool PrivacySandboxSettingsImpl::IsAttributionReportingAllowed(
const url::Origin& top_frame_origin,
const url::Origin& reporting_origin,
content::RenderFrameHost* console_frame) const {
// Check for attestation on the reporting origin.
Status attestation_status =
PrivacySandboxAttestations::GetInstance()->IsSiteAttested(
net::SchemefulSite(reporting_origin),
PrivacySandboxAttestationsGatedAPI::kAttributionReporting);
if (!IsAllowed(attestation_status)) {
JoinHistogram(kIsAttributionReportingAllowedHistogram, attestation_status);
if (console_frame) {
console_frame->AddMessageToConsole(
blink::mojom::ConsoleMessageLevel::kError,
"Attestation check for Attribution Reporting on " +
reporting_origin.Serialize() + " failed.");
}
dwa::builders::PrivacySandbox_IsAttributionReportingAllowed()
.SetContent(reporting_origin.Serialize())
.SetStatus(static_cast<int64_t>(attestation_status))
.Record(metrics::dwa::DwaRecorder::Get());
return false;
}
Status status =
GetM1AdMeasurementAllowedStatus(top_frame_origin, reporting_origin);
JoinHistogram(kIsAttributionReportingAllowedHistogram, status);
dwa::builders::PrivacySandbox_IsAttributionReportingAllowed()
.SetContent(reporting_origin.Serialize())
.SetStatus(static_cast<int64_t>(status))
.Record(metrics::dwa::DwaRecorder::Get());
return IsAllowed(status);
}
bool PrivacySandboxSettingsImpl::MaySendAttributionReport(
const url::Origin& source_origin,
const url::Origin& destination_origin,
const url::Origin& reporting_origin,
content::RenderFrameHost* console_frame) const {
// Check for attestation on the reporting origin.
Status attestation_status =
PrivacySandboxAttestations::GetInstance()->IsSiteAttested(
net::SchemefulSite(reporting_origin),
PrivacySandboxAttestationsGatedAPI::kAttributionReporting,
AttestationsDefaultBehavior::kAllow);
if (!IsAllowed(attestation_status)) {
JoinHistogram(kMaySendAttributionReportHistogram, attestation_status);
if (console_frame) {
console_frame->AddMessageToConsole(
blink::mojom::ConsoleMessageLevel::kError,
"Attestation check for Attribution Reporting on " +
reporting_origin.Serialize() + " failed.");
}
return false;
}
Status status = GetM1AdMeasurementAllowedStatus(
/*top_frame_origin=*/source_origin,
/*reporting_origin=*/reporting_origin);
if (IsAllowed(status)) {
status = GetM1AdMeasurementAllowedStatus(
/*top_frame_origin=*/destination_origin,
/*reporting_origin=*/reporting_origin);
}
JoinHistogram(kMaySendAttributionReportHistogram, status);
return IsAllowed(status);
}
bool PrivacySandboxSettingsImpl::
IsAttributionReportingTransitionalDebuggingAllowed(
const url::Origin& top_frame_origin,
const url::Origin& reporting_origin,
bool& can_bypass) const {
content_settings::CookieSettingsBase::CookieSettingWithMetadata
cookie_setting_with_metadata;
// With what is available here, we can create a cookie_partition_key for the
// given top_frame_origin and we can assume the ancestor chain bit is
// cross_site since the `IsFullCookieAccessAllowed` call below uses a null
// `SiteForCookies`, which means that we will always be in a cross site
// context.
net::SchemefulSite top_frame_site(top_frame_origin);
std::optional<net::CookiePartitionKey> cookie_partition_key =
net::CookiePartitionKey::FromStorageKeyComponents(
top_frame_site,
net::CookiePartitionKey::BoolToAncestorChainBit(/*cross_site=*/true),
/*nonce=*/std::nullopt);
// Third party cookies must also be available for this context. An empty site
// for cookies is provided so the context is always treated as a third party.
bool allowed = cookie_settings_->IsFullCookieAccessAllowed(
reporting_origin.GetURL(), net::SiteForCookies(), top_frame_origin,
net::CookieSettingOverrides(), cookie_partition_key,
&cookie_setting_with_metadata);
if (base::FeatureList::IsEnabled(
kAttributionDebugReportingCookieDeprecationTesting)) {
can_bypass =
cookie_setting_with_metadata.BlockedByThirdPartyCookieBlocking() &&
delegate_->AreThirdPartyCookiesBlockedByCookieDeprecationExperiment();
} else {
can_bypass = false;
}
return allowed;
}
void PrivacySandboxSettingsImpl::SetFledgeJoiningAllowed(
const std::string& top_frame_etld_plus1,
bool allowed) {
ScopedDictPrefUpdate scoped_pref_update(
pref_service_, prefs::kPrivacySandboxFledgeJoinBlocked);
// Ensure that the provided etld_plus1 actually is an etld+1.
auto effective_top_frame_etld_plus1 =
net::registry_controlled_domains::GetDomainAndRegistry(
top_frame_etld_plus1,
net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
// Hosts are also accepted as a fallback. This may occur if the private
// registry has changed, and what the caller may be assuming is an eTLD+1 no
// longer is. Simply ignoring non-eTLD+1's may thus result in unexpected
// access.
if (effective_top_frame_etld_plus1 != top_frame_etld_plus1) {
// Add a dummy scheme and use GURL to confirm the provided string is a valid
// host.
const GURL url("https://" + top_frame_etld_plus1);
effective_top_frame_etld_plus1 = url.host();
}
// Ignore attempts to configure an empty etld+1. This will also catch the
// case where the eTLD+1 was not even a host, as GURL will have canonicalised
// it to empty.
if (effective_top_frame_etld_plus1.length() == 0) {
NOTREACHED() << "Cannot control FLEDGE joining for empty eTLD+1";
}
if (allowed) {
// Existence of the key implies blocking, so simply removing the key is
// sufficient. If the key wasn't already present, the following is a no-op.
scoped_pref_update->Remove(effective_top_frame_etld_plus1);
} else {
// Overriding the creation date for keys which already exist is acceptable.
// Time range based deletions are typically started from the current time,
// and so this will be more aggressively removed. This decreases the chance
// a potentially sensitive website remains in preferences.
scoped_pref_update->Set(effective_top_frame_etld_plus1,
base::TimeToValue(base::Time::Now()));
}
}
void PrivacySandboxSettingsImpl::ClearFledgeJoiningAllowedSettings(
base::Time start_time,
base::Time end_time) {
ScopedDictPrefUpdate scoped_pref_update(
pref_service_, prefs::kPrivacySandboxFledgeJoinBlocked);
auto& pref_data = scoped_pref_update.Get();
// Shortcut for maximum time range deletion
if (start_time == base::Time() && end_time == base::Time::Max()) {
pref_data.clear();
return;
}
std::vector<std::string> keys_to_remove;
for (auto entry : pref_data) {
std::optional<base::Time> created_time = base::ValueToTime(entry.second);
if (created_time.has_value() && start_time <= created_time &&
created_time <= end_time) {
keys_to_remove.push_back(entry.first);
}
}
for (const auto& key : keys_to_remove) {
pref_data.Remove(key);
}
}
bool PrivacySandboxSettingsImpl::IsFledgeJoiningAllowed(
const url::Origin& top_frame_origin) const {
ScopedDictPrefUpdate scoped_pref_update(
pref_service_, prefs::kPrivacySandboxFledgeJoinBlocked);
auto& pref_data = scoped_pref_update.Get();
for (auto entry : pref_data) {
if (std::ranges::any_of(FledgeBlockToContentSettingsPatterns(entry.first),
[&](const auto& pattern) {
return pattern.Matches(top_frame_origin.GetURL());
})) {
return false;
}
}
return true;
}
PrivacySandboxSettingsImpl::Status
PrivacySandboxSettingsImpl::GetM1FledgeAllowedStatus(
const url::Origin& top_frame_origin,
const url::Origin& auction_party) const {
Status status = GetM1PrivacySandboxApiEnabledStatus(
prefs::kPrivacySandboxM1FledgeEnabled);
if (!IsAllowed(status)) {
return status;
}
return GetSiteAccessAllowedStatus(top_frame_origin, auction_party.GetURL());
}
PrivacySandboxSettingsImpl::Status
PrivacySandboxSettingsImpl::GetFencedStorageReadEnabledStatus() const {
// User has turned on the setting to block all third party cookies.
if (cookie_settings_->ShouldBlockThirdPartyCookies() &&
!cookie_settings_->AreThirdPartyCookiesLimited()) {
return Status::kApisDisabled;
}
// This feature is default enabled when 3PCs are not blocked.
return Status::kAllowed;
}
bool PrivacySandboxSettingsImpl::IsEventReportingDestinationAttested(
const url::Origin& destination_origin,
privacy_sandbox::PrivacySandboxAttestationsGatedAPI invoking_api) const {
// Check for attestation on the event recipient's site with whichever API
// created the frame that invoked the event reporting.
Status attestation_status =
PrivacySandboxAttestations::GetInstance()->IsSiteAttested(
net::SchemefulSite(destination_origin), invoking_api);
JoinHistogram(kIsPrivacySandboxReportingDestinationAttestedHistogram,
attestation_status);
return IsAllowed(attestation_status);
}
bool PrivacySandboxSettingsImpl::IsFledgeAllowed(
const url::Origin& top_frame_origin,
const url::Origin& auction_party,
content::InterestGroupApiOperation interest_group_api_operation,
content::RenderFrameHost* console_frame) const {
// Check for attestation on the auction party's site. The auction party is a
// variety of entities during the auction, all of which need to be attested.
Status attestation_status =
PrivacySandboxAttestations::GetInstance()->IsSiteAttested(
net::SchemefulSite(auction_party),
PrivacySandboxAttestationsGatedAPI::kProtectedAudience);
if (!IsAllowed(attestation_status)) {
JoinFledgeHistogram(interest_group_api_operation, attestation_status);
if (console_frame) {
console_frame->AddMessageToConsole(
blink::mojom::ConsoleMessageLevel::kError,
"Attestation check for Protected Audience on " +
auction_party.Serialize() + " failed.");
}
return false;
}
if (interest_group_api_operation ==
content::InterestGroupApiOperation::kJoin &&
!IsFledgeJoiningAllowed(top_frame_origin)) {
JoinFledgeHistogram(interest_group_api_operation,
Status::kJoiningTopFrameBlocked);
return false;
}
Status status = GetM1FledgeAllowedStatus(top_frame_origin, auction_party);
JoinFledgeHistogram(interest_group_api_operation, status);
return IsAllowed(status);
}
bool PrivacySandboxSettingsImpl::IsSharedStorageAllowed(
const url::Origin& top_frame_origin,
const url::Origin& accessing_origin,
std::string* out_debug_message,
content::RenderFrameHost* console_frame,
bool* out_block_is_site_setting_specific) const {
// Check for attestation on the caller's site.
Status attestation_status =
PrivacySandboxAttestations::GetInstance()->IsSiteAttested(
net::SchemefulSite(accessing_origin),
PrivacySandboxAttestationsGatedAPI::kSharedStorage);
SetOutBlockIsSiteSettingSpecificFromStatus(
attestation_status, out_block_is_site_setting_specific);
if (!IsAllowed(attestation_status)) {
JoinHistogram(kIsSharedStorageAllowedHistogram, attestation_status);
std::string error_message =
base::StrCat({"Attestation check for Shared Storage on ",
accessing_origin.Serialize(), " failed."});
if (out_debug_message) {
*out_debug_message = base::StrCat(
{error_message, "\nReturned status ",
base::NumberToString(int(attestation_status)),
"; see `PrivacySandboxSettingsImpl::Status` at ",
"https://chromium.googlesource.com/chromium/src/+/refs/heads/main/",
"components/privacy_sandbox/privacy_sandbox_settings_impl.h."});
}
if (console_frame) {
console_frame->AddMessageToConsole(
blink::mojom::ConsoleMessageLevel::kError, error_message);
}
return false;
}
Status status = GetPrivacySandboxAllowedStatus();
SetOutBlockIsSiteSettingSpecificFromStatus(
status, out_block_is_site_setting_specific);
if (IsAllowed(status)) {
status =
GetSiteAccessAllowedStatus(top_frame_origin, accessing_origin.GetURL());
SetOutBlockIsSiteSettingSpecificFromStatus(
status, out_block_is_site_setting_specific);
if (out_debug_message) {
*out_debug_message = base::StrCat(
{"Site access settings returned status ",
base::NumberToString(int(status)), " for accessing origin ",
accessing_origin.Serialize(), " and top-frame origin ",
top_frame_origin.Serialize(),
"; see `PrivacySandboxSettingsImpl::Status` at ",
"https://chromium.googlesource.com/chromium/src/+/refs/heads/main/",
"components/privacy_sandbox/privacy_sandbox_settings_impl.h."});
}
} else if (out_debug_message) {
*out_debug_message = base::StrCat(
{"Privacy Sandbox settings returned status ",
base::NumberToString(int(status)),
"; see `PrivacySandboxSettingsImpl::Status` at ",
"https://chromium.googlesource.com/chromium/src/+/refs/heads/main/",
"components/privacy_sandbox/privacy_sandbox_settings_impl.h."});
}
JoinHistogram(kIsSharedStorageAllowedHistogram, status);
return IsAllowed(status);
}
bool PrivacySandboxSettingsImpl::IsSharedStorageSelectURLAllowed(
const url::Origin& top_frame_origin,
const url::Origin& accessing_origin,
std::string* out_debug_message,
bool* out_block_is_site_setting_specific) const {
Status status = GetM1FledgeAllowedStatus(top_frame_origin, accessing_origin);
SetOutBlockIsSiteSettingSpecificFromStatus(
status, out_block_is_site_setting_specific);
JoinHistogram(kIsSharedStorageSelectURLAllowedHistogram, status);
if (out_debug_message) {
*out_debug_message = base::StrCat(
{"M1 measurement settings returned status ",
base::NumberToString(int(status)), " for accessing origin ",
accessing_origin.Serialize(), " and top-frame origin ",
top_frame_origin.Serialize(),
"; see `PrivacySandboxSettingsImpl::Status` at ",
"https://chromium.googlesource.com/chromium/src/+/refs/heads/main/",
"components/privacy_sandbox/privacy_sandbox_settings_impl.h."});
}
return IsAllowed(status);
}
bool PrivacySandboxSettingsImpl::IsFencedStorageReadAllowed(
const url::Origin& top_frame_origin,
const url::Origin& accessing_origin,
content::RenderFrameHost* console_frame) const {
if (Status status = GetFencedStorageReadEnabledStatus(); !IsAllowed(status)) {
JoinHistogram(kIsFencedStorageReadAllowedHistogram, status);
if (console_frame) {
console_frame->AddMessageToConsole(
blink::mojom::ConsoleMessageLevel::kError,
"Fenced storage read is disabled because all third-party cookies are "
"blocked.");
}
return false;
}
Status attestation_status =
PrivacySandboxAttestations::GetInstance()->IsSiteAttested(
net::SchemefulSite(accessing_origin),
PrivacySandboxAttestationsGatedAPI::kFencedStorageRead);
if (!IsAllowed(attestation_status)) {
JoinHistogram(kIsFencedStorageReadAllowedHistogram, attestation_status);
if (console_frame) {
console_frame->AddMessageToConsole(
blink::mojom::ConsoleMessageLevel::kError,
base::StrCat({"Attestation check for fenced storage read on ",
accessing_origin.Serialize(), " failed."}));
}
return false;
}
Status status = GetPrivacySandboxAllowedStatus();
if (IsAllowed(status)) {
status =
GetSiteAccessAllowedStatus(top_frame_origin, accessing_origin.GetURL());
}
JoinHistogram(kIsFencedStorageReadAllowedHistogram, status);
return IsAllowed(status);
}
bool PrivacySandboxSettingsImpl::IsPrivateAggregationAllowed(
const url::Origin& top_frame_origin,
const url::Origin& reporting_origin,
bool* out_block_is_site_setting_specific) const {
// Check for attestation on the worklet's site.
Status attestation_status =
PrivacySandboxAttestations::GetInstance()->IsSiteAttested(
net::SchemefulSite(reporting_origin),
PrivacySandboxAttestationsGatedAPI::kPrivateAggregation);
SetOutBlockIsSiteSettingSpecificFromStatus(
attestation_status, out_block_is_site_setting_specific);
if (!IsAllowed(attestation_status)) {
JoinHistogram(kIsPrivateAggregationAllowedHistogram, attestation_status);
dwa::builders::PrivacySandbox_IsPrivateAggregationAllowed()
.SetContent(reporting_origin.Serialize())
.SetStatus(static_cast<int64_t>(attestation_status))
.Record(metrics::dwa::DwaRecorder::Get());
return false;
}
Status status =
GetM1AdMeasurementAllowedStatus(top_frame_origin, reporting_origin);
SetOutBlockIsSiteSettingSpecificFromStatus(
status, out_block_is_site_setting_specific);
JoinHistogram(kIsPrivateAggregationAllowedHistogram, status);
dwa::builders::PrivacySandbox_IsPrivateAggregationAllowed()
.SetContent(reporting_origin.Serialize())
.SetStatus(static_cast<int64_t>(status))
.Record(metrics::dwa::DwaRecorder::Get());
return IsAllowed(status);
}
bool PrivacySandboxSettingsImpl::IsPrivateAggregationDebugModeAllowed(
const url::Origin& top_frame_origin,
const url::Origin& reporting_origin) const {
if (!IsPrivateAggregationAllowed(
top_frame_origin, reporting_origin,
/*out_block_is_site_setting_specific=*/nullptr)) {
return false;
}
// If this feature is disabled, provide a top-frame origin anyway to match
// previous behavior.
std::optional<url::Origin> top_frame_origin_to_query;
if (!base::FeatureList::IsEnabled(
kPrivateAggregationDebugReportingIgnoreSiteExceptions)) {
top_frame_origin_to_query = top_frame_origin;
}
// With what is available here, we can create a cookie_partition_key for the
// given top_frame_origin and we can assume the ancestor chain bit is
// cross_site since the `IsFullCookieAccessAllowed` call below uses a null
// `SiteForCookies`, which means that we will always be in a cross site
// context.
net::SchemefulSite top_frame_site(top_frame_origin);
std::optional<net::CookiePartitionKey> cookie_partition_key =
net::CookiePartitionKey::FromStorageKeyComponents(
top_frame_site,
net::CookiePartitionKey::BoolToAncestorChainBit(/*cross_site=*/true),
/*nonce=*/std::nullopt);
// Third party cookies must also be available for this context. An empty site
// for cookies and empty top-frame origin is provided so the context is always
// treated as a third party. That is, we ignore any top-level site cookie
// exceptions (see crbug.com/364318217).
content_settings::CookieSettingsBase::CookieSettingWithMetadata
cookie_setting_with_metadata;
if (cookie_settings_->IsFullCookieAccessAllowed(
reporting_origin.GetURL(), net::SiteForCookies(),
top_frame_origin_to_query, net::CookieSettingOverrides(),
cookie_partition_key, &cookie_setting_with_metadata)) {
return true;
}
// Third-party cookie access is disabled, but we may still allow Private
// Aggregation's debug mode in this context if it was only blocked due to the
// 3PCD experiment.
return base::FeatureList::IsEnabled(
kPrivateAggregationDebugReportingCookieDeprecationTesting) &&
cookie_setting_with_metadata.BlockedByThirdPartyCookieBlocking() &&
delegate_->AreThirdPartyCookiesBlockedByCookieDeprecationExperiment();
}
void PrivacySandboxSettingsImpl::SetAllPrivacySandboxAllowedForTesting() {
pref_service_->SetBoolean(prefs::kPrivacySandboxM1FledgeEnabled, true);
pref_service_->SetBoolean(prefs::kPrivacySandboxM1TopicsEnabled, true);
pref_service_->SetBoolean(prefs::kPrivacySandboxM1AdMeasurementEnabled, true);
}
void PrivacySandboxSettingsImpl::SetTopicsBlockedForTesting() {
pref_service_->SetBoolean(prefs::kPrivacySandboxM1TopicsEnabled, false);
}
bool PrivacySandboxSettingsImpl::IsPrivacySandboxRestricted() const {
return true;
}
bool PrivacySandboxSettingsImpl::IsPrivacySandboxCurrentlyUnrestricted() const {
return delegate_->IsPrivacySandboxCurrentlyUnrestricted();
}
bool PrivacySandboxSettingsImpl::IsSubjectToM1NoticeRestricted() const {
return delegate_->IsSubjectToM1NoticeRestricted();
}
bool PrivacySandboxSettingsImpl::IsRestrictedNoticeEnabled() const {
return delegate_->IsRestrictedNoticeEnabled();
}
void PrivacySandboxSettingsImpl::OnCookiesCleared() {
SetTopicsDataAccessibleFromNow();
}
void PrivacySandboxSettingsImpl::OnRelatedWebsiteSetsEnabledPrefChanged() {
for (auto& observer : observers_) {
observer.OnRelatedWebsiteSetsEnabledChanged(AreRelatedWebsiteSetsEnabled());
}
}
void PrivacySandboxSettingsImpl::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void PrivacySandboxSettingsImpl::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void PrivacySandboxSettingsImpl::SetDelegateForTesting(
std::unique_ptr<Delegate> delegate) {
delegate_ = std::move(delegate);
}
void PrivacySandboxSettingsImpl::SetTopicsDataAccessibleFromNow() const {
pref_service_->SetTime(prefs::kPrivacySandboxTopicsDataAccessibleSince,
base::Time::Now());
for (auto& observer : observers_) {
observer.OnTopicsDataAccessibleSinceUpdated();
}
}
PrivacySandboxSettingsImpl::Status
PrivacySandboxSettingsImpl::GetSiteAccessAllowedStatus(
const url::Origin& top_frame_origin,
const GURL& url) const {
// Relying on |host_content_settings_map_| instead of |cookie_settings_|
// allows to query whether the site associated with the |url| is allowed to
// access Site data (aka ContentSettingsType::COOKIES) without considering any
// 3P cookie blocking setting.
return content_settings::CookieSettingsBase::IsAllowed(
host_content_settings_map_->GetContentSetting(
url, top_frame_origin.GetURL(), ContentSettingsType::COOKIES))
? Status::kAllowed
: Status::kSiteDataAccessBlocked;
}
PrivacySandboxSettingsImpl::Status
PrivacySandboxSettingsImpl::GetPrivacySandboxAllowedStatus(
bool should_ignore_restriction /*=false*/) const {
if (delegate_->IsIncognitoProfile()) {
return Status::kIncognitoProfile;
}
return Status::kRestricted;
}
PrivacySandboxSettingsImpl::Status
PrivacySandboxSettingsImpl::GetM1PrivacySandboxApiEnabledStatus(
const std::string& pref_name) const {
DCHECK(pref_name == prefs::kPrivacySandboxM1TopicsEnabled ||
pref_name == prefs::kPrivacySandboxM1FledgeEnabled ||
pref_name == prefs::kPrivacySandboxM1AdMeasurementEnabled);
if (delegate_->IsCookieDeprecationExperimentEligible() &&
features::kCookieDeprecationTestingDisableAdsAPIs.Get()) {
return Status::kBlockedBy3pcdExperiment;
}
bool should_ignore_restriction =
pref_name == prefs::kPrivacySandboxM1AdMeasurementEnabled &&
IsRestrictedNoticeEnabled();
PrivacySandboxSettingsImpl::Status status =
GetPrivacySandboxAllowedStatus(should_ignore_restriction);
if (!IsAllowed(status)) {
return status;
}
// For Measurement and Relevance APIs, we explicitly do not require the
// underlying pref to be enabled if there is a local flag enabling the APIs to
// allow for local testing.
if (base::FeatureList::IsEnabled(
privacy_sandbox::kOverridePrivacySandboxSettingsLocalTesting)) {
return Status::kAllowed;
}
status = (pref_service_->GetBoolean(pref_name)) ? Status::kAllowed
: Status::kApisDisabled;
return status;
}
TpcdExperimentEligibility
PrivacySandboxSettingsImpl::GetCookieDeprecationExperimentCurrentEligibility()
const {
return delegate_->GetCookieDeprecationExperimentCurrentEligibility();
}
bool PrivacySandboxSettingsImpl::IsCookieDeprecationLabelAllowed() const {
return delegate_->IsCookieDeprecationLabelAllowed();
}
bool PrivacySandboxSettingsImpl::IsCookieDeprecationLabelAllowedForContext(
const url::Origin& top_frame_origin,
const url::Origin& context_origin) const {
if (!IsCookieDeprecationLabelAllowed()) {
return false;
}
return IsAllowed(
GetSiteAccessAllowedStatus(top_frame_origin, context_origin.GetURL()));
}
void PrivacySandboxSettingsImpl::OnBlockAllThirdPartyCookiesChanged() {
for (auto& observer : observers_) {
observer.OnRelatedWebsiteSetsEnabledChanged(AreRelatedWebsiteSetsEnabled());
}
}
bool PrivacySandboxSettingsImpl::AreRelatedWebsiteSetsEnabled() const {
if (tracking_protection_settings_->IsTrackingProtection3pcdEnabled()) {
return cookie_settings_->AreThirdPartyCookiesLimited();
}
return pref_service_->GetBoolean(
prefs::kPrivacySandboxRelatedWebsiteSetsEnabled);
}
void PrivacySandboxSettingsImpl::SetOutBlockIsSiteSettingSpecificFromStatus(
Status status,
bool* out_block_is_site_setting_specific) const {
if (out_block_is_site_setting_specific != nullptr) {
*out_block_is_site_setting_specific =
status == Status::kSiteDataAccessBlocked;
}
}
} // namespace privacy_sandbox
|