Bug 1594781 -- Refactor nsGlobalWindowOuter r=ckerschb

Differential Revision: https://phabricator.services.mozilla.com/D52186

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Sebastian Streich 2020-01-22 17:14:08 +00:00
parent d0561affbf
commit be513b0703
4 changed files with 65 additions and 46 deletions

View File

@ -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<nsIURI> 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<nsIURI> 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<nsIURI> prinURI;
nsresult rv = GetURI(getter_AddRefs(prinURI));
if (NS_FAILED(rv) || !prinURI) {
return NS_OK;
}
nsCOMPtr<nsIURIFixup> fixup(components::URIFixup::Service());
nsCOMPtr<nsIURIFixup> urifixup = services::GetURIFixup();
if (NS_WARN_IF(!urifixup)) {
return NS_OK;
}
nsCOMPtr<nsIURI> 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();

View File

@ -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<JS::Value> 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(

View File

@ -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
*

View File

@ -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<nsIURI> uri;
nsresult rv = aSubjectPrincipal->GetURI(getter_AddRefs(uri));
if (NS_SUCCEEDED(rv) && uri) {
// remove user:pass for privacy and spoof prevention
nsCOMPtr<nsIURIFixup> fixup(components::URIFixup::Service());
if (fixup) {
nsCOMPtr<nsIURI> 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<nsIURI> 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<nsIURI> 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<nsIURI> 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");