(from Waterfox Classic) Refactor addonId access in BasePrincipal and related files

This commit is contained in:
wuggy 2026-04-22 00:13:33 -07:00
commit 203d58a5cd
6 changed files with 26 additions and 13 deletions

View file

@ -681,7 +681,10 @@ BasePrincipal::GetUnknownAppId(bool* aUnknownAppId)
bool
BasePrincipal::AddonHasPermission(const nsAString& aPerm)
{
if (mOriginAttributes.mAddonId.IsEmpty()) {
nsAutoString addonId;
NS_ENSURE_SUCCESS(GetAddonId(addonId), false);
if (addonId.IsEmpty()) {
return false;
}
nsCOMPtr<nsIAddonPolicyService> aps =
@ -689,7 +692,7 @@ BasePrincipal::AddonHasPermission(const nsAString& aPerm)
NS_ENSURE_TRUE(aps, false);
bool retval = false;
nsresult rv = aps->AddonHasPermission(mOriginAttributes.mAddonId, aPerm, &retval);
nsresult rv = aps->AddonHasPermission(addonId, aPerm, &retval);
NS_ENSURE_SUCCESS(rv, false);
return retval;
}
@ -767,7 +770,10 @@ BasePrincipal::CloneStrippingUserContextIdAndFirstPartyDomain()
bool
BasePrincipal::AddonAllowsLoad(nsIURI* aURI)
{
if (mOriginAttributes.mAddonId.IsEmpty()) {
nsAutoString addonId;
NS_ENSURE_SUCCESS(GetAddonId(addonId), false);
if (addonId.IsEmpty()) {
return false;
}
@ -775,7 +781,7 @@ BasePrincipal::AddonAllowsLoad(nsIURI* aURI)
NS_ENSURE_TRUE(aps, false);
bool allowed = false;
nsresult rv = aps->AddonMayLoadURI(mOriginAttributes.mAddonId, aURI, &allowed);
nsresult rv = aps->AddonMayLoadURI(addonId, aURI, &allowed);
return NS_SUCCEEDED(rv) && allowed;
}

View file

@ -93,7 +93,7 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=1161831
is(stripTrailingSlash(prin.URI.spec), url, 'Principal uri is correct: ' + url);
function stripPath(s) { return s.replace(/(.*\/\/.+)\/.*/, '$1'); };
is(prin.originNoSuffix, stripPath(url), 'Principal origin is correct: ' + prin.originNoSuffix);
is(prin.originAttributes.addonId, 'imaginaryaddon-' + url[url.indexOf('/') + 2], 'addonId is correct');
is(prin.addonId, 'imaginaryaddon-' + url[url.indexOf('/') + 2], 'addonId is correct');
if (/_blank/.test(url)) {
is(SpecialPowers.wrap(ifr.contentWindow).document.documentElement.innerHTML,
'<head></head><body></body>', 'blank document looks right');

View file

@ -1390,8 +1390,8 @@ TabActor.prototype = {
.outerWindowID;
}
// Collect the addonID from the document origin attributes.
let addonID = window.document.nodePrincipal.originAttributes.addonId;
// Collect the addonID from the document principal.
let addonID = window.document.nodePrincipal.addonId;
return {
id,

View file

@ -309,7 +309,15 @@ WebExtensionActor.prototype._shouldAddNewGlobalAsDebuggee = function (newGlobal)
const global = unwrapDebuggerObjectGlobal(newGlobal);
if (global instanceof Ci.nsIDOMWindow) {
return global.document.nodePrincipal.originAttributes.addonId == this.id;
try {
global.document;
} catch (e) {
// Sandboxes can expose a window in their prototype chain without
// having permission to access their own proto after navigation.
return false;
}
return global.document.nodePrincipal.addonId == this.id;
}
try {

View file

@ -356,7 +356,7 @@ class ContentScriptContextChild extends BaseContext {
// the tab holding the content page.
let metadata = {
"inner-window-id": this.innerWindowID,
addonId: attrs.addonId,
addonId: extensionPrincipal.addonId,
};
this.sandbox = Cu.Sandbox(principal, {

View file

@ -223,8 +223,7 @@ var Service = {
},
// Finds the add-on ID associated with a given moz-extension:// URI.
// This is used to set the addonId on the originAttributes for the
// nsIPrincipal attached to the URI.
// This is used to set the addonId on the nsIPrincipal attached to the URI.
extensionURIToAddonID(uri) {
let uuid = uri.host;
let extension = this.uuidMap.get(uuid);
@ -235,11 +234,11 @@ var Service = {
// API Levels Helpers
// Find the add-on associated with this document via the
// principal's originAttributes. This value is computed by
// principal's addonId. This value is computed by
// extensionURIToAddonID, which ensures that we don't inject our
// API into webAccessibleResources or remote web pages.
function getAddonIdForWindow(window) {
return Cu.getObjectPrincipal(window).originAttributes.addonId;
return Cu.getObjectPrincipal(window).addonId;
}
const API_LEVELS = Object.freeze({