-
-
Notifications
You must be signed in to change notification settings - Fork 111
/
WhiteListService.js
125 lines (107 loc) · 4.4 KB
/
WhiteListService.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
const defaultWhiteList = "defaultWhiteList";
const greyPrefix = "-Grey";
class WhiteListService {
constructor(items, contextualIdentitiesEnabled = false, cache) {
this.cookieWhiteList = new Map();
// Sets up the whitelist for the map
if (contextualIdentitiesEnabled) {
cache.nameCacheMap.forEach((value, key, map) => {
let cookieStoreId = key;
if (items[cookieStoreId] !== undefined) {
this.cookieWhiteList.set(cookieStoreId, new Set(items[key]));
} else {
this.cookieWhiteList.set(cookieStoreId, new Set());
}
});
// let firefoxDefault = "firefox-default";
// if (items[firefoxDefault] !== undefined) {
// this.cookieWhiteList.set(firefoxDefault, new Set(items[firefoxDefault]));
// } else {
// this.cookieWhiteList.set(firefoxDefault, new Set());
// }
// contextualIdentitieies disabled
} else {
if (items[defaultWhiteList] !== undefined) {
this.cookieWhiteList.set(defaultWhiteList, new Set(items[defaultWhiteList]));
} else {
this.cookieWhiteList.set(defaultWhiteList, new Set());
}
if(items[defaultWhiteList + greyPrefix] !== undefined) {
this.cookieWhiteList.set(defaultWhiteList + greyPrefix, new Set(items[defaultWhiteList + greyPrefix]));
} else {
this.cookieWhiteList.set(defaultWhiteList + greyPrefix, new Set(items[defaultWhiteList + greyPrefix]));
}
}
}
// See if the set has the url depending on the cookieStoreId
hasHost(url, cookieStoreId = defaultWhiteList) {
if (!this.cookieWhiteList.has(cookieStoreId)) {
this.cookieWhiteList.set(cookieStoreId, new Set());
return false;
}
return this.cookieWhiteList.get(cookieStoreId).has(url);
}
hasHostSubdomain(cookieDomainHost, cookieBaseDomainHost, cookieStoreId = defaultWhiteList) {
return this.hasHost(cookieDomainHost, cookieStoreId) || this.hasHost(cookieBaseDomainHost, cookieStoreId);
}
hasHostInWhiteOrGrey(cookieDomainHost, cookieBaseDomainHost, cookieStoreId = defaultWhiteList) {
let otherList = this.returnOtherList(cookieStoreId);
return this.hasHostSubdomain(cookieDomainHost, cookieBaseDomainHost, cookieStoreId) || this.hasHostSubdomain(cookieDomainHost, cookieBaseDomainHost, otherList);
}
// Return the Set as an array
returnList(cookieStoreId = defaultWhiteList) {
if (!this.cookieWhiteList.has(cookieStoreId)) {
this.cookieWhiteList.set(cookieStoreId, new Set());
}
return Array.from(this.cookieWhiteList.get(cookieStoreId));
}
// Stores the set in the local storage of the browser as an array depending on the cookieStoreId
storeLocal(cookieStoreId = defaultWhiteList) {
let otherList = this.returnOtherList(cookieStoreId);
// console.log(cookieStoreId);
browser.storage.local.set({[cookieStoreId]: Array.from(this.cookieWhiteList.get(cookieStoreId))});
browser.storage.local.set({[otherList]: Array.from(this.cookieWhiteList.get(otherList))});
}
// Add the url to the set depending on the cookieStoreId
addURL(url, cookieStoreId = defaultWhiteList) {
if (!this.cookieWhiteList.has(cookieStoreId)) {
this.cookieWhiteList.set(cookieStoreId, new Set());
}
this.removeURLFromLists(url, cookieStoreId);
this.cookieWhiteList.get(cookieStoreId).add(url);
this.storeLocal(cookieStoreId);
}
// Remove the url from the set depending on the cookieStoreId
removeURL(url, cookieStoreId = defaultWhiteList) {
if (!this.cookieWhiteList.has(cookieStoreId)) {
this.cookieWhiteList.set(cookieStoreId, new Set());
return;
}
this.cookieWhiteList.get(cookieStoreId).delete(url);
this.storeLocal(cookieStoreId);
}
// Remove the url from the white and grey lists
removeURLFromLists(url, cookieStoreId = defaultWhiteList) {
let otherList = this.returnOtherList(cookieStoreId);
// console.log(this.cookieWhiteList);
// console.log(url);
// console.log(cookieStoreId + " " + otherList);
this.cookieWhiteList.get(cookieStoreId).delete(url);
this.cookieWhiteList.get(otherList).delete(url);
}
// returns the id of the other list depending if it was greylist or whitelist
returnOtherList(list) {
if(list.endsWith(greyPrefix)) {
return list.replace(greyPrefix, "");
}
return list + greyPrefix;
}
// Clears the set depending on the cookieStoreId
clearURL(cookieStoreId = defaultWhiteList) {
let otherList = this.returnOtherList(cookieStoreId);
this.cookieWhiteList.get(cookieStoreId).clear();
this.cookieWhiteList.get(otherList).clear();
this.storeLocal(cookieStoreId);
}
}
module.exports = WhiteListService;