Source code
Revision control
Copy as Markdown
Other Tools
Test Info: Warnings
- This test gets skipped with pattern: appname == 'thunderbird' OR os == 'android' OR os == 'mac' && debug OR socketprocess_networking && fission OR os == 'linux' && socketprocess_networking && !fission && debug OR os == 'linux' && tsan && fission
- Manifest: toolkit/components/extensions/test/xpcshell/xpcshell-e10s.toml includes toolkit/components/extensions/test/xpcshell/xpcshell-content.toml
- Manifest: toolkit/components/extensions/test/xpcshell/xpcshell-remote.toml includes toolkit/components/extensions/test/xpcshell/xpcshell-content.toml
- Manifest: toolkit/components/extensions/test/xpcshell/xpcshell.toml includes toolkit/components/extensions/test/xpcshell/xpcshell-content.toml
"use strict";
Services.prefs.setBoolPref("extensions.manifestV3.enabled", true);
Services.prefs.setBoolPref("extensions.originControls.grantByDefault", false);
const server = createHttpServer({ hosts: ["example.com", "example.net"] });
server.registerDirectory("/data/", do_get_file("data"));
const { ExtensionPermissions } = ChromeUtils.importESModule(
"resource://gre/modules/ExtensionPermissions.sys.mjs"
);
function grantOptional({ extension: ext }, origins) {
return ExtensionPermissions.add(ext.id, { origins, permissions: [] }, ext);
}
function revokeOptional({ extension: ext }, origins) {
return ExtensionPermissions.remove(ext.id, { origins, permissions: [] }, ext);
}
function makeExtension(id, content_scripts) {
return ExtensionTestUtils.loadExtension({
manifest: {
manifest_version: 3,
browser_specific_settings: { gecko: { id } },
content_scripts,
permissions: ["scripting"],
host_permissions: HOSTS,
},
files: {
"cs.js"() {
browser.test.log(`${browser.runtime.id} script on ${location.host}`);
browser.test.sendMessage(`${browser.runtime.id}_on_${location.host}`);
},
},
background() {
browser.test.onMessage.addListener(async (msg, origins) => {
browser.test.log(`${browser.runtime.id} registering content scripts`);
await browser.scripting.registerContentScripts([
{
id: "cs1",
persistAcrossSessions: false,
matches: origins,
js: ["cs.js"],
},
]);
browser.test.sendMessage("done");
});
},
});
}
// Test that content scripts in MV3 enforce origin permissions.
// Test granted optional permissions are available in newly spawned processes.
add_task(async function test_contentscript_mv3_permissions() {
// Alpha lists content scripts in the manifest.
let alpha = makeExtension("alpha@test", [{ matches: HOSTS, js: ["cs.js"] }]);
let beta = makeExtension("beta@test");
await grantOptional(alpha, HOSTS);
info("Granted initial permissions for both.");
await alpha.startup();
await beta.startup();
// Beta registers same content scripts using the scripting api.
beta.sendMessage("register", HOSTS);
await beta.awaitMessage("done");
// Only Alpha has origin permissions for example.com.
{
let page = await ExtensionTestUtils.loadContentPage(
);
info("Loaded a page from example.com.");
await alpha.awaitMessage("alpha@test_on_example.com");
info("Got a message from alpha@test on example.com.");
await page.close();
}
info("Revoked example.net permissions from Alpha.");
// Now only Beta has origin permissions for example.net.
{
let page = await ExtensionTestUtils.loadContentPage(
);
info("Loaded a page from example.net.");
await beta.awaitMessage("beta@test_on_example.net");
info("Got a message from beta@test on example.net.");
await page.close();
}
info("Done, unloading Alpha and Beta.");
await beta.unload();
await alpha.unload();
});