summaryrefslogtreecommitdiffstats
path: root/dom/fetch/tests/browser_origin_trial_coep_credentialless_worker.js
blob: be77add88e22282b6f40b5326479371bc1a6f68f (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
const TOP_LEVEL_URL =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  ) + "open_credentialless_document.sjs";

const WORKER_URL =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  ) + "credentialless_worker.sjs";

const GET_STATE_URL =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  ) + "store_header.sjs?getstate";

const SAME_ORIGIN = "https://example.com";
const CROSS_ORIGIN = "https://test1.example.com";

const WORKER_USES_CREDENTIALLESS = "credentialless";
const WORKER_NOT_USE_CREDENTIALLESS = "";

async function addCookieToOrigin(origin) {
  const fetchRequestURL =
    getRootDirectory(gTestPath).replace("chrome://mochitests/content", origin) +
    "store_header.sjs?addcookie";

  const addcookieTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    fetchRequestURL
  );

  await SpecialPowers.spawn(addcookieTab.linkedBrowser, [], async function () {
    content.document.cookie = "coep=credentialless; SameSite=None; Secure";
  });
  await BrowserTestUtils.removeTab(addcookieTab);
}

async function testOrigin(
  fetchOrigin,
  isCredentialless,
  workerUsesCredentialless,
  expectedCookieResult
) {
  let topLevelUrl = TOP_LEVEL_URL;
  if (isCredentialless) {
    topLevelUrl += "?credentialless";
  }
  const noCredentiallessTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    topLevelUrl
  );

  const fetchRequestURL =
    getRootDirectory(gTestPath).replace(
      "chrome://mochitests/content",
      fetchOrigin
    ) + "store_header.sjs?checkheader";

  let workerScriptURL = WORKER_URL + "?" + workerUsesCredentialless;

  await SpecialPowers.spawn(
    noCredentiallessTab.linkedBrowser,
    [fetchRequestURL, GET_STATE_URL, workerScriptURL, expectedCookieResult],
    async function (
      fetchRequestURL,
      getStateURL,
      workerScriptURL,
      expectedCookieResult
    ) {
      const worker = new content.Worker(workerScriptURL, {});

      // When the worker receives this message, it'll send
      // a fetch request to fetchRequestURL, and fetchRequestURL
      // will store whether it has received the cookie as a
      // shared state.
      worker.postMessage(fetchRequestURL);

      if (expectedCookieResult == "error") {
        await new Promise(r => {
          worker.onerror = function () {
            ok(true, "worker has error");
            r();
          };
        });
      } else {
        await new Promise(r => {
          worker.addEventListener("message", async function () {
            // This request is used to get the saved state from the
            // previous fetch request.
            const response = await content.fetch(getStateURL, {
              mode: "cors",
            });
            const text = await response.text();
            is(text, expectedCookieResult);
            r();
          });
        });
      }
    }
  );
  await BrowserTestUtils.removeTab(noCredentiallessTab);
}

async function dedicatedWorkerTest(
  origin,
  workerCOEP,
  expectedCookieResultForNoCredentialless,
  expectedCookieResultForCredentialless
) {
  await testOrigin(
    origin,
    false,
    workerCOEP,
    expectedCookieResultForNoCredentialless
  );
  await testOrigin(
    origin,
    true,
    workerCOEP,
    expectedCookieResultForCredentialless
  );
}

add_task(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.tabs.remote.coep.credentialless", false], // Explicitly set credentialless to false because we want to test origin trial
      ["dom.origin-trials.enabled", true],
      ["dom.origin-trials.test-key.enabled", true],
    ],
  });

  await addCookieToOrigin(SAME_ORIGIN);
  await addCookieToOrigin(CROSS_ORIGIN);

  await dedicatedWorkerTest(
    SAME_ORIGIN,
    WORKER_NOT_USE_CREDENTIALLESS,
    "hasCookie",
    "error"
  );
  await dedicatedWorkerTest(
    SAME_ORIGIN,
    WORKER_USES_CREDENTIALLESS,
    "hasCookie",
    "hasCookie"
  );

  await dedicatedWorkerTest(
    CROSS_ORIGIN,
    WORKER_NOT_USE_CREDENTIALLESS,
    "hasCookie",
    "error"
  );
  await dedicatedWorkerTest(
    CROSS_ORIGIN,
    WORKER_USES_CREDENTIALLESS,
    "noCookie",
    "noCookie"
  );
});