summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_tabs_containerIsolation.js
blob: 1960366bb5d91d412d50013e77220f263affffe1 (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
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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
/* eslint-disable mozilla/no-arbitrary-setTimeout */
"use strict";
const { XPCShellContentUtils } = ChromeUtils.importESModule(
  "resource://testing-common/XPCShellContentUtils.sys.mjs"
);
XPCShellContentUtils.initMochitest(this);
const server = XPCShellContentUtils.createHttpServer({
  hosts: ["www.example.com"],
});
server.registerPathHandler("/", (request, response) => {
  response.setHeader("Content-Type", "text/html; charset=UTF-8", false);
  response.write(`<!DOCTYPE html>
  <html>
    <head>
      <meta charset="utf-8">
    </head>
    <body>
      This is example.com page content
    </body>
  </html>
  `);
});

add_task(async function containerIsolation_restricted() {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["extensions.userContextIsolation.enabled", true],
      ["privacy.userContext.enabled", true],
    ],
  });

  let helperExtension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["tabs", "cookies"],
    },

    async background() {
      browser.test.onMessage.addListener(async message => {
        let tab;
        if (message?.subject !== "createTab") {
          browser.test.fail(
            `Unexpected test message received: ${JSON.stringify(message)}`
          );
          return;
        }

        tab = await browser.tabs.create({
          url: message.data.url,
          cookieStoreId: message.data.cookieStoreId,
        });
        browser.test.sendMessage("tabCreated", tab.id);
        browser.test.assertEq(
          message.data.cookieStoreId,
          tab.cookieStoreId,
          "Created tab is associated with the expected cookieStoreId"
        );
      });
    },
  });

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["tabs", "cookies", "<all_urls>", "tabHide"],
    },
    async background() {
      const [restrictedTab, unrestrictedTab] = await new Promise(resolve => {
        browser.test.onMessage.addListener(message => resolve(message));
      });

      // Check that print preview method fails
      await browser.test.assertRejects(
        browser.tabs.printPreview(),
        /Cannot access activeTab/,
        "should refuse to print a preview of the tab for the container which doesn't have permission"
      );

      // Check that save As PDF method fails
      await browser.test.assertRejects(
        browser.tabs.saveAsPDF({}),
        /Cannot access activeTab/,
        "should refuse to save as PDF of the tab for the container which doesn't have permission"
      );

      // Check that create method fails
      await browser.test.assertRejects(
        browser.tabs.create({ cookieStoreId: "firefox-container-1" }),
        /Cannot access firefox-container-1/,
        "should refuse to create container tab for the container which doesn't have permission"
      );

      // Check that detect language method fails
      await browser.test.assertRejects(
        browser.tabs.detectLanguage(restrictedTab),
        /Invalid tab ID/,
        "should refuse to detect language of a tab for the container which doesn't have permission"
      );

      // Check that move tabs method fails
      await browser.test.assertRejects(
        browser.tabs.move(restrictedTab, { index: 1 }),
        /Invalid tab ID/,
        "should refuse to move tab for the container which doesn't have permission"
      );

      // Check that duplicate method fails.
      await browser.test.assertRejects(
        browser.tabs.duplicate(restrictedTab),
        /Invalid tab ID:/,
        "should refuse to duplicate tab for the container which doesn't have permission"
      );

      // Check that captureTab method fails.
      await browser.test.assertRejects(
        browser.tabs.captureTab(restrictedTab),
        /Invalid tab ID/,
        "should refuse to capture the tab for the container which doesn't have permission"
      );

      // Check that discard method fails.
      await browser.test.assertRejects(
        browser.tabs.discard([restrictedTab]),
        /Invalid tab ID/,
        "should refuse to discard the tab for the container which doesn't have permission "
      );

      // Check that get method fails.
      await browser.test.assertRejects(
        browser.tabs.get(restrictedTab),
        /Invalid tab ID/,
        "should refuse to get the tab for the container which doesn't have permissiond"
      );

      // Check that highlight method fails.
      await browser.test.assertRejects(
        browser.tabs.highlight({ populate: true, tabs: [restrictedTab] }),
        `No tab at index: ${restrictedTab}`,
        "should refuse to highlight the tab for the container which doesn't have permission"
      );

      // Test for moveInSuccession method of tab

      await browser.test.assertRejects(
        browser.tabs.moveInSuccession([restrictedTab]),
        /Invalid tab ID/,
        "should refuse to moveInSuccession for the container which doesn't have permission"
      );

      // Check that executeScript method fails.
      await browser.test.assertRejects(
        browser.tabs.executeScript(restrictedTab, { frameId: 0 }),
        /Invalid tab ID/,
        "should refuse to execute a script of the tab for the container which doesn't have permission"
      );

      // Check that getZoom method fails.

      await browser.test.assertRejects(
        browser.tabs.getZoom(restrictedTab),
        /Invalid tab ID/,
        "should refuse to zoom the tab for the container which doesn't have permission"
      );

      // Check that getZoomSetting method fails.
      await browser.test.assertRejects(
        browser.tabs.getZoomSettings(restrictedTab),
        /Invalid tab ID/,
        "should refuse the setting of zoom of the tab for the container which doesn't have permission"
      );

      //Test for hide method of tab
      await browser.test.assertRejects(
        browser.tabs.hide(restrictedTab),
        /Invalid tab ID/,
        "should refuse to hide a tab for the container which doesn't have permission"
      );

      // Check that insertCSS method fails.
      await browser.test.assertRejects(
        browser.tabs.insertCSS(restrictedTab, { frameId: 0 }),
        /Invalid tab ID/,
        "should refuse to insert a stylesheet to the tab for the container which doesn't have permission"
      );

      // Check that removeCSS method fails.
      await browser.test.assertRejects(
        browser.tabs.removeCSS(restrictedTab, { frameId: 0 }),
        /Invalid tab ID/,
        "should refuse to remove a stylesheet to the tab for the container which doesn't have permission"
      );

      //Test for show method of tab
      await browser.test.assertRejects(
        browser.tabs.show([restrictedTab]),
        /Invalid tab ID/,
        "should refuse to show the tab for the container which doesn't have permission"
      );

      // Check that toggleReaderMode method fails.

      await browser.test.assertRejects(
        browser.tabs.toggleReaderMode(restrictedTab),
        /Invalid tab ID/,
        "should refuse to toggle reader mode in the tab for the container which doesn't have permission"
      );

      // Check that setZoom method fails.
      await browser.test.assertRejects(
        browser.tabs.setZoom(restrictedTab, 0),
        /Invalid tab ID/,
        "should refuse to set zoom of the tab for the container which doesn't have permission"
      );

      // Check that setZoomSettings method fails.
      await browser.test.assertRejects(
        browser.tabs.setZoomSettings(restrictedTab, { defaultZoomFactor: 1 }),
        /Invalid tab ID/,
        "should refuse to set zoom setting of the tab for the container which doesn't have permission"
      );

      // Check that goBack method fails.

      await browser.test.assertRejects(
        browser.tabs.goBack(restrictedTab),
        /Invalid tab ID/,
        "should refuse to go back to the tab for the container which doesn't have permission"
      );

      // Check that goForward method fails.

      await browser.test.assertRejects(
        browser.tabs.goForward(restrictedTab),
        /Invalid tab ID/,
        "should refuse to go forward to the tab for the container which doesn't have permission"
      );

      // Check that update method fails.
      await browser.test.assertRejects(
        browser.tabs.update(restrictedTab, { highlighted: true }),
        /Invalid tab ID/,
        "should refuse to update the tab for the container which doesn't have permission"
      );

      // Check that reload method fails.
      await browser.test.assertRejects(
        browser.tabs.reload(restrictedTab),
        /Invalid tab ID/,
        "should refuse to reload tab for the container which doesn't have permission"
      );

      //Test for warmup method of tab
      await browser.test.assertRejects(
        browser.tabs.warmup(restrictedTab),
        /Invalid tab ID/,
        "should refuse to warmup a tab for the container which doesn't have permission"
      );

      let gettab = await browser.tabs.get(unrestrictedTab);
      browser.test.assertEq(
        gettab.cookieStoreId,
        "firefox-container-2",
        "get tab should open"
      );

      let lang = await browser.tabs.detectLanguage(unrestrictedTab);
      await browser.test.assertEq(
        "en",
        lang,
        "English document should be detected"
      );

      let duptab = await browser.tabs.duplicate(unrestrictedTab);

      browser.test.assertEq(
        duptab.cookieStoreId,
        "firefox-container-2",
        "duplicated tab should open"
      );
      await browser.tabs.remove(duptab.id);

      let moved = await browser.tabs.move(unrestrictedTab, {
        index: 0,
      });
      browser.test.assertEq(moved.length, 1, "move() returned no moved tab");

      //Test for query method of tab
      let tabs = await browser.tabs.query({
        cookieStoreId: "firefox-container-1",
      });
      await browser.test.assertEq(
        0,
        tabs.length,
        "should not return restricted container's tab"
      );

      tabs = await browser.tabs.query({});
      await browser.test.assertEq(
        tabs
          .map(tab => tab.cookieStoreId)
          .sort()
          .join(","),
        "firefox-container-2,firefox-default",
        "should return two tabs - firefox-default and firefox-container-2"
      );

      // Check that remove method fails.

      await browser.test.assertRejects(
        browser.tabs.remove([restrictedTab]),
        /Invalid tab ID/,
        "should refuse to remove tab for the container which doesn't have permission"
      );

      let removedPromise = new Promise(resolve => {
        browser.tabs.onRemoved.addListener(tabId => {
          browser.test.assertEq(unrestrictedTab, tabId, "expected remove tab");
          resolve();
        });
      });
      await browser.tabs.remove(unrestrictedTab);
      await removedPromise;

      browser.test.sendMessage("done");
    },
  });

  await helperExtension.startup();

  helperExtension.sendMessage({
    subject: "createTab",
    data: {
      url: "http://www.example.com/",
      cookieStoreId: "firefox-container-2",
    },
  });
  const unrestrictedTab = await helperExtension.awaitMessage("tabCreated");

  helperExtension.sendMessage({
    subject: "createTab",
    data: {
      url: "http://www.example.com/",
      cookieStoreId: "firefox-container-1",
    },
  });
  const restrictedTab = await helperExtension.awaitMessage("tabCreated");

  await SpecialPowers.pushPrefEnv({
    set: [["extensions.userContextIsolation.defaults.restricted", "[1]"]],
  });

  await extension.startup();
  extension.sendMessage([restrictedTab, unrestrictedTab]);

  await extension.awaitMessage("done");
  await extension.unload();
  await helperExtension.unload();
  BrowserTestUtils.removeTab(gBrowser.selectedTab);
  await SpecialPowers.popPrefEnv();
});