From 82984ed40f8fc5960075f3d0c45baa041181b6ea Mon Sep 17 00:00:00 2001 From: Tim Huang Date: Tue, 30 Nov 2021 13:40:32 +0000 Subject: [PATCH] Bug 1743236 - Part 1: Add StoragePrincipalHelper::ShouldUsePartitionPrincipalForServiceWorker() for WorkerPrivate. r=dimi Differential Revision: https://phabricator.services.mozilla.com/D132369 --- .../antitracking/StoragePrincipalHelper.cpp | 24 +++++++++++++++++++ .../antitracking/StoragePrincipalHelper.h | 6 ++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/toolkit/components/antitracking/StoragePrincipalHelper.cpp b/toolkit/components/antitracking/StoragePrincipalHelper.cpp index c2e7f4f60f42..71f2ffc34829 100644 --- a/toolkit/components/antitracking/StoragePrincipalHelper.cpp +++ b/toolkit/components/antitracking/StoragePrincipalHelper.cpp @@ -8,6 +8,7 @@ #include "mozilla/ipc/PBackgroundSharedTypes.h" #include "mozilla/ContentBlocking.h" +#include "mozilla/dom/WorkerPrivate.h" #include "mozilla/ScopeExit.h" #include "mozilla/StaticPrefs_privacy.h" #include "mozilla/StorageAccess.h" @@ -406,6 +407,29 @@ bool StoragePrincipalHelper::ShouldUsePartitionPrincipalForServiceWorker( : aDocShell->GetBrowsingContext()); } +// static +bool StoragePrincipalHelper::ShouldUsePartitionPrincipalForServiceWorker( + dom::WorkerPrivate* aWorkerPrivate) { + MOZ_ASSERT(aWorkerPrivate); + + // We don't use the partitioned principal for service workers if it's + // disabled. + if (!StaticPrefs::privacy_partition_serviceWorkers()) { + return false; + } + + nsCOMPtr cookieJarSettings = + aWorkerPrivate->CookieJarSettings(); + + // We only support partitioned service workers when dFPI is enabled. + if (cookieJarSettings->GetCookieBehavior() != + nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN) { + return false; + } + + return aWorkerPrivate->IsThirdPartyContextToTopWindow(); +} + // static bool StoragePrincipalHelper::GetOriginAttributes( nsIChannel* aChannel, mozilla::OriginAttributes& aAttributes, diff --git a/toolkit/components/antitracking/StoragePrincipalHelper.h b/toolkit/components/antitracking/StoragePrincipalHelper.h index e2b2ae768793..b3c429d6fee8 100644 --- a/toolkit/components/antitracking/StoragePrincipalHelper.h +++ b/toolkit/components/antitracking/StoragePrincipalHelper.h @@ -222,7 +222,8 @@ namespace mozilla { namespace dom { class Document; -} +class WorkerPrivate; +} // namespace dom namespace ipc { class PrincipalInfo; @@ -301,6 +302,9 @@ class StoragePrincipalHelper final { static bool ShouldUsePartitionPrincipalForServiceWorker( nsIDocShell* aDocShell); + static bool ShouldUsePartitionPrincipalForServiceWorker( + dom::WorkerPrivate* aWorkerPrivate); + /** * Extract the right OriginAttributes from the channel's triggering * principal.