summaryrefslogtreecommitdiffstats
path: root/netwerk/cookie/test/browser/browser_sameSiteConsole.js
blob: 84527296b29be285483b71e32e97b25f8f13a75a (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
"use strict";

const SAMESITE_DOMAIN = "http://example.com/";
const SAMESITE_PATH = "browser/netwerk/cookie/test/browser/";
const SAMESITE_TOP_PAGE = SAMESITE_DOMAIN + SAMESITE_PATH + "sameSite.sjs";

add_task(async _ => {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["network.cookie.sameSite.laxByDefault", true],
      ["network.cookie.sameSite.noneRequiresSecure", true],
    ],
  });

  const expected = [];

  const consoleListener = {
    observe(what) {
      if (!(what instanceof Ci.nsIConsoleMessage)) {
        return;
      }

      info("Console Listener: " + what);
      for (let i = expected.length - 1; i >= 0; --i) {
        const e = expected[i];

        if (what.message.includes(e.match)) {
          ok(true, "Message received: " + e.match);
          expected.splice(i, 1);
          e.resolve();
        }
      }
    },
  };

  Services.console.registerListener(consoleListener);

  registerCleanupFunction(() =>
    Services.console.unregisterListener(consoleListener)
  );

  const netPromises = [
    new Promise(resolve => {
      expected.push({
        resolve,
        match:
          "Cookie “a” has “SameSite” policy set to “Lax” because it is missing a “SameSite” attribute, and “SameSite=Lax” is the default value for this attribute.",
      });
    }),

    new Promise(resolve => {
      expected.push({
        resolve,
        match:
          "Cookie “b” rejected because it has the “SameSite=None” attribute but is missing the “secure” attribute.",
      });
    }),

    new Promise(resolve => {
      expected.push({
        resolve,
        match:
          "Invalid “SameSite“ value for cookie “c”. The supported values are: “Lax“, “Strict“, “None“.",
      });
    }),

    new Promise(resolve => {
      expected.push({
        resolve,
        match:
          "Cookie “c” has “SameSite” policy set to “Lax” because it is missing a “SameSite” attribute, and “SameSite=Lax” is the default value for this attribute.",
      });
    }),
  ];

  // Let's open our tab.
  const tab = BrowserTestUtils.addTab(gBrowser, SAMESITE_TOP_PAGE);
  gBrowser.selectedTab = tab;

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

  // Let's wait for the first set of console events.
  await Promise.all(netPromises);

  // the DOM list of events.
  const domPromises = [
    new Promise(resolve => {
      expected.push({
        resolve,
        match:
          "Cookie “d” has “SameSite” policy set to “Lax” because it is missing a “SameSite” attribute, and “SameSite=Lax” is the default value for this attribute.",
      });
    }),

    new Promise(resolve => {
      expected.push({
        resolve,
        match:
          "Cookie “e” rejected because it has the “SameSite=None” attribute but is missing the “secure” attribute.",
      });
    }),

    new Promise(resolve => {
      expected.push({
        resolve,
        match:
          "Invalid “SameSite“ value for cookie “f”. The supported values are: “Lax“, “Strict“, “None“.",
      });
    }),

    new Promise(resolve => {
      expected.push({
        resolve,
        match:
          "Cookie “f” has “SameSite” policy set to “Lax” because it is missing a “SameSite” attribute, and “SameSite=Lax” is the default value for this attribute.",
      });
    }),
  ];

  // Let's use document.cookie
  SpecialPowers.spawn(browser, [], () => {
    content.document.cookie = "d=4";
    content.document.cookie = "e=5; sameSite=none";
    content.document.cookie = "f=6; sameSite=batmat";
  });

  // Let's wait for the dom events.
  await Promise.all(domPromises);

  // Let's close the tab.
  BrowserTestUtils.removeTab(tab);
});