Bug 1731982 - Part 1: Implement StoragePrincipalHelper::ShouldUsePartitionPrincipalForServiceWorker(). r=dimi

We implement
StoragePrincipalHelper::ShouldUsePartitionPrincipalForServiceWorker() to
help with deciding the principal needed to be used when creating a
initial clientSource in docShell.

Differential Revision: https://phabricator.services.mozilla.com/D127628
This commit is contained in:
Tim Huang 2021-11-25 13:11:29 +00:00
Родитель 3509ad57ad
Коммит 8ec7198e19
2 изменённых файлов: 50 добавлений и 0 удалений

Просмотреть файл

@ -11,6 +11,7 @@
#include "mozilla/ScopeExit.h"
#include "mozilla/StorageAccess.h"
#include "nsContentUtils.h"
#include "nsIDocShell.h"
#include "nsIEffectiveTLDService.h"
namespace mozilla {
@ -320,6 +321,48 @@ nsresult StoragePrincipalHelper::GetPrincipal(nsPIDOMWindowInner* aWindow,
return NS_OK;
}
// static
bool StoragePrincipalHelper::ShouldUsePartitionPrincipalForServiceWorker(
nsIDocShell* aDocShell) {
MOZ_ASSERT(aDocShell);
RefPtr<Document> document = aDocShell->GetExtantDocument();
// If we cannot get the document from the docShell, we turn to get its
// parent's document.
if (!document) {
nsCOMPtr<nsIDocShellTreeItem> parentItem;
aDocShell->GetInProcessSameTypeParent(getter_AddRefs(parentItem));
if (parentItem) {
document = parentItem->GetDocument();
}
}
nsCOMPtr<nsICookieJarSettings> cookieJarSettings;
if (document) {
cookieJarSettings = document->CookieJarSettings();
} else {
// If there was no document, we create one cookieJarSettings here in order
// to get the cookieBehavior.
cookieJarSettings = CookieJarSettings::Create(CookieJarSettings::eRegular);
}
// We only support partitioned service workers when dFPI is enabled.
if (cookieJarSettings->GetCookieBehavior() !=
nsICookieService::BEHAVIOR_REJECT_TRACKER_AND_PARTITION_FOREIGN) {
return false;
}
// Only the third-party context will need to use the partitioned principal. A
// first-party context is still using the regular principal for the service
// worker.
return AntiTrackingUtils::IsThirdPartyContext(
document ? document->GetBrowsingContext()
: aDocShell->GetBrowsingContext());
}
// static
bool StoragePrincipalHelper::GetOriginAttributes(
nsIChannel* aChannel, mozilla::OriginAttributes& aAttributes,

Просмотреть файл

@ -212,6 +212,7 @@
class nsIChannel;
class nsICookieJarSettings;
class nsIDocShell;
class nsILoadGroup;
class nsIPrincipal;
class nsIURI;
@ -285,6 +286,12 @@ class StoragePrincipalHelper final {
PrincipalType aPrincipalType,
nsIPrincipal** aPrincipal);
// Check if we need to use the partitioned principal for the service worker of
// the given docShell. Please do not use this API unless you cannot get the
// foreign partitioned principal, e.g. creating the inital about:blank page.
static bool ShouldUsePartitionPrincipalForServiceWorker(
nsIDocShell* aDocShell);
/**
* Extract the right OriginAttributes from the channel's triggering
* principal.