Bug 1287007 - Derive context.principal from sandbox r?billm
`context.principal` should be equal to the principal of the sandbox, so
that if a new sandbox is created using `Cu.Sandbox(principal)`, that
objects can be shared between the new sandbox and `context.cloneScope`
(= `context.sandbox`) without issues.
Without this change, using `context.jsonStringify` on an object from a
content script would trigger the following error:
> Error: Permission denied to access property "toJSON"
This scenario is covered by the test
toolkit/components/extensions/test/mochitest/test_ext_storage_content.html
in the next commit.
MozReview-Commit-ID: E4Jt8TDwNAZ
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -267,18 +267,16 @@ class ExtensionContext extends BaseConte
let contentPrincipal = contentWindow.document.nodePrincipal;
let ssm = Services.scriptSecurityManager;
// copy origin attributes from the content window origin attributes to
// preserve the user context id. overwrite the addonId.
let attrs = contentPrincipal.originAttributes;
attrs.addonId = this.extension.id;
let extensionPrincipal = ssm.createCodebasePrincipal(this.extension.baseURI, attrs);
- Object.defineProperty(this, "principal",
- {value: extensionPrincipal, enumerable: true, configurable: true});
if (ssm.isSystemPrincipal(contentPrincipal)) {
// Make sure we don't hand out the system principal by accident.
// also make sure that the null principal has the right origin attributes
prin = ssm.createNullPrincipal(attrs);
} else {
prin = [contentPrincipal, extensionPrincipal];
}
@@ -317,16 +315,22 @@ class ExtensionContext extends BaseConte
Cu.evalInSandbox(`
window.JSON = JSON;
window.XMLHttpRequest = XMLHttpRequest;
window.fetch = fetch;
`, this.sandbox);
}
+ Object.defineProperty(this, "principal", {
+ value: Cu.getObjectPrincipal(this.sandbox),
+ enumerable: true,
+ configurable: true,
+ });
+
let url = contentWindow.location.href;
// The |sender| parameter is passed directly to the extension.
let sender = {id: this.extension.uuid, frameId, url};
let filter = {extensionId: this.extension.id};
let optionalFilter = {frameId};
this.messenger = new Messenger(this, [this.messageManager], sender, filter, optionalFilter);
this.chromeObj = Cu.createObjectIn(this.sandbox, {defineAs: "browser"});