summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/unit/test_nuke_webextension_wrappers.js
blob: 34fd0511bc2d30d68b9804f6124bee2aa9e179ab (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
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1273251

const {NetUtil} = ChromeUtils.import("resource://gre/modules/NetUtil.jsm");
ChromeUtils.importESModule("resource://gre/modules/Timer.sys.mjs");
const {TestUtils} = ChromeUtils.importESModule("resource://testing-common/TestUtils.sys.mjs");

function getWindowlessBrowser(url) {
  let ssm = Services.scriptSecurityManager;

  let uri = NetUtil.newURI(url);

  let principal = ssm.createContentPrincipal(uri, {});

  let webnav = Services.appShell.createWindowlessBrowser(false);

  let docShell = webnav.docShell;

  docShell.createAboutBlankContentViewer(principal, principal);

  return webnav;
}

function StubPolicy(id) {
  return new WebExtensionPolicy({
    id,
    mozExtensionHostname: id,
    baseURL: `file:///{id}`,

    allowedOrigins: new MatchPatternSet([]),
    localizeCallback(string) {},
  });
}

add_task(async function() {
  let policy = StubPolicy("foo");
  policy.active = true;

  let webnavA = getWindowlessBrowser("moz-extension://foo/a.html");
  let webnavB = getWindowlessBrowser("moz-extension://foo/b.html");

  let winA = Cu.waiveXrays(webnavA.document.defaultView);
  let winB = Cu.waiveXrays(webnavB.document.defaultView);

  winB.winA = winA;
  winB.eval(`winA.thing = {foo: "bar"};`);

  let getThing = winA.eval(String(() => {
    try {
      return thing.foo;
    } catch (e) {
      return String(e);
    }
  }));

  // Check that the object can be accessed normally before windowB is closed.
  equal(getThing(), "bar");

  webnavB.close();

  // Wrappers are nuked asynchronously, so wait for that to happen.
  await TestUtils.topicObserved("inner-window-nuked");

  // Check that it can't be accessed after he window has been closed.
  let result = getThing();
  ok(/dead object/.test(result),
     `Result should show a dead wrapper error: ${result}`);

  webnavA.close();

  policy.active = false;
});