diff --git a/dom/base/nsGlobalWindowInner.cpp b/dom/base/nsGlobalWindowInner.cpp index 6e4008c9cd6d..a9623f9db534 100644 --- a/dom/base/nsGlobalWindowInner.cpp +++ b/dom/base/nsGlobalWindowInner.cpp @@ -6255,6 +6255,38 @@ nsGlobalWindowInner::GetParentInternal() return outer->GetParentInternal(); } +nsIPrincipal* +nsGlobalWindowInner::GetTopLevelPrincipal() +{ + nsPIDOMWindowOuter* outerWindow = GetOuterWindowInternal(); + if (!outerWindow) { + return nullptr; + } + + nsPIDOMWindowOuter* topLevelOuterWindow = GetTopInternal(); + if (!topLevelOuterWindow) { + return nullptr; + } + + if (topLevelOuterWindow == outerWindow) { + return nullptr; + } + + nsPIDOMWindowInner* topLevelInnerWindow = + topLevelOuterWindow->GetCurrentInnerWindow(); + if (NS_WARN_IF(!topLevelInnerWindow)) { + return nullptr; + } + + nsIPrincipal* topLevelPrincipal = + nsGlobalWindowInner::Cast(topLevelInnerWindow)->GetPrincipal(); + if (NS_WARN_IF(!topLevelPrincipal)) { + return nullptr; + } + + return topLevelPrincipal; +} + nsIPrincipal* nsGlobalWindowInner::GetTopLevelStorageAreaPrincipal() { diff --git a/dom/base/nsGlobalWindowInner.h b/dom/base/nsGlobalWindowInner.h index 36a48cbaa2aa..935a84e60cca 100644 --- a/dom/base/nsGlobalWindowInner.h +++ b/dom/base/nsGlobalWindowInner.h @@ -1212,7 +1212,12 @@ public: public: virtual already_AddRefed GetTopWindowRoot() override; - // Get the parent principal, returns null if this is a toplevel window. + // Get the toplevel principal, returns null if this is a toplevel window. + nsIPrincipal* GetTopLevelPrincipal(); + + // Get the parent principal, returns null if this or the parent are not a + // toplevel window. This is mainly used to determine the anti-tracking storage + // area. nsIPrincipal* GetTopLevelStorageAreaPrincipal(); protected: diff --git a/ipc/glue/BackgroundUtils.cpp b/ipc/glue/BackgroundUtils.cpp index 19d7cbe6fe6e..bcf93aa536cf 100644 --- a/ipc/glue/BackgroundUtils.cpp +++ b/ipc/glue/BackgroundUtils.cpp @@ -339,6 +339,15 @@ LoadInfoToLoadInfoArgs(nsILoadInfo *aLoadInfo, sandboxedLoadingPrincipalInfo = sandboxedLoadingPrincipalInfoTemp; } + OptionalPrincipalInfo topLevelPrincipalInfo = mozilla::void_t(); + if (aLoadInfo->TopLevelPrincipal()) { + PrincipalInfo topLevelPrincipalInfoTemp; + rv = PrincipalToPrincipalInfo(aLoadInfo->TopLevelPrincipal(), + &topLevelPrincipalInfoTemp); + NS_ENSURE_SUCCESS(rv, rv); + topLevelPrincipalInfo = topLevelPrincipalInfoTemp; + } + OptionalPrincipalInfo topLevelStorageAreaPrincipalInfo = mozilla::void_t(); if (aLoadInfo->TopLevelStorageAreaPrincipal()) { PrincipalInfo topLevelStorageAreaPrincipalInfoTemp; @@ -408,6 +417,7 @@ LoadInfoToLoadInfoArgs(nsILoadInfo *aLoadInfo, triggeringPrincipalInfo, principalToInheritInfo, sandboxedLoadingPrincipalInfo, + topLevelPrincipalInfo, topLevelStorageAreaPrincipalInfo, optionalResultPrincipalURI, aLoadInfo->GetSecurityFlags(), @@ -488,6 +498,13 @@ LoadInfoArgsToLoadInfo(const OptionalLoadInfoArgs& aOptionalLoadInfoArgs, NS_ENSURE_SUCCESS(rv, rv); } + nsCOMPtr topLevelPrincipal; + if (loadInfoArgs.topLevelPrincipalInfo().type() != OptionalPrincipalInfo::Tvoid_t) { + topLevelPrincipal = + PrincipalInfoToPrincipal(loadInfoArgs.topLevelPrincipalInfo(), &rv); + NS_ENSURE_SUCCESS(rv, rv); + } + nsCOMPtr topLevelStorageAreaPrincipal; if (loadInfoArgs.topLevelStorageAreaPrincipalInfo().type() != OptionalPrincipalInfo::Tvoid_t) { topLevelStorageAreaPrincipal = @@ -561,6 +578,7 @@ LoadInfoArgsToLoadInfo(const OptionalLoadInfoArgs& aOptionalLoadInfoArgs, triggeringPrincipal, principalToInherit, sandboxedLoadingPrincipal, + topLevelPrincipal, topLevelStorageAreaPrincipal, resultPrincipalURI, clientInfo, diff --git a/netwerk/base/LoadInfo.cpp b/netwerk/base/LoadInfo.cpp index 1eea1af924c0..e999a5cca840 100644 --- a/netwerk/base/LoadInfo.cpp +++ b/netwerk/base/LoadInfo.cpp @@ -155,6 +155,7 @@ LoadInfo::LoadInfo(nsIPrincipal* aLoadingPrincipal, nsGlobalWindowInner* innerWindow = nsGlobalWindowInner::Cast(contextOuter->GetCurrentInnerWindow()); if (innerWindow) { + mTopLevelPrincipal = innerWindow->GetTopLevelPrincipal(); mTopLevelStorageAreaPrincipal = innerWindow->GetTopLevelStorageAreaPrincipal(); } @@ -346,6 +347,7 @@ LoadInfo::LoadInfo(nsPIDOMWindowOuter* aOuterWindow, nsGlobalWindowInner* innerWindow = nsGlobalWindowInner::Cast(aOuterWindow->GetCurrentInnerWindow()); if (innerWindow) { + mTopLevelPrincipal = innerWindow->GetTopLevelPrincipal(); mTopLevelStorageAreaPrincipal = innerWindow->GetTopLevelStorageAreaPrincipal(); } @@ -371,6 +373,7 @@ LoadInfo::LoadInfo(const LoadInfo& rhs) , mTriggeringPrincipal(rhs.mTriggeringPrincipal) , mPrincipalToInherit(rhs.mPrincipalToInherit) , mSandboxedLoadingPrincipal(rhs.mSandboxedLoadingPrincipal) + , mTopLevelPrincipal(rhs.mTopLevelPrincipal) , mTopLevelStorageAreaPrincipal(rhs.mTopLevelStorageAreaPrincipal) , mResultPrincipalURI(rhs.mResultPrincipalURI) , mClientInfo(rhs.mClientInfo) @@ -423,6 +426,7 @@ LoadInfo::LoadInfo(nsIPrincipal* aLoadingPrincipal, nsIPrincipal* aTriggeringPrincipal, nsIPrincipal* aPrincipalToInherit, nsIPrincipal* aSandboxedLoadingPrincipal, + nsIPrincipal* aTopLevelPrincipal, nsIPrincipal* aTopLevelStorageAreaPrincipal, nsIURI* aResultPrincipalURI, const Maybe& aClientInfo, @@ -464,6 +468,7 @@ LoadInfo::LoadInfo(nsIPrincipal* aLoadingPrincipal, : mLoadingPrincipal(aLoadingPrincipal) , mTriggeringPrincipal(aTriggeringPrincipal) , mPrincipalToInherit(aPrincipalToInherit) + , mTopLevelPrincipal(aTopLevelPrincipal) , mTopLevelStorageAreaPrincipal(aTopLevelStorageAreaPrincipal) , mResultPrincipalURI(aResultPrincipalURI) , mClientInfo(aClientInfo) @@ -647,6 +652,19 @@ LoadInfo::GetSandboxedLoadingPrincipal(nsIPrincipal** aPrincipal) return NS_OK; } +NS_IMETHODIMP +LoadInfo::GetTopLevelPrincipal(nsIPrincipal** aTopLevelPrincipal) +{ + NS_IF_ADDREF(*aTopLevelPrincipal = mTopLevelPrincipal); + return NS_OK; +} + +nsIPrincipal* +LoadInfo::TopLevelPrincipal() +{ + return mTopLevelPrincipal; +} + NS_IMETHODIMP LoadInfo::GetTopLevelStorageAreaPrincipal(nsIPrincipal** aTopLevelStorageAreaPrincipal) { diff --git a/netwerk/base/LoadInfo.h b/netwerk/base/LoadInfo.h index d5368a94e646..7d5b178bc0e9 100644 --- a/netwerk/base/LoadInfo.h +++ b/netwerk/base/LoadInfo.h @@ -97,6 +97,7 @@ private: nsIPrincipal* aTriggeringPrincipal, nsIPrincipal* aPrincipalToInherit, nsIPrincipal* aSandboxedLoadingPrincipal, + nsIPrincipal* aTopLevelPrincipal, nsIPrincipal* aTopLevelStorageAreaPrincipal, nsIURI* aResultPrincipalURI, const Maybe& aClientInfo, @@ -161,6 +162,7 @@ private: nsCOMPtr mTriggeringPrincipal; nsCOMPtr mPrincipalToInherit; nsCOMPtr mSandboxedLoadingPrincipal; + nsCOMPtr mTopLevelPrincipal; nsCOMPtr mTopLevelStorageAreaPrincipal; nsCOMPtr mResultPrincipalURI; diff --git a/netwerk/base/nsILoadInfo.idl b/netwerk/base/nsILoadInfo.idl index 0882db823ec7..a29664918e29 100644 --- a/netwerk/base/nsILoadInfo.idl +++ b/netwerk/base/nsILoadInfo.idl @@ -883,6 +883,18 @@ interface nsILoadInfo : nsISupports */ [noscript] readonly attribute nsIPrincipal sandboxedLoadingPrincipal; + /** + * Return the top-level principal, which is the principal of the top-level + * window. + */ + [noscript] readonly attribute nsIPrincipal topLevelPrincipal; + + /** + * A C++-friendly version of topLevelPrincipal. + */ + [noscript, notxpcom, nostdcall, binaryname(TopLevelPrincipal)] + nsIPrincipal binaryTopLevelPrincipal(); + /** * Return the top-level storage area principal, which is the principal of * the top-level window if it's not a 3rd party context, non tracking diff --git a/netwerk/ipc/NeckoChannelParams.ipdlh b/netwerk/ipc/NeckoChannelParams.ipdlh index 69e80b94a1c1..9b8c08b38108 100644 --- a/netwerk/ipc/NeckoChannelParams.ipdlh +++ b/netwerk/ipc/NeckoChannelParams.ipdlh @@ -42,6 +42,7 @@ struct LoadInfoArgs PrincipalInfo triggeringPrincipalInfo; OptionalPrincipalInfo principalToInheritInfo; OptionalPrincipalInfo sandboxedLoadingPrincipalInfo; + OptionalPrincipalInfo topLevelPrincipalInfo; OptionalPrincipalInfo topLevelStorageAreaPrincipalInfo; OptionalURIParams resultPrincipalURI; uint32_t securityFlags; diff --git a/toolkit/components/antitracking/AntiTrackingCommon.cpp b/toolkit/components/antitracking/AntiTrackingCommon.cpp index 8784b831217b..76fb7a435a1b 100644 --- a/toolkit/components/antitracking/AntiTrackingCommon.cpp +++ b/toolkit/components/antitracking/AntiTrackingCommon.cpp @@ -237,6 +237,12 @@ AntiTrackingCommon::IsFirstPartyStorageAccessGrantedFor(nsIHttpChannel* aChannel nsIPrincipal* parentPrincipal = loadInfo->TopLevelStorageAreaPrincipal(); if (!parentPrincipal) { + // parentPrincipal can be null if the parent window is not the top-level + // window. + if (loadInfo->TopLevelPrincipal()) { + return false; + } + parentPrincipal = loadInfo->TriggeringPrincipal(); if (NS_WARN_IF(!parentPrincipal)) { // Why we are here?!?