(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 bool
BasePrincipal::AddonHasPermission(const nsAString& aPerm) BasePrincipal::AddonHasPermission(const nsAString& aPerm)
{ {
if (mOriginAttributes.mAddonId.IsEmpty()) { nsAutoString addonId;
NS_ENSURE_SUCCESS(GetAddonId(addonId), false);
if (addonId.IsEmpty()) {
return false; return false;
} }
nsCOMPtr<nsIAddonPolicyService> aps = nsCOMPtr<nsIAddonPolicyService> aps =
@ -689,7 +692,7 @@ BasePrincipal::AddonHasPermission(const nsAString& aPerm)
NS_ENSURE_TRUE(aps, false); NS_ENSURE_TRUE(aps, false);
bool retval = false; bool retval = false;
nsresult rv = aps->AddonHasPermission(mOriginAttributes.mAddonId, aPerm, &retval); nsresult rv = aps->AddonHasPermission(addonId, aPerm, &retval);
NS_ENSURE_SUCCESS(rv, false); NS_ENSURE_SUCCESS(rv, false);
return retval; return retval;
} }
@ -767,7 +770,10 @@ BasePrincipal::CloneStrippingUserContextIdAndFirstPartyDomain()
bool bool
BasePrincipal::AddonAllowsLoad(nsIURI* aURI) BasePrincipal::AddonAllowsLoad(nsIURI* aURI)
{ {
if (mOriginAttributes.mAddonId.IsEmpty()) { nsAutoString addonId;
NS_ENSURE_SUCCESS(GetAddonId(addonId), false);
if (addonId.IsEmpty()) {
return false; return false;
} }
@ -775,7 +781,7 @@ BasePrincipal::AddonAllowsLoad(nsIURI* aURI)
NS_ENSURE_TRUE(aps, false); NS_ENSURE_TRUE(aps, false);
bool allowed = false; bool allowed = false;
nsresult rv = aps->AddonMayLoadURI(mOriginAttributes.mAddonId, aURI, &allowed); nsresult rv = aps->AddonMayLoadURI(addonId, aURI, &allowed);
return NS_SUCCEEDED(rv) && 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); is(stripTrailingSlash(prin.URI.spec), url, 'Principal uri is correct: ' + url);
function stripPath(s) { return s.replace(/(.*\/\/.+)\/.*/, '$1'); }; function stripPath(s) { return s.replace(/(.*\/\/.+)\/.*/, '$1'); };
is(prin.originNoSuffix, stripPath(url), 'Principal origin is correct: ' + prin.originNoSuffix); 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)) { if (/_blank/.test(url)) {
is(SpecialPowers.wrap(ifr.contentWindow).document.documentElement.innerHTML, is(SpecialPowers.wrap(ifr.contentWindow).document.documentElement.innerHTML,
'<head></head><body></body>', 'blank document looks right'); '<head></head><body></body>', 'blank document looks right');

View file

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

View file

@ -309,7 +309,15 @@ WebExtensionActor.prototype._shouldAddNewGlobalAsDebuggee = function (newGlobal)
const global = unwrapDebuggerObjectGlobal(newGlobal); const global = unwrapDebuggerObjectGlobal(newGlobal);
if (global instanceof Ci.nsIDOMWindow) { 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 { try {

View file

@ -356,7 +356,7 @@ class ContentScriptContextChild extends BaseContext {
// the tab holding the content page. // the tab holding the content page.
let metadata = { let metadata = {
"inner-window-id": this.innerWindowID, "inner-window-id": this.innerWindowID,
addonId: attrs.addonId, addonId: extensionPrincipal.addonId,
}; };
this.sandbox = Cu.Sandbox(principal, { 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. // Finds the add-on ID associated with a given moz-extension:// URI.
// This is used to set the addonId on the originAttributes for the // This is used to set the addonId on the nsIPrincipal attached to the URI.
// nsIPrincipal attached to the URI.
extensionURIToAddonID(uri) { extensionURIToAddonID(uri) {
let uuid = uri.host; let uuid = uri.host;
let extension = this.uuidMap.get(uuid); let extension = this.uuidMap.get(uuid);
@ -235,11 +234,11 @@ var Service = {
// API Levels Helpers // API Levels Helpers
// Find the add-on associated with this document via the // 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 // extensionURIToAddonID, which ensures that we don't inject our
// API into webAccessibleResources or remote web pages. // API into webAccessibleResources or remote web pages.
function getAddonIdForWindow(window) { function getAddonIdForWindow(window) {
return Cu.getObjectPrincipal(window).originAttributes.addonId; return Cu.getObjectPrincipal(window).addonId;
} }
const API_LEVELS = Object.freeze({ const API_LEVELS = Object.freeze({