Bug 1396449: Part 2 - Use atoms to test WebExtension permissions. r=krizsa

The extension policy services uses atoms internally for permission names, so
using them directly rather than strings is considerably cheaper.

MozReview-Commit-ID: Io8EuOXHKVy

--HG--
extra : rebase_source : 577b4bdf7f899729e4cf92961a8e9e25bf886a72
This commit is contained in:
Kris Maglione 2017-09-03 18:51:02 -07:00
Родитель b3ba0520ee
Коммит 04acc0aace
13 изменённых файлов: 24 добавлений и 18 удалений

Просмотреть файл

@ -347,7 +347,7 @@ BasePrincipal::AddonPolicy()
}
bool
BasePrincipal::AddonHasPermission(const nsAString& aPerm)
BasePrincipal::AddonHasPermission(const nsIAtom* aPerm)
{
if (auto policy = AddonPolicy()) {
return policy->HasPermission(aPerm);

Просмотреть файл

@ -12,6 +12,7 @@
#include "mozilla/Attributes.h"
#include "mozilla/OriginAttributes.h"
class nsIAtom;
class nsIContentSecurityPolicy;
class nsIObjectOutputStream;
class nsIObjectInputStream;
@ -85,7 +86,7 @@ public:
NS_IMETHOD GetUserContextId(uint32_t* aUserContextId) final;
NS_IMETHOD GetPrivateBrowsingId(uint32_t* aPrivateBrowsingId) final;
virtual bool AddonHasPermission(const nsAString& aPerm);
virtual bool AddonHasPermission(const nsIAtom* aPerm);
virtual bool IsCodebasePrincipal() const { return false; };

Просмотреть файл

@ -172,7 +172,7 @@ ExpandedPrincipal::GetAddonId(nsAString& aAddonId)
};
bool
ExpandedPrincipal::AddonHasPermission(const nsAString& aPerm)
ExpandedPrincipal::AddonHasPermission(const nsIAtom* aPerm)
{
for (size_t i = 0; i < mPrincipals.Length(); ++i) {
if (BasePrincipal::Cast(mPrincipals[i])->AddonHasPermission(aPerm)) {

Просмотреть файл

@ -34,7 +34,7 @@ public:
NS_IMETHOD SetDomain(nsIURI* aDomain) override;
NS_IMETHOD GetBaseDomain(nsACString& aBaseDomain) override;
NS_IMETHOD GetAddonId(nsAString& aAddonId) override;
virtual bool AddonHasPermission(const nsAString& aPerm) override;
virtual bool AddonHasPermission(const nsIAtom* aPerm) override;
virtual nsresult GetScriptLocation(nsACString &aStr) override;
protected:

Просмотреть файл

@ -2331,7 +2331,7 @@ nsContentUtils::CanCallerAccess(nsPIDOMWindowInner* aWindow)
// static
bool
nsContentUtils::PrincipalHasPermission(nsIPrincipal* aPrincipal, const nsAString& aPerm)
nsContentUtils::PrincipalHasPermission(nsIPrincipal* aPrincipal, const nsIAtom* aPerm)
{
// Chrome gets access by default.
if (IsSystemPrincipal(aPrincipal)) {
@ -2344,7 +2344,7 @@ nsContentUtils::PrincipalHasPermission(nsIPrincipal* aPrincipal, const nsAString
// static
bool
nsContentUtils::CallerHasPermission(JSContext* aCx, const nsAString& aPerm)
nsContentUtils::CallerHasPermission(JSContext* aCx, const nsIAtom* aPerm)
{
return PrincipalHasPermission(SubjectPrincipal(aCx), aPerm);
}
@ -7350,7 +7350,7 @@ nsContentUtils::IsCutCopyAllowed(nsIPrincipal* aSubjectPrincipal)
return true;
}
return PrincipalHasPermission(aSubjectPrincipal, NS_LITERAL_STRING("clipboardWrite"));
return PrincipalHasPermission(aSubjectPrincipal, nsGkAtoms::clipboardWrite);
}
/* static */

Просмотреть файл

@ -57,6 +57,7 @@ class imgRequestProxy;
class nsAutoScriptBlockerSuppressNodeRemoved;
class nsCacheableFuncStringHTMLCollection;
class nsHtml5StringParser;
class nsIAtom;
class nsIChannel;
class nsIConsoleService;
class nsIContent;
@ -590,10 +591,10 @@ public:
static bool CanCallerAccess(nsPIDOMWindowInner* aWindow);
// Check if the principal is chrome or an addon with the permission.
static bool PrincipalHasPermission(nsIPrincipal* aPrincipal, const nsAString& aPerm);
static bool PrincipalHasPermission(nsIPrincipal* aPrincipal, const nsIAtom* aPerm);
// Check if the JS caller is chrome or an addon with the permission.
static bool CallerHasPermission(JSContext* aCx, const nsAString& aPerm);
static bool CallerHasPermission(JSContext* aCx, const nsIAtom* aPerm);
/**
* GetDocumentFromCaller gets its document by looking at the last called

Просмотреть файл

@ -2069,6 +2069,11 @@ GK_ATOM(mozinputrangeignorepreventdefault, "mozinputrangeignorepreventdefault")
// WebExtensions
GK_ATOM(moz_extension, "moz-extension")
GK_ATOM(all_urlsPermission, "<all_urls>")
GK_ATOM(clipboardRead, "clipboardRead")
GK_ATOM(clipboardWrite, "clipboardWrite")
GK_ATOM(debugger, "debugger")
GK_ATOM(tabs, "tabs")
GK_ATOM(webRequestBlocking, "webRequestBlocking")
GK_ATOM(http, "http")
GK_ATOM(https, "https")

Просмотреть файл

@ -5687,7 +5687,7 @@ CanvasRenderingContext2D::GetImageData(JSContext* aCx, double aSx,
// JSContext, and we're at least _somewhat_ perf-sensitive (so may not
// want to compute the caller type in the common non-write-only case), so
// let's just use what we have.
!nsContentUtils::CallerHasPermission(aCx, NS_LITERAL_STRING("<all_urls>")))
!nsContentUtils::CallerHasPermission(aCx, nsGkAtoms::all_urlsPermission))
{
// XXX ERRMSG we need to report an error to developers here! (bug 329026)
aError.Throw(NS_ERROR_DOM_SECURITY_ERR);

Просмотреть файл

@ -118,7 +118,7 @@ CoerceDouble(const JS::Value& v, double* d)
bool
HasDrawWindowPrivilege(JSContext* aCx, JSObject* /* unused */)
{
return nsContentUtils::CallerHasPermission(aCx, NS_LITERAL_STRING("<all_urls>"));
return nsContentUtils::CallerHasPermission(aCx, nsGkAtoms::all_urlsPermission);
}
} // namespace CanvasUtils

Просмотреть файл

@ -652,7 +652,7 @@ HTMLCanvasElement::ToDataURL(JSContext* aCx, const nsAString& aType,
{
// do a trust check if this is a write-only canvas
if (mWriteOnly &&
!nsContentUtils::CallerHasPermission(aCx, NS_LITERAL_STRING("<all_urls>"))) {
!nsContentUtils::CallerHasPermission(aCx, nsGkAtoms::all_urlsPermission)) {
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
return;
}
@ -835,7 +835,7 @@ HTMLCanvasElement::ToBlob(JSContext* aCx,
{
// do a trust check if this is a write-only canvas
if (mWriteOnly &&
!nsContentUtils::CallerHasPermission(aCx, NS_LITERAL_STRING("<all_urls>"))) {
!nsContentUtils::CallerHasPermission(aCx, nsGkAtoms::all_urlsPermission)) {
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
return;
}

Просмотреть файл

@ -7570,9 +7570,8 @@ HTMLMediaElement::MarkAsTainted()
bool
HasDebuggerOrTabsPrivilege(JSContext* aCx, JSObject* aObj)
{
return nsContentUtils::CallerHasPermission(aCx,
NS_LITERAL_STRING("debugger")) ||
nsContentUtils::CallerHasPermission(aCx, NS_LITERAL_STRING("tabs"));
return nsContentUtils::CallerHasPermission(aCx, nsGkAtoms::debugger) ||
nsContentUtils::CallerHasPermission(aCx, nsGkAtoms::tabs);
}
void

Просмотреть файл

@ -3290,7 +3290,7 @@ nsHTMLDocument::ExecCommand(const nsAString& commandID,
bool restricted = commandID.LowerCaseEqualsLiteral("paste");
if (restricted && !nsContentUtils::PrincipalHasPermission(&aSubjectPrincipal,
NS_LITERAL_STRING("clipboardRead"))) {
nsGkAtoms::clipboardRead)) {
return false;
}

Просмотреть файл

@ -261,7 +261,7 @@ StreamFilter::FireErrorEvent(const nsAString& aError)
/* static */ bool
StreamFilter::IsAllowedInContext(JSContext* aCx, JSObject* /* unused */)
{
return nsContentUtils::CallerHasPermission(aCx, NS_LITERAL_STRING("webRequestBlocking"));
return nsContentUtils::CallerHasPermission(aCx, nsGkAtoms::webRequestBlocking);
}
JSObject*