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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
<!-- Any copyright is dedicated to the Public Domain.
- http://creativecommons.org/publicdomain/zero/1.0/ -->
<!DOCTYPE HTML>
<html>
<head>
<title>Test browsingData.remove indexedDB</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script src="head.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<script type="text/javascript">
"use strict";
add_task(async function setup() {
// make sure userContext is enabled.
return SpecialPowers.pushPrefEnv({
set: [["privacy.userContext.enabled", true]],
});
});
add_task(async function testLocalStorage() {
async function background() {
function waitForTabs() {
return new Promise(resolve => {
let tabs = {};
let listener = async (msg, { tab }) => {
if (msg !== "content-script-ready") {
return;
}
tabs[tab.url] = tab;
if (Object.keys(tabs).length == 3) {
browser.runtime.onMessage.removeListener(listener);
resolve(tabs);
}
};
browser.runtime.onMessage.addListener(listener);
});
}
function sendMessageToTabs(tabs, message) {
return Promise.all(
Object.values(tabs).map(tab => {
return browser.tabs.sendMessage(tab.id, message);
})
);
}
let tabs = await waitForTabs();
browser.test.assertRejects(
browser.browsingData.removeLocalStorage({ since: Date.now() }),
"Firefox does not support clearing localStorage with 'since'.",
"Expected error received when using unimplemented parameter 'since'."
);
await sendMessageToTabs(tabs, "resetLocalStorage");
await browser.browsingData.removeLocalStorage({
hostnames: ["example.com"],
});
await browser.tabs.sendMessage(tabs["https://example.com/"].id, "checkLocalStorageCleared");
await browser.tabs.sendMessage(tabs["https://example.net/"].id, "checkLocalStorageSet");
if (
SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled ===
false
) {
// This assertion fails when localStorage is using the legacy
// implementation (See Bug 1595431).
browser.test.log("Skipped assertion on nextGenLocalStorageEnabled=false");
} else {
await browser.tabs.sendMessage(tabs["https://test1.example.com/"].id, "checkLocalStorageSet");
}
await sendMessageToTabs(tabs, "resetLocalStorage");
await sendMessageToTabs(tabs, "checkLocalStorageSet");
await browser.browsingData.removeLocalStorage({});
await sendMessageToTabs(tabs, "checkLocalStorageCleared");
await sendMessageToTabs(tabs, "resetLocalStorage");
await sendMessageToTabs(tabs, "checkLocalStorageSet");
await browser.browsingData.remove({}, { localStorage: true });
await sendMessageToTabs(tabs, "checkLocalStorageCleared");
// Can only delete cookieStoreId with LSNG enabled.
if (SpecialPowers.Services.domStorageManager.nextGenLocalStorageEnabled) {
await sendMessageToTabs(tabs, "resetLocalStorage");
await sendMessageToTabs(tabs, "checkLocalStorageSet");
await browser.browsingData.removeLocalStorage({
cookieStoreId: "firefox-container-1",
});
await browser.tabs.sendMessage(tabs["https://example.com/"].id, "checkLocalStorageSet");
await browser.tabs.sendMessage(tabs["https://example.net/"].id, "checkLocalStorageSet");
// TODO: containers support is lacking on GeckoView (Bug 1643740)
if (!navigator.userAgent.includes("Android")) {
await browser.tabs.sendMessage(tabs["https://test1.example.com/"].id, "checkLocalStorageCleared");
}
await sendMessageToTabs(tabs, "resetLocalStorage");
await sendMessageToTabs(tabs, "checkLocalStorageSet");
// Hostname doesn't match, so nothing cleared.
await browser.browsingData.removeLocalStorage({
cookieStoreId: "firefox-container-1",
hostnames: ["example.net"],
});
await sendMessageToTabs(tabs, "checkLocalStorageSet");
await sendMessageToTabs(tabs, "resetLocalStorage");
await sendMessageToTabs(tabs, "checkLocalStorageSet");
// Deleting private browsing mode data is silently ignored.
await browser.browsingData.removeLocalStorage({
cookieStoreId: "firefox-private",
});
await sendMessageToTabs(tabs, "checkLocalStorageSet");
} else {
await browser.test.assertRejects(
browser.browsingData.removeLocalStorage({
cookieStoreId: "firefox-container-1",
}),
"Firefox does not support clearing localStorage with 'cookieStoreId'.",
"removeLocalStorage with cookieStoreId requires LSNG"
);
}
// Cleanup (checkLocalStorageCleared creates empty LS databases).
await browser.browsingData.removeLocalStorage({});
browser.test.notifyPass("done");
}
function contentScript() {
browser.runtime.onMessage.addListener(msg => {
if (msg === "resetLocalStorage") {
localStorage.clear();
localStorage.setItem("test", "test");
} else if (msg === "checkLocalStorageSet") {
browser.test.assertEq(
"test",
localStorage.getItem("test"),
`checkLocalStorageSet: ${location.href}`
);
} else if (msg === "checkLocalStorageCleared") {
browser.test.assertEq(
null,
localStorage.getItem("test"),
`checkLocalStorageCleared: ${location.href}`
);
}
});
browser.runtime.sendMessage("content-script-ready");
}
// This extension is responsible for opening tabs with a specified
// cookieStoreId, we use a separate extension to make sure that browsingData
// works without the cookies permission.
let openTabsExtension = ExtensionTestUtils.loadExtension({
useAddonManager: "permanent",
manifest: {
name: "Open tabs",
browser_specific_settings: { gecko: { id: "open-tabs@tests.mozilla.org" }, },
permissions: ["cookies"],
},
async background() {
const TABS = [
{ url: "https://example.com" },
{ url: "https://example.net" },
{
url: "https://test1.example.com",
cookieStoreId: 'firefox-container-1',
},
];
function awaitLoad(tabId) {
return new Promise(resolve => {
browser.tabs.onUpdated.addListener(function listener(tabId_, changed, tab) {
if (tabId == tabId_ && changed.status == "complete") {
browser.tabs.onUpdated.removeListener(listener);
resolve();
}
});
});
}
let tabs = [];
let loaded = [];
for (let options of TABS) {
let tab = await browser.tabs.create(options);
loaded.push(awaitLoad(tab.id));
tabs.push(tab);
}
await Promise.all(loaded);
browser.test.onMessage.addListener(async msg => {
if (msg === "cleanup") {
const tabIds = tabs.map(tab => tab.id);
let removedTabs = 0;
browser.tabs.onRemoved.addListener(tabId => {
browser.test.log(`Removing tab ${tabId}.`);
if (tabIds.includes(tabId)) {
removedTabs++;
if (removedTabs == tabIds.length) {
browser.test.sendMessage("done");
}
}
});
await browser.tabs.remove(tabIds);
}
});
}
});
let extension = ExtensionTestUtils.loadExtension({
useAddonManager: "permanent",
background,
manifest: {
name: "Test Extension",
browser_specific_settings: { gecko: { id: "localStorage@tests.mozilla.org" } },
permissions: ["browsingData", "tabs"],
content_scripts: [
{
matches: [
"https://example.com/",
"https://example.net/",
"https://test1.example.com/",
],
js: ["content-script.js"],
run_at: "document_end",
},
],
},
files: {
"content-script.js": contentScript,
},
});
await openTabsExtension.startup();
await extension.startup();
await extension.awaitFinish("done");
await extension.unload();
await openTabsExtension.sendMessage("cleanup");
await openTabsExtension.awaitMessage("done");
await openTabsExtension.unload();
});
// Verify that browsingData.removeLocalStorage doesn't break on data stored
// in about:newtab or file principals.
add_task(async function test_browserData_on_aboutnewtab_and_file_data() {
let extension = ExtensionTestUtils.loadExtension({
useAddonManager: "permanent",
async background() {
await browser.browsingData.removeLocalStorage({}).catch(err => {
browser.test.fail(`${err} :: ${err.stack}`);
});
browser.test.sendMessage("done");
},
manifest: {
browser_specific_settings: { gecko: { id: "indexed-db-file@test.mozilla.org" } },
permissions: ["browsingData"],
},
});
await new Promise(resolve => {
const chromeScript = SpecialPowers.loadChromeScript(async () => {
/* eslint-env mozilla/chrome-script */
const { SiteDataTestUtils } = ChromeUtils.import(
"resource://testing-common/SiteDataTestUtils.jsm"
);
await SiteDataTestUtils.addToIndexedDB("about:newtab");
await SiteDataTestUtils.addToIndexedDB("file:///fake/file");
sendAsyncMessage("done");
});
chromeScript.addMessageListener("done", () => {
chromeScript.destroy();
resolve();
});
});
await extension.startup();
await extension.awaitMessage("done");
await extension.unload();
});
add_task(async function test_browserData_should_not_remove_extension_data() {
if (SpecialPowers.getBoolPref("dom.storage.enable_unsupported_legacy_implementation")) {
// When LSNG isn't enabled, the browsingData API does still clear
// all the extensions localStorage if called without a list of specific
// origins to clear.
info("Test skipped because LSNG is currently disabled");
return;
}
let extension = ExtensionTestUtils.loadExtension({
useAddonManager: "permanent",
async background() {
window.localStorage.setItem("key", "value");
await browser.browsingData.removeLocalStorage({}).catch(err => {
browser.test.fail(`${err} :: ${err.stack}`);
});
browser.test.sendMessage("done", window.localStorage.getItem("key"));
},
manifest: {
browser_specific_settings: { gecko: { id: "extension-data@tests.mozilla.org" } },
permissions: ["browsingData"],
},
});
await extension.startup();
const lsValue = await extension.awaitMessage("done");
is(lsValue, "value", "Got the expected localStorage data");
await extension.unload();
});
</script>
</body>
</html>
|