@whotracksme/reporting 5.1.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +12 -0
- package/.eslintignore +3 -0
- package/.eslintrc +16 -0
- package/.github/workflows/release.yml +34 -0
- package/.github/workflows/tests.yml +20 -0
- package/.prettierrc +9 -0
- package/.tool-versions +1 -0
- package/CHANGELOG.md +795 -0
- package/LICENSE +373 -0
- package/README.md +1 -0
- package/communication/README.md +11 -0
- package/communication/example/index.js +43 -0
- package/communication/example/manifest.json +16 -0
- package/communication/karma.conf.cjs +19 -0
- package/communication/package.json +52 -0
- package/communication/rollup.common-config.cjs +17 -0
- package/communication/rollup.config.cjs +10 -0
- package/communication/src/encoding.js +54 -0
- package/communication/src/errors.js +84 -0
- package/communication/src/index.js +76 -0
- package/communication/src/logger.js +41 -0
- package/communication/src/padding.js +60 -0
- package/communication/src/proxied-http.js +231 -0
- package/communication/src/server-public-key-accessor.js +123 -0
- package/communication/src/timestamps.js +37 -0
- package/communication/src/trusted-clock.js +58 -0
- package/communication/src/utils.js +28 -0
- package/communication/src/zlib.js +17 -0
- package/communication/test/helpers/in-memory-database.js +43 -0
- package/communication/test/server-public-key-accessor.spec.js +135 -0
- package/communication/test/utils.spec.js +43 -0
- package/package.json +50 -0
- package/reporting/TODO.md +2 -0
- package/reporting/assets/request/bloom_filter.dat +0 -0
- package/reporting/assets/request/update.json +1 -0
- package/reporting/example/content.js +11 -0
- package/reporting/example/index.js +132 -0
- package/reporting/example/inspector/index.html +17 -0
- package/reporting/example/inspector/index.js +27 -0
- package/reporting/example/manifests/chromium.json +37 -0
- package/reporting/example/manifests/firefox.json +35 -0
- package/reporting/example/rules.json +62 -0
- package/reporting/example/setup.js +1 -0
- package/reporting/example/tests/test1.html +10 -0
- package/reporting/karma.conf.cjs +22 -0
- package/reporting/package.json +63 -0
- package/reporting/rollup.common-config.cjs +19 -0
- package/reporting/rollup.config.cjs +20 -0
- package/reporting/src/LRU.js +149 -0
- package/reporting/src/activity-estimator.js +298 -0
- package/reporting/src/alive-check.js +139 -0
- package/reporting/src/alive-message-generator.js +142 -0
- package/reporting/src/bloom-filters.js +345 -0
- package/reporting/src/country-provider.js +295 -0
- package/reporting/src/cyrb53.js +32 -0
- package/reporting/src/digest.js +33 -0
- package/reporting/src/doublefetch-page-handler.js +548 -0
- package/reporting/src/duplicate-detector.js +233 -0
- package/reporting/src/errors.js +84 -0
- package/reporting/src/event-listener-queue.js +121 -0
- package/reporting/src/fixed-size-cache.js +63 -0
- package/reporting/src/hash-detector.js +1526 -0
- package/reporting/src/html-parser.js +17 -0
- package/reporting/src/http.js +265 -0
- package/reporting/src/index.js +14 -0
- package/reporting/src/job-scheduler.js +912 -0
- package/reporting/src/logger.js +121 -0
- package/reporting/src/md5.js +334 -0
- package/reporting/src/message-sender.js +51 -0
- package/reporting/src/network.js +140 -0
- package/reporting/src/new-page-approver.js +119 -0
- package/reporting/src/page-aggregator.js +185 -0
- package/reporting/src/page-quorum-check-handler.js +200 -0
- package/reporting/src/page-structure.js +136 -0
- package/reporting/src/pagedb.js +671 -0
- package/reporting/src/pages.js +1798 -0
- package/reporting/src/patterns-updater.js +310 -0
- package/reporting/src/patterns.js +416 -0
- package/reporting/src/persisted-bitarray.js +265 -0
- package/reporting/src/persisted-hashes.js +189 -0
- package/reporting/src/quorum-checker.js +327 -0
- package/reporting/src/random.js +39 -0
- package/reporting/src/reporting.js +290 -0
- package/reporting/src/request/config.js +116 -0
- package/reporting/src/request/content-script.js +77 -0
- package/reporting/src/request/database.js +147 -0
- package/reporting/src/request/dnt.js +24 -0
- package/reporting/src/request/hash.js +101 -0
- package/reporting/src/request/index.js +874 -0
- package/reporting/src/request/page-telemetry.js +42 -0
- package/reporting/src/request/qs-whitelist2.js +247 -0
- package/reporting/src/request/resources/prob.js +601 -0
- package/reporting/src/request/steps/block-rules.js +40 -0
- package/reporting/src/request/steps/check-context.js +37 -0
- package/reporting/src/request/steps/cookie-context.js +209 -0
- package/reporting/src/request/steps/oauth-detector.js +101 -0
- package/reporting/src/request/steps/page-logger.js +177 -0
- package/reporting/src/request/steps/redirect-tagger.js +75 -0
- package/reporting/src/request/steps/subdomain-check.js +34 -0
- package/reporting/src/request/steps/token-checker/index.js +189 -0
- package/reporting/src/request/steps/token-checker/token-domain.js +129 -0
- package/reporting/src/request/steps/token-examiner.js +182 -0
- package/reporting/src/request/steps/token-telemetry/cached-entry-pipeline.js +210 -0
- package/reporting/src/request/steps/token-telemetry/index.js +234 -0
- package/reporting/src/request/steps/token-telemetry/key-pipeline.js +109 -0
- package/reporting/src/request/steps/token-telemetry/token-pipeline.js +105 -0
- package/reporting/src/request/time.js +73 -0
- package/reporting/src/request/utils/bloom-filter-packed.js +107 -0
- package/reporting/src/request/utils/chrome-storage-map.js +282 -0
- package/reporting/src/request/utils/subject.js +37 -0
- package/reporting/src/request/utils/temp-set.js +56 -0
- package/reporting/src/request/utils/tlds.js +55 -0
- package/reporting/src/request/utils.js +41 -0
- package/reporting/src/request-reporter.js +111 -0
- package/reporting/src/sanitizer.js +519 -0
- package/reporting/src/search-extractor.js +338 -0
- package/reporting/src/self-check.js +105 -0
- package/reporting/src/seq-executor.js +43 -0
- package/reporting/src/session-storage.js +399 -0
- package/reporting/src/url-analyzer.js +161 -0
- package/reporting/src/url-cleaner.js +60 -0
- package/reporting/src/utils/pacemaker.js +279 -0
- package/reporting/src/utils/url.js +114 -0
- package/reporting/src/utils.js +345 -0
- package/reporting/src/webrequest-pipeline/cname-uncloak.js +110 -0
- package/reporting/src/webrequest-pipeline/index.js +287 -0
- package/reporting/src/webrequest-pipeline/logger.js +14 -0
- package/reporting/src/webrequest-pipeline/page-store.js +282 -0
- package/reporting/src/webrequest-pipeline/page.js +140 -0
- package/reporting/src/webrequest-pipeline/pipeline.js +249 -0
- package/reporting/src/webrequest-pipeline/utils/webrequest.js +113 -0
- package/reporting/src/webrequest-pipeline/webrequest-context.js +153 -0
- package/reporting/test/README.md +2 -0
- package/reporting/test/activity-estimator.spec.js +356 -0
- package/reporting/test/alive-check.spec.js +156 -0
- package/reporting/test/alive-message-generator.spec.js +186 -0
- package/reporting/test/country-provider.spec.js +263 -0
- package/reporting/test/doublefetch-page-handler.spec.js +818 -0
- package/reporting/test/duplicate-detector.spec.js +689 -0
- package/reporting/test/helpers/dom-parsers.js +41 -0
- package/reporting/test/helpers/fake-session-storage.js +69 -0
- package/reporting/test/helpers/fast-check-utils.js +28 -0
- package/reporting/test/helpers/fetch-mock.js +57 -0
- package/reporting/test/helpers/in-memory-database.js +14 -0
- package/reporting/test/helpers/memory-map.js +72 -0
- package/reporting/test/helpers/pagedb-mocks.js +41 -0
- package/reporting/test/index.js +47 -0
- package/reporting/test/job-scheduler.spec.js +691 -0
- package/reporting/test/network.spec.js +72 -0
- package/reporting/test/page-aggregator.spec.js +104 -0
- package/reporting/test/page-quorum-check-handler.spec.js +86 -0
- package/reporting/test/pagedb.spec.js +250 -0
- package/reporting/test/pages.spec.js +693 -0
- package/reporting/test/patterns-updater.spec.js +446 -0
- package/reporting/test/patterns.spec.js +751 -0
- package/reporting/test/persisted-hashes.spec.js +169 -0
- package/reporting/test/reporting.spec.js +205 -0
- package/reporting/test/request/database.spec.js +59 -0
- package/reporting/test/request/hash.spec.js +108 -0
- package/reporting/test/request/index.spec.js +378 -0
- package/reporting/test/request/qs-whitelist2.spec.js +189 -0
- package/reporting/test/request/steps/check-context.spec.js +51 -0
- package/reporting/test/request/steps/oauth-detector.spec.js +339 -0
- package/reporting/test/request/test-pages.js +1246 -0
- package/reporting/test/request/utils/bloom-filter-packed.spec.js +37 -0
- package/reporting/test/request/utils/chrome-storage-map.spec.js +28 -0
- package/reporting/test/request/utils/tldts.spec.js +62 -0
- package/reporting/test/request/utils.spec.js +168 -0
- package/reporting/test/sanitizer.spec.js +1087 -0
- package/reporting/test/search-extractor.spec.js +590 -0
- package/reporting/test/seq-executor.spec.js +158 -0
- package/reporting/test/session-storage.spec.js +400 -0
- package/reporting/test/setup.js +24 -0
- package/reporting/test/url-analyzer.spec.js +451 -0
- package/reporting/test/url-cleaner.spec.js +102 -0
- package/reporting/test/utils/pacemaker.spec.js +222 -0
- package/reporting/test/utils/url.spec.js +660 -0
- package/reporting/test/utils.spec.js +299 -0
- package/reporting/test/webrequest-pipeline/pipeline.spec.js +251 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import logger from './logger';
|
|
13
|
+
import SeqExecutor from './seq-executor';
|
|
14
|
+
import { intersectMapKeys } from './utils';
|
|
15
|
+
import SelfCheck from './self-check';
|
|
16
|
+
|
|
17
|
+
const SECOND = 1000;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Stub for chrome.storage.session that won't persist. Note that in an
|
|
21
|
+
* environment with persistent background scripts, this implementation
|
|
22
|
+
* will be even superior to using the real API.
|
|
23
|
+
*/
|
|
24
|
+
const noopSessionStorage = {
|
|
25
|
+
async get() {
|
|
26
|
+
return {};
|
|
27
|
+
},
|
|
28
|
+
|
|
29
|
+
async set() {
|
|
30
|
+
// do nothing
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
async remove() {
|
|
34
|
+
// do nothing
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
async clear() {
|
|
38
|
+
// do nothing
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Helper class to do the necessary bookkeeping to write to session storage
|
|
44
|
+
* in batched operations. By batching updates, we can keep the number of
|
|
45
|
+
* API calls low and avoid exceeding browser specific quotas (writes per hour).
|
|
46
|
+
*/
|
|
47
|
+
class DirtyKeys {
|
|
48
|
+
constructor() {
|
|
49
|
+
this.addedKeys = new Set();
|
|
50
|
+
this.removedKeys = new Set();
|
|
51
|
+
this.markedDirtyAt = null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get hasPendingUpdates() {
|
|
55
|
+
return this.markedDirtyAt !== null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
scheduleAdd(key) {
|
|
59
|
+
this.addedKeys.add(key);
|
|
60
|
+
this.removedKeys.delete(key);
|
|
61
|
+
this.markedDirtyAt = this.markedDirtyAt || Date.now();
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
scheduleRemoval(key) {
|
|
65
|
+
this.addedKeys.delete(key);
|
|
66
|
+
this.removedKeys.add(key);
|
|
67
|
+
this.markedDirtyAt = this.markedDirtyAt || Date.now();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
scheduleClear(allPresentKeys) {
|
|
71
|
+
this.addedKeys.clear();
|
|
72
|
+
allPresentKeys.forEach((key) => this.removedKeys.add(key));
|
|
73
|
+
if (this.addedKeys.size + this.removedKeys.size > 0) {
|
|
74
|
+
this.markedDirtyAt = this.markedDirtyAt || Date.now();
|
|
75
|
+
} else {
|
|
76
|
+
this.markedDirtyAt = null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
prepareUpdate() {
|
|
81
|
+
const keysToAdd = [...this.addedKeys];
|
|
82
|
+
const keysToRemove = [...this.removedKeys];
|
|
83
|
+
const markedDirtyAt = this.markedDirtyAt;
|
|
84
|
+
this.addedKeys.clear();
|
|
85
|
+
this.removedKeys.clear();
|
|
86
|
+
this.markedDirtyAt = null;
|
|
87
|
+
return { keysToAdd, keysToRemove, markedDirtyAt };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* If the update failed, we can use the following error recover strategy:
|
|
92
|
+
* - reschedule all remove operations again (unless the key has been readded later)
|
|
93
|
+
* - mark all existing keys to be added (forcing a full-sync)
|
|
94
|
+
* - restore the dirty flag to the time before the update
|
|
95
|
+
*/
|
|
96
|
+
rollbackAfterFailedUpdate({
|
|
97
|
+
allPresentKeys,
|
|
98
|
+
uncompletedRemovals,
|
|
99
|
+
markedDirtyAt,
|
|
100
|
+
}) {
|
|
101
|
+
uncompletedRemovals.forEach((key) => {
|
|
102
|
+
this.removedKeys.add(key);
|
|
103
|
+
});
|
|
104
|
+
allPresentKeys.forEach((key) => {
|
|
105
|
+
this.addedKeys.add(key);
|
|
106
|
+
this.removedKeys.delete(key);
|
|
107
|
+
});
|
|
108
|
+
if (this.addedKeys.size + this.removedKeys.size > 0) {
|
|
109
|
+
this.markedDirtyAt = markedDirtyAt;
|
|
110
|
+
} else {
|
|
111
|
+
this.markedDirtyAt = null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* This class is a wrapper around chrome.storage.session
|
|
118
|
+
* (https://developer.chrome.com/docs/extensions/reference/storage/#property-session).
|
|
119
|
+
* Use it to store data that does not need to be persisted on disk across browser
|
|
120
|
+
* restarts.
|
|
121
|
+
*
|
|
122
|
+
* Why not directly use the session API? The wrapper is intended to centralize
|
|
123
|
+
* workarounds to handle limitations of the API:
|
|
124
|
+
* - (If needed) lift quota limits of 8K per object and 1M (or 10M) in total
|
|
125
|
+
* (Note clear if we hit those, but in principle you could workaround by
|
|
126
|
+
* storing only a key in memory, persist encrypted data on disk, and purge
|
|
127
|
+
* leftover on the next startup.)
|
|
128
|
+
* - Fallback to a pure in-memory implementation if the API is not available
|
|
129
|
+
*
|
|
130
|
+
* For convenience, also expose simplified synchronized APIs (but enforces an
|
|
131
|
+
* initial sync in "init").
|
|
132
|
+
*/
|
|
133
|
+
export default class SessionStorageWrapper {
|
|
134
|
+
constructor({
|
|
135
|
+
sessionApi = typeof chrome !== 'undefined' && chrome?.storage?.session,
|
|
136
|
+
namespace = 'wtm::reporting::default',
|
|
137
|
+
version = 1,
|
|
138
|
+
} = {}) {
|
|
139
|
+
this.prefix = `${namespace}::v${version}::`;
|
|
140
|
+
this.data = null;
|
|
141
|
+
this.sessionApi = sessionApi || noopSessionStorage;
|
|
142
|
+
|
|
143
|
+
this._initExecutor = new SeqExecutor();
|
|
144
|
+
this._dirtyKeys = new DirtyKeys();
|
|
145
|
+
this._pendingFlush = null;
|
|
146
|
+
this._flushExecutor = new SeqExecutor();
|
|
147
|
+
this._flushExecutionInProgress = null;
|
|
148
|
+
|
|
149
|
+
// These values control the flush logic. It tries to find a balance
|
|
150
|
+
// between aggregating values and but providing guarantees that
|
|
151
|
+
// changes will be eventually written. The guarantee comes from the
|
|
152
|
+
// hard limit: if the hard limit is exceeded, a flush will be forced.
|
|
153
|
+
// The other (soft) limit will be extended after each change. Changes
|
|
154
|
+
// typically come in bursts, so trying to time the end of the burst
|
|
155
|
+
// should be a reasonable strategy.
|
|
156
|
+
this._minFlushIntervalInMs = 50;
|
|
157
|
+
this._hardFlushIntervalInMs = 300;
|
|
158
|
+
|
|
159
|
+
this.verboseLogging = false;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
isReady() {
|
|
163
|
+
return this.data !== null;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async init() {
|
|
167
|
+
if (!this.isReady()) {
|
|
168
|
+
await this._initExecutor.run(async () => {
|
|
169
|
+
if (!this.data) {
|
|
170
|
+
const allEntries = await this.sessionApi.get();
|
|
171
|
+
this.data = Object.fromEntries(
|
|
172
|
+
Object.entries(allEntries)
|
|
173
|
+
.filter(([key]) => this._isInternalKey(key))
|
|
174
|
+
.map(([key, value]) => [this._fromInternalKey(key), value]),
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
has(key) {
|
|
182
|
+
return Object.hasOwn(this.data, key);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
get(key) {
|
|
186
|
+
return this.data[key];
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
set(key, value) {
|
|
190
|
+
this.setItems({ [key]: value });
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
remove(key) {
|
|
194
|
+
if (this.has(key)) {
|
|
195
|
+
delete this.data[key];
|
|
196
|
+
this._dirtyKeys.scheduleRemoval(key);
|
|
197
|
+
this._scheduleFlush();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Note: clears all manages keys, but leaves the others intact.
|
|
202
|
+
// Thus, it is not identical to chrome.storage.session.clear.
|
|
203
|
+
clear() {
|
|
204
|
+
this._ensureInit();
|
|
205
|
+
const keys = Object.keys(this.data);
|
|
206
|
+
if (keys.length > 0) {
|
|
207
|
+
this.data = {};
|
|
208
|
+
this._dirtyKeys.scheduleClear(keys);
|
|
209
|
+
this._scheduleFlush();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
getEntries(filter) {
|
|
214
|
+
if (filter) {
|
|
215
|
+
const shouldInclude = Array.isArray(filter)
|
|
216
|
+
? (key) => filter.includes(key)
|
|
217
|
+
: filter;
|
|
218
|
+
if (typeof filter !== 'function') {
|
|
219
|
+
throw new Error(
|
|
220
|
+
'Illegal argument: "filter" should be a function or a list of keys',
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
return Object.fromEntries(
|
|
224
|
+
Object.entries(this.data).filter(([key]) => shouldInclude(key)),
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
return { ...this.data };
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
setItems(items) {
|
|
231
|
+
this._ensureInit();
|
|
232
|
+
for (const [key, value] of Object.entries(items)) {
|
|
233
|
+
this.data[key] = value;
|
|
234
|
+
this._dirtyKeys.scheduleAdd(key);
|
|
235
|
+
}
|
|
236
|
+
this._scheduleFlush();
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
replaceItems(items) {
|
|
240
|
+
this.clear();
|
|
241
|
+
this.setItems(items);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
_scheduleFlush() {
|
|
245
|
+
if (!this._dirtyKeys.hasPendingUpdates) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const now = Date.now();
|
|
250
|
+
let elapsedSinceLastFlush = now - this._dirtyKeys.markedDirtyAt;
|
|
251
|
+
if (elapsedSinceLastFlush < 0) {
|
|
252
|
+
logger.warn(
|
|
253
|
+
`Clock jumped from ${this._dirtyKeys.markedDirtyAt} back to ${now}. Forcing flush...`,
|
|
254
|
+
);
|
|
255
|
+
elapsedSinceLastFlush = this._hardFlushIntervalInMs;
|
|
256
|
+
}
|
|
257
|
+
const timeoutInMs = Math.max(
|
|
258
|
+
Math.min(
|
|
259
|
+
this._minFlushIntervalInMs,
|
|
260
|
+
this._hardFlushIntervalInMs - elapsedSinceLastFlush,
|
|
261
|
+
),
|
|
262
|
+
0,
|
|
263
|
+
);
|
|
264
|
+
clearTimeout(this._pendingFlush);
|
|
265
|
+
this._pendingFlush = setTimeout(() => {
|
|
266
|
+
this._pendingFlush = null;
|
|
267
|
+
this.flush().catch((e) => {
|
|
268
|
+
logger.warn('Failed to update chrome.storage.session.', e);
|
|
269
|
+
this._scheduleFlush();
|
|
270
|
+
});
|
|
271
|
+
}, timeoutInMs);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
async flush() {
|
|
275
|
+
this._ensureInit();
|
|
276
|
+
await this._flushExecutor.run(async () => {
|
|
277
|
+
try {
|
|
278
|
+
if (this._dirtyKeys.hasPendingUpdates) {
|
|
279
|
+
this._flushExecutionInProgress = this._dirtyKeys.prepareUpdate();
|
|
280
|
+
const { keysToAdd, keysToRemove, markedDirtyAt } =
|
|
281
|
+
this._flushExecutionInProgress;
|
|
282
|
+
try {
|
|
283
|
+
const pending = [];
|
|
284
|
+
if (keysToAdd.length > 0) {
|
|
285
|
+
const changeSet = Object.fromEntries(
|
|
286
|
+
keysToAdd.map((key) => [
|
|
287
|
+
this._toInternalKey(key),
|
|
288
|
+
this.data[key],
|
|
289
|
+
]),
|
|
290
|
+
);
|
|
291
|
+
pending.push(this.sessionApi.set(changeSet));
|
|
292
|
+
}
|
|
293
|
+
if (keysToRemove.length > 0) {
|
|
294
|
+
const keys = keysToRemove.map((key) => this._toInternalKey(key));
|
|
295
|
+
pending.push(this.sessionApi.remove(keys));
|
|
296
|
+
}
|
|
297
|
+
await Promise.all(pending);
|
|
298
|
+
if (this.verboseLogging) {
|
|
299
|
+
logger.debug(
|
|
300
|
+
'Updated chrome.storage.session:',
|
|
301
|
+
keysToAdd.length,
|
|
302
|
+
'keys added,',
|
|
303
|
+
keysToRemove.length,
|
|
304
|
+
'keys removed',
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
} catch (e) {
|
|
308
|
+
// Note: may also happen if the browser starts throtteling the API.
|
|
309
|
+
// To recover, mark the whole state as modified to force a full sync.
|
|
310
|
+
logger.error(
|
|
311
|
+
'Unable to update chrome.storage.session.',
|
|
312
|
+
'For error recovery, mark all keys as dirty (forces a full sync next time).',
|
|
313
|
+
e,
|
|
314
|
+
);
|
|
315
|
+
this._dirtyKeys.rollbackAfterFailedUpdate({
|
|
316
|
+
allPresentKeys: Object.keys(this.data),
|
|
317
|
+
uncompletedRemovals: keysToRemove,
|
|
318
|
+
markedDirtyAt,
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
} finally {
|
|
323
|
+
this._flushExecutionInProgress = null;
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
_ensureInit() {
|
|
329
|
+
if (!this.data) {
|
|
330
|
+
throw new Error('Illegal state: "init" must be called first');
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
_isInternalKey(key) {
|
|
335
|
+
return key.startsWith(this.prefix);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
_toInternalKey(key) {
|
|
339
|
+
return this.prefix + key;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
_fromInternalKey(key) {
|
|
343
|
+
return key.slice(this.prefix.length);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
_hasPendingUpdates() {
|
|
347
|
+
return this._addedKeys.size + this._removedKeys.size > 0;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
async selfChecks(check = new SelfCheck()) {
|
|
351
|
+
if (this._dirtyKeys.markedDirtyAt) {
|
|
352
|
+
const now = Date.now();
|
|
353
|
+
if (this._dirtyKeys.markedDirtyAt + 5 * SECOND < now) {
|
|
354
|
+
check.fail('Unwritten changes for more than 5 seconds');
|
|
355
|
+
} else if (this._dirtyKeys.markedDirtyAt + SECOND < now) {
|
|
356
|
+
check.warn('Unwritten changes for more than one second');
|
|
357
|
+
}
|
|
358
|
+
} else {
|
|
359
|
+
check.pass('Local state in sync with session storage');
|
|
360
|
+
}
|
|
361
|
+
if (this._dirtyKeys.hasPendingUpdates && !this._pendingFlush) {
|
|
362
|
+
check.warn('Out of sync with the session, but no flush is scheduled.');
|
|
363
|
+
}
|
|
364
|
+
if (this._flushExecutionInProgress) {
|
|
365
|
+
check.warn(
|
|
366
|
+
'Pending flush operation (this may happen, but only rarely)',
|
|
367
|
+
this._flushExecutionInProgress,
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
const duplicatedKeys = intersectMapKeys(
|
|
372
|
+
this._dirtyKeys.addedKeys,
|
|
373
|
+
this._dirtyKeys.removedKeys,
|
|
374
|
+
);
|
|
375
|
+
if (duplicatedKeys.size > 0) {
|
|
376
|
+
check.fail('Data corruption: keys marked as both added and removed', {
|
|
377
|
+
duplicatedKeys,
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
const vanishedAddedKeys = [...this._dirtyKeys.addedKeys].filter(
|
|
381
|
+
(key) => !this.has(key),
|
|
382
|
+
);
|
|
383
|
+
if (vanishedAddedKeys.length > 0) {
|
|
384
|
+
check.fail('Data corruption: added keys vanished', {
|
|
385
|
+
vanishedAddedKeys,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
const zombieKeys = [...this._dirtyKeys.removedKeys].filter((key) =>
|
|
389
|
+
this.has(key),
|
|
390
|
+
);
|
|
391
|
+
if (zombieKeys.length > 0) {
|
|
392
|
+
check.fail('Data corruption: removed keys still present', {
|
|
393
|
+
zombieKeys,
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
return check;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { ImmutableURL } from '@cliqz/url-parser';
|
|
13
|
+
import logger from './logger';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Can be used to add search engines. Detecting search engines enables
|
|
17
|
+
* some optimizations (e.g. quorum checks can be skipped).
|
|
18
|
+
*/
|
|
19
|
+
const URL_PATTERNS = [
|
|
20
|
+
{
|
|
21
|
+
category: 'search-goi',
|
|
22
|
+
regexp:
|
|
23
|
+
/^https:[/][/][^/]*[.]google[.].*?[#?&;]((q=[^&]+&([^&]+&)*udm=2)|(udm=2&([^&]+&)*q=[^&]+))/,
|
|
24
|
+
prefix: 'search?udm=2&q=',
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
category: 'search-gov',
|
|
28
|
+
regexp:
|
|
29
|
+
/^https:[/][/][^/]*[.]google[.].*?[#?&;]((q=[^&]+&([^&]+&)*tbm=vid)|(tbm=vid&([^&]+&)*q=[^&]+))/,
|
|
30
|
+
prefix: 'search?tbm=vid&q=',
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
category: 'search-go',
|
|
34
|
+
regexp: /^https:[/][/][^/]*[.]google[.].*?[#?&;]/,
|
|
35
|
+
prefix: 'search?q=',
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
category: 'search-ya',
|
|
39
|
+
regexp: /^https:[/][/][^/]*[.]search[.]yahoo[.].*?[#?&;][pq]=[^$&]+/,
|
|
40
|
+
prefix: 'search?q=',
|
|
41
|
+
queryFinder(parsedUrl) {
|
|
42
|
+
return parsedUrl.searchParams.get('q') || parsedUrl.searchParams.get('p');
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
category: 'search-bii',
|
|
47
|
+
regexp: /^https:[/][/][^/]*[.]bing[.][^/]+[/]images[/]search[?]q=[^$&]+/,
|
|
48
|
+
prefix: 'images/search?q=',
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
category: 'search-bi',
|
|
52
|
+
regexp: /^https:[/][/][^/]*[.]bing[.].*?[#?&;]q=[^$&]+/,
|
|
53
|
+
prefix: 'search?q=',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
category: 'search-am',
|
|
57
|
+
regexp:
|
|
58
|
+
/^https:[/][/][^/]*[.]amazon[.][^/]+[/](s[?]k=[^$&]+|.*[?&]field-keywords=[^$&]+)/,
|
|
59
|
+
prefix: 's/?field-keywords=',
|
|
60
|
+
queryFinder(parsedUrl) {
|
|
61
|
+
return (
|
|
62
|
+
parsedUrl.searchParams.get('field-keywords') ||
|
|
63
|
+
parsedUrl.searchParams.get('k')
|
|
64
|
+
);
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
category: 'search-dd',
|
|
69
|
+
regexp:
|
|
70
|
+
/^https:[/][/]duckduckgo[.]com[/](?:html$|.*[?&]q=[^&]+.*&ia=web|[?]q=[^&]+$)/,
|
|
71
|
+
prefix: '?q=',
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
category: 'search-gh',
|
|
75
|
+
regexp: /^https:[/][/](glowstery|ghosterysearch)[.]com[/]search[?]q=[^&]+/,
|
|
76
|
+
prefix: 'search?q=',
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
category: 'search-ghi',
|
|
80
|
+
regexp: /^https:[/][/](glowstery|ghosterysearch)[.]com[/]images[?]q=[^&]+/,
|
|
81
|
+
prefix: 'search?q=',
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
category: 'search-ghv',
|
|
85
|
+
regexp: /^https:[/][/](glowstery|ghosterysearch)[.]com[/]videos[?]q=[^&]+/,
|
|
86
|
+
prefix: 'search?q=',
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
category: 'search-br',
|
|
90
|
+
regexp: /^https:[/][/]search[.]brave[.]com[/]search[?]q=[^&]+/,
|
|
91
|
+
prefix: 'search?q=',
|
|
92
|
+
},
|
|
93
|
+
{
|
|
94
|
+
category: 'search-bri',
|
|
95
|
+
regexp: /^https:[/][/]search[.]brave[.]com[/]images[?]q=[^&]+/,
|
|
96
|
+
prefix: 'images?q=',
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
category: 'search-brn',
|
|
100
|
+
regexp: /^https:[/][/]search[.]brave[.]com[/]news[?]q=[^&]+/,
|
|
101
|
+
prefix: 'news?q=',
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
category: 'search-brv',
|
|
105
|
+
regexp: /^https:[/][/]search[.]brave[.]com[/]videos[?]q=[^&]+/,
|
|
106
|
+
prefix: 'videos?q=',
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
category: 'search-ec',
|
|
110
|
+
regexp:
|
|
111
|
+
/^https:[/][/]www[.]ecosia[.]org[/]search[?](?:method=index&)?q=[^&]+/,
|
|
112
|
+
prefix: 'search?q=',
|
|
113
|
+
},
|
|
114
|
+
];
|
|
115
|
+
|
|
116
|
+
export default class UrlAnalyzer {
|
|
117
|
+
constructor(patterns) {
|
|
118
|
+
this.patterns = patterns;
|
|
119
|
+
this._urlPatterns = URL_PATTERNS;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
parseSearchLinks(url) {
|
|
123
|
+
for (const {
|
|
124
|
+
category,
|
|
125
|
+
regexp,
|
|
126
|
+
prefix,
|
|
127
|
+
queryFinder = (parsedUrl) => parsedUrl.searchParams.get('q'),
|
|
128
|
+
} of this._urlPatterns) {
|
|
129
|
+
if (regexp.test(url)) {
|
|
130
|
+
// Workaround for an encoding issue (source: https://stackoverflow.com/a/24417399/783510).
|
|
131
|
+
// Reason: we want to preserve the original search term. In other words, searches
|
|
132
|
+
// for "abc def" and "abc+def" should be distinguishable. That is why we need to
|
|
133
|
+
// avoid the ambigious '+' character and use explicit white space encoding.
|
|
134
|
+
const url_ = url.replaceAll('+', '%20');
|
|
135
|
+
const parsedUrl = new ImmutableURL(url_);
|
|
136
|
+
|
|
137
|
+
const query = queryFinder(parsedUrl);
|
|
138
|
+
if (!query) {
|
|
139
|
+
return { isSupported: false };
|
|
140
|
+
}
|
|
141
|
+
const query_ = encodeURIComponent(query).replaceAll('%20', '+');
|
|
142
|
+
const doublefetchUrl = `https://${parsedUrl.host}/${prefix}${query_}`;
|
|
143
|
+
const doublefetchRequest = this.patterns.createDoublefetchRequest(
|
|
144
|
+
category,
|
|
145
|
+
doublefetchUrl,
|
|
146
|
+
);
|
|
147
|
+
if (!doublefetchRequest) {
|
|
148
|
+
logger.debug(
|
|
149
|
+
'Matching rule for',
|
|
150
|
+
url,
|
|
151
|
+
'skipped (no matching server side rules exist)',
|
|
152
|
+
);
|
|
153
|
+
return { isSupported: false, category, query };
|
|
154
|
+
}
|
|
155
|
+
return { isSupported: true, category, query, doublefetchRequest };
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return { isSupported: false };
|
|
160
|
+
}
|
|
161
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { split0 } from './utils';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Given a URL and a list of query parameters, it returns an
|
|
16
|
+
* equivalent URL, but with those query parameters removed.
|
|
17
|
+
*
|
|
18
|
+
* Note: this function will not do any decoding. Instead, it will try
|
|
19
|
+
* to preserve the original URL as best as it can (e.g. the invalid URL
|
|
20
|
+
* "https://example.test?q=x y" will not be normalized to the valid URL
|
|
21
|
+
* "https://example.test/?q=x%20y").
|
|
22
|
+
*/
|
|
23
|
+
export function removeQueryParams(url, queryParams) {
|
|
24
|
+
const searchStart = url.indexOf('?');
|
|
25
|
+
if (searchStart === -1) {
|
|
26
|
+
return url;
|
|
27
|
+
}
|
|
28
|
+
const searchEnd = url.indexOf('#', searchStart + 1);
|
|
29
|
+
const search =
|
|
30
|
+
searchEnd === -1
|
|
31
|
+
? url.slice(searchStart + 1)
|
|
32
|
+
: url.slice(searchStart + 1, searchEnd);
|
|
33
|
+
if (!search) {
|
|
34
|
+
return url;
|
|
35
|
+
}
|
|
36
|
+
const parts = search
|
|
37
|
+
.split('&')
|
|
38
|
+
.filter((x) => !queryParams.includes(split0(x, '=')));
|
|
39
|
+
const beforeSearch = url.slice(0, searchStart);
|
|
40
|
+
|
|
41
|
+
const hash = searchEnd === -1 ? '' : url.slice(searchEnd);
|
|
42
|
+
if (parts.length === 0) {
|
|
43
|
+
return beforeSearch + hash;
|
|
44
|
+
} else {
|
|
45
|
+
return `${beforeSearch}?${parts.join('&')}${hash}`;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Given a URL, it returns an equivalent URL, but with the hash removed.
|
|
51
|
+
* If the URL did not have a hash, the unmodifed URL will be returned.
|
|
52
|
+
*
|
|
53
|
+
* Note: this function will not do any decoding. Instead, it will try
|
|
54
|
+
* to preserve the original URL as best as it can (e.g. the invalid URL
|
|
55
|
+
* "https://example.test?q=x y" will not be normalized to the valid URL
|
|
56
|
+
* "https://example.test/?q=x%20y").
|
|
57
|
+
*/
|
|
58
|
+
export function removeSearchHash(url) {
|
|
59
|
+
return split0(url, '#');
|
|
60
|
+
}
|