Bug 1663992 - Check loading principal if there is no browsing context in AntiTrackingUtils::ComputeIsThirdPartyToTopWindow(). r=dimi, a=pascalc
Differential Revision:
https://phabricator.services.mozilla.com/D90816
--- a/toolkit/components/antitracking/AntiTrackingUtils.cpp
+++ b/toolkit/components/antitracking/AntiTrackingUtils.cpp
@@ -559,24 +559,37 @@ void AntiTrackingUtils::ComputeIsThirdPa
if (policyType == nsIContentPolicy::TYPE_DOCUMENT) {
loadInfo->SetIsThirdPartyContextToTopWindow(false);
return;
}
RefPtr<BrowsingContext> bc;
loadInfo->GetBrowsingContext(getter_AddRefs(bc));
- // In xpcshell-tests we don't always have a browsingContext.
+ nsCOMPtr<nsIURI> uri;
+ Unused << aChannel->GetURI(getter_AddRefs(uri));
+
+ // In some cases we don't have a browsingContext. For example, in xpcshell
+ // tests or channels that are used to download images.
if (!bc) {
+ // We turn to check the loading principal if there is no browsing context.
+ auto* loadingPrincipal =
+ BasePrincipal::Cast(loadInfo->GetLoadingPrincipal());
+
+ if (uri && loadingPrincipal) {
+ bool isThirdParty = true;
+ nsresult rv = loadingPrincipal->IsThirdPartyURI(uri, &isThirdParty);
+
+ if (NS_SUCCEEDED(rv)) {
+ loadInfo->SetIsThirdPartyContextToTopWindow(isThirdParty);
+ }
+ }
return;
}
- nsCOMPtr<nsIURI> uri;
- Unused << aChannel->GetURI(getter_AddRefs(uri));
-
RefPtr<WindowGlobalParent> topWindow =
GetTopWindowExcludingExtensionAccessibleContentFrames(bc->Canonical(),
uri);
if (NS_WARN_IF(!topWindow)) {
return;
}