forked from containers-everywhere/contain-google
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
463 lines (413 loc) · 13.6 KB
/
background.js
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
// Param values from https://developer.mozilla.org/Add-ons/WebExtensions/API/contextualIdentities/create
const MICROSOFT_CONTAINER_NAME = "Microsoft";
const MICROSOFT_CONTAINER_COLOR = "blue";
const MICROSOFT_CONTAINER_ICON = "briefcase";
let MICROSOFT_DOMAINS = [
"microsoft.com", "microsoft.org", "microsoft.net", "microsoft.co", "microsoft.co.uk", "microsoft.nl",
"githubapp.com", "azurewebsites.net","azure-mobile.net","cloudapp.net",
"microsoftproductionstudios.org", "azure.com", "windowsazure.com", "bing.com", "bing.net", "bingtoolbar.com", "outlook.com",
"skype.com", "hotmail.com", "live.com", "sharepoint.com", "bingtoolbar.com", "internetexplorer.com", "onedrive.com",
"office.com","office365.com", "xbox.com", "visualstudio.com","microsoftvisualstudio.com", "microsoftonline.com",
"Surface.com", "Zone.com", "rare.com", "Sites.com", "msdn.com", "microsoftstream.com"
];
const GITHUB_DOMAINS = [
"github.com", "github.io", "githubusercontent.com"
];
MICROSOFT_DOMAINS = MICROSOFT_DOMAINS.concat(GITHUB_DOMAINS);
const MAC_ADDON_ID = "@testpilot-containers";
let macAddonEnabled = false;
let microsoftCookieStoreId = null;
let extensionSettings = {};
const canceledRequests = {};
const tabsWaitingToLoad = {};
const microsoftHostREs = [];
const githubHostREs = [];
const whitelistedHostREs = [];
const allowlistedHostREs = [];
async function isMACAddonEnabled () {
try {
const macAddonInfo = await browser.management.get(MAC_ADDON_ID);
if (macAddonInfo.enabled) {
return true;
}
} catch (e) {
return false;
}
return false;
}
async function setupMACAddonManagementListeners () {
browser.management.onInstalled.addListener(info => {
if (info.id === MAC_ADDON_ID) {
macAddonEnabled = true;
}
});
browser.management.onUninstalled.addListener(info => {
if (info.id === MAC_ADDON_ID) {
macAddonEnabled = false;
}
});
browser.management.onEnabled.addListener(info => {
if (info.id === MAC_ADDON_ID) {
macAddonEnabled = true;
}
});
browser.management.onDisabled.addListener(info => {
if (info.id === MAC_ADDON_ID) {
macAddonEnabled = false;
}
});
}
async function getMACAssignment (url) {
if (!macAddonEnabled) {
return false;
}
try {
const assignment = await browser.runtime.sendMessage(MAC_ADDON_ID, {
method: "getAssignment",
url
});
return assignment;
} catch (e) {
return false;
}
}
function cancelRequest (tab, options) {
// we decided to cancel the request at this point, register canceled request
canceledRequests[tab.id] = {
requestIds: {
[options.requestId]: true
},
urls: {
[options.url]: true
}
};
// since webRequest onCompleted and onErrorOccurred are not 100% reliable
// we register a timer here to cleanup canceled requests, just to make sure we don't
// end up in a situation where certain urls in a tab.id stay canceled
setTimeout(() => {
if (canceledRequests[tab.id]) {
delete canceledRequests[tab.id];
}
}, 2000);
}
function shouldCancelEarly (tab, options) {
// we decided to cancel the request at this point
if (!canceledRequests[tab.id]) {
cancelRequest(tab, options);
} else {
let cancelEarly = false;
if (canceledRequests[tab.id].requestIds[options.requestId] ||
canceledRequests[tab.id].urls[options.url]) {
// same requestId or url from the same tab
// this is a redirect that we have to cancel early to prevent opening two tabs
cancelEarly = true;
}
// register this requestId and url as canceled too
canceledRequests[tab.id].requestIds[options.requestId] = true;
canceledRequests[tab.id].urls[options.url] = true;
if (cancelEarly) {
return true;
}
}
return false;
}
function generateMicrosoftHostREs () {
const matchOperatorsRegex = /[|\\{}()[\]^$+*?.-]/g;
for (let microsoftDomain of MICROSOFT_DOMAINS) {
microsoftDomain = microsoftDomain.replace(matchOperatorsRegex, '\\$&');
microsoftHostREs.push(new RegExp(`(^|\\.)${microsoftDomain}$`));
}
for (let githubDomain of GITHUB_DOMAINS) {
githubDomain = githubDomain.replace(matchOperatorsRegex, '\\$&');
githubHostREs.push(new RegExp(`(^|\\.)${githubDomain}$`));
}
}
function generateWhitelistedHostREs () {
if (whitelistedHostREs.length != 0) {return;}
const matchOperatorsRegex = /[|\\{}()[\]^$+*?.-]/g;
for (let whitelistedDomain of extensionSettings.whitelist) {
whitelistedDomain = whitelistedDomain.replace(matchOperatorsRegex, '\\$&');
whitelistedHostREs.push(new RegExp(`(^|\\.)${whitelistedDomain}$`));
}
}
function generateAllowlistedHostREs () {
if (allowlistedHostREs.length != 0) {return;}
const matchOperatorsRegex = /[|\\{}()[\]^$+*?.-]/g;
for (let allowlistedDomain of extensionSettings.allowlist) {
allowlistedDomain = allowlistedDomain.replace(matchOperatorsRegex, '\\$&');
allowlistedHostREs.push(new RegExp(`(^|\\.)${allowlistedDomain}$`));
}
}
async function loadExtensionSettings () {
extensionSettings = await browser.storage.sync.get();
if (extensionSettings.whitelist === undefined){
extensionSettings.whitelist = "";
}
if (extensionSettings.allowlist === undefined){
extensionSettings.allowlist = "";
}
}
async function clearMicrosoftCookies () {
// Clear all microsoft cookies
const containers = await browser.contextualIdentities.query({});
containers.push({
cookieStoreId: "firefox-default"
});
let macAssignments = [];
if (macAddonEnabled) {
const promises = MICROSOFT_DOMAINS.map(async microsoftDomain => {
const assigned = await getMACAssignment(`https://${microsoftDomain}/`);
return assigned ? microsoftDomain : null;
});
macAssignments = await Promise.all(promises);
}
MICROSOFT_DOMAINS.map(async microsoftDomain => {
const microsoftCookieUrl = `https://${microsoftDomain}/`;
// dont clear cookies for microsoftDomain if mac assigned (with or without www.)
if (macAddonEnabled &&
(macAssignments.includes(microsoftDomain) ||
macAssignments.includes(`www.${microsoftDomain}`))) {
return;
}
containers.map(async container => {
const storeId = container.cookieStoreId;
if (storeId === microsoftCookieStoreId) {
// Don't clear cookies in the Microsoft Container
return;
}
const cookies = await browser.cookies.getAll({
domain: microsoftDomain,
storeId
});
cookies.map(cookie => {
browser.cookies.remove({
name: cookie.name,
url: microsoftCookieUrl,
storeId
});
});
});
});
}
async function setupContainer () {
// Use existing Microsoft container, or create one
const contexts = await browser.contextualIdentities.query({name: MICROSOFT_CONTAINER_NAME});
if (contexts.length > 0) {
microsoftCookieStoreId = contexts[0].cookieStoreId;
} else {
const context = await browser.contextualIdentities.create({
name: MICROSOFT_CONTAINER_NAME,
color: MICROSOFT_CONTAINER_COLOR,
icon: MICROSOFT_CONTAINER_ICON
});
microsoftCookieStoreId = context.cookieStoreId;
}
}
function reopenTab ({url, tab, cookieStoreId}) {
browser.tabs.create({
url,
cookieStoreId,
active: tab.active,
index: tab.index + 1,
windowId: tab.windowId
});
browser.tabs.remove(tab.id);
}
function isMicrosoftURL (url) {
const parsedUrl = new URL(url);
for (let microsoftHostRE of microsoftHostREs) {
if (microsoftHostRE.test(parsedUrl.hostname)) {
return true;
}
}
return false;
}
function isGitHubURL (url) {
const parsedUrl = new URL(url);
for (let githubHostRE of githubHostREs) {
if (githubHostRE.test(parsedUrl.hostname)) {
return true;
}
}
return false;
}
function isWhitelistedURL (url) {
generateWhitelistedHostREs();
const parsedUrl = new URL(url);
for (let whitelistedHostRE of whitelistedHostREs) {
if (whitelistedHostRE.test(parsedUrl.hostname)) {
return true;
}
}
return false;
}
function isAllowlistedURL (url) {
generateAllowlistedHostREs();
const parsedUrl = new URL(url);
for (let allowlistedHostRE of allowlistedHostREs) {
if (allowlistedHostRE.test(parsedUrl.hostname)) {
return true;
}
}
return false;
}
function shouldContainInto (url, tab) {
if (!url.startsWith("http")) {
// we only handle URLs starting with http(s)
return false;
}
let handleUrl = isMicrosoftURL(url) || (extensionSettings.allowlist.length!=0 && isAllowlistedURL(url));
if (handleUrl && extensionSettings.whitelist.length!=0 && isWhitelistedURL(url)) {
handleUrl = false;
}
if (handleUrl && extensionSettings.ignore_github && isGitHubURL(url)) {
handleUrl = false;
}
if (handleUrl) {
if (tab.cookieStoreId !== microsoftCookieStoreId) {
if (tab.cookieStoreId !== "firefox-default" && extensionSettings.dont_override_containers) {
// Tab is already in a container, the user doesn't want us to override containers
return false;
}
// Microsoft-URL outside of Microsoft Container Tab
// Should contain into Microsoft Container
return microsoftCookieStoreId;
}
} else if (tab.cookieStoreId === microsoftCookieStoreId) {
// Non-Microsoft-URL inside Microsoft Container Tab
// Should contain into Default Container
return "firefox-default";
}
return false;
}
async function maybeReopenAlreadyOpenTabs () {
const maybeReopenTab = async tab => {
const macAssigned = await getMACAssignment(tab.url);
if (macAssigned) {
// We don't reopen MAC assigned urls
return;
}
const cookieStoreId = shouldContainInto(tab.url, tab);
if (!cookieStoreId) {
// Tab doesn't need to be contained
return;
}
reopenTab({
url: tab.url,
tab,
cookieStoreId
});
};
const tabsOnUpdated = (tabId, changeInfo, tab) => {
if (changeInfo.url && tabsWaitingToLoad[tabId]) {
// Tab we're waiting for switched it's url, maybe we reopen
delete tabsWaitingToLoad[tabId];
maybeReopenTab(tab);
}
if (tab.status === "complete" && tabsWaitingToLoad[tabId]) {
// Tab we're waiting for completed loading
delete tabsWaitingToLoad[tabId];
}
if (!Object.keys(tabsWaitingToLoad).length) {
// We're done waiting for tabs to load, remove event listener
browser.tabs.onUpdated.removeListener(tabsOnUpdated);
}
};
// Query for already open Tabs
const tabs = await browser.tabs.query({});
tabs.map(async tab => {
if (tab.incognito) {
return;
}
if (tab.url === "about:blank") {
if (tab.status !== "loading") {
return;
}
// about:blank Tab is still loading, so we indicate that we wait for it to load
// and register the event listener if we haven't yet.
//
// This is a workaround until platform support is implemented:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1447551
// https://github.com/mozilla/multi-account-containers/issues/474
tabsWaitingToLoad[tab.id] = true;
if (!browser.tabs.onUpdated.hasListener(tabsOnUpdated)) {
browser.tabs.onUpdated.addListener(tabsOnUpdated);
}
} else {
// Tab already has an url, maybe we reopen
maybeReopenTab(tab);
}
});
}
async function containMicrosoft (options) {
// Listen to requests and open Microsoft into its Container,
// open other sites into the default tab context
if (options.tabId === -1) {
// Request doesn't belong to a tab
return;
}
if (tabsWaitingToLoad[options.tabId]) {
// Cleanup just to make sure we don't get a race-condition with startup reopening
delete tabsWaitingToLoad[options.tabId];
}
// We have to check with every request if the requested URL is assigned with MAC
// because the user can assign URLs at any given time (needs MAC Events)
const macAssigned = await getMACAssignment(options.url);
if (macAssigned) {
// This URL is assigned with MAC, so we don't handle this request
return;
}
const tab = await browser.tabs.get(options.tabId);
if (tab.incognito) {
// We don't handle incognito tabs
return;
}
// Check whether we should contain this request into another container
const cookieStoreId = shouldContainInto(options.url, tab);
if (!cookieStoreId) {
// Request doesn't need to be contained
return;
}
if (shouldCancelEarly(tab, options)) {
// We need to cancel early to prevent multiple reopenings
return {cancel: true};
}
// Decided to contain
reopenTab({
url: options.url,
tab,
cookieStoreId
});
return {cancel: true};
}
(async function init() {
await setupMACAddonManagementListeners();
macAddonEnabled = await isMACAddonEnabled();
try {
await setupContainer();
} catch (error) {
// TODO: Needs backup strategy
// See https://github.com/mozilla/contain-facebook/issues/23
// Sometimes this add-on is installed but doesn't get a microsoftCookieStoreId ?
// eslint-disable-next-line no-console
console.log(error);
return;
}
loadExtensionSettings();
clearMicrosoftCookies();
generateMicrosoftHostREs();
// Clean up canceled requests
browser.webRequest.onCompleted.addListener((options) => {
if (canceledRequests[options.tabId]) {
delete canceledRequests[options.tabId];
}
},{urls: ["<all_urls>"], types: ["main_frame"]});
browser.webRequest.onErrorOccurred.addListener((options) => {
if (canceledRequests[options.tabId]) {
delete canceledRequests[options.tabId];
}
},{urls: ["<all_urls>"], types: ["main_frame"]});
// Add the request listener
browser.webRequest.onBeforeRequest.addListener(containMicrosoft, {urls: ["<all_urls>"], types: ["main_frame"]}, ["blocking"]);
maybeReopenAlreadyOpenTabs();
})();