1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
<!doctype html>
<html>
<head>
<title>Test content scripts at blob:-URLs</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script src="head.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
function loadTestExtension({ manifest_version }) {
return ExtensionTestUtils.loadExtension({
manifest: {
name: "MV" + manifest_version,
manifest_version,
content_scripts: [
{
matches: ["*://mochi.test/*"],
// match_origin_as_fallback: false, // false by default
js: ["moaf_false.js"],
all_frames: true,
run_at: "document_start",
},
{
matches: ["*://mochi.test/*"],
match_origin_as_fallback: true,
js: ["moaf_true.js"],
all_frames: true,
run_at: "document_start",
},
],
},
files: {
"moaf_false.js": () => {
if (location.protocol == "blob:") {
const { name } = browser.runtime.getManifest();
browser.test.log(`moaf_false.js: Ran ${name} at ${document.URL}`);
browser.test.sendMessage(name + ":moaf_false:" + document.URL);
}
},
"moaf_true.js": () => {
if (location.protocol == "blob:") {
const { name } = browser.runtime.getManifest();
browser.test.log(`moaf_true.js: Ran ${name} at ${document.URL}`);
browser.test.sendMessage(name + ":moaf_true:" + document.URL);
}
},
},
});
}
function createBlobURL() {
function blobScript() {
window.onload = () => {
console.log(`Web page ${document.URL} loaded at origin ${origin}`);
parent.postMessage(document.URL, "*");
};
}
const html = `<!DOCTYPE html><script>(${blobScript})()<\/script>`;
return URL.createObjectURL(new Blob([html], { type: "text/html" }));
}
async function createFrameAndAwaitLoad(blobUrl, sandboxed) {
let { promise, resolve } = Promise.withResolvers();
let f = document.createElement("iframe");
f.src = blobUrl;
if (sandboxed) {
f.sandbox = "allow-scripts";
}
function onmessage(event) {
if (event.source === f.contentWindow) {
is(event.data, blobUrl, "Got message from frame");
is(event.origin, sandboxed ? "null" : origin, "Frame has correct origin");
resolve();
}
}
window.addEventListener("message", onmessage);
document.body.append(f);
await promise;
window.removeEventListener("message", onmessage);
f.remove();
}
async function test_contentscript_at_blob(legacy) {
// TODO bug 1899134: Drop the pref and legacy=true case.
await SpecialPowers.pushPrefEnv({
set: [["extensions.script_blob_without_match_origin_as_fallback", legacy]],
});
const extension2 = loadTestExtension({ manifest_version: 2 });
const extension3 = loadTestExtension({ manifest_version: 3 });
await extension2.startup();
await extension3.startup();
const blobUrlSameOrigin = createBlobURL();
info(`Expecting content scripts at blobUrlSameOrigin:${blobUrlSameOrigin}`);
await createFrameAndAwaitLoad(blobUrlSameOrigin, /* sandboxed */ false);
await Promise.all([
await extension2.awaitMessage("MV2:moaf_true:" + blobUrlSameOrigin),
await extension3.awaitMessage("MV3:moaf_true:" + blobUrlSameOrigin),
]);
if (legacy) {
await extension2.awaitMessage("MV2:moaf_false:" + blobUrlSameOrigin);
}
// MV3:moaf_false should never be observed because match_origin_as_fallback
// is required in order to execute content scripts in blob:-URLs.
const blobUrlNullOrigin = createBlobURL();
info(`Expecting content scripts at blobUrlNullOrigin:${blobUrlNullOrigin}`);
await createFrameAndAwaitLoad(blobUrlNullOrigin, /* sandboxed */ true);
await Promise.all([
await extension2.awaitMessage("MV2:moaf_true:" + blobUrlNullOrigin),
await extension3.awaitMessage("MV3:moaf_true:" + blobUrlNullOrigin),
]);
if (legacy) {
await extension2.awaitMessage("MV2:moaf_false:" + blobUrlNullOrigin);
}
await extension2.unload();
await extension3.unload();
await SpecialPowers.popPrefEnv();
}
add_task(async function test_contentscript_at_blob_default() {
await test_contentscript_at_blob(/* legacy */ false);
});
// Exactly the same as test_contentscript_at_blob_default, except
// manifest_version 2 also run at blob: when match_origin_as_fallback is false.
add_task(async function test_contentscript_at_blob_legacy_behavior() {
await test_contentscript_at_blob(/* legacy */ true);
});
</script>
</body>
</html>
|