summaryrefslogtreecommitdiffstats
path: root/toolkit/components/cleardata/tests/browser/browser_sessionStorage.js
blob: 013ae0fa92a80c0b58df5fb80b34ab4b7ebf79dd (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const BASE_DOMAIN_A = "example.com";
const ORIGIN_A = `https://${BASE_DOMAIN_A}`;
const ORIGIN_A_HTTP = `http://${BASE_DOMAIN_A}`;
const ORIGIN_A_SUB = `https://test1.${BASE_DOMAIN_A}`;

const BASE_DOMAIN_B = "example.org";
const ORIGIN_B = `https://${BASE_DOMAIN_B}`;
const ORIGIN_B_HTTP = `http://${BASE_DOMAIN_B}`;
const ORIGIN_B_SUB = `https://test1.${BASE_DOMAIN_B}`;

const TEST_ROOT_DIR = getRootDirectory(gTestPath);

// Session storage is only valid for the lifetime of a tab, so we need to keep
// tabs open for the duration of a test.
let originToTabs = {};

function getTestURLForOrigin(origin) {
  return TEST_ROOT_DIR.replace("chrome://mochitests/content", origin);
}

function getTestEntryName(origin, partitioned) {
  return `${origin}${partitioned ? "_partitioned" : ""}`;
}

async function testHasEntry(originFirstParty, isSet, originThirdParty) {
  let tabs = originToTabs[originFirstParty];

  for (let tab of tabs) {
    // For the partition test we inspect the sessionStorage of the iframe.
    let browsingContext = originThirdParty
      ? tab.linkedBrowser.browsingContext.children[0]
      : tab.linkedBrowser.browsingContext;
    let actualSet = await SpecialPowers.spawn(
      browsingContext,
      [
        getTestEntryName(
          originThirdParty || originFirstParty,
          !!originThirdParty
        ),
      ],
      key => {
        return !!content.sessionStorage.getItem(key);
      }
    );

    let msg = `${originFirstParty}${isSet ? " " : " not "}set`;
    if (originThirdParty) {
      msg = "Partitioned under " + msg;
    }

    is(actualSet, isSet, msg);
  }
}

/**
 * Creates tabs and sets sessionStorage entries in first party and third party
 * context.
 * @returns {Promise} - Promise which resolves once all tabs are initialized,
 * {@link originToTabs} is populated and (sub-)resources have loaded.
 */
function addTestTabs() {
  let promises = [
    [ORIGIN_A, ORIGIN_B],
    [ORIGIN_A_SUB, ORIGIN_B_SUB],
    [ORIGIN_A_HTTP, ORIGIN_B_HTTP],
    [ORIGIN_B, ORIGIN_A],
    [ORIGIN_B_SUB, ORIGIN_A_SUB],
    [ORIGIN_B_HTTP, ORIGIN_A_HTTP],
  ].map(async ([firstParty, thirdParty]) => {
    info("Creating new tab for " + firstParty);
    let tab = BrowserTestUtils.addTab(gBrowser, firstParty);
    await BrowserTestUtils.browserLoaded(tab.linkedBrowser);

    info("Creating iframe for " + thirdParty);
    await SpecialPowers.spawn(
      tab.linkedBrowser,
      [getTestEntryName(firstParty, false), thirdParty],
      async (storageKey, url) => {
        // Set unpartitioned sessionStorage for firstParty.
        content.sessionStorage.setItem(storageKey, "foo");

        let iframe = content.document.createElement("iframe");
        iframe.src = url;

        let loadPromise = ContentTaskUtils.waitForEvent(iframe, "load", false);
        content.document.body.appendChild(iframe);
        await loadPromise;
      }
    );

    await SpecialPowers.spawn(
      tab.linkedBrowser.browsingContext.children[0],
      [getTestEntryName(thirdParty, true)],
      async storageKey => {
        // Set sessionStorage in partitioned third-party iframe.
        content.sessionStorage.setItem(storageKey, "foo");
      }
    );

    let tabs = originToTabs[firstParty];
    if (!tabs) {
      tabs = [];
      originToTabs[firstParty] = tabs;
    }
    tabs.push(tab);
  });

  return Promise.all(promises);
}

function cleanup() {
  Object.values(originToTabs).flat().forEach(BrowserTestUtils.removeTab);
  originToTabs = {};
  Services.obs.notifyObservers(null, "browser:purge-sessionStorage");
}

add_setup(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [["network.cookie.cookieBehavior", 5]],
  });
  cleanup();
});

add_task(async function test_deleteByBaseDomain() {
  await addTestTabs();

  info("Clearing sessionStorage for base domain A " + BASE_DOMAIN_A);
  await new Promise(resolve => {
    Services.clearData.deleteDataFromBaseDomain(
      BASE_DOMAIN_A,
      false,
      Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
      resolve
    );
  });

  info("All entries for A should have been cleared.");
  await testHasEntry(ORIGIN_A, false);
  await testHasEntry(ORIGIN_A_SUB, false);
  await testHasEntry(ORIGIN_A_HTTP, false);

  info("Entries for B should still exist.");
  await testHasEntry(ORIGIN_B, true);
  await testHasEntry(ORIGIN_B_SUB, true);
  await testHasEntry(ORIGIN_B_HTTP, true);

  info("All partitioned entries for B under A should have been cleared.");
  await testHasEntry(ORIGIN_A, false, ORIGIN_B);
  await testHasEntry(ORIGIN_A_SUB, false, ORIGIN_B_SUB);
  await testHasEntry(ORIGIN_A_HTTP, false, ORIGIN_B_HTTP);

  info("All partitioned entries of A under B should have been cleared.");
  await testHasEntry(ORIGIN_B, false, ORIGIN_A);
  await testHasEntry(ORIGIN_B_SUB, false, ORIGIN_A_SUB);
  await testHasEntry(ORIGIN_B_HTTP, false, ORIGIN_A_HTTP);

  cleanup();
});

add_task(async function test_deleteAll() {
  await addTestTabs();

  info("Clearing sessionStorage for base domain A " + BASE_DOMAIN_A);
  await new Promise(resolve => {
    Services.clearData.deleteData(
      Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
      resolve
    );
  });

  info("All entries should have been cleared.");
  await testHasEntry(ORIGIN_A, false);
  await testHasEntry(ORIGIN_A_SUB, false);
  await testHasEntry(ORIGIN_A_HTTP, false);
  await testHasEntry(ORIGIN_B, false);
  await testHasEntry(ORIGIN_B_SUB, false);
  await testHasEntry(ORIGIN_B_HTTP, false);

  info("All partitioned entries should have been cleared.");
  await testHasEntry(ORIGIN_A, false, ORIGIN_B);
  await testHasEntry(ORIGIN_A_SUB, false, ORIGIN_B_SUB);
  await testHasEntry(ORIGIN_A_HTTP, false, ORIGIN_B_HTTP);
  await testHasEntry(ORIGIN_B, false, ORIGIN_A);
  await testHasEntry(ORIGIN_B_SUB, false, ORIGIN_A_SUB);
  await testHasEntry(ORIGIN_B_HTTP, false, ORIGIN_A_HTTP);

  cleanup();
});

add_task(async function test_deleteFromPrincipal() {
  await addTestTabs();

  info("Clearing sessionStorage for partitioned principal A " + BASE_DOMAIN_A);

  let principalA = Services.scriptSecurityManager.createContentPrincipal(
    Services.io.newURI(ORIGIN_A),
    { partitionKey: `(https,${BASE_DOMAIN_B})` }
  );

  info("principal: " + principalA.origin);
  info("principal partitionKey " + principalA.originAttributes.partitionKey);
  await new Promise(resolve => {
    Services.clearData.deleteDataFromPrincipal(
      principalA,
      false,
      Ci.nsIClearDataService.CLEAR_DOM_QUOTA,
      resolve
    );
  });

  info("Unpartitioned entries should still exist.");
  await testHasEntry(ORIGIN_A, true);
  await testHasEntry(ORIGIN_A_SUB, true);
  await testHasEntry(ORIGIN_A_HTTP, true);
  await testHasEntry(ORIGIN_B, true);
  await testHasEntry(ORIGIN_B_SUB, true);
  await testHasEntry(ORIGIN_B_HTTP, true);

  info("Only entries of principal should have been cleared.");
  await testHasEntry(ORIGIN_A, true, ORIGIN_B);
  await testHasEntry(ORIGIN_A_SUB, true, ORIGIN_B_SUB);
  await testHasEntry(ORIGIN_A_HTTP, true, ORIGIN_B_HTTP);

  await testHasEntry(ORIGIN_B, false, ORIGIN_A);

  await testHasEntry(ORIGIN_B_SUB, true, ORIGIN_A_SUB);
  await testHasEntry(ORIGIN_B_HTTP, true, ORIGIN_A_HTTP);

  cleanup();
});