diff --git a/toolkit/components/antitracking/AntiTrackingCommon.cpp b/toolkit/components/antitracking/AntiTrackingCommon.cpp index 654eac770aea..1fadcc117eee 100644 --- a/toolkit/components/antitracking/AntiTrackingCommon.cpp +++ b/toolkit/components/antitracking/AntiTrackingCommon.cpp @@ -376,25 +376,42 @@ bool CheckContentBlockingAllowList(nsIHttpChannel* aChannel) { return entry.Data().mResult; } - nsCOMPtr chan = do_QueryInterface(aChannel); - if (chan) { - nsCOMPtr topWinURI; - nsresult rv = chan->GetTopWindowURI(getter_AddRefs(topWinURI)); - if (NS_SUCCEEDED(rv)) { - const bool result = CheckContentBlockingAllowList( - topWinURI, NS_UsePrivateBrowsing(aChannel)); + nsCOMPtr loadInfo = aChannel->LoadInfo(); + nsContentPolicyType contentPolicyType = + loadInfo->GetExternalContentPolicyType(); - entry.Set(ContentBlockingAllowListEntry(aChannel, result)); + nsCOMPtr uri; - return result; + // This is the top-level request. Let's use the channel URI. + if (contentPolicyType == nsIContentPolicy::TYPE_DOCUMENT) { + nsresult rv = NS_GetFinalChannelURI(aChannel, getter_AddRefs(uri)); + if (NS_WARN_IF(NS_FAILED(rv)) || !uri) { + LOG( + ("Could not check the content blocking allow list because the " + "channel URI is not accessible")); + entry.Set(ContentBlockingAllowListEntry(aChannel, false)); + return false; + } + } else { + nsCOMPtr chan = do_QueryInterface(aChannel); + MOZ_ASSERT(chan); + + nsresult rv = chan->GetTopWindowURI(getter_AddRefs(uri)); + if (NS_WARN_IF(NS_FAILED(rv)) || !uri) { + LOG( + ("Could not check the content blocking allow list because the top " + "window wasn't accessible")); + entry.Set(ContentBlockingAllowListEntry(aChannel, false)); + return false; } } - LOG( - ("Could not check the content blocking allow list because the top " - "window wasn't accessible")); - entry.Set(ContentBlockingAllowListEntry(aChannel, false)); - return false; + MOZ_ASSERT(uri); + + const bool result = + CheckContentBlockingAllowList(uri, NS_UsePrivateBrowsing(aChannel)); + entry.Set(ContentBlockingAllowListEntry(aChannel, result)); + return result; } void ReportBlockingToConsole(nsPIDOMWindowOuter* aWindow, nsIURI* aURI,