File: test_ext_subframes_privileges.html

package info (click to toggle)
thunderbird 1%3A68.10.0-1~deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,754,812 kB
  • sloc: cpp: 5,411,679; javascript: 4,161,772; ansic: 2,639,702; python: 763,064; java: 346,606; xml: 266,623; asm: 265,884; sh: 117,270; lisp: 41,340; makefile: 23,560; perl: 18,042; objc: 5,277; yacc: 1,778; ada: 1,681; pascal: 1,673; lex: 1,417; cs: 879; exp: 527; awk: 495; php: 436; ruby: 221; sed: 69; csh: 27
file content (244 lines) | stat: -rw-r--r-- 7,529 bytes parent folder | download | duplicates (2)
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
<!DOCTYPE HTML>
<html>
<head>
  <title>WebExtension test</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";
/* eslint-disable mozilla/balanced-listeners */

add_task(async function test_webext_tab_subframe_privileges() {
  function background() {
    browser.runtime.onMessage.addListener(async ({msg, success, tabId, error}) => {
      if (msg == "webext-tab-subframe-privileges") {
        if (success) {
          await browser.tabs.remove(tabId);

          browser.test.notifyPass(msg);
        } else {
          browser.test.log(`Got an unexpected error: ${error}`);

          let tabs = await browser.tabs.query({active: true});
          await browser.tabs.remove(tabs[0].id);

          browser.test.notifyFail(msg);
        }
      }
    });
    browser.tabs.create({url: browser.runtime.getURL("/tab.html")});
  }

  async function tabSubframeScript() {
    browser.test.assertTrue(browser.tabs != undefined,
                            "Subframe of a privileged page has access to privileged APIs");
    if (browser.tabs) {
      try {
        let tab = await browser.tabs.getCurrent();
        browser.runtime.sendMessage({
          msg: "webext-tab-subframe-privileges",
          success: true,
          tabId: tab.id,
        });
      } catch (e) {
        browser.runtime.sendMessage({msg: "webext-tab-subframe-privileges", success: false, error: `${e}`});
      }
    } else {
      browser.runtime.sendMessage({
        msg: "webext-tab-subframe-privileges",
        success: false,
        error: `Privileged APIs missing in WebExtension tab sub-frame`,
      });
    }
  }

  let extensionData = {
    background,
    files: {
      "tab.html": `<!DOCTYPE>
          <head>
            <meta charset="utf-8">
          </head>
          <body>
            <iframe src="tab-subframe.html"></iframe>
          </body>
        </html>`,
      "tab-subframe.html": `<!DOCTYPE>
          <head>
            <meta charset="utf-8">
            <script src="tab-subframe.js"><\/script>
          </head>
        </html>`,
      "tab-subframe.js": tabSubframeScript,
    },
  };
  let extension = ExtensionTestUtils.loadExtension(extensionData);

  await extension.startup();

  await extension.awaitFinish("webext-tab-subframe-privileges");
  await extension.unload();
});

add_task(async function test_webext_background_subframe_privileges() {
  function backgroundSubframeScript() {
    browser.test.assertTrue(browser.tabs != undefined,
                            "Subframe of a background page has access to privileged APIs");
    browser.test.notifyPass("webext-background-subframe-privileges");
  }

  let extensionData = {
    manifest: {
      background: {
        page: "background.html",
      },
    },
    files: {
      "background.html": `<!DOCTYPE>
         <head>
           <meta charset="utf-8">
         </head>
         <body>
           <iframe src="background-subframe.html"></iframe>
         </body>
       </html>`,
      "background-subframe.html": `<!DOCTYPE>
         <head>
           <meta charset="utf-8">
           <script src="background-subframe.js"><\/script>
         </head>
       </html>`,
      "background-subframe.js": backgroundSubframeScript,
    },
  };
  let extension = ExtensionTestUtils.loadExtension(extensionData);

  await extension.startup();

  await extension.awaitFinish("webext-background-subframe-privileges");
  await extension.unload();
});

add_task(async function test_webext_contentscript_iframe_subframe_privileges() {
  function background() {
    browser.runtime.onMessage.addListener(({name, hasTabsAPI, hasStorageAPI}) => {
      if (name == "contentscript-iframe-loaded") {
        browser.test.assertFalse(hasTabsAPI,
                                 "Subframe of a content script privileged iframes has no access to privileged APIs");
        browser.test.assertTrue(hasStorageAPI,
                                "Subframe of a content script privileged iframes has access to content script APIs");

        browser.test.notifyPass("webext-contentscript-subframe-privileges");
      }
    });
  }

  function subframeScript() {
    browser.runtime.sendMessage({
      name: "contentscript-iframe-loaded",
      hasTabsAPI: browser.tabs != undefined,
      hasStorageAPI: browser.storage != undefined,
    });
  }

  function contentScript() {
    let iframe = document.createElement("iframe");
    iframe.setAttribute("src", browser.runtime.getURL("/contentscript-iframe.html"));
    document.body.appendChild(iframe);
  }

  let extensionData = {
    background,
    manifest: {
      "permissions": ["storage"],
      "content_scripts": [{
        "matches": ["http://example.com/*"],
        "js": ["contentscript.js"],
      }],
      web_accessible_resources: [
        "contentscript-iframe.html",
      ],
    },
    files: {
      "contentscript.js": contentScript,
      "contentscript-iframe.html": `<!DOCTYPE>
         <head>
           <meta charset="utf-8">
         </head>
         <body>
           <iframe src="contentscript-iframe-subframe.html"></iframe>
         </body>
       </html>`,
      "contentscript-iframe-subframe.html": `<!DOCTYPE>
         <head>
           <meta charset="utf-8">
           <script src="contentscript-iframe-subframe.js"><\/script>
         </head>
       </html>`,
      "contentscript-iframe-subframe.js": subframeScript,
    },
  };
  let extension = ExtensionTestUtils.loadExtension(extensionData);

  await extension.startup();

  let win = window.open("http://example.com");

  await extension.awaitFinish("webext-contentscript-subframe-privileges");

  win.close();

  await extension.unload();
});

add_task(async function test_webext_background_remote_subframe_privileges() {
  function backgroundSubframeScript() {
    window.addEventListener("message", evt => {
      browser.test.assertEq("http://mochi.test:8888", evt.origin, "postmessage origin ok");
      browser.test.assertFalse(evt.data.tabs, "remote frame cannot access webextension APIs");
      browser.test.assertEq("cookie=monster", evt.data.cookie, "Expected cookie value");
      browser.test.notifyPass("webext-background-subframe-privileges");
    }, {once: true});
    browser.cookies.set({url: "http://mochi.test:8888", name: "cookie", "value": "monster"});
  }

  let extensionData = {
    manifest: {
      permissions: ["cookies", "*://mochi.test/*", "tabs"],
      background: {
        page: "background.html",
      },
    },
    files: {
      "background.html": `<!DOCTYPE>
         <head>
           <meta charset="utf-8">
           <script src="background-subframe.js"><\/script>
         </head>
         <body>
           <iframe src='${SimpleTest.getTestFileURL("file_remote_frame.html")}'></iframe>
         </body>
       </html>`,
      "background-subframe.js": backgroundSubframeScript,
    },
  };
  // Need remote webextensions to be able to load remote content from a background page.
  if (!SpecialPowers.getBoolPref("extensions.webextensions.remote", true)) {
    return;
  }
  let extension = ExtensionTestUtils.loadExtension(extensionData);

  await extension.startup();

  await extension.awaitFinish("webext-background-subframe-privileges");
  await extension.unload();
});
</script>

</body>
</html>