From bf00e6e2cfadbfeb345cfeb1007711aba684922a Mon Sep 17 00:00:00 2001 From: Tim Huang Date: Tue, 30 Nov 2021 13:40:31 +0000 Subject: [PATCH] Bug 1731999 - Part 2: Use regular principal for service workers if pref 'privacy.partition.serviceWorkers' is false. r=pbz Differential Revision: https://phabricator.services.mozilla.com/D131787 --- dom/base/nsGlobalWindowInner.cpp | 7 ++++++- dom/clients/manager/ClientChannelHelper.cpp | 15 ++++++++++++--- dom/serviceworkers/ServiceWorkerContainer.cpp | 6 +++++- .../ServiceWorkerInterceptController.cpp | 6 +++++- dom/serviceworkers/ServiceWorkerManager.cpp | 9 ++++++--- dom/serviceworkers/ServiceWorkerPrivate.cpp | 17 ++++++++++++----- .../antitracking/StoragePrincipalHelper.cpp | 7 +++++++ 7 files changed, 53 insertions(+), 14 deletions(-) diff --git a/dom/base/nsGlobalWindowInner.cpp b/dom/base/nsGlobalWindowInner.cpp index 1acf2e6f0c13..9cf9ece8bdbd 100644 --- a/dom/base/nsGlobalWindowInner.cpp +++ b/dom/base/nsGlobalWindowInner.cpp @@ -81,6 +81,7 @@ #include "mozilla/Sprintf.h" #include "mozilla/StaticPrefs_browser.h" #include "mozilla/StaticPrefs_dom.h" +#include "mozilla/StaticPrefs_privacy.h" #include "mozilla/StorageAccess.h" #include "mozilla/StoragePrincipalHelper.h" #include "mozilla/TaskCategory.h" @@ -1866,8 +1867,12 @@ nsresult nsGlobalWindowInner::EnsureClientSource() { } nsCOMPtr foreignPartitionedPrincipal; + nsresult rv = StoragePrincipalHelper::GetPrincipal( - this, StoragePrincipalHelper::eForeignPartitionedPrincipal, + this, + StaticPrefs::privacy_partition_serviceWorkers() + ? StoragePrincipalHelper::eForeignPartitionedPrincipal + : StoragePrincipalHelper::eRegularPrincipal, getter_AddRefs(foreignPartitionedPrincipal)); NS_ENSURE_SUCCESS(rv, rv); diff --git a/dom/clients/manager/ClientChannelHelper.cpp b/dom/clients/manager/ClientChannelHelper.cpp index 2d047914ac6a..f36cbb53191b 100644 --- a/dom/clients/manager/ClientChannelHelper.cpp +++ b/dom/clients/manager/ClientChannelHelper.cpp @@ -12,6 +12,7 @@ #include "mozilla/dom/ClientsBinding.h" #include "mozilla/dom/ServiceWorkerDescriptor.h" #include "mozilla/ipc/BackgroundUtils.h" +#include "mozilla/StaticPrefs_privacy.h" #include "mozilla/StoragePrincipalHelper.h" #include "nsContentUtils.h" #include "nsIAsyncVerifyRedirectCallback.h" @@ -111,7 +112,9 @@ class ClientChannelHelper : public nsIInterfaceRequestor, nsCOMPtr foreignPartitionedPrincipal; rv = StoragePrincipalHelper::GetPrincipal( aNewChannel, - StoragePrincipalHelper::eForeignPartitionedPrincipal, + StaticPrefs::privacy_partition_serviceWorkers() + ? StoragePrincipalHelper::eForeignPartitionedPrincipal + : StoragePrincipalHelper::eRegularPrincipal, getter_AddRefs(foreignPartitionedPrincipal)); NS_ENSURE_SUCCESS(rv, rv); reservedClient.reset(); @@ -132,7 +135,10 @@ class ClientChannelHelper : public nsIInterfaceRequestor, else { nsCOMPtr foreignPartitionedPrincipal; rv = StoragePrincipalHelper::GetPrincipal( - aNewChannel, StoragePrincipalHelper::eForeignPartitionedPrincipal, + aNewChannel, + StaticPrefs::privacy_partition_serviceWorkers() + ? StoragePrincipalHelper::eForeignPartitionedPrincipal + : StoragePrincipalHelper::eRegularPrincipal, getter_AddRefs(foreignPartitionedPrincipal)); NS_ENSURE_SUCCESS(rv, rv); @@ -298,7 +304,10 @@ nsresult AddClientChannelHelperInternal(nsIChannel* aChannel, nsCOMPtr channelForeignPartitionedPrincipal; nsresult rv = StoragePrincipalHelper::GetPrincipal( - aChannel, StoragePrincipalHelper::eForeignPartitionedPrincipal, + aChannel, + StaticPrefs::privacy_partition_serviceWorkers() + ? StoragePrincipalHelper::eForeignPartitionedPrincipal + : StoragePrincipalHelper::eRegularPrincipal, getter_AddRefs(channelForeignPartitionedPrincipal)); NS_ENSURE_SUCCESS(rv, rv); diff --git a/dom/serviceworkers/ServiceWorkerContainer.cpp b/dom/serviceworkers/ServiceWorkerContainer.cpp index fc7fa7aa37ca..8f75de972691 100644 --- a/dom/serviceworkers/ServiceWorkerContainer.cpp +++ b/dom/serviceworkers/ServiceWorkerContainer.cpp @@ -24,6 +24,7 @@ #include "mozilla/LoadInfo.h" #include "mozilla/SchedulerGroup.h" #include "mozilla/StaticPrefs_extensions.h" +#include "mozilla/StaticPrefs_privacy.h" #include "mozilla/StorageAccess.h" #include "mozilla/StoragePrincipalHelper.h" #include "mozilla/dom/ClientIPCTypes.h" @@ -613,7 +614,10 @@ void ServiceWorkerContainer::GetScopeForUrl(const nsAString& aUrl, nsCOMPtr principal; nsresult rv = StoragePrincipalHelper::GetPrincipal( - window, StoragePrincipalHelper::eForeignPartitionedPrincipal, + window, + StaticPrefs::privacy_partition_serviceWorkers() + ? StoragePrincipalHelper::eForeignPartitionedPrincipal + : StoragePrincipalHelper::eRegularPrincipal, getter_AddRefs(principal)); if (NS_WARN_IF(NS_FAILED(rv))) { diff --git a/dom/serviceworkers/ServiceWorkerInterceptController.cpp b/dom/serviceworkers/ServiceWorkerInterceptController.cpp index b4afb4810ed0..12abddcc5fff 100644 --- a/dom/serviceworkers/ServiceWorkerInterceptController.cpp +++ b/dom/serviceworkers/ServiceWorkerInterceptController.cpp @@ -8,6 +8,7 @@ #include "mozilla/BasePrincipal.h" #include "mozilla/StaticPrefs_dom.h" +#include "mozilla/StaticPrefs_privacy.h" #include "mozilla/StorageAccess.h" #include "mozilla/StoragePrincipalHelper.h" #include "nsCOMPtr.h" @@ -66,7 +67,10 @@ ServiceWorkerInterceptController::ShouldPrepareForIntercept( nsCOMPtr principal; nsresult rv = StoragePrincipalHelper::GetPrincipal( - aChannel, StoragePrincipalHelper::eForeignPartitionedPrincipal, + aChannel, + StaticPrefs::privacy_partition_serviceWorkers() + ? StoragePrincipalHelper::eForeignPartitionedPrincipal + : StoragePrincipalHelper::eRegularPrincipal, getter_AddRefs(principal)); NS_ENSURE_SUCCESS(rv, rv); diff --git a/dom/serviceworkers/ServiceWorkerManager.cpp b/dom/serviceworkers/ServiceWorkerManager.cpp index b4db6b1cfc11..3dfb617ca11b 100644 --- a/dom/serviceworkers/ServiceWorkerManager.cpp +++ b/dom/serviceworkers/ServiceWorkerManager.cpp @@ -61,6 +61,7 @@ #include "mozilla/PermissionManager.h" #include "mozilla/ScopeExit.h" #include "mozilla/StaticPrefs_extensions.h" +#include "mozilla/StaticPrefs_privacy.h" #include "mozilla/StoragePrincipalHelper.h" #include "mozilla/Unused.h" #include "mozilla/EnumSet.h" @@ -2044,9 +2045,11 @@ void ServiceWorkerManager::DispatchFetchEvent(nsIInterceptedChannel* aChannel, // non-subresource request means the URI contains the principal OriginAttributes attrs = loadInfo->GetOriginAttributes(); - StoragePrincipalHelper::GetOriginAttributes( - internalChannel, attrs, - StoragePrincipalHelper::eForeignPartitionedPrincipal); + if (StaticPrefs::privacy_partition_serviceWorkers()) { + StoragePrincipalHelper::GetOriginAttributes( + internalChannel, attrs, + StoragePrincipalHelper::eForeignPartitionedPrincipal); + } nsCOMPtr principal = BasePrincipal::CreateContentPrincipal(uri, attrs); diff --git a/dom/serviceworkers/ServiceWorkerPrivate.cpp b/dom/serviceworkers/ServiceWorkerPrivate.cpp index adac950c0e18..c42a6fe511af 100644 --- a/dom/serviceworkers/ServiceWorkerPrivate.cpp +++ b/dom/serviceworkers/ServiceWorkerPrivate.cpp @@ -53,6 +53,7 @@ #include "mozilla/Telemetry.h" #include "mozilla/DebugOnly.h" #include "mozilla/StaticPrefs_dom.h" +#include "mozilla/StaticPrefs_privacy.h" #include "mozilla/Unused.h" #include "nsIReferrerInfo.h" @@ -1691,12 +1692,18 @@ nsresult ServiceWorkerPrivate::SpawnWorkerIfNeeded(WakeUpReason aWhy, ->SetPartitionKey(info.mResolvedScriptURI); } - nsCOMPtr partitionedPrincipal; - StoragePrincipalHelper::CreatePartitionedPrincipalForServiceWorker( - info.mPrincipal, info.mCookieJarSettings, - getter_AddRefs(partitionedPrincipal)); + if (StaticPrefs::privacy_partition_serviceWorkers()) { + nsCOMPtr partitionedPrincipal; + StoragePrincipalHelper::CreatePartitionedPrincipalForServiceWorker( + info.mPrincipal, info.mCookieJarSettings, + getter_AddRefs(partitionedPrincipal)); - info.mPartitionedPrincipal = partitionedPrincipal; + info.mPartitionedPrincipal = partitionedPrincipal; + } else { + // The partitioned principal will be the same as the mPrincipal if + // partitioned service worker is disabled. + info.mPartitionedPrincipal = info.mPrincipal; + } info.mStorageAccess = StorageAllowedForServiceWorker(info.mPrincipal, info.mCookieJarSettings); diff --git a/toolkit/components/antitracking/StoragePrincipalHelper.cpp b/toolkit/components/antitracking/StoragePrincipalHelper.cpp index e0a09059243d..c2e7f4f60f42 100644 --- a/toolkit/components/antitracking/StoragePrincipalHelper.cpp +++ b/toolkit/components/antitracking/StoragePrincipalHelper.cpp @@ -9,6 +9,7 @@ #include "mozilla/ipc/PBackgroundSharedTypes.h" #include "mozilla/ContentBlocking.h" #include "mozilla/ScopeExit.h" +#include "mozilla/StaticPrefs_privacy.h" #include "mozilla/StorageAccess.h" #include "nsContentUtils.h" #include "nsIDocShell.h" @@ -362,6 +363,12 @@ bool StoragePrincipalHelper::ShouldUsePartitionPrincipalForServiceWorker( nsIDocShell* aDocShell) { MOZ_ASSERT(aDocShell); + // We don't use the partitioned principal for service workers if it's + // disabled. + if (!StaticPrefs::privacy_partition_serviceWorkers()) { + return false; + } + RefPtr document = aDocShell->GetExtantDocument(); // If we cannot get the document from the docShell, we turn to get its