summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/test/mochitest/test_ext_subframes_privileges.html
blob: 1f3a9a3c9fbc72242ea968262c2128449b5ac222 (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
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
<!DOCTYPE HTML>
<html>
<head>
  <title>WebExtension test</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" 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";

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

  yield extension.startup();

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

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

  yield extension.startup();

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

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

  yield extension.startup();

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

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

  win.close();

  yield extension.unload();
});

</script>

</body>
</html>