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

function makeExtension({ id, isPrivileged, withScriptingAPI = false }) {
  let permissions = [];
  let content_scripts = [];
  let background = () => {
    browser.test.sendMessage("background-ready");
  };

  if (isPrivileged) {
    permissions.push("mozillaAddons");
  }

  if (withScriptingAPI) {
    permissions.push("scripting");
    // When we don't use a content script registered via the manifest, we
    // should add the origin as a permission.
    permissions.push("resource://foo/file_sample.html");

    // Redefine background script to dynamically register the content script.
    if (isPrivileged) {
      background = async () => {
        await browser.scripting.registerContentScripts([
          {
            id: "content_script",
            js: ["content_script.js"],
            matches: ["resource://foo/file_sample.html"],
            persistAcrossSessions: false,
            runAt: "document_start",
          },
        ]);

        let scripts = await browser.scripting.getRegisteredContentScripts();
        browser.test.assertEq(1, scripts.length, "expected 1 script");

        browser.test.sendMessage("background-ready");
      };
    } else {
      background = async () => {
        await browser.test.assertRejects(
          browser.scripting.registerContentScripts([
            {
              id: "content_script",
              js: ["content_script.js"],
              matches: ["resource://foo/file_sample.html"],
              persistAcrossSessions: false,
              runAt: "document_start",
            },
          ]),
          /Invalid url pattern: resource:/,
          "got expected error"
        );

        browser.test.sendMessage("background-ready");
      };
    }
  } else {
    content_scripts.push({
      js: ["content_script.js"],
      matches: ["resource://foo/file_sample.html"],
      run_at: "document_start",
    });
  }

  return ExtensionTestUtils.loadExtension({
    isPrivileged,

    manifest: {
      manifest_version: 2,
      browser_specific_settings: { gecko: { id } },
      content_scripts,
      permissions,
    },

    background,

    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}`);
      },
    },
  });
}

const verifyRestrictSchemes = async ({ withScriptingAPI }) => {
  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({
    id: "unprivileged@tests.mozilla.org",
    isPrivileged: false,
    withScriptingAPI,
  });
  let privileged = makeExtension({
    id: "privileged@tests.mozilla.org",
    isPrivileged: true,
    withScriptingAPI,
  });

  await unprivileged.startup();
  await unprivileged.awaitMessage("background-ready");

  await privileged.startup();
  await privileged.awaitMessage("background-ready");

  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();
};

// Bug 1780507: this only works with MV2 currently because MV3's optional
// permission mechanism lacks `restrictSchemes` flags.
add_task(async function test_contentscript_restrictSchemes_mv2() {
  await verifyRestrictSchemes({ withScriptingAPI: false });
});

// Bug 1780507: this only works with MV2 currently because MV3's optional
// permission mechanism lacks `restrictSchemes` flags.
add_task(async function test_contentscript_restrictSchemes_scripting_mv2() {
  await verifyRestrictSchemes({ withScriptingAPI: true });
});