From 66e1e333d1e4db4a6833e42aa68b8f5fb0f00d1e Mon Sep 17 00:00:00 2001 From: Matt Woodrow Date: Fri, 8 May 2020 02:53:44 +0000 Subject: [PATCH] Bug 1635992 - Use LoadInfo and WindowContext in nsMixedContentBlocker instead of the docshell and Document. r=ckerschb Differential Revision: https://phabricator.services.mozilla.com/D74169 --- dom/security/nsMixedContentBlocker.cpp | 47 ++++++++++++-------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/dom/security/nsMixedContentBlocker.cpp b/dom/security/nsMixedContentBlocker.cpp index bd7de6bde7ee..e5e43ab5f7e1 100644 --- a/dom/security/nsMixedContentBlocker.cpp +++ b/dom/security/nsMixedContentBlocker.cpp @@ -15,6 +15,7 @@ #include "nsIWebProgressListener.h" #include "nsContentUtils.h" #include "mozilla/dom/BrowsingContext.h" +#include "mozilla/dom/WindowContext.h" #include "mozilla/dom/Document.h" #include "nsIChannel.h" #include "nsIParentChannel.h" @@ -510,8 +511,8 @@ nsresult nsMixedContentBlocker::ShouldLoad(bool aHadInsecureImageRedirect, nsCOMPtr requestingContext = aLoadInfo->GetLoadingContext(); nsCOMPtr loadingPrincipal = aLoadInfo->GetLoadingPrincipal(); nsCOMPtr triggeringPrincipal = aLoadInfo->TriggeringPrincipal(); - - bool isPreload = nsContentUtils::IsPreloadType(contentType); + RefPtr requestingWindow = + WindowContext::GetById(aLoadInfo->GetInnerWindowID()); // The content policy type that we receive may be an internal type for // scripts. Let's remember if we have seen a worker type, and reset it to the @@ -786,10 +787,9 @@ nsresult nsMixedContentBlocker::ShouldLoad(bool aHadInsecureImageRedirect, } // Otherwise, we must have a docshell NS_ENSURE_TRUE(docShell, NS_OK); + NS_ENSURE_TRUE(requestingWindow, NS_OK); - Document* document = docShell->GetDocument(); - MOZ_ASSERT(document, "Expected a document"); - if (isHttpScheme && document->GetUpgradeInsecureRequests(isPreload)) { + if (isHttpScheme && aLoadInfo->GetUpgradeInsecureRequests()) { *aDecision = ACCEPT; return NS_OK; } @@ -812,7 +812,7 @@ nsresult nsMixedContentBlocker::ShouldLoad(bool aHadInsecureImageRedirect, // Block all non secure loads in case the CSP directive is present. Please // note that at this point we already know, based on |schemeSecure| that the // load is not secure, so we can bail out early at this point. - if (document->GetBlockAllMixedContent(isPreload)) { + if (aLoadInfo->GetBlockAllMixedContent()) { // log a message to the console before returning. nsAutoCString spec; nsresult rv = aContentLocation->GetSpec(spec); @@ -821,25 +821,23 @@ nsresult nsMixedContentBlocker::ShouldLoad(bool aHadInsecureImageRedirect, AutoTArray params; CopyUTF8toUTF16(spec, *params.AppendElement()); - CSP_LogLocalizedStr( - "blockAllMixedContent", params, - EmptyString(), // aSourceFile - EmptyString(), // aScriptSample - 0, // aLineNumber - 0, // aColumnNumber - nsIScriptError::errorFlag, NS_LITERAL_CSTRING("blockAllMixedContent"), - document->InnerWindowID(), - !!document->NodePrincipal()->OriginAttributesRef().mPrivateBrowsingId); + CSP_LogLocalizedStr("blockAllMixedContent", params, + EmptyString(), // aSourceFile + EmptyString(), // aScriptSample + 0, // aLineNumber + 0, // aColumnNumber + nsIScriptError::errorFlag, + NS_LITERAL_CSTRING("blockAllMixedContent"), + requestingWindow->Id(), + !!aLoadInfo->GetOriginAttributes().mPrivateBrowsingId); *aDecision = REJECT_REQUEST; return NS_OK; } // Determine if the rootDoc is https and if the user decided to allow Mixed // Content - RefPtr bc = docShell->GetBrowsingContext(); - RefPtr rootBC = bc->Top(); - bool rootHasSecureConnection = rootBC->GetIsSecure(); - WindowContext* topWC = bc->GetTopWindowContext(); + WindowContext* topWC = requestingWindow->TopWindowContext(); + bool rootHasSecureConnection = topWC->GetBrowsingContext()->GetIsSecure(); bool allowMixedContent = topWC->GetAllowMixedContent(); // When navigating an iframe, the iframe may be https @@ -848,11 +846,10 @@ nsresult nsMixedContentBlocker::ShouldLoad(bool aHadInsecureImageRedirect, if (contentType == TYPE_SUBDOCUMENT && !rootHasSecureConnection) { bool httpsParentExists = false; - RefPtr curBC = docShell->GetBrowsingContext(); - - while (!httpsParentExists && curBC) { - httpsParentExists = curBC->GetIsSecure(); - curBC = curBC->GetParent(); + RefPtr curWindow = requestingWindow; + while (!httpsParentExists && curWindow) { + httpsParentExists = curWindow->GetBrowsingContext()->GetIsSecure(); + curWindow = curWindow->GetParentWindowContext(); } if (!httpsParentExists) { @@ -862,7 +859,7 @@ nsresult nsMixedContentBlocker::ShouldLoad(bool aHadInsecureImageRedirect, } // Get the root document from the rootShell - nsCOMPtr rootShell = rootBC->GetDocShell(); + nsCOMPtr rootShell = topWC->GetBrowsingContext()->GetDocShell(); nsCOMPtr rootDoc = rootShell ? rootShell->GetDocument() : nullptr; // TODO Fission: Bug 1631405: Make Mixed Content UI fission compatible