From be513b070371f80f61e59493d8649ccdae260d6b Mon Sep 17 00:00:00 2001 From: Sebastian Streich Date: Wed, 22 Jan 2020 17:14:08 +0000 Subject: [PATCH] Bug 1594781 -- Refactor nsGlobalWindowOuter r=ckerschb Differential Revision: https://phabricator.services.mozilla.com/D52186 --HG-- extra : moz-landing-system : lando --- caps/BasePrincipal.cpp | 39 ++++++++++++++++++++- caps/BasePrincipal.h | 2 ++ caps/nsIPrincipal.idl | 10 ++++++ dom/base/nsGlobalWindowOuter.cpp | 60 ++++++++------------------------ 4 files changed, 65 insertions(+), 46 deletions(-) diff --git a/caps/BasePrincipal.cpp b/caps/BasePrincipal.cpp index 4f91355a441f..994dc0910b79 100644 --- a/caps/BasePrincipal.cpp +++ b/caps/BasePrincipal.cpp @@ -22,6 +22,8 @@ #include "mozilla/dom/ChromeUtils.h" #include "mozilla/dom/ToJSValue.h" #include "mozilla/dom/nsMixedContentBlocker.h" +#include "mozilla/Components.h" +#include "nsIURIFixup.h" #include "json/json.h" #include "nsSerializationHelper.h" @@ -53,7 +55,7 @@ BasePrincipal::GetAsciiOrigin(nsACString& aOrigin) { nsCOMPtr prinURI; nsresult rv = GetURI(getter_AddRefs(prinURI)); if (NS_FAILED(rv) || !prinURI) { - return NS_OK; + return NS_ERROR_NOT_AVAILABLE; } return nsContentUtils::GetASCIIOrigin(prinURI, aOrigin); } @@ -320,6 +322,17 @@ BasePrincipal::EqualsConsideringDomain(nsIPrincipal* aOther, bool* aResult) { return NS_OK; } +NS_IMETHODIMP +BasePrincipal::EqualsURI(nsIURI* aOtherURI, bool* aResult) { + *aResult = false; + nsCOMPtr prinURI; + nsresult rv = GetURI(getter_AddRefs(prinURI)); + if (NS_FAILED(rv) || !prinURI) { + return NS_OK; + } + return prinURI->EqualsExceptRef(aOtherURI, aResult); +} + NS_IMETHODIMP BasePrincipal::Subsumes(nsIPrincipal* aOther, bool* aResult) { NS_ENSURE_ARG_POINTER(aOther); @@ -480,6 +493,30 @@ BasePrincipal::GetAsciiSpec(nsACString& aSpec) { return prinURI->GetAsciiSpec(aSpec); } +NS_IMETHODIMP +BasePrincipal::GetExposablePrePath(nsACString& aPrepath) { + aPrepath.Truncate(); + nsCOMPtr prinURI; + nsresult rv = GetURI(getter_AddRefs(prinURI)); + if (NS_FAILED(rv) || !prinURI) { + return NS_OK; + } + + nsCOMPtr fixup(components::URIFixup::Service()); + nsCOMPtr urifixup = services::GetURIFixup(); + if (NS_WARN_IF(!urifixup)) { + return NS_OK; + } + + nsCOMPtr fixedURI; + rv = fixup->CreateExposableURI(prinURI, getter_AddRefs(fixedURI)); + + if (NS_FAILED(rv) || NS_WARN_IF(!fixedURI)) { + return NS_OK; + } + return fixedURI->GetDisplayPrePath(aPrepath); +} + NS_IMETHODIMP BasePrincipal::GetIsSystemPrincipal(bool* aResult) { *aResult = IsSystemPrincipal(); diff --git a/caps/BasePrincipal.h b/caps/BasePrincipal.h index 3b29971cf520..c9425a75ad59 100644 --- a/caps/BasePrincipal.h +++ b/caps/BasePrincipal.h @@ -105,6 +105,7 @@ class BasePrincipal : public nsJSPrincipals { NS_IMETHOD GetOriginNoSuffix(nsACString& aOrigin) final; NS_IMETHOD Equals(nsIPrincipal* other, bool* _retval) final; NS_IMETHOD EqualsConsideringDomain(nsIPrincipal* other, bool* _retval) final; + NS_IMETHOD EqualsURI(nsIURI* aOtherURI, bool* _retval) override; NS_IMETHOD Subsumes(nsIPrincipal* other, bool* _retval) final; NS_IMETHOD SubsumesConsideringDomain(nsIPrincipal* other, bool* _retval) final; @@ -126,6 +127,7 @@ class BasePrincipal : public nsJSPrincipals { NS_IMETHOD GetOriginAttributes(JSContext* aCx, JS::MutableHandle aVal) final; NS_IMETHOD GetAsciiSpec(nsACString& aSpec) override; + NS_IMETHOD GetExposablePrePath(nsACString& aResult) override; NS_IMETHOD GetOriginSuffix(nsACString& aOriginSuffix) final; NS_IMETHOD GetIsOnion(bool* aIsOnion) override; NS_IMETHOD GetIsInIsolatedMozBrowserElement( diff --git a/caps/nsIPrincipal.idl b/caps/nsIPrincipal.idl index d2f39122af03..7f40eb4f1cdb 100644 --- a/caps/nsIPrincipal.idl +++ b/caps/nsIPrincipal.idl @@ -66,6 +66,11 @@ interface nsIPrincipal : nsISerializable DECL_FAST_INLINE_HELPER(EqualsConsideringDomain) %} + /* + * Returns whether the Principals URI is equal to the other URI + */ + boolean EqualsURI(in nsIURI aOtherURI); + /** * Returns a hash value for the principal. */ @@ -222,6 +227,11 @@ interface nsIPrincipal : nsISerializable */ [noscript] readonly attribute ACString asciiSpec; + /* Returns the Pre Path of the Principals URI with + * user:pass stripped for privacy and spoof prevention + */ + readonly attribute ACString ExposablePrePath; + /** * Checks if the Principal's URI Scheme matches with the parameter * diff --git a/dom/base/nsGlobalWindowOuter.cpp b/dom/base/nsGlobalWindowOuter.cpp index 4482348e8ba3..84da2dce988a 100644 --- a/dom/base/nsGlobalWindowOuter.cpp +++ b/dom/base/nsGlobalWindowOuter.cpp @@ -4562,34 +4562,13 @@ void nsGlobalWindowOuter::MakeScriptDialogTitle( // Try to get a host from the running principal -- this will do the // right thing for javascript: and data: documents. - nsCOMPtr uri; - nsresult rv = aSubjectPrincipal->GetURI(getter_AddRefs(uri)); - if (NS_SUCCEEDED(rv) && uri) { - // remove user:pass for privacy and spoof prevention - - nsCOMPtr fixup(components::URIFixup::Service()); - if (fixup) { - nsCOMPtr fixedURI; - rv = fixup->CreateExposableURI(uri, getter_AddRefs(fixedURI)); - if (NS_SUCCEEDED(rv) && fixedURI) { - nsAutoCString host; - fixedURI->GetHost(host); - - if (!host.IsEmpty()) { - // if this URI has a host we'll show it. For other - // schemes (e.g. file:) we fall back to the localized - // generic string - - nsAutoCString prepath; - fixedURI->GetDisplayPrePath(prepath); - - NS_ConvertUTF8toUTF16 ucsPrePath(prepath); - nsContentUtils::FormatLocalizedString( - aOutTitle, nsContentUtils::eCOMMON_DIALOG_PROPERTIES, - "ScriptDlgHeading", ucsPrePath); - } - } - } + nsAutoCString prepath; + nsresult rv = aSubjectPrincipal->GetExposablePrePath(prepath); + if (NS_SUCCEEDED(rv) && !prepath.IsEmpty()) { + NS_ConvertUTF8toUTF16 ucsPrePath(prepath); + nsContentUtils::FormatLocalizedString( + aOutTitle, nsContentUtils::eCOMMON_DIALOG_PROPERTIES, + "ScriptDlgHeading", ucsPrePath); } if (aOutTitle.IsEmpty()) { @@ -5509,13 +5488,9 @@ bool nsGlobalWindowOuter::SameLoadingURI(Document* aDoc, nsIChannel* aChannel) { // return false return false; } - nsCOMPtr channelLoadingURI; - channelLoadingPrincipal->GetURI(getter_AddRefs(channelLoadingURI)); - if (!channelLoadingURI) { - return false; - } bool equals = false; - nsresult rv = docURI->EqualsExceptRef(channelLoadingURI, &equals); + nsresult rv = channelLoadingPrincipal->EqualsURI(docURI, &equals); + return NS_SUCCEEDED(rv) && equals; } @@ -5796,14 +5771,11 @@ bool nsGlobalWindowOuter::GatherPostMessageData( return false; } - nsCOMPtr callerOuterURI; - if (NS_FAILED(callerPrin->GetURI(getter_AddRefs(callerOuterURI)))) { - return false; - } - - if (callerOuterURI) { - // if the principal has a URI, use that to generate the origin - nsContentUtils::GetUTFOrigin(callerPrin, aOrigin); + // if the principal has a URI, use that to generate the origin + if (!callerPrin->IsSystemPrincipal()) { + nsAutoCString asciiOrigin; + callerPrin->GetAsciiOrigin(asciiOrigin); + aOrigin = NS_ConvertUTF8toUTF16(asciiOrigin); } else if (callerInnerWin) { if (!*aCallerURI) { return false; @@ -5880,13 +5852,11 @@ bool nsGlobalWindowOuter::GetPrincipalForPostMessage( auto principal = BasePrincipal::Cast(GetPrincipal()); if (attrs != principal->OriginAttributesRef()) { - nsCOMPtr targetURI; nsAutoCString targetURL; nsAutoCString sourceOrigin; nsAutoCString targetOrigin; - if (NS_FAILED(principal->GetURI(getter_AddRefs(targetURI))) || - NS_FAILED(targetURI->GetAsciiSpec(targetURL)) || + if (NS_FAILED(principal->GetAsciiSpec(targetURL)) || NS_FAILED(principal->GetOrigin(targetOrigin)) || NS_FAILED(aSubjectPrincipal.GetOrigin(sourceOrigin))) { NS_WARNING("Failed to get source and target origins");