File: test_ext_executeScript_mozextension.html

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (439 lines) | stat: -rw-r--r-- 14,262 bytes parent folder | download
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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Tests tabs/scripting.executeScript() on moz-extension pages</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">

"use strict";

add_setup(async function () {
  // Make sure the restriction is enabled while running this test file,
  //
  // TODO(Bug 2015559): Remove this once pref flip along with letting the restriction to
  // to be riding the release train.
  await SpecialPowers.pushPrefEnv({
    set: [["extensions.webextensions.allow_executeScript_in_moz_extension", false]],
  })
});

async function testExecuteScript({ manifest_version, activeTabPermission }) {
  const TEST_SUBFRAME_URL = "https://example.com/?test=mozExtIframe";
  const EXPECTED_ERROR_MESSAGE = "Missing host permission for the tab";
  const EXPECTED_SUCCESS_RESULT = `Executed successfully on ${TEST_SUBFRAME_URL}`;

  let permissions = {};
  if (activeTabPermission) {
    permissions =
      manifest_version === 3
        ? { permissions: ["scripting", "activeTab"], action: {} }
        : { permissions: ["scripting", "activeTab"], browser_action: {} };
  } else {
    // host_permissions is merged into permissions as part of
    // ExtensionTestCommon's internal normalization if the
    // test extension is an manifest_version 2 extension.
    permissions = {
      permissions: ["scripting"],
      host_permissions: ["https://example.com/*"],
    };
  }

  const web_accessible_resources =
    manifest_version === 3
      ? [
          {
            resources: ["extpage.html", "extpage-sandboxed.html"],
            matches: ["*://*/*"],
          },
        ]
      : ["extpage.html", "extpage-sandboxed.html"];

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      manifest_version,
      content_scripts: [
        {
          js: ["createSubFrame.js"],
          matches: [TEST_SUBFRAME_URL],
        },
      ],
      web_accessible_resources,
      ...permissions,
    },
    background() {
      function contentScript() {
        return `Executed successfully on ${window.location}`;
      }

      function tabsExecuteScript({ allFrames = false }) {
        return browser.tabs.executeScript({
          code: `(${contentScript})()`,
          allFrames,
        });
      }

      async function scriptingExecuteScript({ allFrames = false }) {
        const [tab] = await browser.tabs.query({ active: true });
        return browser.scripting.executeScript({
          target: { tabId: tab.id, allFrames },
          func: contentScript,
        });
      }

      browser.test.onMessage.addListener(async (msg, opts = {}) => {
        const { assertion, activeTabPermission } = opts;
        const runTestCase = async () => {
          try {
            let res;
            switch (msg) {
              case "tabs.executeScript":
                res = await tabsExecuteScript(opts);
                break;
              case "scripting.executeScript":
                res = await scriptingExecuteScript(opts);
                res = res
                  .map(r => (r.error ? { ...r, error: r.error.message } : r))
                  .map(r =>
                    // Replace non-zero frameIds for an easier assertion.
                    r.frameId === 0 ? r : { ...r, frameId: "non-zero" }
                  );
                break;
              default:
                browser.test.fail(`Got unexpected test message: ${msg}`);
                browser.test.sendMessage(`${msg}:done`);
                return;
            }
            browser.test.assertDeepEq(
              assertion.expected,
              { success: res },
              assertion.message
            );
            browser.test.sendMessage(`${msg}:done`);
          } catch (err) {
            browser.test.assertDeepEq(
              assertion.expected,
              { error: `${err.message}` },
              assertion.message
            );
            browser.test.sendMessage(`${msg}:done`);
          }
        };
        if (activeTabPermission) {
          const actionAPI = browser.action ?? browser.browserAction;
          actionAPI.onClicked.addListener(async function runOnActionClicked () {
            actionAPI.onClicked.removeListener(runOnActionClicked);
            await runTestCase();
          });
          browser.test.sendMessage("bgpage:action-listener-registered");
        } else {
          await runTestCase();
        }

      });
      browser.test.sendMessage("bgpage:ready");
    },
    files: {
      "extpage.html": `<script src='extpage.js'><\/script>`,
      "extpage.js": function () {
        browser.test.sendMessage("extpage:ready");
      },
      "extpage-sandboxed.html": `<h1>sandboxed iframe</h1>`,
      "createSubFrame.js": function () {
        const iframeExt = document.createElement("iframe");
        iframeExt.src = browser.runtime.getURL("extpage.html");
        const iframeExtSandboxed = document.createElement("iframe");
        iframeExtSandboxed.src = browser.runtime.getURL(
          "extpage-sandboxed.html"
        );
        iframeExtSandboxed.setAttribute("sandbox", "allow-scripts");
        const promiseSandboxFrameLoaded = new Promise(
          resolve => iframeExtSandboxed.addEventListener("load", resolve, { once: true })
        );
        document.body.append(iframeExtSandboxed);
        document.body.append(iframeExt);
        promiseSandboxFrameLoaded.then(
          () => browser.test.sendMessage("sandboxed-subframe:ready")
        );
      },
    },
  });

  const activateActiveTabPermission = async () => {
    info("Clicking on the browser action to activate the activeTab permission");
    await extension.awaitMessage("bgpage:action-listener-registered");
    await AppTestDelegate.clickBrowserAction(window, extension);
  };

  await extension.startup();

  await extension.awaitMessage("bgpage:ready");

  info("Test on top-level moz-extension page");

  const testTopLevelMozExtTab = await AppTestDelegate.openNewForegroundTab(
    window,
    `moz-extension://${extension.uuid}/extpage.html`
  );
  await extension.awaitMessage("extpage:ready");

  if (manifest_version < 3) {
    extension.sendMessage("tabs.executeScript", {
      activeTabPermission,
      assertion: {
        expected: { error: EXPECTED_ERROR_MESSAGE },
        message:
          "tabs.executeScript should reject on top-level moz-extension page",
      },
    });

    if (activeTabPermission) {
      await activateActiveTabPermission();
    }
    await extension.awaitMessage("tabs.executeScript:done");
  }

  extension.sendMessage("scripting.executeScript", {
    activeTabPermission,
    assertion: {
      expected: { error: EXPECTED_ERROR_MESSAGE },
      message:
        "scripting.executeScript should reject on top-level moz-extension page",
    },
  });

  if (activeTabPermission) {
    // NOTE: the activeTab permission may have been granted at this point
    // if it was already granted right above for tabs.executeScript and
    // the tab has not been navigated.
    await activateActiveTabPermission();
  }
  await extension.awaitMessage("scripting.executeScript:done");

  await AppTestDelegate.removeTab(window, testTopLevelMozExtTab);

  info("Test on sub-frame moz-extension page");

  const testSubFrameMozExtTab = await AppTestDelegate.openNewForegroundTab(
    window,
    TEST_SUBFRAME_URL,
    true
  );
  info("Wait for the extension subframe to be loaded");
  await extension.awaitMessage("extpage:ready");
  info("Wait for the sandboxed subframe to be loaded")
  await extension.awaitMessage("sandboxed-subframe:ready");

  if (manifest_version < 3) {
    extension.sendMessage("tabs.executeScript", {
      activeTabPermission,
      allFrames: true,
      assertion: {
        expected: { success: [EXPECTED_SUCCESS_RESULT] },
        message:
          "tabs.executeScript should ignore and omit moz-extension sub-frames",
      },
    });

    if (activeTabPermission) {
      await activateActiveTabPermission();
    }
    await extension.awaitMessage("tabs.executeScript:done");
  }

  extension.sendMessage("scripting.executeScript", {
    activeTabPermission,
    allFrames: true,
    assertion: {
      expected: {
        success: [
          {
            frameId: 0,
            result: EXPECTED_SUCCESS_RESULT,
          },
        ],
      },
      message:
        "scripting.executeScript should ignore and omit moz-extension sub-frames"
    },
  });

  if (activeTabPermission) {
    // NOTE: the activeTab permission may have been granted at this point
    // if it was already granted right above for tabs.executeScript and
    // the tab has not been navigated.
    await activateActiveTabPermission();
  }
  await extension.awaitMessage("scripting.executeScript:done");

  await AppTestDelegate.removeTab(window, testSubFrameMozExtTab);

  await extension.unload();
}

add_task(function testExecuteScript_manifestV2() {
  return testExecuteScript({ manifest_version: 2 });
});

add_task(function testExecuteScript_manifestV2_activeTabPermission() {
  return testExecuteScript({ manifest_version: 2, activeTabPermission: true });
});

add_task(function testExecuteScript_manifestV3() {
  return testExecuteScript({ manifest_version: 3 });
});

add_task(function testExecuteScript_manifestV2_activeTabPermission() {
  return testExecuteScript({ manifest_version: 3, activeTabPermission: true });
});

add_task(async function testContentScriptsAndUserScriptsRegister() {
  const extensionMV2 = ExtensionTestUtils.loadExtension({
    manifest: {
      manifest_version: 2,
      user_scripts: {},
    },
    files: {
      "content-script.js": function() {},
    },
    background() {
      browser.test.assertThrows(
        () => browser.contentScripts.register({
          js: [{ file: "content-script.js" }],
          matches: ["moz-extension://*/*",],
        }),
        /Error processing matches\.0: Value "moz-extension:\/\/\*\/\*" must either/,
        "MV2 contentScripts.register should throw on invalid moz-extension:// matches"
      );
      browser.test.assertThrows(
        () => browser.userScripts.register({
          js: [{ file: "content-script.js" }],
          matches: ["moz-extension://*/*",],
        }),
        /Error processing matches\.0: Value "moz-extension:\/\/\*\/\*" must either/,
        "MV2 userScripts.register should throw on invalid moz-extension:// matches"
      );
      browser.test.sendMessage("bgpage:test-done");
    },
  });
  await extensionMV2.startup();
  await extensionMV2.awaitMessage("bgpage:test-done");
  await extensionMV2.unload();

  await SpecialPowers.pushPrefEnv({
    set: [
      ["extensions.webextOptionalPermissionPrompts", false],
    ]
  });
  const extensionMV3 = ExtensionTestUtils.loadExtension({
    manifest: {
      manifest_version: 3,
      optional_permissions: ["userScripts"],
    },
    files: {
      "content-script.js": function() {},
    },
    async background() {
      // NOTE: manually wrapping the call to withHandlingUserInput in a promise
      // because it does not return the value that the callback returns.
      let granted = await new Promise(resolve =>
        browser.test.withHandlingUserInput(
          () => browser.permissions.request({
            permissions: ["userScripts"],
          }).then(resolve)
        )
      );
      browser.test.assertTrue(
        granted,
        "Expect browser.permissions.request to be successful"
      );
      browser.test.assertTrue(
        await browser.permissions.contains({
          permissions: ["userScripts"],
        }),
        "Expect userScripts permission to be granted"
      );
      await browser.test.assertThrows(
        () => browser.userScripts.register([{
          id: "",
          js: [{ file: "content-script.js" }],
          matches: ["moz-extension://*/*",],
        }]),
        /Error processing 0\.matches\.0: Value "moz-extension:\/\/\*\/\*" must either/,
        "MV3 userScripts.register should throw on invalid moz-extension:// matches"
      );
      await browser.permissions.remove({ permissions: ["userScripts"] });
      browser.test.sendMessage("bgpage:test-done");
    }
  });
  await extensionMV3.startup();
  await extensionMV3.awaitMessage("bgpage:test-done");
  await extensionMV3.unload();
  await SpecialPowers.popPrefEnv();
});

add_task(async function testScriptingRegister() {
  const extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["scripting"],
    },
    files: {
      "content-script.js": function contentScript() {
        browser.test.fail(
          `Script executed on ${window.location} (${document.readyState})`
        );
      },
      "extpage.html": `<h1>TestExtPage</h1>`,
    },
    async background() {
      await browser.scripting.registerContentScripts([
        {
          id: "test-mozextension-cs-matching",
          js: ["content-script.js"],
          // Registering the script to be executed on document start
          // so that by the time the extension page had been fully
          // loaded we expect the content script to have been already
          // detected and blocked.
          runAt: "document_start",
          matches: [
            "moz-extension://*/*",
          ],
        }
      ]);
      browser.test.sendMessage(
        "bgpage:script-registered",
        browser.runtime.getURL("extpage.html")
      );
    }
  });

  await extension.startup();
  const extPageURL = await extension.awaitMessage(
    "bgpage:script-registered"
  );

  info("Open an extension page in a new tab");
  let tab = await AppTestDelegate.openNewForegroundTab(
    window,
    extPageURL,
    true
  );
  // NOTE: using an assertion here to make sure the test
  // doesn't hit failue due to no checks being detected
  // by the test harness when executed on its own.
  ok(true, "Extension page load completed");

  await AppTestDelegate.removeTab(window, tab);
  await extension.unload();
});

</script>

</body>
</html>