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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
|
<!DOCTYPE HTML>
<html>
<head>
<title>Test the web_accessible_resources manifest directive</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="head.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
// add_setup not available in mochitest
add_task(async function setup() {
await SpecialPowers.pushPrefEnv({set: [["extensions.manifestV3.enabled", true]]});
})
let image = atob(
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAA" +
"ACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII="
);
const IMAGE_ARRAYBUFFER = Uint8Array.from(image, byte => byte.charCodeAt(0))
.buffer;
const ANDROID = navigator.userAgent.includes("Android");
async function testImageLoading(src, expectedAction) {
let imageLoadingPromise = new Promise((resolve) => {
let cleanupListeners;
let testImage = document.createElement("img");
// Set the src via wrappedJSObject so the load is triggered with the
// content page's principal rather than ours.
testImage.wrappedJSObject.setAttribute("src", src);
let loadListener = () => {
cleanupListeners();
resolve(expectedAction === "loaded");
};
let errorListener = () => {
cleanupListeners();
resolve(expectedAction === "blocked");
};
cleanupListeners = () => {
testImage.removeEventListener("load", loadListener);
testImage.removeEventListener("error", errorListener);
};
testImage.addEventListener("load", loadListener);
testImage.addEventListener("error", errorListener);
document.body.appendChild(testImage);
});
let success = await imageLoadingPromise;
browser.runtime.sendMessage({
name: "image-loading",
expectedAction,
success,
});
}
async function _test_web_accessible_resources({
manifest,
expectShouldLoadByDefault = true,
usePagePrincipal = false,
}) {
function background(shouldLoad, usePagePrincipal) {
let gotURL;
let tabId;
let expectBrowserAPI;
function loadFrame(url, sandbox = null, srcdoc = false) {
return new Promise(resolve => {
browser.tabs.sendMessage(
tabId,
["load-iframe", url, sandbox, srcdoc, usePagePrincipal],
reply => {
resolve(reply);
}
);
});
}
// shouldLoad will be true unless we expect all attempts to fail.
let urls = [
// { url, shouldLoad, sandbox, srcdoc }
{
url: browser.runtime.getURL("accessible.html"),
shouldLoad,
},
{
url: browser.runtime.getURL("accessible.html") + "?foo=bar",
shouldLoad,
},
{
url: browser.runtime.getURL("accessible.html") + "#!foo=bar",
shouldLoad,
},
{
url: browser.runtime.getURL("accessible.html"),
shouldLoad,
sandbox: "allow-scripts",
},
{
url: browser.runtime.getURL("accessible.html"),
shouldLoad,
sandbox: "allow-same-origin allow-scripts",
},
{
url: browser.runtime.getURL("accessible.html"),
shouldLoad,
sandbox: "allow-scripts",
srcdoc: true,
},
{
url: browser.runtime.getURL("inaccessible.html"),
shouldLoad: false,
},
{
url: browser.runtime.getURL("inaccessible.html"),
shouldLoad: false,
sandbox: "allow-same-origin allow-scripts",
},
{
url: browser.runtime.getURL("inaccessible.html"),
shouldLoad: false,
sandbox: "allow-same-origin allow-scripts",
srcdoc: true,
},
{
url: browser.runtime.getURL("wild1.html"),
shouldLoad,
},
{
url: browser.runtime.getURL("wild2.htm"),
shouldLoad: false,
},
];
async function runTests() {
for (let { url, shouldLoad, sandbox, srcdoc } of urls) {
// Sandboxed pages with an opaque origin do not get browser api.
expectBrowserAPI = !sandbox || sandbox.includes("allow-same-origin");
let success = await loadFrame(url, sandbox, srcdoc);
browser.test.assertEq(shouldLoad, success, "Load was successful");
if (shouldLoad && !srcdoc) {
browser.test.assertEq(url, gotURL, "Got expected url");
} else {
browser.test.assertEq(undefined, gotURL, "Got no url");
}
gotURL = undefined;
}
browser.test.notifyPass("web-accessible-resources");
}
browser.runtime.onMessage.addListener(
([msg, url, hasBrowserAPI], sender) => {
if (msg == "content-script-ready") {
tabId = sender.tab.id;
runTests();
} else if (msg == "page-script") {
browser.test.assertEq(
undefined,
gotURL,
"Should have gotten only one message"
);
browser.test.assertEq("string", typeof url, "URL should be a string");
browser.test.assertEq(
expectBrowserAPI,
hasBrowserAPI,
"has access to browser api"
);
gotURL = url;
}
}
);
browser.test.sendMessage("ready");
}
function contentScript() {
window.addEventListener("message", event => {
// bounce the postmessage to the background script
if (event.data[0] == "page-script") {
browser.runtime.sendMessage(event.data);
}
});
browser.runtime.onMessage.addListener(
([msg, url, sandboxed, srcdoc, usePagePrincipal], sender, respond) => {
if (msg == "load-iframe") {
// construct the frame using srcdoc if requested.
if (srcdoc) {
sandboxed = sandboxed !== null ? `sandbox="${sandboxed}"` : "";
let frameSrc = `<iframe ${sandboxed} src="${url}" onload="parent.postMessage(true, '*')" onerror="parent.postMessage(false, '*')">`;
let frame = document.createElement("iframe");
frame.setAttribute("srcdoc", frameSrc);
window.addEventListener("message", function listener(event) {
if (event.source === frame.contentWindow) {
window.removeEventListener("message", listener);
respond(event.data);
}
});
document.body.appendChild(frame);
return true;
}
let iframe = document.createElement("iframe");
if (sandboxed !== null) {
iframe.setAttribute("sandbox", sandboxed);
}
if (usePagePrincipal) {
// Test using the page principal
iframe.wrappedJSObject.src = url;
} else {
// Test using the expanded principal
iframe.src = url;
}
iframe.addEventListener("load", () => {
respond(true);
});
iframe.addEventListener("error", () => {
respond(false);
});
document.body.appendChild(iframe);
return true;
}
}
);
browser.runtime.sendMessage(["content-script-ready"]);
}
let extension = ExtensionTestUtils.loadExtension({
useAddonManager: "temporary",
manifest: {
content_scripts: [
{
matches: ["https://example.com/"],
js: ["content_script.js"],
run_at: "document_idle",
},
],
...manifest,
},
background: `(${background})(${expectShouldLoadByDefault}, ${usePagePrincipal})`,
files: {
"content_script.js": contentScript,
"accessible.html": `<!DOCTYPE html><html><head>
<meta charset="utf-8">
<script src="pagescript.js"><\/script>
</head></html>`,
"inaccessible.html": `<!DOCTYPE html><html><head>
<meta charset="utf-8">
<script src="pagescript.js"><\/script>
</head></html>`,
"wild1.html": `<!DOCTYPE html><html><head>
<meta charset="utf-8">
<script src="pagescript.js"><\/script>
</head></html>`,
"wild2.htm": `<!DOCTYPE html><html><head>
<meta charset="utf-8">
<script src="pagescript.js"><\/script>
</head></html>`,
"pagescript.js":
// We postmessage so we can determine when browser is not available
'window.parent.postMessage(["page-script", location.href, typeof browser !== "undefined"], "*");',
},
});
await extension.startup();
await extension.awaitMessage("ready");
let win = window.open("https://example.com/");
await extension.awaitFinish("web-accessible-resources");
win.close();
await extension.unload();
};
add_task(async function test_web_accessible_resources_v2() {
await SpecialPowers.pushPrefEnv({set: [["extensions.content_web_accessible.enabled", true]]});
consoleMonitor.start([
{message: /Content at https:\/\/example.com\/ may not load or link to.*inaccessible.html/},
]);
await _test_web_accessible_resources({
manifest: {
manifest_version: 2,
web_accessible_resources: ["/accessible.html", "wild*.html"],
}
});
await consoleMonitor.finished();
await SpecialPowers.popPrefEnv();
});
// Same test as above, but using only the content principal
add_task(async function test_web_accessible_resources_v2_content() {
await SpecialPowers.pushPrefEnv({set: [["extensions.content_web_accessible.enabled", true]]});
consoleMonitor.start([
{message: /Content at https:\/\/example.com\/ may not load or link to.*inaccessible.html/},
]);
await _test_web_accessible_resources({
manifest: {
manifest_version: 2,
web_accessible_resources: ["/accessible.html", "wild*.html"],
},
usePagePrincipal: true,
});
await consoleMonitor.finished();
await SpecialPowers.popPrefEnv();
});
add_task(async function test_web_accessible_resources_v3() {
// MV3 always requires this, pref off to ensure it works.
await SpecialPowers.pushPrefEnv({set: [["extensions.content_web_accessible.enabled", false]]});
consoleMonitor.start([
{message: /Content at https:\/\/example.com\/ may not load or link to.*inaccessible.html/},
]);
await _test_web_accessible_resources({
manifest: {
manifest_version: 3,
web_accessible_resources: [
{
resources: ["/accessible.html", "wild*.html"],
matches: ["*://example.com/*"]
},
],
host_permissions: ["*://example.com/*"],
granted_host_permissions: true,
}
});
await consoleMonitor.finished();
await SpecialPowers.popPrefEnv();
});
add_task(async function test_web_accessible_resources_v3_by_id() {
consoleMonitor.start([
{message: /Content at https:\/\/example.com\/ may not load or link to.*accessible.html/},
{message: /Content at https:\/\/example.com\/ may not load or link to.*inaccessible.html/},
]);
await _test_web_accessible_resources({
manifest: {
manifest_version: 3,
browser_specific_settings: {
gecko: {
id: "extension_wac@mochitest",
},
},
web_accessible_resources: [
{
resources: ["/accessible.html", "wild*.html"],
extension_ids: ["extension_wac@mochitest"]
},
],
host_permissions: ["*://example.com/*"],
// Work-around for bug 1766752 to allow content_scripts to run:
granted_host_permissions: true,
},
expectShouldLoadByDefault: false,
});
await consoleMonitor.finished();
});
add_task(async function test_web_accessible_resources_mixed_content() {
function background() {
browser.runtime.onMessage.addListener(msg => {
if (msg.name === "image-loading") {
browser.test.assertTrue(msg.success, `Image was ${msg.expectedAction}`);
browser.test.sendMessage(`image-${msg.expectedAction}`);
} else {
browser.test.sendMessage(msg);
if (msg === "accessible-script-loaded") {
browser.test.notifyPass("mixed-test");
}
}
});
browser.test.sendMessage("background-ready");
}
async function content() {
await testImageLoading(
"http://example.com/tests/toolkit/components/extensions/test/mochitest/file_image_bad.png",
"blocked"
);
await testImageLoading(browser.runtime.getURL("image.png"), "loaded");
let testScriptElement = document.createElement("script");
// Set the src via wrappedJSObject so the load is triggered with the
// content page's principal rather than ours.
testScriptElement.wrappedJSObject.setAttribute(
"src",
browser.runtime.getURL("test_script.js")
);
document.head.appendChild(testScriptElement);
window.addEventListener("message", event => {
browser.runtime.sendMessage(event.data);
});
}
function testScript() {
window.postMessage("accessible-script-loaded", "*");
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
content_scripts: [
{
matches: ["https://example.com/*/file_mixed.html"],
run_at: "document_end",
js: ["content_script_helper.js", "content_script.js"],
},
],
web_accessible_resources: ["image.png", "test_script.js"],
},
background,
files: {
"content_script_helper.js": `${testImageLoading}`,
"content_script.js": content,
"test_script.js": testScript,
"image.png": IMAGE_ARRAYBUFFER,
},
});
await SpecialPowers.pushPrefEnv({set: [
["security.mixed_content.upgrade_display_content", false],
["security.mixed_content.block_display_content", true],
]});
await Promise.all([
extension.startup(),
extension.awaitMessage("background-ready"),
]);
let win = window.open(
"https://example.com/tests/toolkit/components/extensions/test/mochitest/file_mixed.html"
);
await Promise.all([
extension.awaitMessage("image-blocked"),
extension.awaitMessage("image-loaded"),
extension.awaitMessage("accessible-script-loaded"),
]);
await extension.awaitFinish("mixed-test");
win.close();
await extension.unload();
await SpecialPowers.popPrefEnv();
});
// test that MV2 extensions continue to open other MV2 extension pages
// when they are not listed in web_accessible_resources. This test also
// covers mobile/android tab creation.
add_task(async function test_web_accessible_resources_extensions_MV2() {
function background() {
let newtab;
let win;
let expectUrl;
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (!expectUrl || tab.url != expectUrl || changeInfo.status !== "complete") {
return;
}
expectUrl = undefined;
browser.test.log(`onUpdated ${JSON.stringify(changeInfo)} ${tab.url}`);
browser.test.sendMessage("onUpdated", tab.url);
});
browser.test.onMessage.addListener(async (msg, url) => {
browser.test.log(`onMessage ${msg} ${url}`);
expectUrl = url;
if (msg == "create") {
newtab = await browser.tabs.create({ url });
browser.test.assertTrue(
newtab.id !== browser.tabs.TAB_ID_NONE,
"New tab was created."
);
} else if (msg == "update") {
await browser.tabs.update(newtab.id, { url });
} else if (msg == "remove") {
await browser.tabs.remove(newtab.id);
newtab = null;
browser.test.sendMessage("completed");
} else if (msg == "open-window") {
win = await browser.windows.create({ url });
} else if (msg == "close-window") {
await browser.windows.remove(win.id);
browser.test.sendMessage("completed");
win = null;
}
});
}
let extension = ExtensionTestUtils.loadExtension({
manifest: {
browser_specific_settings: { gecko: { id: "this-mv2@mochitest" } },
},
background,
files: {
"page.html": `<!DOCTYPE html><html><head>
<meta charset="utf-8">
</head></html>`,
},
});
async function testTabsAction(ext, action, url) {
ext.sendMessage(action, url);
is(await ext.awaitMessage("onUpdated"), url, "extension url was loaded");
}
await extension.startup();
let extensionUrl = `moz-extension://${extension.uuid}/page.html`;
// Test opening its own pages
await testTabsAction(extension, "create", `${extensionUrl}?q=1`);
await testTabsAction(extension, "update", `${extensionUrl}?q=2`);
extension.sendMessage("remove");
await extension.awaitMessage("completed");
if (!ANDROID) {
await testTabsAction(extension, "open-window", `${extensionUrl}?q=3`);
extension.sendMessage("close-window");
await extension.awaitMessage("completed");
}
// Extension used to open the homepage in a new window.
let other = ExtensionTestUtils.loadExtension({
manifest: {
permissions: ["tabs", "<all_urls>"],
},
background,
});
await other.startup();
// Test opening another extensions pages
await testTabsAction(other, "create", `${extensionUrl}?q=4`);
await testTabsAction(other, "update", `${extensionUrl}?q=5`);
other.sendMessage("remove");
await other.awaitMessage("completed");
if (!ANDROID) {
await testTabsAction(other, "open-window", `${extensionUrl}?q=6`);
other.sendMessage("close-window");
await other.awaitMessage("completed");
}
await extension.unload();
await other.unload();
});
</script>
</body>
</html>
|