summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_contentscript_restrictSchemes.js
blob: e9b1dbe57c06aa21e11b9c4ce53400337f06dc53 (plain)
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
"use strict";

function makeExtension(id, isPrivileged) {
  return ExtensionTestUtils.loadExtension({
    isPrivileged,

    manifest: {
      applications: { gecko: { id } },

      permissions: isPrivileged ? ["mozillaAddons"] : [],

      content_scripts: [
        {
          matches: ["resource://foo/file_sample.html"],
          js: ["content_script.js"],
          run_at: "document_start",
        },
      ],
    },

    files: {
      "content_script.js"() {
        browser.test.assertEq(
          "resource://foo/file_sample.html",
          document.documentURI,
          `Loaded content script into the correct document (extension: ${browser.runtime.id})`
        );
        browser.test.sendMessage(`content-script-${browser.runtime.id}`);
      },
    },
  });
}

add_task(async function test_contentscript_restrictSchemes() {
  let resProto = Services.io
    .getProtocolHandler("resource")
    .QueryInterface(Ci.nsIResProtocolHandler);
  resProto.setSubstitutionWithFlags(
    "foo",
    Services.io.newFileURI(do_get_file("data")),
    resProto.ALLOW_CONTENT_ACCESS
  );

  let unprivileged = makeExtension("unprivileged@tests.mozilla.org", false);
  let privileged = makeExtension("privileged@tests.mozilla.org", true);

  await unprivileged.startup();
  await privileged.startup();

  unprivileged.onMessage(
    "content-script-unprivileged@tests.mozilla.org",
    () => {
      ok(
        false,
        "Unprivileged extension executed content script on resource URL"
      );
    }
  );

  let contentPage = await ExtensionTestUtils.loadContentPage(
    `resource://foo/file_sample.html`
  );

  await privileged.awaitMessage("content-script-privileged@tests.mozilla.org");

  await contentPage.close();

  await privileged.unload();
  await unprivileged.unload();
});