From 074d88de5adf28aded1fffe00d24126423dd4deb Mon Sep 17 00:00:00 2001 From: Adrian Wielgosik Date: Fri, 11 May 2018 19:46:15 +0200 Subject: [PATCH] Bug 1460940 - Convert nsIPrincipal to use nsIDocument. r=bz MozReview-Commit-ID: z1TGWtS1KG --HG-- extra : rebase_source : e5291c40eb017c1e3fd69333ac108dda852fb8cd --- caps/BasePrincipal.cpp | 4 +-- caps/BasePrincipal.h | 4 +-- caps/SystemPrincipal.cpp | 4 +-- caps/SystemPrincipal.h | 4 +-- caps/nsIPrincipal.idl | 7 ++--- dom/html/HTMLMetaElement.cpp | 3 +-- .../security/nsIContentSecurityPolicy.idl | 5 ++-- dom/security/nsCSPContext.cpp | 27 +++++++++---------- parser/html/nsHtml5TreeOpExecutor.cpp | 4 +-- 9 files changed, 30 insertions(+), 32 deletions(-) diff --git a/caps/BasePrincipal.cpp b/caps/BasePrincipal.cpp index 26f7a6d76c7e..b9c3f321578e 100644 --- a/caps/BasePrincipal.cpp +++ b/caps/BasePrincipal.cpp @@ -189,7 +189,7 @@ BasePrincipal::SetCsp(nsIContentSecurityPolicy* aCsp) } NS_IMETHODIMP -BasePrincipal::EnsureCSP(nsIDOMDocument* aDocument, +BasePrincipal::EnsureCSP(nsIDocument* aDocument, nsIContentSecurityPolicy** aCSP) { if (mCSP) { @@ -219,7 +219,7 @@ BasePrincipal::GetPreloadCsp(nsIContentSecurityPolicy** aPreloadCSP) } NS_IMETHODIMP -BasePrincipal::EnsurePreloadCSP(nsIDOMDocument* aDocument, +BasePrincipal::EnsurePreloadCSP(nsIDocument* aDocument, nsIContentSecurityPolicy** aPreloadCSP) { if (mPreloadCSP) { diff --git a/caps/BasePrincipal.h b/caps/BasePrincipal.h index 97e8f45dbd99..5d84aedf8781 100644 --- a/caps/BasePrincipal.h +++ b/caps/BasePrincipal.h @@ -71,9 +71,9 @@ public: NS_IMETHOD GetAddonPolicy(nsISupports** aResult) final; NS_IMETHOD GetCsp(nsIContentSecurityPolicy** aCsp) override; NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp) override; - NS_IMETHOD EnsureCSP(nsIDOMDocument* aDocument, nsIContentSecurityPolicy** aCSP) override; + NS_IMETHOD EnsureCSP(nsIDocument* aDocument, nsIContentSecurityPolicy** aCSP) override; NS_IMETHOD GetPreloadCsp(nsIContentSecurityPolicy** aPreloadCSP) override; - NS_IMETHOD EnsurePreloadCSP(nsIDOMDocument* aDocument, nsIContentSecurityPolicy** aCSP) override; + NS_IMETHOD EnsurePreloadCSP(nsIDocument* aDocument, nsIContentSecurityPolicy** aCSP) override; NS_IMETHOD GetCspJSON(nsAString& outCSPinJSON) override; NS_IMETHOD GetIsNullPrincipal(bool* aResult) override; NS_IMETHOD GetIsCodebasePrincipal(bool* aResult) override; diff --git a/caps/SystemPrincipal.cpp b/caps/SystemPrincipal.cpp index 47e81afb7522..4f29953dbf62 100644 --- a/caps/SystemPrincipal.cpp +++ b/caps/SystemPrincipal.cpp @@ -81,7 +81,7 @@ SystemPrincipal::SetCsp(nsIContentSecurityPolicy* aCsp) } NS_IMETHODIMP -SystemPrincipal::EnsureCSP(nsIDOMDocument* aDocument, +SystemPrincipal::EnsureCSP(nsIDocument* aDocument, nsIContentSecurityPolicy** aCSP) { // CSP on a system principal makes no sense @@ -96,7 +96,7 @@ SystemPrincipal::GetPreloadCsp(nsIContentSecurityPolicy** aPreloadCSP) } NS_IMETHODIMP -SystemPrincipal::EnsurePreloadCSP(nsIDOMDocument* aDocument, +SystemPrincipal::EnsurePreloadCSP(nsIDocument* aDocument, nsIContentSecurityPolicy** aPreloadCSP) { // CSP on a system principal makes no sense diff --git a/caps/SystemPrincipal.h b/caps/SystemPrincipal.h index aa5ece34fd5c..d5866ab8ea7f 100644 --- a/caps/SystemPrincipal.h +++ b/caps/SystemPrincipal.h @@ -40,9 +40,9 @@ public: NS_IMETHOD SetDomain(nsIURI* aDomain) override; NS_IMETHOD GetCsp(nsIContentSecurityPolicy** aCsp) override; NS_IMETHOD SetCsp(nsIContentSecurityPolicy* aCsp) override; - NS_IMETHOD EnsureCSP(nsIDOMDocument* aDocument, nsIContentSecurityPolicy** aCSP) override; + NS_IMETHOD EnsureCSP(nsIDocument* aDocument, nsIContentSecurityPolicy** aCSP) override; NS_IMETHOD GetPreloadCsp(nsIContentSecurityPolicy** aPreloadCSP) override; - NS_IMETHOD EnsurePreloadCSP(nsIDOMDocument* aDocument, nsIContentSecurityPolicy** aCSP) override; + NS_IMETHOD EnsurePreloadCSP(nsIDocument* aDocument, nsIContentSecurityPolicy** aCSP) override; NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override; NS_IMETHOD GetAddonId(nsAString& aAddonId) override; diff --git a/caps/nsIPrincipal.idl b/caps/nsIPrincipal.idl index 4d1cf6a62c0d..7ab8f81ecf63 100644 --- a/caps/nsIPrincipal.idl +++ b/caps/nsIPrincipal.idl @@ -38,7 +38,8 @@ class OriginAttributes; interface nsIURI; interface nsIContentSecurityPolicy; -interface nsIDOMDocument; + +webidl Document; [ptr] native JSContext(JSContext); [ptr] native JSPrincipals(JSPrincipals); @@ -165,7 +166,7 @@ interface nsIPrincipal : nsISerializable * Please note if aDocument is null, then setRequestContext on the * CSP object is called using the current principal. */ - [noscript] nsIContentSecurityPolicy ensureCSP(in nsIDOMDocument aDocument); + [noscript] nsIContentSecurityPolicy ensureCSP(in Document aDocument); /** * A speculative Content Security Policy associated with this @@ -186,7 +187,7 @@ interface nsIPrincipal : nsISerializable * Please note if aDocument is null, then setRequestContext on the * speculative CSP object is called using the current principal. */ - [noscript] nsIContentSecurityPolicy ensurePreloadCSP(in nsIDOMDocument aDocument); + [noscript] nsIContentSecurityPolicy ensurePreloadCSP(in Document aDocument); /** * The CSP of the principal in JSON notation. diff --git a/dom/html/HTMLMetaElement.cpp b/dom/html/HTMLMetaElement.cpp index e8cab99c5792..c4d3c48f612e 100644 --- a/dom/html/HTMLMetaElement.cpp +++ b/dom/html/HTMLMetaElement.cpp @@ -108,8 +108,7 @@ HTMLMetaElement::BindToTree(nsIDocument* aDocument, nsIContent* aParent, nsIPrincipal* principal = aDocument->NodePrincipal(); nsCOMPtr csp; - nsCOMPtr domDoc = do_QueryInterface(aDocument); - principal->EnsureCSP(domDoc, getter_AddRefs(csp)); + principal->EnsureCSP(aDocument, getter_AddRefs(csp)); if (csp) { if (LOG_ENABLED()) { nsAutoCString documentURIspec; diff --git a/dom/interfaces/security/nsIContentSecurityPolicy.idl b/dom/interfaces/security/nsIContentSecurityPolicy.idl index fcdebed3cfb3..8dfe6acbdf69 100644 --- a/dom/interfaces/security/nsIContentSecurityPolicy.idl +++ b/dom/interfaces/security/nsIContentSecurityPolicy.idl @@ -7,10 +7,11 @@ interface nsIURI; interface nsIDocShell; -interface nsIDOMDocument; interface nsIEventTarget; interface nsIPrincipal; +webidl Document; + /** * nsIContentSecurityPolicy * Describes an XPCOM component used to model and enforce CSPs. Instances of @@ -207,7 +208,7 @@ interface nsIContentSecurityPolicy : nsISerializable * * aDocument (preferred), or if no document is available, then provide * * aPrincipal */ - void setRequestContext(in nsIDOMDocument aDocument, + void setRequestContext(in Document aDocument, in nsIPrincipal aPrincipal); /** diff --git a/dom/security/nsCSPContext.cpp b/dom/security/nsCSPContext.cpp index 5db378cdce52..b8045d928f95 100644 --- a/dom/security/nsCSPContext.cpp +++ b/dom/security/nsCSPContext.cpp @@ -18,7 +18,7 @@ #include "nsIClassInfoImpl.h" #include "nsIDocShell.h" #include "nsIDocShellTreeItem.h" -#include "nsIDOMDocument.h" +#include "nsIDocument.h" #include "nsIDOMNode.h" #include "nsIHttpChannel.h" #include "nsIInterfaceRequestor.h" @@ -689,29 +689,28 @@ nsCSPContext::LogViolationDetails(uint16_t aViolationType, #undef CASE_CHECK_AND_REPORT NS_IMETHODIMP -nsCSPContext::SetRequestContext(nsIDOMDocument* aDOMDocument, +nsCSPContext::SetRequestContext(nsIDocument* aDocument, nsIPrincipal* aPrincipal) { - MOZ_ASSERT(aDOMDocument || aPrincipal, + MOZ_ASSERT(aDocument || aPrincipal, "Can't set context without doc or principal"); - NS_ENSURE_ARG(aDOMDocument || aPrincipal); + NS_ENSURE_ARG(aDocument || aPrincipal); - if (aDOMDocument) { - nsCOMPtr doc = do_QueryInterface(aDOMDocument); - mLoadingContext = do_GetWeakReference(doc); - mSelfURI = doc->GetDocumentURI(); - mLoadingPrincipal = doc->NodePrincipal(); - doc->GetReferrer(mReferrer); - mInnerWindowID = doc->InnerWindowID(); + if (aDocument) { + mLoadingContext = do_GetWeakReference(aDocument); + mSelfURI = aDocument->GetDocumentURI(); + mLoadingPrincipal = aDocument->NodePrincipal(); + aDocument->GetReferrer(mReferrer); + mInnerWindowID = aDocument->InnerWindowID(); // the innerWindowID is not available for CSPs delivered through the // header at the time setReqeustContext is called - let's queue up // console messages until it becomes available, see flushConsoleMessages mQueueUpMessages = !mInnerWindowID; - mCallingChannelLoadGroup = doc->GetDocumentLoadGroup(); + mCallingChannelLoadGroup = aDocument->GetDocumentLoadGroup(); // set the flag on the document for CSP telemetry - doc->SetHasCSP(true); - mEventTarget = doc->EventTargetFor(TaskCategory::Other); + aDocument->SetHasCSP(true); + mEventTarget = aDocument->EventTargetFor(TaskCategory::Other); } else { CSPCONTEXTLOG(("No Document in SetRequestContext; can not query loadgroup; sending reports may fail.")); diff --git a/parser/html/nsHtml5TreeOpExecutor.cpp b/parser/html/nsHtml5TreeOpExecutor.cpp index fb511be8ca56..364a639f9d05 100644 --- a/parser/html/nsHtml5TreeOpExecutor.cpp +++ b/parser/html/nsHtml5TreeOpExecutor.cpp @@ -25,7 +25,6 @@ #include "nsHtml5TreeOpExecutor.h" #include "nsIContentSecurityPolicy.h" #include "nsIContentViewer.h" -#include "nsIDOMDocument.h" #include "nsIDocShell.h" #include "nsIDocShellTreeItem.h" #include "nsIHTMLDocument.h" @@ -1144,8 +1143,7 @@ nsHtml5TreeOpExecutor::AddSpeculationCSP(const nsAString& aCSP) nsIPrincipal* principal = mDocument->NodePrincipal(); nsCOMPtr preloadCsp; - nsCOMPtr domDoc = do_QueryInterface(mDocument); - nsresult rv = principal->EnsurePreloadCSP(domDoc, getter_AddRefs(preloadCsp)); + nsresult rv = principal->EnsurePreloadCSP(mDocument, getter_AddRefs(preloadCsp)); NS_ENSURE_SUCCESS_VOID(rv); // please note that meta CSPs and CSPs delivered through a header need