summaryrefslogtreecommitdiffstats
path: root/browser/components/originattributes/test/browser/browser_windowOpenerRestriction.js
blob: 0822ba24c960c78d924020141649f4e38838dd5f (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
/**
 * Bug 1339336 - A test case for testing pref 'privacy.firstparty.isolate.restrict_opener_access'
 */

const CC = Components.Constructor;

const FIRST_PARTY_OPENER = "example.com";
const FIRST_PARTY_TARGET = "example.org";
const OPENER_PAGE =
  "https://" +
  FIRST_PARTY_OPENER +
  "/browser/browser/components/" +
  "originattributes/test/browser/file_windowOpenerRestriction.html";
const TARGET_PAGE =
  "https://" +
  FIRST_PARTY_TARGET +
  "/browser/browser/components/" +
  "originattributes/test/browser/file_windowOpenerRestrictionTarget.html";

async function testPref(aIsPrefEnabled) {
  // Use a random key so we don't access it in later tests.
  let cookieStr =
    "key" + Math.random().toString() + "=" + Math.random().toString();

  // Open the tab for the opener page.
  let tab = BrowserTestUtils.addTab(gBrowser, OPENER_PAGE);

  // Select this tab and make sure its browser is loaded and focused.
  gBrowser.selectedTab = tab;
  tab.ownerGlobal.focus();

  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  await SpecialPowers.spawn(
    browser,
    [{ cookieStr, page: TARGET_PAGE, isPrefEnabled: aIsPrefEnabled }],
    async function (obj) {
      // Acquire the iframe element.
      let childFrame = content.document.getElementById("child");

      // Insert a cookie into this iframe.
      await SpecialPowers.spawn(childFrame, [obj.cookieStr], aCookieStr => {
        content.document.cookie = aCookieStr + "; SameSite=None; Secure;";
      });

      // Open the tab here and focus on it.
      let openedPath = obj.page;
      if (!obj.isPrefEnabled) {
        // If the pref is not enabled, we pass the cookie value through the query string
        // to tell the target page that it should check the cookie value.
        openedPath += "?" + obj.cookieStr;
      }

      // Issue the opener page to open the target page and focus on it.
      content.openedWindow = content.open(openedPath);
      content.openedWindow.focus();
    }
  );

  // Wait until the target page is loaded.
  let targetBrowser = gBrowser.getBrowserForTab(gBrowser.selectedTab);
  await BrowserTestUtils.browserLoaded(targetBrowser);

  // The target page will do the check and show the result through its title.
  is(
    targetBrowser.contentTitle,
    "pass",
    "The behavior of window.opener is correct."
  );

  // Close Tabs.
  await SpecialPowers.spawn(browser, [], async function () {
    content.openedWindow.close();
  });
  BrowserTestUtils.removeTab(tab);

  // Reset cookies
  Services.cookies.removeAll();
}

add_task(async function runTests() {
  let tests = [true, false];

  // First, we test the scenario that the first party isolation is enabled.
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.firstparty.isolate", true]],
  });

  for (let enabled of tests) {
    await SpecialPowers.pushPrefEnv({
      set: [["privacy.firstparty.isolate.restrict_opener_access", enabled]],
    });

    await testPref(enabled);
  }

  // Second, we test the scenario that the first party isolation is disabled.
  await SpecialPowers.pushPrefEnv({
    set: [["privacy.firstparty.isolate", false]],
  });

  for (let enabled of tests) {
    await SpecialPowers.pushPrefEnv({
      set: [["privacy.firstparty.isolate.restrict_opener_access", enabled]],
    });

    // When first party isolation is disabled, this pref will not affect the behavior of
    // window.opener. And the correct behavior here is to allow access since the iframe in
    // the opener page has the same origin with the target page.
    await testPref(false);
  }
});