File: test_ext_scripting_executeScript_null_principal.html

package info (click to toggle)
firefox-esr 128.13.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • 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 (213 lines) | stat: -rw-r--r-- 6,750 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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Tests scripting.executeScript() on null principal</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_task(async function test_executeScript_with_sandboxed_iframes() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      manifest_version: 3,
      permissions: ["scripting"],
      host_permissions: [ "https://example.com/*" ],
      granted_host_permissions: true,
      browser_specific_settings: { gecko: { id: "@executeScript-in-sandbox" } },
    },
    useAddonManager: "temporary",
    async background() {
      const tabs = await browser.tabs.query({ active: true });
      browser.test.assertEq(1, tabs.length, "expected 1 tab");

      const target = { tabId: tabs[0].id, allFrames: true };

      await browser.scripting.insertCSS({
        target,
        css: "body { color: rgb(123, 45, 67); }",
      });
      // Add and remove CSS to verify that removeCSS works.
      await browser.scripting.insertCSS({
        target,
        css: "body { color: rgb(98, 76, 54); }",
      });
      await browser.scripting.removeCSS({
        target,
        css: "body { color: rgb(98, 76, 54); }",
      });
      // Now we should only have the initial code from insertCSS.

      let results = await browser.scripting.executeScript({
        target,
        func: () => {
          let frameDepth = 0;
          for (let w = window; w !== top; w = w.parent) {
            frameDepth++;
          }
          browser.test.assertEq(
            "rgb(123, 45, 67)",
            getComputedStyle(document.body).color,
            "insertCSS should have succeeded in frame " + frameDepth
          );
          return { frameDepth, origin, url: document.URL };
        },
      });
      results.sort((a, b) =>  a.result?.frameDepth - b.result?.frameDepth);
      results = results.map((r, i) => {
        browser.test.assertDeepEq(undefined, r.error, `No error at ${i}`);
        return r.result;
      });

      browser.test.assertDeepEq(
        [
          {
            frameDepth: 0,
            origin: "https://example.com",
            url: "https://example.com/tests/toolkit/components/extensions/test/mochitest/file_with_iframe_sandbox.html",
          },
          {
            frameDepth: 1,
            origin: "null",
            url: "about:srcdoc",
          },
          {
            frameDepth: 2,
            origin: "null",
            url: "data:text/html,<iframe sandbox></iframe>",
          },
          {
            frameDepth: 3,
            origin: "null",
            url: "about:blank",
          },
        ],
        results,
        "expected origins and URLs in frames"
      );

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

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

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

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

async function test_activeTab_executeScript_sandboxed_frames(manifest_version) {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      manifest_version,
      [manifest_version === 2 ? "browser_action": "action"]: {},
      permissions: ["scripting", "activeTab"],
      browser_specific_settings: { gecko: { id: "@executeScript-activeTab" } },
    },
    useAddonManager: "temporary",
    background() {
      browser.action ??= browser.browserAction; // Polyfill for MV2.
      browser.action.onClicked.addListener(async tab => {
        let results = await browser.scripting.executeScript({
          target: { tabId: tab.id, allFrames: true },
          func: () => {
            let frameDepth = 0;
            for (let w = window; w !== top; w = w.parent) {
              frameDepth++;
            }
            return { frameDepth, origin, url: document.URL };
          },
        });
        results.sort((a, b) =>  a.result?.frameDepth - b.result?.frameDepth);
        results = results.map((r, i) => {
          browser.test.assertDeepEq(undefined, r.error, `No error at ${i}`);
          return r.result;
        });

        const expectedResults = [
          {
            frameDepth: 0,
            origin: "https://example.com",
            url: "https://example.com/tests/toolkit/components/extensions/test/mochitest/file_with_iframe_sandbox.html",
          },
          {
            frameDepth: 1,
            origin: "null",
            url: "about:srcdoc",
          },
          {
            frameDepth: 2,
            origin: "null",
            url: "data:text/html,<iframe sandbox></iframe>",
          },
          {
            frameDepth: 3,
            origin: "null",
            url: "about:blank",
          },
        ];

        if (browser.runtime.getManifest().manifest_version === 2) {
          // activeTab in MV2 grants access to all frames.
          browser.test.assertDeepEq(
            expectedResults,
            results,
            "activeTab grants access to all sandboxed frames (MV2)"
          );
        } else {
          // All frames have an opaque origin, and are thus not same-origin to
          // the top. In MV3, activeTab only grants access to same-origin
          // frames.
          browser.test.assertDeepEq(
            expectedResults.slice(0, 1),
            results,
            "activeTab excludes sandboxed, cross-origin frames (MV3)"
          );
        }

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

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

  await extension.startup();
  await AppTestDelegate.clickBrowserAction(window, extension);
  await extension.awaitFinish("execute-script");
  await AppTestDelegate.closeBrowserAction(window, extension);
  await extension.unload();

  await AppTestDelegate.removeTab(window, tab);
}

add_task(async function test_activeTab_executeScript_sandboxed_frames_mv2() {
  await test_activeTab_executeScript_sandboxed_frames(2);
});

add_task(async function test_activeTab_executeScript_sandboxed_frames_mv3() {
  await test_activeTab_executeScript_sandboxed_frames(3);
});

</script>

</body>
</html>