Bug 821809 - Using nsExpandedPrincipal for jetpack content-scripts. r=rFobic

This commit is contained in:
Gabor Krizsanits
2014-04-09 11:52:24 +02:00
parent 8a6768002b
commit 98836df1c4
4 changed files with 115 additions and 8 deletions

View File

@@ -38,6 +38,12 @@ const metadata = require('@loader/options').metadata;
const permissions = (metadata && metadata['permissions']) || {};
const EXPANDED_PRINCIPALS = permissions['cross-domain-content'] || [];
const waiveSecurityMembrane = !!permissions['unsafe-content-script'];
const nsIScriptSecurityManager = Ci.nsIScriptSecurityManager;
const secMan = Cc["@mozilla.org/scriptsecuritymanager;1"].
getService(Ci.nsIScriptSecurityManager);
const JS_VERSION = '1.8';
const WorkerSandbox = Class({
@@ -96,8 +102,10 @@ const WorkerSandbox = Class({
this.emit = this.emit.bind(this);
this.emitSync = this.emitSync.bind(this);
// Eventually use expanded principal sandbox feature, if some are given.
//
// Use expanded principal for content-script if the content is a
// regular web content for better isolation.
// (This behavior can be turned off for now with the unsafe-content-script
// flag to give addon developers time for making the necessary changes)
// But prevent it when the Worker isn't used for a content script but for
// injecting `addon` object into a Panel, Widget, ... scope.
// That's because:
@@ -110,12 +118,17 @@ const WorkerSandbox = Class({
// domain principal.
let principals = window;
let wantGlobalProperties = [];
if (EXPANDED_PRINCIPALS.length > 0 && !requiresAddonGlobal(worker)) {
principals = EXPANDED_PRINCIPALS.concat(window);
// We have to replace XHR constructor of the content document
// with a custom cross origin one, automagically added by platform code:
delete proto.XMLHttpRequest;
wantGlobalProperties.push('XMLHttpRequest');
let isSystemPrincipal = secMan.isSystemPrincipal(
window.document.nodePrincipal);
if (!isSystemPrincipal && !requiresAddonGlobal(worker)) {
if (EXPANDED_PRINCIPALS.length > 0) {
// We have to replace XHR constructor of the content document
// with a custom cross origin one, automagically added by platform code:
delete proto.XMLHttpRequest;
wantGlobalProperties.push('XMLHttpRequest');
}
if (!waiveSecurityMembrane)
principals = EXPANDED_PRINCIPALS.concat(window);
}
// Instantiate trusted code in another Sandbox in order to prevent content
@@ -129,6 +142,7 @@ const WorkerSandbox = Class({
sandboxPrototype: proto,
wantXrays: true,
wantGlobalProperties: wantGlobalProperties,
wantExportHelpers: !waiveSecurityMembrane,
sameZoneAs: window,
metadata: {
SDKContentScript: true,