@whotracksme/reporting 5.1.28 → 5.1.30
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/CHANGELOG.md +37 -0
- package/package.json +1 -1
- package/reporting/example/index.js +2 -12
- package/reporting/example/manifests/chromium.json +1 -0
- package/reporting/snapshots/0001/events.log.br +0 -0
- package/reporting/snapshots/0001/snapshot.json +4173 -0
- package/reporting/snapshots/0002/events.log.br +0 -0
- package/reporting/snapshots/0002/snapshot.json +2539 -0
- package/reporting/snapshots/0003/events.log.br +0 -0
- package/reporting/snapshots/0003/snapshot.json +855 -0
- package/reporting/snapshots/0004/events.log.br +0 -0
- package/reporting/snapshots/0004/snapshot.json +802 -0
- package/reporting/src/hash-detector.js +1 -1
- package/reporting/src/index.js +1 -2
- package/reporting/src/request/config.js +9 -4
- package/reporting/src/request/{hash.js → hash/index.js} +3 -3
- package/reporting/src/request/index.js +447 -496
- package/reporting/src/{webrequest-pipeline → request}/page-store.js +65 -75
- package/reporting/src/request/qs-whitelist2.js +0 -2
- package/reporting/src/request/steps/cookie-context.js +6 -5
- package/reporting/src/request/steps/oauth-detector.js +3 -24
- package/reporting/src/request/steps/page-logger.js +3 -4
- package/reporting/src/request/steps/token-checker/token-domain.js +1 -1
- package/reporting/src/request/steps/token-examiner.js +3 -4
- package/reporting/src/request/steps/token-telemetry/cached-entry-pipeline.js +55 -75
- package/reporting/src/request/steps/token-telemetry/index.js +86 -115
- package/reporting/src/request/steps/token-telemetry/key-pipeline.js +3 -2
- package/reporting/src/request/steps/token-telemetry/token-pipeline.js +3 -2
- package/reporting/src/request/utils/temp-set.js +2 -4
- package/reporting/src/request/{time.js → utils/time.js} +0 -5
- package/reporting/src/request/utils/tlds.js +1 -11
- package/reporting/src/request/{utils.js → utils/utils.js} +9 -0
- package/reporting/src/{webrequest-pipeline/webrequest-context.js → request/utils/webrequest.js} +84 -4
- package/reporting/src/utils/url.js +0 -25
- package/reporting/test/helpers/scenarios.js +76 -17
- package/reporting/test/index.js +3 -8
- package/reporting/test/request/database.test.js +96 -0
- package/reporting/test/request/{hash.spec.js → hash/index.spec.js} +2 -2
- package/reporting/test/request/index.spec.js +24 -46
- package/reporting/test/{request-reporter.test.js → request/index.test.js} +102 -46
- package/reporting/test/{webrequest-pipeline → request}/page-store.test.js +1 -1
- package/reporting/test/request/steps/oauth-detector.spec.js +15 -25
- package/reporting/test/request/{utils.spec.js → utils/utils.spec.js} +2 -2
- package/reporting/test/setup.unit.js +1 -0
- package/reporting/test/utils/url.spec.js +1 -26
- package/reporting/src/request/page-telemetry.js +0 -42
- package/reporting/src/request/steps/subdomain-check.js +0 -34
- package/reporting/src/request/utils/subject.js +0 -37
- package/reporting/src/request-reporter.js +0 -111
- package/reporting/src/utils/pacemaker.js +0 -279
- package/reporting/src/webrequest-pipeline/cname-uncloak.js +0 -110
- package/reporting/src/webrequest-pipeline/index.js +0 -286
- package/reporting/src/webrequest-pipeline/logger.js +0 -14
- package/reporting/src/webrequest-pipeline/pipeline.js +0 -249
- package/reporting/src/webrequest-pipeline/utils/webrequest.js +0 -119
- package/reporting/test/utils/pacemaker.spec.js +0 -222
- package/reporting/test/webrequest-pipeline/index.test.js +0 -160
- package/reporting/test/webrequest-pipeline/pipeline.spec.js +0 -251
- /package/reporting/src/request/{resources → hash}/prob.js +0 -0
|
@@ -1,110 +0,0 @@
|
|
|
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.js';
|
|
13
|
-
import ChromeStorageMap from '../request/utils/chrome-storage-map.js';
|
|
14
|
-
|
|
15
|
-
function checkUserAgent(pattern) {
|
|
16
|
-
return navigator.userAgent.indexOf(pattern) !== -1;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
const isFirefox = checkUserAgent('Firefox');
|
|
20
|
-
|
|
21
|
-
async function withTimeout(promise, timeoutInMs) {
|
|
22
|
-
let timer;
|
|
23
|
-
try {
|
|
24
|
-
timer = new Promise((_, reject) => {
|
|
25
|
-
setTimeout(
|
|
26
|
-
() =>
|
|
27
|
-
reject(new Error(`Timed out after ${timeoutInMs / 1000} seconds`)),
|
|
28
|
-
timeoutInMs,
|
|
29
|
-
);
|
|
30
|
-
});
|
|
31
|
-
return await Promise.race([promise, timer]);
|
|
32
|
-
} finally {
|
|
33
|
-
clearTimeout(timer);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* CNAME un-cloaking only works in Firefox and if `dns` API is available.
|
|
39
|
-
*/
|
|
40
|
-
export function isCnameUncloakSupported(browser) {
|
|
41
|
-
return (
|
|
42
|
-
// Async webRequest only available in Firefox
|
|
43
|
-
isFirefox &&
|
|
44
|
-
// Feature discovery of `browser.dns.resolve(...)`
|
|
45
|
-
browser &&
|
|
46
|
-
browser.dns &&
|
|
47
|
-
browser.dns.resolve
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export default class CnameUnloak {
|
|
52
|
-
/**
|
|
53
|
-
* @param {function} dnsResolve dns.resolve WebExtension API (Firefox)
|
|
54
|
-
*/
|
|
55
|
-
constructor(dnsResolve, dnsTTL = 10 * 60 * 1000 /* 10 minutes */) {
|
|
56
|
-
this.dnsResolve = dnsResolve;
|
|
57
|
-
this.dnsCache = new ChromeStorageMap({
|
|
58
|
-
storageKey: 'wtm-request-reporting:webrequest-pipeline:dns-cache',
|
|
59
|
-
ttlInMs: dnsTTL,
|
|
60
|
-
});
|
|
61
|
-
this.dnsPending = new Map();
|
|
62
|
-
this.dnsTTL = dnsTTL;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
unload() {}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Resolve CNAME for `hostname`.
|
|
69
|
-
*/
|
|
70
|
-
resolveCNAME(hostname) {
|
|
71
|
-
// This means we already have a cached CNAME for this hostname.
|
|
72
|
-
const cached = this.dnsCache.get(hostname);
|
|
73
|
-
if (cached !== undefined && Date.now() - cached.ts <= this.dnsTTL) {
|
|
74
|
-
logger.debug('[cname] cached', { hostname, cname: cached.cname });
|
|
75
|
-
return cached.cname;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// This means we already have a request on-going for this hostname.
|
|
79
|
-
const pending = this.dnsPending.get(hostname);
|
|
80
|
-
if (pending !== undefined) {
|
|
81
|
-
logger.debug('[cname] pending', hostname);
|
|
82
|
-
return pending;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// Request CNAME from DNS for `hostname` with timeout of 2 seconds. We
|
|
86
|
-
// assume that 2 seconds should be enough to get an answer in all cases but
|
|
87
|
-
// we do not want to risk blocking the webRequest callback for too long.
|
|
88
|
-
const dnsRequest = withTimeout(
|
|
89
|
-
this.dnsResolve(hostname, ['canonical_name']),
|
|
90
|
-
2000,
|
|
91
|
-
)
|
|
92
|
-
.catch((ex) => {
|
|
93
|
-
logger.error('[cname] error while resolving', hostname, ex);
|
|
94
|
-
return { canonicalName: '' };
|
|
95
|
-
})
|
|
96
|
-
.then(({ canonicalName = '' } = {}) => {
|
|
97
|
-
const cname = canonicalName === hostname ? '' : canonicalName;
|
|
98
|
-
logger.debug('[cname] got record', cname);
|
|
99
|
-
this.dnsCache.set(hostname, {
|
|
100
|
-
cname,
|
|
101
|
-
ts: Date.now(),
|
|
102
|
-
});
|
|
103
|
-
this.dnsPending.delete(hostname);
|
|
104
|
-
return cname;
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
this.dnsPending.set(hostname, dnsRequest);
|
|
108
|
-
return dnsRequest;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
@@ -1,286 +0,0 @@
|
|
|
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 {
|
|
13
|
-
VALID_RESPONSE_PROPERTIES,
|
|
14
|
-
default as WebRequestListenersManager,
|
|
15
|
-
} from './utils/webrequest.js';
|
|
16
|
-
|
|
17
|
-
import Pipeline from './pipeline.js';
|
|
18
|
-
import WebRequestContext from './webrequest-context.js';
|
|
19
|
-
import PageStore from './page-store.js';
|
|
20
|
-
import logger from './logger.js';
|
|
21
|
-
import CnameUncloaker, { isCnameUncloakSupported } from './cname-uncloak.js';
|
|
22
|
-
|
|
23
|
-
function modifyHeaderByType(headers, name, value) {
|
|
24
|
-
const lowerCaseName = name.toLowerCase();
|
|
25
|
-
const filteredHeaders = headers.filter(
|
|
26
|
-
(h) => h.name.toLowerCase() !== lowerCaseName,
|
|
27
|
-
);
|
|
28
|
-
if (value) {
|
|
29
|
-
filteredHeaders.push({ name, value });
|
|
30
|
-
}
|
|
31
|
-
return filteredHeaders;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Small abstraction on top of blocking responses expected by WebRequest API. It
|
|
36
|
-
* provides a few helpers to block, redirect or modify headers. It is also able
|
|
37
|
-
* to create a valid blocking response taking into account platform-specific
|
|
38
|
-
* allowed capabilities.
|
|
39
|
-
*/
|
|
40
|
-
class BlockingResponse {
|
|
41
|
-
constructor(details) {
|
|
42
|
-
this.details = details;
|
|
43
|
-
|
|
44
|
-
// Blocking response
|
|
45
|
-
this.redirectUrl = undefined;
|
|
46
|
-
this.cancel = undefined;
|
|
47
|
-
this.responseHeaders = undefined;
|
|
48
|
-
this.requestHeaders = undefined;
|
|
49
|
-
this.shouldIncrementCounter = undefined;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
redirectTo(url) {
|
|
53
|
-
this.redirectUrl = url;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
block() {
|
|
57
|
-
this.cancel = true;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
modifyHeader(name, value) {
|
|
61
|
-
this.requestHeaders = modifyHeaderByType(
|
|
62
|
-
this.requestHeaders || this.details.requestHeaders || [],
|
|
63
|
-
name,
|
|
64
|
-
value,
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
modifyResponseHeader(name, value) {
|
|
69
|
-
this.responseHeaders = modifyHeaderByType(
|
|
70
|
-
this.responseHeaders || this.details.responseHeaders || [],
|
|
71
|
-
name,
|
|
72
|
-
value,
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
toWebRequestResponse(event) {
|
|
77
|
-
const allowedProperties = VALID_RESPONSE_PROPERTIES[event];
|
|
78
|
-
const response = {};
|
|
79
|
-
|
|
80
|
-
for (let i = 0; i < allowedProperties.length; i += 1) {
|
|
81
|
-
const prop = allowedProperties[i];
|
|
82
|
-
const value = this[prop];
|
|
83
|
-
if (value !== undefined) {
|
|
84
|
-
response[prop] = value;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
return response;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
export default class WebRequestPipeline {
|
|
93
|
-
constructor() {
|
|
94
|
-
this.initialized = false;
|
|
95
|
-
this.onPageStagedListeners = new Set();
|
|
96
|
-
this.listenerManager = new WebRequestListenersManager();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
enabled() {
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
init(_settings, browser) {
|
|
104
|
-
// Optionally enable CNAME-uncloaking.
|
|
105
|
-
this.cnameUncloaker = isCnameUncloakSupported(browser)
|
|
106
|
-
? new CnameUncloaker(browser.dns.resolve)
|
|
107
|
-
: null;
|
|
108
|
-
|
|
109
|
-
if (this.initialized) {
|
|
110
|
-
return;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
this.pipelines = new Map();
|
|
114
|
-
this.pageStore = new PageStore({
|
|
115
|
-
notifyPageStageListeners: this.notifyPageStageListeners.bind(this),
|
|
116
|
-
});
|
|
117
|
-
this.pageStore.init();
|
|
118
|
-
|
|
119
|
-
this.initialized = true;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
unload() {
|
|
123
|
-
if (!this.initialized) {
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
this.onPageStagedListeners.clear();
|
|
128
|
-
|
|
129
|
-
if (this.cnameUncloaker !== null) {
|
|
130
|
-
this.cnameUncloaker.unload();
|
|
131
|
-
this.cnameUncloaker = null;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// Remove webrequest listeners
|
|
135
|
-
this.pipelines.forEach((pipeline, event) => {
|
|
136
|
-
this.unloadPipeline(event);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
this.pageStore.unload();
|
|
140
|
-
this.initialized = false;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
addOnPageStageListener(listener) {
|
|
144
|
-
this.onPageStagedListeners.add(listener);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
notifyPageStageListeners(page) {
|
|
148
|
-
this.onPageStagedListeners.forEach((listener) => {
|
|
149
|
-
try {
|
|
150
|
-
listener(page);
|
|
151
|
-
} catch (e) {
|
|
152
|
-
logger.error('Page stage listener failed', e);
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
unloadPipeline(event) {
|
|
158
|
-
if (this.pipelines.has(event)) {
|
|
159
|
-
const pipeline = this.pipelines.get(event);
|
|
160
|
-
this[event] = undefined;
|
|
161
|
-
this.listenerManager.removeListener(event, pipeline.listener);
|
|
162
|
-
pipeline.pipeline.unload();
|
|
163
|
-
this.pipelines.delete(event);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
getPipeline(event) {
|
|
168
|
-
if (this.pipelines.has(event)) {
|
|
169
|
-
return this.pipelines.get(event).pipeline;
|
|
170
|
-
}
|
|
171
|
-
// Create pipeline step
|
|
172
|
-
const pipeline = new Pipeline(`webRequestPipeline.${event}`, [], false);
|
|
173
|
-
|
|
174
|
-
// Register listener for this event
|
|
175
|
-
const listener = (details) => {
|
|
176
|
-
const webRequestContext = WebRequestContext.fromDetails(
|
|
177
|
-
details,
|
|
178
|
-
this.pageStore,
|
|
179
|
-
event,
|
|
180
|
-
);
|
|
181
|
-
|
|
182
|
-
// Request is not supported, so do not alter
|
|
183
|
-
if (webRequestContext === null) {
|
|
184
|
-
logger.debug('Ignore unsupported request', details);
|
|
185
|
-
return {};
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const response = new BlockingResponse(details);
|
|
189
|
-
|
|
190
|
-
// Optionally uncloack first-party CNAME to uncover 1st-party tracking.
|
|
191
|
-
// This feature can only be enabled in the following cases:
|
|
192
|
-
//
|
|
193
|
-
// 1. `browser.dns` API is available (feature-detection)
|
|
194
|
-
// 2. webRequest supports async response (Firefox)
|
|
195
|
-
if (
|
|
196
|
-
// Will be set if uncloaking is supported on this platform (check
|
|
197
|
-
// background `init(...)` for more details).
|
|
198
|
-
this.cnameUncloaker !== null &&
|
|
199
|
-
// We ignore 'main_frame' to make sure we do not introduce any latency
|
|
200
|
-
// on the main request (because of DNS resolve) and also because we do
|
|
201
|
-
// not expect any privacy leak from this first request.
|
|
202
|
-
webRequestContext.type !== 'main_frame' &&
|
|
203
|
-
// Check that request is 1st-party
|
|
204
|
-
webRequestContext.urlParts !== null &&
|
|
205
|
-
webRequestContext.tabUrlParts !== null &&
|
|
206
|
-
webRequestContext.tabUrlParts.generalDomain ===
|
|
207
|
-
webRequestContext.urlParts.generalDomain &&
|
|
208
|
-
// Check that hostname is not the same as general domain (we need a subdomain)
|
|
209
|
-
webRequestContext.urlParts.hostname !==
|
|
210
|
-
webRequestContext.urlParts.generalDomain
|
|
211
|
-
) {
|
|
212
|
-
const cnameResult = this.cnameUncloaker.resolveCNAME(
|
|
213
|
-
webRequestContext.urlParts.hostname,
|
|
214
|
-
);
|
|
215
|
-
|
|
216
|
-
// Synchronous response means that the CNAME was cached.
|
|
217
|
-
if (typeof cnameResult === 'string') {
|
|
218
|
-
if (cnameResult !== '') {
|
|
219
|
-
webRequestContext.setCNAME(cnameResult);
|
|
220
|
-
}
|
|
221
|
-
pipeline.safeExecute(webRequestContext, response, true);
|
|
222
|
-
return response.toWebRequestResponse(event);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// Otherwise it's a promise and we wait for CNAME before processing.
|
|
226
|
-
return cnameResult.then((cname) => {
|
|
227
|
-
if (cnameResult !== '') {
|
|
228
|
-
webRequestContext.setCNAME(cname);
|
|
229
|
-
}
|
|
230
|
-
pipeline.safeExecute(webRequestContext, response, true);
|
|
231
|
-
return response.toWebRequestResponse(event);
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
pipeline.safeExecute(webRequestContext, response, true);
|
|
236
|
-
return response.toWebRequestResponse(event);
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
this.pipelines.set(event, {
|
|
240
|
-
pipeline,
|
|
241
|
-
listener,
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
// Register the event listener as an attribute of background so that we
|
|
245
|
-
// can call it: `webRequestPipeline.background.onBeforeRequest(details)`.
|
|
246
|
-
this[event] = listener;
|
|
247
|
-
|
|
248
|
-
this.listenerManager.addListener(event, listener);
|
|
249
|
-
|
|
250
|
-
return pipeline;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
addPipelineStep(stage, opts) {
|
|
254
|
-
if (this.initialized) {
|
|
255
|
-
const pipeline = this.getPipeline(stage);
|
|
256
|
-
if (pipeline === null) {
|
|
257
|
-
logger.error('WebRequest pipeline (add) does not have stage', stage);
|
|
258
|
-
} else {
|
|
259
|
-
pipeline.addPipelineStep(opts);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
removePipelineStep(stage, name) {
|
|
265
|
-
if (this.initialized) {
|
|
266
|
-
const pipeline = this.getPipeline(stage);
|
|
267
|
-
|
|
268
|
-
if (pipeline === null) {
|
|
269
|
-
logger.error('WebRequest pipeline (remove) does not have stage', stage);
|
|
270
|
-
} else {
|
|
271
|
-
pipeline.removePipelineStep(name);
|
|
272
|
-
if (pipeline.length === 0) {
|
|
273
|
-
this.unloadPipeline(stage);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
getPageStore() {
|
|
280
|
-
return this.pageStore;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
getPageForTab(tabId) {
|
|
284
|
-
return this.pageStore.tabs.get(tabId);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
@@ -1,14 +0,0 @@
|
|
|
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.js';
|
|
13
|
-
|
|
14
|
-
export default Logger.get('webrequest-pipeline', { level: 'error' });
|
|
@@ -1,249 +0,0 @@
|
|
|
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.js';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Handles a set of function pipelines. These pipelines start with an initial state
|
|
16
|
-
* and empty object return-value, then flow these arguments through the functions in
|
|
17
|
-
* the pipeline until one returns false.
|
|
18
|
-
*
|
|
19
|
-
* @class Pipeline
|
|
20
|
-
* @namespace antitracking
|
|
21
|
-
*/
|
|
22
|
-
export default class Pipeline {
|
|
23
|
-
/**
|
|
24
|
-
* Pipeline constructor. Creates an empty pipeline with the default
|
|
25
|
-
* stages (open, modify and response).
|
|
26
|
-
*/
|
|
27
|
-
constructor(name, steps = [], isBreakable = true) {
|
|
28
|
-
this.name = name;
|
|
29
|
-
this.isBreakable = isBreakable;
|
|
30
|
-
this.verboseLogging = false;
|
|
31
|
-
|
|
32
|
-
// Init empty pipeline
|
|
33
|
-
this.unload({ shallow: true });
|
|
34
|
-
|
|
35
|
-
this.addAll(steps);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
get length() {
|
|
39
|
-
return this.pipeline.length;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
unload() {
|
|
43
|
-
this.pipeline = [];
|
|
44
|
-
this.stepNames = new Set();
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Bulk add functions to stages
|
|
49
|
-
* @param {Array.<Function>} fns
|
|
50
|
-
*/
|
|
51
|
-
addAll(steps) {
|
|
52
|
-
for (let i = 0; i < steps.length; i += 1) {
|
|
53
|
-
this.addPipelineStep(steps[i]);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Adds a step to the pipeline with the given constraints.
|
|
59
|
-
*
|
|
60
|
-
* By default the step is placed at the end of the pipeline. If after or
|
|
61
|
-
* before are specified, the step is placed at the first available position
|
|
62
|
-
* which satisfies these constraints.
|
|
63
|
-
*
|
|
64
|
-
* @param {String} step.name The name of the function. Must be unique.
|
|
65
|
-
* @param {String} step.spec The kind of step: collect, break, blocking, annotate.
|
|
66
|
-
* @param {Function} options.fn The step function
|
|
67
|
-
* @param {Array.<String>} options.after Array of step names for which this step should be
|
|
68
|
-
* put afterwards
|
|
69
|
-
* @param {Array.<String>} options.before Array of step names for which this step should be
|
|
70
|
-
* put before
|
|
71
|
-
*/
|
|
72
|
-
addPipelineStep(step) {
|
|
73
|
-
const { name, fn, after = [], before = [], spec } = step;
|
|
74
|
-
|
|
75
|
-
if (name === undefined) {
|
|
76
|
-
throw new Error(
|
|
77
|
-
`Every step of the pipeline should be given a name in ${this.name}`,
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if (this.stepNames.has(name)) {
|
|
82
|
-
throw new Error(`Trying to overwrite existing ${this.name}.${name}`);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (
|
|
86
|
-
spec !== 'annotate' &&
|
|
87
|
-
spec !== 'collect' &&
|
|
88
|
-
spec !== 'blocking' &&
|
|
89
|
-
spec !== 'break'
|
|
90
|
-
) {
|
|
91
|
-
throw new Error(
|
|
92
|
-
`Every step of the pipeline should be given a valid spec (got ${spec}): ${this.name}.${name}`,
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (fn === undefined) {
|
|
97
|
-
throw new Error(
|
|
98
|
-
`Every step of the pipeline should have a function ('fn' argument): ${this.name}.${name}`,
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (spec === 'break' && !this.isBreakable) {
|
|
103
|
-
throw new Error(
|
|
104
|
-
`Cannot add a break step '${name}' to an unbreakable pipeline`,
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
this.stepNames.add(name);
|
|
109
|
-
|
|
110
|
-
const emptyPipeline = this.pipeline.length === 0;
|
|
111
|
-
const hasAfter = after.length > 0;
|
|
112
|
-
const hasBefore = before.length > 0;
|
|
113
|
-
|
|
114
|
-
if ((!hasAfter && !hasBefore) || emptyPipeline) {
|
|
115
|
-
// just put it at the end
|
|
116
|
-
this.pipeline.push(step);
|
|
117
|
-
} else if (hasAfter && !hasBefore) {
|
|
118
|
-
// Find the first index which is after any of the step names in `after`
|
|
119
|
-
let insertAt = -1;
|
|
120
|
-
for (let i = 0; i < this.pipeline.length; i += 1) {
|
|
121
|
-
if (after.indexOf(this.pipeline[i].name) !== -1) {
|
|
122
|
-
insertAt = i + 1;
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
if (insertAt === -1) {
|
|
127
|
-
throw new Error(
|
|
128
|
-
`no step from 'after' list found, after=${JSON.stringify(
|
|
129
|
-
after,
|
|
130
|
-
)}, pipeline=${this.name}`,
|
|
131
|
-
);
|
|
132
|
-
} else if (insertAt === this.pipeline.length) {
|
|
133
|
-
this.pipeline.push(step);
|
|
134
|
-
} else {
|
|
135
|
-
this.pipeline.splice(insertAt, 0, step);
|
|
136
|
-
}
|
|
137
|
-
} else if (hasBefore && !hasAfter) {
|
|
138
|
-
// Find the last index which is before any of the step names in `before`
|
|
139
|
-
let insertAt = -1;
|
|
140
|
-
for (let i = 0; i < this.pipeline.length; i += 1) {
|
|
141
|
-
if (before.indexOf(this.pipeline[i].name) !== -1) {
|
|
142
|
-
insertAt = i;
|
|
143
|
-
break;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (insertAt === -1) {
|
|
148
|
-
this.pipeline.push(step);
|
|
149
|
-
} else {
|
|
150
|
-
this.pipeline.splice(insertAt, 0, step);
|
|
151
|
-
}
|
|
152
|
-
} else {
|
|
153
|
-
throw new Error('cannot take both before and after constraints');
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Removes the given step name from the pipeline
|
|
159
|
-
* @param {String} name of the step
|
|
160
|
-
*/
|
|
161
|
-
removePipelineStep(name) {
|
|
162
|
-
this.pipeline = this.pipeline.filter((step) => step.name !== name);
|
|
163
|
-
this.stepNames.delete(name);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Run pipeline on the given `webRequestContext`. The `response` argument can
|
|
168
|
-
* be altered by steps and will be used to generate the blocking response for
|
|
169
|
-
* the webrequest listener.
|
|
170
|
-
*/
|
|
171
|
-
execute(webRequestContext, response, canAlterRequest = true) {
|
|
172
|
-
for (let i = 0; i < this.pipeline.length; i += 1) {
|
|
173
|
-
// Measure time elapsed while running this step
|
|
174
|
-
const { name, fn, spec } = this.pipeline[i];
|
|
175
|
-
let cont = true;
|
|
176
|
-
|
|
177
|
-
// Execute step
|
|
178
|
-
switch (spec) {
|
|
179
|
-
case 'break':
|
|
180
|
-
// A `break` step is usually used to disrupt the pipeline
|
|
181
|
-
// life-cycle. It does not have access to the response (cannot
|
|
182
|
-
// block/redirect/change headers) and shall not mutate the
|
|
183
|
-
// webRequestContext.
|
|
184
|
-
cont = fn(webRequestContext);
|
|
185
|
-
break;
|
|
186
|
-
case 'annotate':
|
|
187
|
-
// An `annotate` step is used to mutate/annotate the
|
|
188
|
-
// webRequestContext. For example it might be used to keep track of
|
|
189
|
-
// some information on the page-load (statistics about number of
|
|
190
|
-
// cookies blocked, etc.). It does not have access to the response
|
|
191
|
-
// (cannot block/redirect/change headers).
|
|
192
|
-
cont = fn(webRequestContext);
|
|
193
|
-
break;
|
|
194
|
-
case 'collect':
|
|
195
|
-
// A `collect` step is used to only observe the http life-cycle and
|
|
196
|
-
// is never used to alter it in any way. It does not have access to
|
|
197
|
-
// the response either. Because of these constraints, we can safely
|
|
198
|
-
// run these steps asynchronously to not block the processing of
|
|
199
|
-
// requests.
|
|
200
|
-
setTimeout(fn.bind(null, webRequestContext), 1);
|
|
201
|
-
break;
|
|
202
|
-
case 'blocking':
|
|
203
|
-
// A `blocking` step is used to alter the life-cycle of a request:
|
|
204
|
-
// blocking, redirecting, modifying headers. It can also mutate the
|
|
205
|
-
// webRequestContext, although it would be better to use an
|
|
206
|
-
// `annotate` step for this purpose.
|
|
207
|
-
//
|
|
208
|
-
// When `canAlterRequest` is `false`, blocking steps are ignored.
|
|
209
|
-
// This can be the case if a domain has been white-listed at the
|
|
210
|
-
// webrequest-pipeline level.
|
|
211
|
-
if (canAlterRequest === true) {
|
|
212
|
-
cont = fn(webRequestContext, response);
|
|
213
|
-
}
|
|
214
|
-
break;
|
|
215
|
-
default:
|
|
216
|
-
logger.error('Invalid spec for step', this.pipeline[i]);
|
|
217
|
-
break;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
// Handle early termination of the pipeline. If a step returns `false` and
|
|
221
|
-
// this pipeline is allowed to be 'broken', then we ignore the remaining
|
|
222
|
-
// steps.
|
|
223
|
-
if (cont === false) {
|
|
224
|
-
if (this.isBreakable === true) {
|
|
225
|
-
if (this.verboseLogging) {
|
|
226
|
-
logger.debug(this.name, webRequestContext.url, 'Break at', name);
|
|
227
|
-
}
|
|
228
|
-
break;
|
|
229
|
-
}
|
|
230
|
-
// we only reach here if the pipeline is not breakable: show a warning
|
|
231
|
-
// that we ignored the break
|
|
232
|
-
logger.error(
|
|
233
|
-
this.name,
|
|
234
|
-
webRequestContext.url,
|
|
235
|
-
'ignoring attempted break of unbreakable pipeline at',
|
|
236
|
-
name,
|
|
237
|
-
);
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
safeExecute(context, response, canAlterRequest) {
|
|
243
|
-
try {
|
|
244
|
-
this.execute(context, response, canAlterRequest);
|
|
245
|
-
} catch (ex) {
|
|
246
|
-
logger.error('while running pipeline', context, ex);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|