diff --git a/dom/base/nsContentSink.cpp b/dom/base/nsContentSink.cpp index 066ec84466..2040b1a70f 100644 --- a/dom/base/nsContentSink.cpp +++ b/dom/base/nsContentSink.cpp @@ -820,7 +820,8 @@ nsContentSink::ProcessStyleLink(nsIContent* aElement, // We don't support CORS for processing instructions bool isAlternate; bool isExplicitlyEnabled; - rv = mCSSLoader->LoadStyleLink(aElement, url, aTitle, aMedia, aAlternate, + rv = mCSSLoader->LoadStyleLink(aElement, url, nullptr /* triggeringPrincipal */, + aTitle, aMedia, aAlternate, CORS_NONE, referrerPolicy, integrity, mRunsToCompletion ? nullptr : this, &isAlternate, &isExplicitlyEnabled); diff --git a/dom/base/nsStyleLinkElement.cpp b/dom/base/nsStyleLinkElement.cpp index 971af21b4c..2da27ca2f6 100644 --- a/dom/base/nsStyleLinkElement.cpp +++ b/dom/base/nsStyleLinkElement.cpp @@ -224,8 +224,7 @@ nsStyleLinkElement::UpdateStyleSheet(nsICSSLoaderObserver* aObserver, { if (aForceReload) { // We remove this stylesheet from the cache to load a new version. - nsCOMPtr thisContent; - CallQueryInterface(this, getter_AddRefs(thisContent)); + nsCOMPtr thisContent = do_QueryInterface(this); nsCOMPtr doc = thisContent->IsInShadowTree() ? thisContent->OwnerDoc() : thisContent->GetUncomposedDoc(); if (doc && doc->CSSLoader()->GetEnabled() && @@ -306,11 +305,10 @@ nsStyleLinkElement::DoUpdateStyleSheet(nsIDocument* aOldDocument, { *aWillNotify = false; - nsCOMPtr thisContent; - CallQueryInterface(this, getter_AddRefs(thisContent)); + nsCOMPtr thisContent = do_QueryInterface(this); // All instances of nsStyleLinkElement should implement nsIContent. - NS_ENSURE_TRUE(thisContent, NS_ERROR_FAILURE); + MOZ_ASSERT(thisContent); if (thisContent->IsInAnonymousSubtree() && thisContent->IsAnonymousContentInSVGUseSubtree()) { @@ -362,7 +360,8 @@ nsStyleLinkElement::DoUpdateStyleSheet(nsIDocument* aOldDocument, } bool isInline; - nsCOMPtr uri = GetStyleSheetURL(&isInline); + nsCOMPtr triggeringPrincipal; + nsCOMPtr uri = GetStyleSheetURL(&isInline, getter_AddRefs(triggeringPrincipal)); if (!aForceUpdate && mStyleSheet && !isInline && uri) { nsIURI* oldURI = mStyleSheet->GetSheetURI(); @@ -455,8 +454,8 @@ nsStyleLinkElement::DoUpdateStyleSheet(nsIDocument* aOldDocument, uri->Clone(getter_AddRefs(clonedURI)); NS_ENSURE_TRUE(clonedURI, NS_ERROR_OUT_OF_MEMORY); rv = doc->CSSLoader()-> - LoadStyleLink(thisContent, clonedURI, title, media, isAlternate, - GetCORSMode(), referrerPolicy, integrity, + LoadStyleLink(thisContent, clonedURI, triggeringPrincipal, title, media, + isAlternate, GetCORSMode(), referrerPolicy, integrity, aObserver, &isAlternate, &isExplicitlyEnabled); if (NS_FAILED(rv)) { // Don't propagate LoadStyleLink() errors further than this, since some @@ -486,8 +485,7 @@ nsStyleLinkElement::UpdateStyleSheetScopedness(bool aIsNowScoped) CSSStyleSheet* sheet = mStyleSheet->AsConcrete(); - nsCOMPtr thisContent; - CallQueryInterface(this, getter_AddRefs(thisContent)); + nsCOMPtr thisContent = do_QueryInterface(this); Element* oldScopeElement = sheet->GetScopeElement(); Element* newScopeElement = aIsNowScoped ? diff --git a/dom/base/nsStyleLinkElement.h b/dom/base/nsStyleLinkElement.h index a76cd8a6b2..b7d57ca3d1 100644 --- a/dom/base/nsStyleLinkElement.h +++ b/dom/base/nsStyleLinkElement.h @@ -95,7 +95,7 @@ protected: void UpdateStyleSheetScopedness(bool aIsNowScoped); - virtual already_AddRefed GetStyleSheetURL(bool* aIsInline) = 0; + virtual already_AddRefed GetStyleSheetURL(bool* aIsInline, nsIPrincipal** aTriggeringPrincipal) = 0; virtual void GetStyleSheetInfo(nsAString& aTitle, nsAString& aType, nsAString& aMedia, @@ -140,6 +140,7 @@ private: RefPtr mStyleSheet; protected: + nsCOMPtr mTriggeringPrincipal; bool mDontLoadStyle; bool mUpdatesEnabled; uint32_t mLineNumber; diff --git a/dom/html/HTMLLinkElement.cpp b/dom/html/HTMLLinkElement.cpp index 7b6379ae3f..e4426f0c21 100644 --- a/dom/html/HTMLLinkElement.cpp +++ b/dom/html/HTMLLinkElement.cpp @@ -374,6 +374,12 @@ HTMLLinkElement::AfterSetAttr(int32_t aNameSpaceID, nsIAtom* aName, } } + if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::href) { + mTriggeringPrincipal = nsContentUtils::GetAttrTriggeringPrincipal( + this, aValue ? aValue->GetStringValue() : EmptyString(), + aSubjectPrincipal); + } + if (aValue) { if (aNameSpaceID == kNameSpaceID_None && (aName == nsGkAtoms::href || @@ -520,14 +526,20 @@ HTMLLinkElement::GetHrefURI() const } already_AddRefed -HTMLLinkElement::GetStyleSheetURL(bool* aIsInline) +HTMLLinkElement::GetStyleSheetURL(bool* aIsInline, nsIPrincipal** aTriggeringPrincipal) { *aIsInline = false; + *aTriggeringPrincipal = nullptr; + nsAutoString href; GetAttr(kNameSpaceID_None, nsGkAtoms::href, href); if (href.IsEmpty()) { return nullptr; } + + nsCOMPtr prin = mTriggeringPrincipal; + prin.forget(aTriggeringPrincipal); + nsCOMPtr uri = Link::GetURI(); return uri.forget(); } diff --git a/dom/html/HTMLLinkElement.h b/dom/html/HTMLLinkElement.h index 7eb6b78e18..2c4af10c7f 100644 --- a/dom/html/HTMLLinkElement.h +++ b/dom/html/HTMLLinkElement.h @@ -88,10 +88,14 @@ public: // WebIDL bool Disabled() const; void SetDisabled(bool aDisabled, ErrorResult& aRv); - // XPCOM GetHref is fine. - void SetHref(const nsAString& aHref, ErrorResult& aRv) + + void GetHref(nsString& aValue, nsIPrincipal&) { - SetHTMLAttr(nsGkAtoms::href, aHref, aRv); + GetHref(aValue); + } + void SetHref(const nsAString& aHref, nsIPrincipal& aTriggeringPrincipal, ErrorResult& aRv) + { + SetHTMLAttr(nsGkAtoms::href, aHref, aTriggeringPrincipal, aRv); } void GetCrossOrigin(nsAString& aResult) { @@ -189,7 +193,7 @@ protected: virtual ~HTMLLinkElement(); // nsStyleLinkElement - virtual already_AddRefed GetStyleSheetURL(bool* aIsInline) override; + virtual already_AddRefed GetStyleSheetURL(bool* aIsInline, nsIPrincipal** aTriggeringPrincipal) override; virtual void GetStyleSheetInfo(nsAString& aTitle, nsAString& aType, nsAString& aMedia, diff --git a/dom/html/HTMLStyleElement.cpp b/dom/html/HTMLStyleElement.cpp index 4cc4477a55..f9e544ac30 100644 --- a/dom/html/HTMLStyleElement.cpp +++ b/dom/html/HTMLStyleElement.cpp @@ -214,9 +214,10 @@ HTMLStyleElement::SetInnerHTML(const nsAString& aInnerHTML, } already_AddRefed -HTMLStyleElement::GetStyleSheetURL(bool* aIsInline) +HTMLStyleElement::GetStyleSheetURL(bool* aIsInline, nsIPrincipal** aTriggeringPrincipal) { *aIsInline = true; + *aTriggeringPrincipal = nullptr; return nullptr; } diff --git a/dom/html/HTMLStyleElement.h b/dom/html/HTMLStyleElement.h index fe93b01d73..9bbf1da19f 100644 --- a/dom/html/HTMLStyleElement.h +++ b/dom/html/HTMLStyleElement.h @@ -91,7 +91,7 @@ public: protected: virtual ~HTMLStyleElement(); - already_AddRefed GetStyleSheetURL(bool* aIsInline) override; + already_AddRefed GetStyleSheetURL(bool* aIsInline, nsIPrincipal** aTriggeringPrincipal) override; void GetStyleSheetInfo(nsAString& aTitle, nsAString& aType, nsAString& aMedia, diff --git a/dom/svg/SVGStyleElement.cpp b/dom/svg/SVGStyleElement.cpp index e91b498f2a..d5001f55a5 100644 --- a/dom/svg/SVGStyleElement.cpp +++ b/dom/svg/SVGStyleElement.cpp @@ -260,9 +260,10 @@ SVGStyleElement::SetTitle(const nsAString& aTitle, ErrorResult& rv) // nsStyleLinkElement methods already_AddRefed -SVGStyleElement::GetStyleSheetURL(bool* aIsInline) +SVGStyleElement::GetStyleSheetURL(bool* aIsInline, nsIPrincipal** aTriggeringPrincipal) { *aIsInline = true; + *aTriggeringPrincipal = nullptr; return nullptr; } diff --git a/dom/svg/SVGStyleElement.h b/dom/svg/SVGStyleElement.h index 49d63520f4..8f87ef4c20 100644 --- a/dom/svg/SVGStyleElement.h +++ b/dom/svg/SVGStyleElement.h @@ -86,7 +86,7 @@ protected: } // nsStyleLinkElement overrides - already_AddRefed GetStyleSheetURL(bool* aIsInline) override; + already_AddRefed GetStyleSheetURL(bool* aIsInline, nsIPrincipal** aTriggeringPrincipal) override; void GetStyleSheetInfo(nsAString& aTitle, nsAString& aType, diff --git a/dom/webidl/HTMLLinkElement.webidl b/dom/webidl/HTMLLinkElement.webidl index 11149ce6b6..da4827de19 100644 --- a/dom/webidl/HTMLLinkElement.webidl +++ b/dom/webidl/HTMLLinkElement.webidl @@ -16,7 +16,7 @@ interface HTMLLinkElement : HTMLElement { [CEReactions, SetterThrows, Pure] attribute boolean disabled; - [CEReactions, SetterThrows, Pure] + [CEReactions, NeedsSubjectPrincipal, SetterThrows, Pure] attribute DOMString href; [CEReactions, SetterThrows, Pure] attribute DOMString? crossOrigin; diff --git a/dom/xml/XMLStylesheetProcessingInstruction.cpp b/dom/xml/XMLStylesheetProcessingInstruction.cpp index 52b87b2f9c..43609117e4 100644 --- a/dom/xml/XMLStylesheetProcessingInstruction.cpp +++ b/dom/xml/XMLStylesheetProcessingInstruction.cpp @@ -103,9 +103,10 @@ XMLStylesheetProcessingInstruction::OverrideBaseURI(nsIURI* aNewBaseURI) } already_AddRefed -XMLStylesheetProcessingInstruction::GetStyleSheetURL(bool* aIsInline) +XMLStylesheetProcessingInstruction::GetStyleSheetURL(bool* aIsInline, nsIPrincipal** aTriggeringPrincipal) { *aIsInline = false; + *aTriggeringPrincipal = nullptr; nsAutoString href; if (!GetAttrValue(nsGkAtoms::href, href)) { diff --git a/dom/xml/XMLStylesheetProcessingInstruction.h b/dom/xml/XMLStylesheetProcessingInstruction.h index 3581d16bf1..b5b1fc4bf3 100644 --- a/dom/xml/XMLStylesheetProcessingInstruction.h +++ b/dom/xml/XMLStylesheetProcessingInstruction.h @@ -76,7 +76,7 @@ protected: nsCOMPtr mOverriddenBaseURI; - already_AddRefed GetStyleSheetURL(bool* aIsInline) override; + already_AddRefed GetStyleSheetURL(bool* aIsInline, nsIPrincipal** aTriggeringPrincipal) override; void GetStyleSheetInfo(nsAString& aTitle, nsAString& aType, nsAString& aMedia, diff --git a/layout/style/Loader.cpp b/layout/style/Loader.cpp index 41ed9a1af5..581fe9622d 100644 --- a/layout/style/Loader.cpp +++ b/layout/style/Loader.cpp @@ -2015,6 +2015,7 @@ Loader::LoadInlineStyle(nsIContent* aElement, nsresult Loader::LoadStyleLink(nsIContent* aElement, nsIURI* aURL, + nsIPrincipal* aTriggeringPrincipal, const nsAString& aTitle, const nsAString& aMedia, bool aHasAlternateRel, @@ -2044,11 +2045,9 @@ Loader::LoadStyleLink(nsIContent* aElement, nsIPrincipal* loadingPrincipal = aElement ? aElement->NodePrincipal() : mDocument->NodePrincipal(); - //SHOULD BE: - //nsIPrincipal* principal = aTriggeringPrincipal ? aTriggeringPrincipal - // : loadingPrincipal; - nsIPrincipal* principal = loadingPrincipal; - + nsIPrincipal* principal = aTriggeringPrincipal ? aTriggeringPrincipal + : loadingPrincipal; + nsISupports* context = aElement; if (!context) { context = mDocument; diff --git a/layout/style/Loader.h b/layout/style/Loader.h index 830099b3ce..23cf2d54da 100644 --- a/layout/style/Loader.h +++ b/layout/style/Loader.h @@ -254,6 +254,9 @@ public: * * @param aElement the element linking to the the stylesheet. May be null. * @param aURL the URL of the sheet. + * @param aTriggeringPrincipal the triggering principal for the load. May be + * null, in which case the NodePrincipal() of the element (or + * document if aElement is null) should be used. * @param aTitle the title of the sheet. * @param aMedia the media string for the sheet. * @param aHasAlternateRel whether the rel for this link included @@ -269,6 +272,7 @@ public: */ nsresult LoadStyleLink(nsIContent* aElement, nsIURI* aURL, + nsIPrincipal* aTriggeringPrincipal, const nsAString& aTitle, const nsAString& aMedia, bool aHasAlternateRel,