diff --git a/caps/BasePrincipal.cpp b/caps/BasePrincipal.cpp index bbb803e252ef..3cea1f4f0ce1 100644 --- a/caps/BasePrincipal.cpp +++ b/caps/BasePrincipal.cpp @@ -488,6 +488,51 @@ BasePrincipal::IsSameOrigin(nsIURI* aURI, bool aIsPrivateWin, bool* aRes) { ssm->CheckSameOriginURI(prinURI, aURI, false, aIsPrivateWin)); return NS_OK; } + +NS_IMETHODIMP +BasePrincipal::IsL10nAllowed(nsIURI* aURI, bool* aRes) { + *aRes = false; + + if (nsContentUtils::IsErrorPage(aURI)) { + *aRes = true; + return NS_OK; + } + + // The system principal is always allowed. + if (IsSystemPrincipal()) { + *aRes = true; + return NS_OK; + } + + nsCOMPtr uri; + nsresult rv = GetURI(getter_AddRefs(uri)); + NS_ENSURE_SUCCESS(rv, NS_OK); + + bool hasFlags; + + // Allow access to uris that cannot be loaded by web content. + rv = NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_DANGEROUS_TO_LOAD, + &hasFlags); + NS_ENSURE_SUCCESS(rv, NS_OK); + if (hasFlags) { + *aRes = true; + return NS_OK; + } + + // UI resources also get access. + rv = NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_IS_UI_RESOURCE, + &hasFlags); + NS_ENSURE_SUCCESS(rv, NS_OK); + if (hasFlags) { + *aRes = true; + return NS_OK; + } + + auto policy = AddonPolicy(); + *aRes = (policy && policy->IsPrivileged()); + return NS_OK; +} + NS_IMETHODIMP BasePrincipal::AllowsRelaxStrictFileOriginPolicy(nsIURI* aURI, bool* aRes) { *aRes = false; diff --git a/caps/BasePrincipal.h b/caps/BasePrincipal.h index d0dedcccb975..fd27423b776b 100644 --- a/caps/BasePrincipal.h +++ b/caps/BasePrincipal.h @@ -122,6 +122,7 @@ class BasePrincipal : public nsJSPrincipals { NS_IMETHOD GetIsSystemPrincipal(bool* aResult) override; NS_IMETHOD SchemeIs(const char* aScheme, bool* aResult) override; NS_IMETHOD IsURIInPrefList(const char* aPref, bool* aResult) override; + NS_IMETHOD IsL10nAllowed(nsIURI* aURI, bool* aResult) override; NS_IMETHOD GetAboutModuleFlags(uint32_t* flags) override; NS_IMETHOD GetIsAddonOrExpandedAddonPrincipal(bool* aResult) override; NS_IMETHOD GetOriginAttributes(JSContext* aCx, diff --git a/caps/nsIPrincipal.idl b/caps/nsIPrincipal.idl index 64f6737ff9c5..1b262ffb578e 100644 --- a/caps/nsIPrincipal.idl +++ b/caps/nsIPrincipal.idl @@ -462,6 +462,13 @@ interface nsIPrincipal : nsISerializable */ readonly attribute boolean isScriptAllowedByPolicy; + + /* + * Returns true if the Principal can acess l10n + * features for the Provided DocumentURI + */ + boolean isL10nAllowed(in nsIURI aDocumentURI); + /** * Returns if the principal is for an IP address. */ diff --git a/dom/base/Document.cpp b/dom/base/Document.cpp index 281bdfef3596..0c822f65b5b7 100644 --- a/dom/base/Document.cpp +++ b/dom/base/Document.cpp @@ -3874,8 +3874,10 @@ bool Document::DocumentSupportsL10n(JSContext* aCx, JSObject* aObject) { nsCOMPtr callerPrincipal = nsContentUtils::SubjectPrincipal(aCx); nsGlobalWindowInner* win = xpc::WindowOrNull(aObject); - return nsContentUtils::PrincipalAllowsL10n( - *callerPrincipal, win ? win->GetDocumentURI() : nullptr); + bool allowed = false; + callerPrincipal->IsL10nAllowed(win ? win->GetDocumentURI() : nullptr, + &allowed); + return allowed; } void Document::LocalizationLinkAdded(Element* aLinkElement) { @@ -3984,8 +3986,9 @@ void Document::InitialDocumentTranslationCompleted() { } bool Document::AllowsL10n() const { - return nsContentUtils::PrincipalAllowsL10n(*NodePrincipal(), - GetDocumentURI()); + bool allowed = false; + NodePrincipal()->IsL10nAllowed(GetDocumentURI(), &allowed); + return allowed; } bool Document::IsWebAnimationsEnabled(JSContext* aCx, JSObject* /*unused*/) { diff --git a/dom/base/nsContentUtils.cpp b/dom/base/nsContentUtils.cpp index 474ebdeabca0..52d8e3e3a122 100644 --- a/dom/base/nsContentUtils.cpp +++ b/dom/base/nsContentUtils.cpp @@ -1719,8 +1719,8 @@ bool nsContentUtils::OfflineAppAllowed(nsIPrincipal* aPrincipal) { nsresult rv = updateService->OfflineAppAllowed(aPrincipal, &allowed); return NS_SUCCEEDED(rv) && allowed; } - -static bool IsErrorPage(nsIURI* aURI) { +// Static +bool nsContentUtils::IsErrorPage(nsIURI* aURI) { if (!aURI) { return false; } @@ -1737,45 +1737,6 @@ static bool IsErrorPage(nsIURI* aURI) { name.EqualsLiteral("blocked"); } -/* static */ -bool nsContentUtils::PrincipalAllowsL10n(nsIPrincipal& aPrincipal, - nsIURI* aDocumentURI) { - if (IsErrorPage(aDocumentURI)) { - return true; - } - - // The system principal is always allowed. - if (aPrincipal.IsSystemPrincipal()) { - return true; - } - - nsCOMPtr uri; - nsresult rv = aPrincipal.GetURI(getter_AddRefs(uri)); - NS_ENSURE_SUCCESS(rv, false); - - bool hasFlags; - - // Allow access to uris that cannot be loaded by web content. - rv = NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_DANGEROUS_TO_LOAD, - &hasFlags); - NS_ENSURE_SUCCESS(rv, false); - if (hasFlags) { - return true; - } - - // UI resources also get access. - rv = NS_URIChainHasFlags(uri, nsIProtocolHandler::URI_IS_UI_RESOURCE, - &hasFlags); - NS_ENSURE_SUCCESS(rv, false); - if (hasFlags) { - return true; - } - - auto& principal = BasePrincipal::Cast(aPrincipal); - auto policy = principal.AddonPolicy(); - return (policy && policy->IsPrivileged()); -} - // static void nsContentUtils::Shutdown() { sInitialized = false; diff --git a/dom/base/nsContentUtils.h b/dom/base/nsContentUtils.h index 791a1a4a89ea..d93f087daf49 100644 --- a/dom/base/nsContentUtils.h +++ b/dom/base/nsContentUtils.h @@ -237,6 +237,7 @@ class nsContentUtils { #else ; #endif + static bool IsErrorPage(nsIURI* aURI); static bool IsCallerChromeOrFuzzingEnabled(JSContext* aCx, JSObject*) { return ThreadsafeIsSystemCaller(aCx) || IsFuzzingEnabled(); diff --git a/dom/security/nsContentSecurityManager.cpp b/dom/security/nsContentSecurityManager.cpp index e2ce32ac01f7..c6fb07d6b7b5 100644 --- a/dom/security/nsContentSecurityManager.cpp +++ b/dom/security/nsContentSecurityManager.cpp @@ -287,11 +287,11 @@ static nsresult DoCheckLoadURIChecks(nsIURI* aURI, nsILoadInfo* aLoadInfo) { nsIContentPolicy::TYPE_INTERNAL_DTD) { RefPtr doc; aLoadInfo->GetLoadingDocument(getter_AddRefs(doc)); - return nsContentUtils::PrincipalAllowsL10n( - *aLoadInfo->TriggeringPrincipal(), - doc ? doc->GetDocumentURI() : nullptr) - ? NS_OK - : NS_ERROR_DOM_BAD_URI; + bool allowed = false; + aLoadInfo->TriggeringPrincipal()->IsL10nAllowed( + doc ? doc->GetDocumentURI() : nullptr, &allowed); + + return allowed ? NS_OK : NS_ERROR_DOM_BAD_URI; } // This is used in order to allow a privileged DOMParser to parse documents