File: test_ext_scripting_executeScript_world.html

package info (click to toggle)
firefox-esr 128.13.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,230,012 kB
  • sloc: cpp: 7,103,971; javascript: 6,088,450; ansic: 3,653,980; python: 1,212,330; xml: 594,604; asm: 420,652; java: 182,969; sh: 71,124; makefile: 20,747; perl: 13,449; objc: 12,399; yacc: 4,583; cs: 3,846; pascal: 2,973; lex: 1,720; ruby: 1,194; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (395 lines) | stat: -rw-r--r-- 12,279 bytes parent folder | download | duplicates (10)
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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Tests scripting.executeScript() with world option</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";

const MOCHITEST_HOST_PERMISSIONS = [
  "*://mochi.test/",
  "*://mochi.xorigin-test/",
  "*://test1.example.com/",
];

const makeExtension = ({ manifest: manifestProps, ...otherProps }) => {
  return ExtensionTestUtils.loadExtension({
    manifest: {
      manifest_version: 3,
      permissions: ["scripting"],
      host_permissions: [
        ...MOCHITEST_HOST_PERMISSIONS,
        "https://example.com/",
        // Used in `file_contains_iframe.html`
        "https://example.org/",
      ],
      granted_host_permissions: true,
      ...manifestProps,
    },
    useAddonManager: "temporary",
    ...otherProps,
  });
};

add_task(async function test_executeScript_main_world_files() {
  let extension = makeExtension({
    async background() {
      const tabs = await browser.tabs.query({ active: true });
      browser.test.assertEq(1, tabs.length, "expected 1 tab");

      let results = await browser.scripting.executeScript({
        target: { tabId: tabs[0].id },
        files: ["main.js"],
        world: "MAIN",
      });
      browser.test.assertEq(undefined, results[0].error?.message, "No error");
      browser.test.assertEq(0, results[0].frameId, "got the expected frameId");
      browser.test.assertDeepEq(
        {
          varInPageReadByExt: "varInPage",
          thisIsGlobalThis: true,
          thisIsWindow: true,
          browserIsUndefined: true,
          evalBlockedMessage: "call to eval() blocked by CSP",
        },
        results[0].result,
        "executeScript with world:MAIN should run in the page's context"
      );

      browser.test.notifyPass("background-done");
    },
    files: {
      "main.js": () => {
        let evalBlockedMessage = "unexpectedly not blocked???";
        try {
          // eslint-disable-next-line no-eval
          eval("// CSP of file_simple_inline_script.html should block eval.");
        } catch (e) {
          evalBlockedMessage = e.message;
        }
        return {
          // varInPage is defined in file_simple_inline_script.html
          varInPageReadByExt: window.varInPage,
          thisIsGlobalThis: this === globalThis,
          thisIsWindow: this === window,
          browserIsUndefined: typeof browser == "undefined",
          evalBlockedMessage,
        };
      },
    },
  });

  let tab = await AppTestDelegate.openNewForegroundTab(
    window,
    "https://test1.example.com/tests/toolkit/components/extensions/test/mochitest/file_simple_inline_script.html",
    true
  );

  await extension.startup();
  await extension.awaitFinish("background-done");
  await extension.unload();

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

add_task(async function test_executeScript_main_world_func() {
  let extension = makeExtension({
    async background() {
      const tabs = await browser.tabs.query({ active: true });
      browser.test.assertEq(1, tabs.length, "expected 1 tab");

      let results = await browser.scripting.executeScript({
        target: { tabId: tabs[0].id },
        func: () => {
          let evalBlockedMessage = "unexpectedly not blocked???";
          try {
            // eslint-disable-next-line no-eval
            eval("// CSP of file_simple_inline_script.html should block eval.");
          } catch (e) {
            evalBlockedMessage = e.message;
          }
          return {
            // varInPage is defined in file_simple_inline_script.html
            varInPageReadByExt: window.varInPage,
            thisIsGlobalThis: this === globalThis,
            thisIsWindow: this === window,
            browserIsUndefined: typeof browser == "undefined",
            evalBlockedMessage,
          };
        },
        world: "MAIN",
      });
      browser.test.assertEq(undefined, results[0].error?.message, "No error");
      browser.test.assertEq(0, results[0].frameId, "got the expected frameId");
      browser.test.assertDeepEq(
        {
          varInPageReadByExt: "varInPage",
          thisIsGlobalThis: true,
          thisIsWindow: true,
          browserIsUndefined: true,
          evalBlockedMessage: "call to eval() blocked by CSP",
        },
        results[0].result,
        "executeScript with world:MAIN should run in the page's context"
      );

      browser.test.notifyPass("background-done");
    },
  });

  let tab = await AppTestDelegate.openNewForegroundTab(
    window,
    "https://test1.example.com/tests/toolkit/components/extensions/test/mochitest/file_simple_inline_script.html",
    true
  );

  await extension.startup();
  await extension.awaitFinish("background-done");
  await extension.unload();

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

add_task(async function test_executeScript_anonymous_filename() {
  let extension = makeExtension({
    async background() {
      const BACKGROUND_SCRIPT_URL =
        browser.runtime.getManifest().background.scripts[0];
      const tabs = await browser.tabs.query({ active: true });
      browser.test.assertEq(1, tabs.length, "expected 1 tab");

      function funcExtractFilename() {
        let error = new Error();
        return {
          fileName: error.fileName,
          // We only care about file names, so strip line and column numbers.
          stack: error.stack.replaceAll(/:\d+:\d+/g, ""),
        };
      }

      // Verify that the script source cannot be exfiltrated by the MAIN world
      // script. If it were to be possible, then a script on the web page can
      // do the same, which is undesirable because of fingerprinting and script
      // source secrecy.

      let results = await browser.scripting.executeScript({
        target: { tabId: tabs[0].id },
        func: funcExtractFilename,
        world: "MAIN",
      });
      browser.test.assertEq(undefined, results[0].error?.message, "No error");
      browser.test.assertDeepEq(
        {
          fileName: "<anonymous code>",
          stack: "funcExtractFilename@<anonymous code>\n@<anonymous code>\n",
        },
        results[0].result,
        "executeScript with world:MAIN & code should not leak the extension URL"
      );

      results = await browser.scripting.executeScript({
        target: { tabId: tabs[0].id },
        files: ["file_leak_me.js"],
        world: "MAIN",
      });
      browser.test.assertEq(undefined, results[0].error?.message, "No error");
      browser.test.assertDeepEq(
        {
          fileName: "<anonymous code>",
          stack: "fileExtractFilename@<anonymous code>\n@<anonymous code>\n",
        },
        results[0].result,
        "executeScript with world:MAIN & file should not leak the extension URL"
      );

      // Same tests, but with world: "ISOLATED". Since the world is isolated,
      // it does not really matter what the values are, but just to verify that
      // the value is sane, check its value.
      results = await browser.scripting.executeScript({
        target: { tabId: tabs[0].id },
        func: funcExtractFilename,
        world: "ISOLATED",
      });
      browser.test.assertEq(undefined, results[0].error?.message, "No error");
      browser.test.assertDeepEq(
        {
          fileName: "sandbox eval code",
          stack: `funcExtractFilename@sandbox eval code
@sandbox eval code
Async*background@${BACKGROUND_SCRIPT_URL}
async*@${BACKGROUND_SCRIPT_URL}
`
        },
        results[0].result,
        "executeScript with world:ISOLATED & code may expose extension URLs"
      );

      results = await browser.scripting.executeScript({
        target: { tabId: tabs[0].id },
        files: ["file_leak_me.js"],
        world: "ISOLATED",
      });
      browser.test.assertEq(undefined, results[0].error?.message, "No error");
      browser.test.assertDeepEq(
        {
          fileName: location.origin + "/file_leak_me.js",
          stack: `fileExtractFilename@${location.origin}/file_leak_me.js
@${location.origin}/file_leak_me.js
Async*background@${BACKGROUND_SCRIPT_URL}
async*@${BACKGROUND_SCRIPT_URL}
`,
        },
        results[0].result,
        "executeScript with world:ISOLATED & file may expose extension URLs"
      );

      browser.test.notifyPass("background-done");
    },
    files: {
      "file_leak_me.js": function fileExtractFilename() {
        let error = new Error();
        return {
          fileName: error.fileName,
          // We only care about file names, so strip line and column numbers.
          stack: error.stack.replaceAll(/:\d+:\d+/g, ""),
        };
      },
    },
  });

  let tab = await AppTestDelegate.openNewForegroundTab(
    window,
    "https://test1.example.com/tests/toolkit/components/extensions/test/mochitest/file_simple_inline_script.html",
    true
  );

  await extension.startup();
  await extension.awaitFinish("background-done");
  await extension.unload();

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

add_task(async function test_executeScript_invalid_world() {
  let extension = makeExtension({
    async background() {
      browser.test.assertThrows(
        () => {
          browser.scripting.executeScript({
            target: { tabId: 123 },
            func: () => {},
            world: "USER_SCRIPT",
          });
        },
        /world: Invalid enumeration value "USER_SCRIPT"/,
        "executeScript should throw when an invalid world is passed"
      );

      browser.test.notifyPass("background-done");
    },
  });

  await extension.startup();
  await extension.awaitFinish("background-done");
  await extension.unload();
});

add_task(async function test_executeScript_isolated_world() {
  let extension = makeExtension({
    manifest: {
      browser_specific_settings: {
        gecko: { id: "@isolated-addon-id" },
      },
    },
    async background() {
      const tabs = await browser.tabs.query({ active: true });
      browser.test.assertEq(1, tabs.length, "expected 1 tab");

      let results = await browser.scripting.executeScript({
        target: { tabId: tabs[0].id },
        func: () => {
          globalThis.defaultWorldVar = browser.runtime.id;
          return "default world";
        },
      });

      browser.test.assertEq(
        1,
        results.length,
        "got expected number of results"
      );
      browser.test.assertEq(
        "default world",
        results[0].result,
        "got expected return value"
      );

      results = await browser.scripting.executeScript({
        target: { tabId: tabs[0].id },
        func: () => {
          return `isolated: ${browser.runtime.id}; existing default var: ${typeof defaultWorldVar}`;
        },
        world: "ISOLATED",
      });

      browser.test.assertEq(
        1,
        results.length,
        "got expected number of results"
      );
      browser.test.assertEq(
        "isolated: @isolated-addon-id; existing default var: string",
        results[0].result,
        "got expected return value"
      );

      browser.test.notifyPass("execute-script");
    },
  });

  let tab = await AppTestDelegate.openNewForegroundTab(
    window,
    "https://test1.example.com/tests/toolkit/components/extensions/test/mochitest/file_sample.html",
    true
  );

  await extension.startup();
  await extension.awaitFinish("execute-script");
  await extension.unload();

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

add_task(async function test_execution_world_constants() {
  let extension = makeExtension({
    async background() {
      browser.test.assertDeepEq(
        {
          ISOLATED: "ISOLATED",
          MAIN: "MAIN",
        },
        browser.scripting.ExecutionWorld,
        "expected scripting.ExecutionWorld to be defined"
      );

      browser.test.notifyPass("background-done");
    },
  });

  await extension.startup();
  await extension.awaitFinish("background-done");
  await extension.unload();
});

</script>

</body>
</html>