2015-03-24 17:29:16 +03:00
|
|
|
var policy; // To make sure we never leave up an activated domain policy after a failed test, let's make this global.
|
|
|
|
function activateDomainPolicy() {
|
|
|
|
const ssm = Services.scriptSecurityManager;
|
|
|
|
policy = ssm.activateDomainPolicy();
|
|
|
|
}
|
|
|
|
|
|
|
|
function deactivateDomainPolicy() {
|
|
|
|
if (policy) {
|
|
|
|
policy.deactivate();
|
|
|
|
policy = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 13:51:42 +03:00
|
|
|
async function test_domainPolicy() {
|
2015-03-24 17:29:16 +03:00
|
|
|
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.defineModuleGetter(this, "Promise", "resource://gre/modules/Promise.jsm");
|
2018-12-13 03:45:24 +03:00
|
|
|
let outerDeferred = Promise.defer();
|
|
|
|
let currentTask = outerDeferred.promise;
|
2015-03-24 17:29:16 +03:00
|
|
|
SpecialPowers.pushPrefEnv(
|
|
|
|
{set: [["dom.ipc.browser_frames.oop_by_default", false],
|
|
|
|
["browser.pagethumbnails.capturing_disabled", false],
|
|
|
|
["dom.mozBrowserFramesEnabled", false]]},
|
2018-12-13 03:45:24 +03:00
|
|
|
() => { return outerDeferred.resolve(); });
|
2017-06-22 13:51:42 +03:00
|
|
|
await currentTask;
|
2015-03-24 17:29:16 +03:00
|
|
|
|
|
|
|
// Create tab
|
|
|
|
let tab;
|
|
|
|
|
|
|
|
// Init test
|
|
|
|
function initProcess() {
|
2017-05-15 22:49:50 +03:00
|
|
|
tab = BrowserTestUtils.addTab(gBrowser);
|
2015-03-24 17:29:16 +03:00
|
|
|
gBrowser.selectedTab = tab;
|
|
|
|
|
|
|
|
let initPromise = ContentTask.spawn(tab.linkedBrowser, null, function() {
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm");
|
2015-03-24 17:29:16 +03:00
|
|
|
function loadBase() {
|
|
|
|
let deferred = PromiseUtils.defer();
|
|
|
|
let listener = (event) => {
|
|
|
|
removeEventListener("DOMDocElementInserted", listener, true);
|
2018-12-13 03:45:24 +03:00
|
|
|
let listener2 = (event2) => {
|
2018-12-13 03:43:55 +03:00
|
|
|
content.removeEventListener("load", listener2);
|
2015-03-24 17:29:16 +03:00
|
|
|
deferred.resolve();
|
2018-12-13 03:43:55 +03:00
|
|
|
};
|
|
|
|
content.addEventListener("load", listener2);
|
2015-03-24 17:29:16 +03:00
|
|
|
};
|
|
|
|
addEventListener("DOMDocElementInserted", listener, true);
|
|
|
|
return deferred.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
return loadBase();
|
|
|
|
});
|
2018-08-29 17:47:29 +03:00
|
|
|
BrowserTestUtils.loadURI(tab.linkedBrowser, "http://mochi.test:8888/browser/dom/ipc/tests/file_domainPolicy_base.html");
|
2015-03-24 17:29:16 +03:00
|
|
|
return initPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We use ContentTask for the tests, but we also want to pass some data and some helper functions too.
|
|
|
|
// To do that, we serialize an input object via JSON |ipcArgs| and some shared helper functions |initUtils|
|
|
|
|
// and eval them in the content process.
|
|
|
|
var ipcArgs = {};
|
|
|
|
function initUtils(obj) {
|
|
|
|
obj.checkScriptEnabled = function(win, expectEnabled) {
|
|
|
|
win.wrappedJSObject.gFiredOnclick = false;
|
2018-12-13 03:43:55 +03:00
|
|
|
win.document.body.dispatchEvent(new win.Event("click"));
|
2015-03-24 17:29:16 +03:00
|
|
|
return { passed: win.wrappedJSObject.gFiredOnclick == expectEnabled,
|
|
|
|
msg: `Checking script-enabled for ${win.name} (${win.location})`};
|
2018-12-13 03:43:55 +03:00
|
|
|
};
|
2015-03-24 17:29:16 +03:00
|
|
|
|
|
|
|
obj.navigateFrame = function(ifr, src) {
|
|
|
|
let deferred = PromiseUtils.defer();
|
|
|
|
function onload() {
|
2018-12-13 03:43:55 +03:00
|
|
|
ifr.removeEventListener("load", onload);
|
2015-03-24 17:29:16 +03:00
|
|
|
deferred.resolve();
|
|
|
|
}
|
2018-12-13 03:43:55 +03:00
|
|
|
ifr.addEventListener("load", onload);
|
|
|
|
ifr.setAttribute("src", src);
|
2015-03-24 17:29:16 +03:00
|
|
|
return deferred.promise;
|
2018-12-13 03:43:55 +03:00
|
|
|
};
|
|
|
|
}
|
2015-03-24 17:29:16 +03:00
|
|
|
|
|
|
|
function runTest(test) {
|
|
|
|
return ContentTask.spawn(tab.linkedBrowser,
|
2018-12-13 03:43:55 +03:00
|
|
|
"ipcArgs = " + JSON.stringify(ipcArgs) + "; (" + initUtils.toSource() + ")(utils)", test);
|
2015-03-24 17:29:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkAndCleanup(result) {
|
|
|
|
result = [].concat(result);
|
|
|
|
for (var i in result)
|
|
|
|
ok(result[i].passed, result[i].msg);
|
|
|
|
gBrowser.removeTab(tab);
|
|
|
|
deactivateDomainPolicy();
|
|
|
|
ipcArgs = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDomain(domain) {
|
|
|
|
ipcArgs.domain = domain;
|
|
|
|
return (aUtils) => {
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm");
|
2018-12-13 03:45:24 +03:00
|
|
|
// eslint-disable-next-line no-shadow
|
2015-03-24 17:29:16 +03:00
|
|
|
var ipcArgs;
|
|
|
|
var utils = {};
|
2018-12-13 03:45:24 +03:00
|
|
|
// eslint-disable-next-line no-eval
|
2015-03-24 17:29:16 +03:00
|
|
|
eval(aUtils);
|
|
|
|
|
2018-12-13 03:43:55 +03:00
|
|
|
let path = "/browser/dom/ipc/tests/file_disableScript.html";
|
2015-03-24 17:29:16 +03:00
|
|
|
let deferred = PromiseUtils.defer();
|
2018-12-13 03:43:55 +03:00
|
|
|
var rootFrame = content.document.getElementById("root");
|
2015-03-24 17:29:16 +03:00
|
|
|
utils.navigateFrame(rootFrame, ipcArgs.domain + path).then(() => {
|
|
|
|
deferred.resolve(utils.checkScriptEnabled(rootFrame.contentWindow, false));
|
|
|
|
});
|
|
|
|
return deferred.promise;
|
2018-12-13 03:43:55 +03:00
|
|
|
};
|
2015-03-24 17:29:16 +03:00
|
|
|
}
|
|
|
|
|
2018-10-31 20:56:43 +03:00
|
|
|
info("Testing simple blocklist policy");
|
2015-03-24 17:29:16 +03:00
|
|
|
|
|
|
|
info("Creating child process first, activating domainPolicy after");
|
|
|
|
currentTask = initProcess();
|
2017-06-22 13:51:42 +03:00
|
|
|
await currentTask;
|
2015-03-24 17:29:16 +03:00
|
|
|
activateDomainPolicy();
|
2018-10-31 20:56:43 +03:00
|
|
|
var bl = policy.blocklist;
|
2018-12-13 03:43:55 +03:00
|
|
|
bl.add(Services.io.newURI("http://example.com"));
|
2015-03-24 17:29:16 +03:00
|
|
|
currentTask = runTest(testDomain("http://example.com"));
|
2017-06-22 13:51:42 +03:00
|
|
|
checkAndCleanup(await currentTask);
|
2015-03-24 17:29:16 +03:00
|
|
|
|
|
|
|
info("Activating domainPolicy first, creating child process after");
|
|
|
|
activateDomainPolicy();
|
2018-12-13 03:45:24 +03:00
|
|
|
bl = policy.blocklist;
|
2018-12-13 03:43:55 +03:00
|
|
|
bl.add(Services.io.newURI("http://example.com"));
|
2015-03-24 17:29:16 +03:00
|
|
|
currentTask = initProcess();
|
2017-06-22 13:51:42 +03:00
|
|
|
await currentTask;
|
2015-03-24 17:29:16 +03:00
|
|
|
currentTask = runTest(testDomain("http://example.com"));
|
2017-06-22 13:51:42 +03:00
|
|
|
checkAndCleanup(await currentTask);
|
2015-03-24 17:29:16 +03:00
|
|
|
|
|
|
|
function testList(expectEnabled, list) {
|
|
|
|
ipcArgs.expectEnabled = expectEnabled;
|
|
|
|
ipcArgs.list = list;
|
|
|
|
return (aUtils) => {
|
2018-01-30 02:20:18 +03:00
|
|
|
ChromeUtils.import("resource://gre/modules/PromiseUtils.jsm");
|
2018-12-13 03:45:24 +03:00
|
|
|
// eslint-disable-next-line no-shadow
|
2015-03-24 17:29:16 +03:00
|
|
|
var ipcArgs;
|
|
|
|
var utils = {};
|
2018-12-13 03:45:24 +03:00
|
|
|
// eslint-disable-next-line no-eval
|
2015-03-24 17:29:16 +03:00
|
|
|
eval(aUtils);
|
|
|
|
|
|
|
|
var results = [];
|
2018-12-13 03:45:24 +03:00
|
|
|
var testListInternal = function(internalExpectEnabled, internalList, idx) {
|
2015-03-24 17:29:16 +03:00
|
|
|
idx = idx || 0;
|
|
|
|
let deferred = PromiseUtils.defer();
|
2018-12-13 03:43:55 +03:00
|
|
|
let path = "/browser/dom/ipc/tests/file_disableScript.html";
|
2018-12-13 03:45:24 +03:00
|
|
|
let target = internalList[idx] + path;
|
2018-12-13 03:43:55 +03:00
|
|
|
var rootFrame = content.document.getElementById("root");
|
2015-03-24 17:29:16 +03:00
|
|
|
utils.navigateFrame(rootFrame, target).then(function() {
|
2018-12-13 03:45:24 +03:00
|
|
|
results.push(utils.checkScriptEnabled(rootFrame.contentWindow, internalExpectEnabled));
|
|
|
|
if (idx == internalList.length - 1)
|
2015-03-24 17:29:16 +03:00
|
|
|
deferred.resolve(results);
|
|
|
|
else
|
2018-12-13 03:45:24 +03:00
|
|
|
testListInternal(internalExpectEnabled, internalList, idx + 1).then(function(retArg) { deferred.resolve(retArg); });
|
2015-03-24 17:29:16 +03:00
|
|
|
});
|
|
|
|
return deferred.promise;
|
2018-12-13 03:43:55 +03:00
|
|
|
};
|
2015-03-24 17:29:16 +03:00
|
|
|
return testListInternal(ipcArgs.expectEnabled, ipcArgs.list);
|
2018-12-13 03:43:55 +03:00
|
|
|
};
|
2015-03-24 17:29:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let testPolicy = {
|
2018-12-13 03:43:55 +03:00
|
|
|
exceptions: ["http://test1.example.com", "http://example.com"],
|
|
|
|
superExceptions: ["http://test2.example.org", "https://test1.example.com"],
|
|
|
|
exempt: ["http://test1.example.com", "http://example.com",
|
|
|
|
"http://test2.example.org", "http://sub1.test2.example.org",
|
|
|
|
"https://sub1.test1.example.com"],
|
|
|
|
notExempt: ["http://test2.example.com", "http://sub1.test1.example.com",
|
|
|
|
"http://www.example.com", "https://test2.example.com",
|
|
|
|
"https://example.com", "http://test1.example.org"],
|
2015-03-24 17:29:16 +03:00
|
|
|
};
|
|
|
|
|
2018-10-31 20:56:43 +03:00
|
|
|
function activate(isBlock, exceptions, superExceptions) {
|
2015-03-24 17:29:16 +03:00
|
|
|
activateDomainPolicy();
|
2018-12-13 03:45:24 +03:00
|
|
|
let set = isBlock ? policy.blocklist : policy.allowlist;
|
|
|
|
let superSet = isBlock ? policy.superBlocklist : policy.superAllowlist;
|
|
|
|
for (let e of exceptions)
|
|
|
|
set.add(makeURI(e));
|
|
|
|
for (let e of superExceptions)
|
|
|
|
superSet.add(makeURI(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
info("Testing Blocklist-style Domain Policy");
|
|
|
|
info("Activating domainPolicy first, creating child process after");
|
|
|
|
activate(true, testPolicy.exceptions, testPolicy.superExceptions);
|
|
|
|
currentTask = initProcess();
|
|
|
|
await currentTask;
|
|
|
|
let results = [];
|
|
|
|
currentTask = runTest(testList(true, testPolicy.notExempt));
|
|
|
|
results = results.concat(await currentTask);
|
|
|
|
currentTask = runTest(testList(false, testPolicy.exempt));
|
|
|
|
results = results.concat(await currentTask);
|
|
|
|
checkAndCleanup(results);
|
|
|
|
|
|
|
|
info("Creating child process first, activating domainPolicy after");
|
|
|
|
currentTask = initProcess();
|
|
|
|
await currentTask;
|
|
|
|
activate(true, testPolicy.exceptions, testPolicy.superExceptions);
|
|
|
|
results = [];
|
|
|
|
currentTask = runTest(testList(true, testPolicy.notExempt));
|
|
|
|
results = results.concat(await currentTask);
|
|
|
|
currentTask = runTest(testList(false, testPolicy.exempt));
|
|
|
|
results = results.concat(await currentTask);
|
|
|
|
checkAndCleanup(results);
|
|
|
|
|
|
|
|
info("Testing Allowlist-style Domain Policy");
|
|
|
|
let deferred = Promise.defer();
|
2015-03-24 17:29:16 +03:00
|
|
|
currentTask = deferred.promise;
|
2018-12-13 03:43:55 +03:00
|
|
|
SpecialPowers.pushPrefEnv({set: [["javascript.enabled", false]]}, () => { return deferred.resolve(); });
|
2017-06-22 13:51:42 +03:00
|
|
|
await currentTask;
|
2015-03-24 17:29:16 +03:00
|
|
|
|
|
|
|
info("Activating domainPolicy first, creating child process after");
|
|
|
|
activate(false, testPolicy.exceptions, testPolicy.superExceptions);
|
|
|
|
currentTask = initProcess();
|
2017-06-22 13:51:42 +03:00
|
|
|
await currentTask;
|
2015-03-24 17:29:16 +03:00
|
|
|
results = [];
|
|
|
|
currentTask = runTest(testList(false, testPolicy.notExempt));
|
2017-06-22 13:51:42 +03:00
|
|
|
results = results.concat(await currentTask);
|
2015-03-24 17:29:16 +03:00
|
|
|
currentTask = runTest(testList(true, testPolicy.exempt));
|
2017-06-22 13:51:42 +03:00
|
|
|
results = results.concat(await currentTask);
|
2015-03-24 17:29:16 +03:00
|
|
|
checkAndCleanup(results);
|
|
|
|
|
|
|
|
info("Creating child process first, activating domainPolicy after");
|
|
|
|
currentTask = initProcess();
|
2017-06-22 13:51:42 +03:00
|
|
|
await currentTask;
|
2015-03-24 17:29:16 +03:00
|
|
|
activate(false, testPolicy.exceptions, testPolicy.superExceptions);
|
|
|
|
results = [];
|
|
|
|
currentTask = runTest(testList(false, testPolicy.notExempt));
|
2017-06-22 13:51:42 +03:00
|
|
|
results = results.concat(await currentTask);
|
2015-03-24 17:29:16 +03:00
|
|
|
currentTask = runTest(testList(true, testPolicy.exempt));
|
2017-06-22 13:51:42 +03:00
|
|
|
results = results.concat(await currentTask);
|
2015-03-24 17:29:16 +03:00
|
|
|
checkAndCleanup(results);
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
add_task(test_domainPolicy);
|
|
|
|
|
2018-12-13 03:43:55 +03:00
|
|
|
registerCleanupFunction(() => {
|
2015-03-24 17:29:16 +03:00
|
|
|
deactivateDomainPolicy();
|
2018-12-13 03:43:55 +03:00
|
|
|
});
|