From 6c821cef518c136f8d7a67a13858d098f17a4855 Mon Sep 17 00:00:00 2001 From: Tim Huang Date: Mon, 18 Oct 2021 12:20:32 +0000 Subject: [PATCH] Bug 1731982 - Part 5: Using StoragePrincipalHelper::GetPrincipal() in ClientChannelHelper. r=asuth To support foreign partitioned prinicpal for Client, we use StoragePrincipalHelper::GetPrincipal() to get the foreign partitioned principal from the channel in the ClientChannelHelper. And use it to create client. Differential Revision: https://phabricator.services.mozilla.com/D127631 --- dom/clients/manager/ClientChannelHelper.cpp | 51 ++++++++++----------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/dom/clients/manager/ClientChannelHelper.cpp b/dom/clients/manager/ClientChannelHelper.cpp index f6b7c6fc0fe4..2d047914ac6a 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/StoragePrincipalHelper.h" #include "nsContentUtils.h" #include "nsIAsyncVerifyRedirectCallback.h" #include "nsIChannel.h" @@ -107,15 +108,14 @@ class ClientChannelHelper : public nsIInterfaceRequestor, // nsGlobalWindowInner will be controlled as unexpected. if (oldLoadInfo->GetController().isSome() && newLoadInfo->GetController().isNothing()) { - nsIScriptSecurityManager* ssm = - nsContentUtils::GetSecurityManager(); - MOZ_DIAGNOSTIC_ASSERT(ssm); - nsCOMPtr principal; - rv = ssm->GetChannelResultPrincipal(aNewChannel, - getter_AddRefs(principal)); + nsCOMPtr foreignPartitionedPrincipal; + rv = StoragePrincipalHelper::GetPrincipal( + aNewChannel, + StoragePrincipalHelper::eForeignPartitionedPrincipal, + getter_AddRefs(foreignPartitionedPrincipal)); NS_ENSURE_SUCCESS(rv, rv); reservedClient.reset(); - CreateClient(newLoadInfo, principal); + CreateClient(newLoadInfo, foreignPartitionedPrincipal); } else { newLoadInfo->SetReservedClientInfo(reservedClientInfo.ref()); } @@ -130,17 +130,14 @@ class ClientChannelHelper : public nsIInterfaceRequestor, // If it's a cross-origin redirect then we discard the old reserved client // and create a new one. else { - // If CheckSameOrigin() worked, then the security manager must exist. - nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager(); - MOZ_DIAGNOSTIC_ASSERT(ssm); - - nsCOMPtr principal; - rv = ssm->GetChannelResultPrincipal(aNewChannel, - getter_AddRefs(principal)); + nsCOMPtr foreignPartitionedPrincipal; + rv = StoragePrincipalHelper::GetPrincipal( + aNewChannel, StoragePrincipalHelper::eForeignPartitionedPrincipal, + getter_AddRefs(foreignPartitionedPrincipal)); NS_ENSURE_SUCCESS(rv, rv); reservedClient.reset(); - CreateClient(newLoadInfo, principal); + CreateClient(newLoadInfo, foreignPartitionedPrincipal); } uint32_t redirectMode = nsIHttpChannelInternal::REDIRECT_MODE_MANUAL; @@ -299,12 +296,10 @@ nsresult AddClientChannelHelperInternal(nsIChannel* aChannel, nsCOMPtr loadInfo = aChannel->LoadInfo(); - nsIScriptSecurityManager* ssm = nsContentUtils::GetSecurityManager(); - NS_ENSURE_TRUE(ssm, NS_ERROR_FAILURE); - - nsCOMPtr channelPrincipal; - nsresult rv = ssm->GetChannelResultPrincipal( - aChannel, getter_AddRefs(channelPrincipal)); + nsCOMPtr channelForeignPartitionedPrincipal; + nsresult rv = StoragePrincipalHelper::GetPrincipal( + aChannel, StoragePrincipalHelper::eForeignPartitionedPrincipal, + getter_AddRefs(channelForeignPartitionedPrincipal)); NS_ENSURE_SUCCESS(rv, rv); // Only allow the initial ClientInfo to be set if the current channel @@ -314,9 +309,10 @@ nsresult AddClientChannelHelperInternal(nsIChannel* aChannel, PrincipalInfoToPrincipal(initialClientInfo.ref().PrincipalInfo()); bool equals = false; - rv = initialPrincipalOrErr.isErr() ? initialPrincipalOrErr.unwrapErr() - : initialPrincipalOrErr.unwrap()->Equals( - channelPrincipal, &equals); + rv = initialPrincipalOrErr.isErr() + ? initialPrincipalOrErr.unwrapErr() + : initialPrincipalOrErr.unwrap()->Equals( + channelForeignPartitionedPrincipal, &equals); if (NS_FAILED(rv) || !equals) { initialClientInfo.reset(); } @@ -331,8 +327,8 @@ nsresult AddClientChannelHelperInternal(nsIChannel* aChannel, bool equals = false; rv = reservedPrincipalOrErr.isErr() ? reservedPrincipalOrErr.unwrapErr() - : reservedPrincipalOrErr.unwrap()->Equals(channelPrincipal, - &equals); + : reservedPrincipalOrErr.unwrap()->Equals( + channelForeignPartitionedPrincipal, &equals); if (NS_FAILED(rv) || !equals) { reservedClientInfo.reset(); } @@ -345,7 +341,8 @@ nsresult AddClientChannelHelperInternal(nsIChannel* aChannel, RefPtr helper = new T(outerCallbacks, aEventTarget); if (initialClientInfo.isNothing() && reservedClientInfo.isNothing()) { - helper->CreateClientForPrincipal(loadInfo, channelPrincipal, aEventTarget); + helper->CreateClientForPrincipal( + loadInfo, channelForeignPartitionedPrincipal, aEventTarget); } // Only set the callbacks helper if we are able to reserve the client