summaryrefslogtreecommitdiffstats
path: root/toolkit/components/reader/test/browser_bug1453818_samesite_cookie.js
blob: 1fbfdeabfbe66fa857b1b905f652cd3fd534f630 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_ORIGIN1 = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "http://example.com"
);
const TEST_ORIGIN2 = getRootDirectory(gTestPath).replace(
  "chrome://mochitests/content",
  "http://example.org"
);

async function clickLink(browser) {
  info("Waiting for the page to load after clicking the link...");
  let pageLoaded = BrowserTestUtils.waitForContentEvent(
    browser,
    "DOMContentLoaded"
  );
  await SpecialPowers.spawn(browser, [], async function () {
    let link = content.document.getElementById("link");
    ok(link, "The link element was found.");
    link.click();
  });
  await pageLoaded;
}

async function checkCookiePresent(browser) {
  await SpecialPowers.spawn(browser, [], async function () {
    let cookieSpan = content.document.getElementById("cookieSpan");
    ok(cookieSpan, "cookieSpan element should be in document");
    is(
      cookieSpan.textContent,
      "foo=bar",
      "The SameSite cookie was sent correctly."
    );
  });
}

async function checkCookie(browser) {
  info("Check that the SameSite cookie was not sent.");
  await SpecialPowers.spawn(browser, [], async function () {
    let cookieSpan = content.document.getElementById("cookieSpan");
    ok(cookieSpan, "cookieSpan element should be in document");
    is(
      cookieSpan.textContent,
      "",
      "The SameSite cookie was blocked correctly."
    );
  });
}

async function runTest() {
  await SpecialPowers.pushPrefEnv({
    set: [["reader.parse-on-load.enabled", true]],
  });

  info("Set a SameSite=strict cookie.");
  await BrowserTestUtils.withNewTab(
    TEST_ORIGIN1 + "setSameSiteCookie.html",
    () => {}
  );

  info("Check that the cookie has been correctly set.");
  await BrowserTestUtils.withNewTab(
    TEST_ORIGIN1 + "getCookies.sjs",
    async function (browser) {
      await checkCookiePresent(browser);
    }
  );

  info(
    "Open a cross-origin page with a link to the domain that set the cookie."
  );
  {
    let browser;
    let pageLoaded;
    let tab = await BrowserTestUtils.openNewForegroundTab(
      gBrowser,
      () => {
        let t = BrowserTestUtils.addTab(
          gBrowser,
          TEST_ORIGIN2 + "linkToGetCookies.html"
        );
        gBrowser.selectedTab = t;
        browser = gBrowser.selectedBrowser;
        pageLoaded = BrowserTestUtils.waitForContentEvent(
          browser,
          "DOMContentLoaded"
        );
        return t;
      },
      false
    );

    info("Waiting for the page to load in normal mode...");
    await pageLoaded;

    await clickLink(browser);
    await checkCookie(browser);
    await BrowserTestUtils.removeTab(tab);
  }

  info("Open the cross-origin page again.");
  await BrowserTestUtils.withNewTab(
    TEST_ORIGIN2 + "linkToGetCookies.html",
    async function (browser) {
      let pageShown = BrowserTestUtils.waitForContentEvent(
        browser,
        "AboutReaderContentReady"
      );
      let readerButton = document.getElementById("reader-mode-button");
      ok(readerButton, "readerButton should be available");
      readerButton.click();

      info("Waiting for the page to be displayed in reader mode...");
      await pageShown;

      await clickLink(browser);
      await checkCookie(browser);
    }
  );
}

add_task(async function () {
  await runTest(true);
});

add_task(async function () {
  await runTest(false);
});