@whotracksme/reporting 5.2.2 โ 6.1.0
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 +29 -0
- package/package.json +1 -1
- package/reporting/example/index.js +12 -2
- package/reporting/src/communication-proxy/attrack-message-handler.js +85 -0
- package/reporting/src/doublefetch-page-handler.js +31 -0
- package/reporting/src/http.js +235 -167
- package/reporting/src/job-scheduler.js +1 -1
- package/reporting/src/reporting.js +13 -0
- package/reporting/src/request/index.js +3 -9
- package/reporting/test/doublefetch-page-handler.spec.js +42 -0
- package/reporting/test/request/index.spec.js +1 -0
- package/reporting/test/request/index.test.js +13 -20
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
# v6.1.0 (Thu Jan 16 2025)
|
|
2
|
+
|
|
3
|
+
#### ๐ Enhancement
|
|
4
|
+
|
|
5
|
+
- MV3 support for anonymousHttpGet [#151](https://github.com/whotracksme/webextension-packages/pull/151) ([@philipp-classen](https://github.com/philipp-classen))
|
|
6
|
+
|
|
7
|
+
#### Authors: 1
|
|
8
|
+
|
|
9
|
+
- Philipp Claรen ([@philipp-classen](https://github.com/philipp-classen))
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
# v6.0.0 (Wed Jan 15 2025)
|
|
14
|
+
|
|
15
|
+
#### ๐ฅ Breaking Change
|
|
16
|
+
|
|
17
|
+
- Randomly delay sending of request reporter messages [#149](https://github.com/whotracksme/webextension-packages/pull/149) ([@philipp-classen](https://github.com/philipp-classen))
|
|
18
|
+
|
|
19
|
+
#### ๐ Enhancement
|
|
20
|
+
|
|
21
|
+
- Improve title matching [#150](https://github.com/whotracksme/webextension-packages/pull/150) ([@philipp-classen](https://github.com/philipp-classen) [@chrmod](https://github.com/chrmod))
|
|
22
|
+
|
|
23
|
+
#### Authors: 2
|
|
24
|
+
|
|
25
|
+
- Krzysztof Modras ([@chrmod](https://github.com/chrmod))
|
|
26
|
+
- Philipp Claรen ([@philipp-classen](https://github.com/philipp-classen))
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
1
30
|
# v5.2.2 (Wed Jan 15 2025)
|
|
2
31
|
|
|
3
32
|
#### ๐ Bug Fix
|
package/package.json
CHANGED
|
@@ -47,7 +47,11 @@ function createStorage() {
|
|
|
47
47
|
|
|
48
48
|
const communication = {
|
|
49
49
|
send(msg) {
|
|
50
|
-
console.
|
|
50
|
+
console.log(
|
|
51
|
+
'%c[DRY-RUN] send message:',
|
|
52
|
+
'color: blue; font-size: 30px;',
|
|
53
|
+
msg,
|
|
54
|
+
);
|
|
51
55
|
},
|
|
52
56
|
// TODO: use actuall anonymous-communication to access quorum
|
|
53
57
|
sendInstant(msg) {
|
|
@@ -90,7 +94,13 @@ const urlReporter = new UrlReporter({
|
|
|
90
94
|
|
|
91
95
|
const requestReporter = new RequestReporter(config.request, {
|
|
92
96
|
dryRunMode: true,
|
|
93
|
-
|
|
97
|
+
onMessageReady: (msg) => {
|
|
98
|
+
console.log(
|
|
99
|
+
'%c[DRY-RUN] request-reporter message ready:',
|
|
100
|
+
'color: green; font-size: 30px;',
|
|
101
|
+
msg,
|
|
102
|
+
);
|
|
103
|
+
},
|
|
94
104
|
countryProvider: urlReporter.countryProvider,
|
|
95
105
|
trustedClock: communication.trustedClock,
|
|
96
106
|
getBrowserInfo: () => ({ name: 'xx' }),
|
|
@@ -0,0 +1,85 @@
|
|
|
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 { requireParam, requireString } from '../utils';
|
|
14
|
+
|
|
15
|
+
const SECOND = 1000;
|
|
16
|
+
const MINUTE = 60 * SECOND;
|
|
17
|
+
const HOUR = 60 * MINUTE;
|
|
18
|
+
const DAY = 24 * HOUR;
|
|
19
|
+
|
|
20
|
+
const ATTRACK_JOB_TYPE = 'attrack:send-message:v1';
|
|
21
|
+
|
|
22
|
+
export default class AttrackMessageHandler {
|
|
23
|
+
constructor({ communication, jobScheduler }) {
|
|
24
|
+
this.communication = requireParam(communication);
|
|
25
|
+
this.jobScheduler = requireParam(jobScheduler);
|
|
26
|
+
|
|
27
|
+
this._configOverride = {
|
|
28
|
+
'wtm.attrack.tokensv2': {
|
|
29
|
+
readyIn: { min: 2 * SECOND, max: 4 * HOUR },
|
|
30
|
+
},
|
|
31
|
+
'wtm.attrack.keysv2': {
|
|
32
|
+
readyIn: { min: 2 * SECOND, max: 4 * HOUR },
|
|
33
|
+
},
|
|
34
|
+
'wtm.attrack.tp_events': {
|
|
35
|
+
readyIn: { min: 2 * SECOND, max: 30 * MINUTE },
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
this.jobScheduler.registerHandler(
|
|
40
|
+
ATTRACK_JOB_TYPE,
|
|
41
|
+
async (job) => {
|
|
42
|
+
const { message } = job.args;
|
|
43
|
+
requireParam(message);
|
|
44
|
+
requireString(message.action);
|
|
45
|
+
|
|
46
|
+
await this.communication.send(message);
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
priority: -1000,
|
|
50
|
+
cooldownInMs: 1 * SECOND,
|
|
51
|
+
maxJobsTotal: 500,
|
|
52
|
+
maxTTL: 14 * DAY,
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
sendInBackground(message) {
|
|
58
|
+
requireParam(message);
|
|
59
|
+
requireString(message.action);
|
|
60
|
+
|
|
61
|
+
if (this.jobScheduler.active) {
|
|
62
|
+
const job = {
|
|
63
|
+
type: ATTRACK_JOB_TYPE,
|
|
64
|
+
args: { message },
|
|
65
|
+
config: this._configOverride[message.action] || {},
|
|
66
|
+
};
|
|
67
|
+
this.jobScheduler.registerJob(job).catch((e) => {
|
|
68
|
+
logger.warn('Failed to register job', job, e);
|
|
69
|
+
this._sendNowInBackground(message);
|
|
70
|
+
});
|
|
71
|
+
} else {
|
|
72
|
+
// Sending directly has the advantage that the request reporter
|
|
73
|
+
// can be used even if the URL reporter is not available.
|
|
74
|
+
// It should happen rarely enough.
|
|
75
|
+
logger.info('jobScheduler not available. Send immediately...');
|
|
76
|
+
this._sendNowInBackground(message);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
_sendNowInBackground(message) {
|
|
81
|
+
this.communication.send(message).catch((e) => {
|
|
82
|
+
logger.error('Failed to send message', e);
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -27,6 +27,25 @@ const ALLOW_HTML_AND_TEXT = [
|
|
|
27
27
|
'application/xhtml+xml', // legacy format
|
|
28
28
|
];
|
|
29
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Checks if one string (subsequence) can be derived from another string (sequence)
|
|
32
|
+
* by only removing characters. This means that the subsequence appears in the
|
|
33
|
+
* sequence in the same relative order but not necessarily consecutively.
|
|
34
|
+
*
|
|
35
|
+
* Example: "anana" is a subsequence of "banana", but "ab" is not.
|
|
36
|
+
*/
|
|
37
|
+
function isSubSequence({ sequence, subsequence }) {
|
|
38
|
+
let i = 0;
|
|
39
|
+
let j = 0;
|
|
40
|
+
while (i < subsequence.length && j < sequence.length) {
|
|
41
|
+
if (subsequence[i] === sequence[j]) {
|
|
42
|
+
i++;
|
|
43
|
+
}
|
|
44
|
+
j++;
|
|
45
|
+
}
|
|
46
|
+
return i === subsequence.length;
|
|
47
|
+
}
|
|
48
|
+
|
|
30
49
|
/**
|
|
31
50
|
* This checks if the extracted title of a website is safe to share.
|
|
32
51
|
* To decide, it compares it with the title on the doublefetch HTML.
|
|
@@ -100,6 +119,18 @@ export function titlesMatchAfterDoublefetch({
|
|
|
100
119
|
}
|
|
101
120
|
}
|
|
102
121
|
|
|
122
|
+
// Removing small parts of text should be generally safe. Note that
|
|
123
|
+
// this is still quite strict; edits like swapping characters or
|
|
124
|
+
// adding a single char will make this heuristic fail.
|
|
125
|
+
if (
|
|
126
|
+
before.length > after.length &&
|
|
127
|
+
after.length > 12 &&
|
|
128
|
+
after.length > 0.75 * before.length &&
|
|
129
|
+
isSubSequence({ sequence: before, subsequence: after })
|
|
130
|
+
) {
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
|
|
103
134
|
return false;
|
|
104
135
|
}
|
|
105
136
|
|
package/reporting/src/http.js
CHANGED
|
@@ -17,9 +17,19 @@ import {
|
|
|
17
17
|
UnableToOverrideHeadersError,
|
|
18
18
|
} from './errors';
|
|
19
19
|
import { split0 } from './utils';
|
|
20
|
+
import SeqExecutor from './seq-executor';
|
|
20
21
|
|
|
21
22
|
const SECOND = 1000;
|
|
22
23
|
|
|
24
|
+
// This ID must not be used by other parts of the extension.
|
|
25
|
+
const RESERVED_DNR_RULE_ID = 1333;
|
|
26
|
+
|
|
27
|
+
function getExtensionDomain() {
|
|
28
|
+
getExtensionDomain.cached =
|
|
29
|
+
getExtensionDomain.cached || new URL(chrome.runtime.getURL('')).host;
|
|
30
|
+
return getExtensionDomain.cached;
|
|
31
|
+
}
|
|
32
|
+
|
|
23
33
|
/**
|
|
24
34
|
* Note: 429 (too many requests) is intentionally not included in the list.
|
|
25
35
|
* Even though it is by definition a temporary error, it depends on the context
|
|
@@ -36,6 +46,111 @@ const httpStatusCodesThatShouldBeRetried = [
|
|
|
36
46
|
599, // Network Connect Timeout Error
|
|
37
47
|
];
|
|
38
48
|
|
|
49
|
+
// The following code overwrite the headers of the request.
|
|
50
|
+
// Note that "fetch" allows to overwrite headers in a simple declarative way,
|
|
51
|
+
// but unfortunately it is limited. For example, it is not possible to
|
|
52
|
+
// overwrite the cookie headers. The following code will work for all
|
|
53
|
+
// type of headers.
|
|
54
|
+
//
|
|
55
|
+
// The matching logic is not perfect, but should be fairly accurate.
|
|
56
|
+
// Ideally, we would want to run the handler only for the request that we
|
|
57
|
+
// are about to trigger, but not for any other requests to avoid unintended
|
|
58
|
+
// side-effects. To mitigate the risk, uninstall the handler at the first
|
|
59
|
+
// opportunity: either if it is called or if the request finished
|
|
60
|
+
// (and we know the handle will never be called).
|
|
61
|
+
function headerOverrideViaWebRequestAPI(url, headers) {
|
|
62
|
+
let uninstallHandler;
|
|
63
|
+
let webRequestHandler = (details) => {
|
|
64
|
+
if (
|
|
65
|
+
details.url !== url ||
|
|
66
|
+
details.type !== 'xmlhttprequest' ||
|
|
67
|
+
details.method !== 'GET'
|
|
68
|
+
) {
|
|
69
|
+
// does that match the request that we intended to trigger
|
|
70
|
+
return {};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// match: now we can already deregister the listener
|
|
74
|
+
// (it should not be executed multiple times)
|
|
75
|
+
uninstallHandler();
|
|
76
|
+
const headerNames = Object.keys(headers);
|
|
77
|
+
const normalizedHeaders = headerNames.map((x) => x.toLowerCase());
|
|
78
|
+
|
|
79
|
+
/* eslint-disable no-param-reassign */
|
|
80
|
+
details.requestHeaders = details.requestHeaders.filter(
|
|
81
|
+
(header) => !normalizedHeaders.includes(header.name.toLowerCase()),
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
headerNames.forEach((name) => {
|
|
85
|
+
details.requestHeaders.push({
|
|
86
|
+
name,
|
|
87
|
+
value: headers[name],
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
requestHeaders: details.requestHeaders,
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
logger.debug('Installing temporary webrequest handler for URL:', url);
|
|
97
|
+
chrome.webRequest.onBeforeSendHeaders.addListener(
|
|
98
|
+
webRequestHandler,
|
|
99
|
+
{
|
|
100
|
+
urls: [url],
|
|
101
|
+
},
|
|
102
|
+
['blocking', 'requestHeaders'],
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
uninstallHandler = () => {
|
|
106
|
+
if (webRequestHandler) {
|
|
107
|
+
logger.debug('Removing temporary webrequest handler for URL:', url);
|
|
108
|
+
chrome.webRequest.onBeforeSendHeaders.removeListener(webRequestHandler);
|
|
109
|
+
webRequestHandler = null;
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
return uninstallHandler;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Like headerOverrideViaWebRequestAPI, but using the DNR API.
|
|
116
|
+
async function headerOverrideViaDNR(url, headers) {
|
|
117
|
+
const requestHeaders = Object.entries(headers).map(([header, value]) => ({
|
|
118
|
+
header,
|
|
119
|
+
operation: 'set',
|
|
120
|
+
value,
|
|
121
|
+
}));
|
|
122
|
+
const rule = {
|
|
123
|
+
id: RESERVED_DNR_RULE_ID,
|
|
124
|
+
priority: 1,
|
|
125
|
+
action: {
|
|
126
|
+
type: 'modifyHeaders',
|
|
127
|
+
requestHeaders,
|
|
128
|
+
},
|
|
129
|
+
condition: {
|
|
130
|
+
urlFilter: url,
|
|
131
|
+
resourceTypes: ['xmlhttprequest'],
|
|
132
|
+
initiatorDomains: [getExtensionDomain()],
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
logger.debug('Installing temporary DNR rule for URL:', url);
|
|
136
|
+
await chrome.declarativeNetRequest.updateSessionRules({
|
|
137
|
+
addRules: [rule],
|
|
138
|
+
});
|
|
139
|
+
return () => {
|
|
140
|
+
logger.debug('Removing temporary DNR rule for URL:', url);
|
|
141
|
+
chrome.declarativeNetRequest
|
|
142
|
+
.updateSessionRules({
|
|
143
|
+
removeRuleIds: [rule.id],
|
|
144
|
+
})
|
|
145
|
+
.catch((e) => {
|
|
146
|
+
logger.warn('Failed to remove DNR rule', e);
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Enforces only one active double-fetch request at a time.
|
|
152
|
+
const LOCK = new SeqExecutor();
|
|
153
|
+
|
|
39
154
|
/**
|
|
40
155
|
* Performs a HTTP Get. Given the constraints, it tries to include
|
|
41
156
|
* as few information as possible (e.g. credentials from cookies will
|
|
@@ -70,196 +185,149 @@ const httpStatusCodesThatShouldBeRetried = [
|
|
|
70
185
|
* (Maybe there is a way to generalize?)
|
|
71
186
|
*/
|
|
72
187
|
export async function anonymousHttpGet(url, params = {}) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
188
|
+
return LOCK.run(async () => {
|
|
189
|
+
const {
|
|
190
|
+
headers = null,
|
|
191
|
+
redirect = 'manual',
|
|
192
|
+
shouldFollowRedirect = () => true,
|
|
193
|
+
timeout = 15 * SECOND,
|
|
194
|
+
treat429AsPermanentError = false,
|
|
195
|
+
downloadLimit = 10 * 1024 * 1024, // 10 MB
|
|
196
|
+
allowedContentTypes = null,
|
|
197
|
+
} = params;
|
|
82
198
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
199
|
+
if (redirect !== 'follow' && params.shouldFollowRedirect) {
|
|
200
|
+
logger.warn(
|
|
201
|
+
'shouldFollowRedirect ignored because redirects will be automatically resolved',
|
|
202
|
+
);
|
|
203
|
+
}
|
|
88
204
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
205
|
+
const controller = new AbortController();
|
|
206
|
+
const options = {
|
|
207
|
+
credentials: 'omit',
|
|
208
|
+
mode: 'no-cors',
|
|
209
|
+
redirect,
|
|
210
|
+
signal: controller.signal,
|
|
211
|
+
};
|
|
96
212
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
213
|
+
let timeoutTimer;
|
|
214
|
+
let timeoutTriggered = false;
|
|
215
|
+
if (timeout && timeout > 0) {
|
|
216
|
+
timeoutTimer = setTimeout(() => {
|
|
217
|
+
const msg = `Request to ${url} timed out (limit: ${timeout} ms)`;
|
|
218
|
+
controller.abort(new TemporarilyUnableToFetchUrlError(msg));
|
|
219
|
+
logger.warn(msg);
|
|
220
|
+
timeoutTimer = null;
|
|
221
|
+
timeoutTriggered = true;
|
|
222
|
+
}, timeout);
|
|
223
|
+
}
|
|
108
224
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
// overwrite the cookie headers. The following code will work for all
|
|
114
|
-
// type of headers.
|
|
115
|
-
//
|
|
116
|
-
// The matching logic is not perfect but should be fairly accurate.
|
|
117
|
-
// Ideally, we would want to run the handler only for the request that we
|
|
118
|
-
// are about to trigger, but not for any other requests to avoid unintended
|
|
119
|
-
// side-effects. To mitigate the risk, uninstall the handler at the first
|
|
120
|
-
// opportunity: either if it is called or if the request finished
|
|
121
|
-
// (and we know the handle will never be called).
|
|
122
|
-
let webRequestHandler;
|
|
123
|
-
const uninstallHandler = () => {
|
|
124
|
-
if (webRequestHandler) {
|
|
125
|
-
chrome.webRequest.onBeforeSendHeaders.removeListener(webRequestHandler);
|
|
126
|
-
webRequestHandler = null;
|
|
127
|
-
}
|
|
128
|
-
};
|
|
129
|
-
const headerNames = Object.keys(headers || {});
|
|
130
|
-
if (headerNames.length > 0) {
|
|
131
|
-
if (
|
|
132
|
-
!chrome.webRequest ||
|
|
133
|
-
!chrome.webRequest.onBeforeSendHeaders ||
|
|
134
|
-
!chrome.runtime.getManifest().permissions.includes('webRequestBlocking')
|
|
135
|
-
) {
|
|
136
|
-
throw new UnableToOverrideHeadersError();
|
|
137
|
-
}
|
|
138
|
-
webRequestHandler = (details) => {
|
|
225
|
+
try {
|
|
226
|
+
// optional cleanup operation that will be run after the request is completed
|
|
227
|
+
let cleanup = () => {};
|
|
228
|
+
if (headers && Object.keys(headers).length > 0) {
|
|
139
229
|
if (
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
230
|
+
chrome?.webRequest?.onBeforeSendHeaders &&
|
|
231
|
+
chrome.runtime
|
|
232
|
+
.getManifest()
|
|
233
|
+
.permissions.includes('webRequestBlocking')
|
|
143
234
|
) {
|
|
144
|
-
|
|
145
|
-
|
|
235
|
+
cleanup = headerOverrideViaWebRequestAPI(url, headers);
|
|
236
|
+
} else if (chrome?.declarativeNetRequest?.updateSessionRules) {
|
|
237
|
+
cleanup = await headerOverrideViaDNR(url, headers);
|
|
238
|
+
} else {
|
|
239
|
+
throw new UnableToOverrideHeadersError();
|
|
146
240
|
}
|
|
241
|
+
}
|
|
147
242
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
243
|
+
let response;
|
|
244
|
+
try {
|
|
245
|
+
response = await fetch(url, options);
|
|
246
|
+
} catch (e) {
|
|
247
|
+
throw new TemporarilyUnableToFetchUrlError(
|
|
248
|
+
`Failed to fetch url ${url}`,
|
|
249
|
+
{
|
|
250
|
+
cause: e,
|
|
251
|
+
},
|
|
252
|
+
);
|
|
253
|
+
} finally {
|
|
254
|
+
cleanup();
|
|
255
|
+
}
|
|
256
|
+
if (
|
|
257
|
+
response.status === 0 &&
|
|
258
|
+
(response.type == 'opaqueredirect' || response.type == 'opaque')
|
|
259
|
+
) {
|
|
260
|
+
throw new PermanentlyUnableToFetchUrlError(
|
|
261
|
+
`Failed to fetch url ${url}: not allowed to follow redirects (response.type=${response.type})`,
|
|
156
262
|
);
|
|
157
|
-
|
|
158
|
-
headerNames.forEach((name) => {
|
|
159
|
-
details.requestHeaders.push({
|
|
160
|
-
name,
|
|
161
|
-
value: headers[name],
|
|
162
|
-
});
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
return {
|
|
166
|
-
requestHeaders: details.requestHeaders,
|
|
167
|
-
};
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
chrome.webRequest.onBeforeSendHeaders.addListener(
|
|
171
|
-
webRequestHandler,
|
|
172
|
-
{
|
|
173
|
-
urls: [url],
|
|
174
|
-
},
|
|
175
|
-
['blocking', 'requestHeaders'],
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
let response;
|
|
180
|
-
try {
|
|
181
|
-
response = await fetch(url, options);
|
|
182
|
-
} catch (e) {
|
|
183
|
-
throw new TemporarilyUnableToFetchUrlError(`Failed to fetch url ${url}`, {
|
|
184
|
-
cause: e,
|
|
185
|
-
});
|
|
186
|
-
} finally {
|
|
187
|
-
uninstallHandler();
|
|
188
|
-
}
|
|
189
|
-
if (
|
|
190
|
-
response.status === 0 &&
|
|
191
|
-
(response.type == 'opaqueredirect' || response.type == 'opaque')
|
|
192
|
-
) {
|
|
193
|
-
throw new PermanentlyUnableToFetchUrlError(
|
|
194
|
-
`Failed to fetch url ${url}: not allowed to follow redirects (response.type=${response.type})`,
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
if (response.url !== url && !shouldFollowRedirect(response.url)) {
|
|
198
|
-
throw new PermanentlyUnableToFetchUrlError(
|
|
199
|
-
`Failed to fetch url ${url}: detected forbidden redirect to ${response.url}`,
|
|
200
|
-
);
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
if (!response.ok) {
|
|
204
|
-
const msg = `Failed to fetch url ${url}: ${response.statusText}`;
|
|
205
|
-
if (response.status === 429) {
|
|
206
|
-
if (treat429AsPermanentError) {
|
|
207
|
-
throw new PermanentlyUnableToFetchUrlError(msg);
|
|
208
|
-
}
|
|
209
|
-
throw new RateLimitedByServerError(msg);
|
|
210
263
|
}
|
|
211
|
-
if (
|
|
212
|
-
throw new
|
|
264
|
+
if (response.url !== url && !shouldFollowRedirect(response.url)) {
|
|
265
|
+
throw new PermanentlyUnableToFetchUrlError(
|
|
266
|
+
`Failed to fetch url ${url}: detected forbidden redirect to ${response.url}`,
|
|
267
|
+
);
|
|
213
268
|
}
|
|
214
|
-
throw new PermanentlyUnableToFetchUrlError(msg);
|
|
215
|
-
}
|
|
216
269
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
270
|
+
if (!response.ok) {
|
|
271
|
+
const msg = `Failed to fetch url ${url}: ${response.statusText}`;
|
|
272
|
+
if (response.status === 429) {
|
|
273
|
+
if (treat429AsPermanentError) {
|
|
274
|
+
throw new PermanentlyUnableToFetchUrlError(msg);
|
|
275
|
+
}
|
|
276
|
+
throw new RateLimitedByServerError(msg);
|
|
277
|
+
}
|
|
278
|
+
if (httpStatusCodesThatShouldBeRetried.includes(response.status)) {
|
|
279
|
+
throw new TemporarilyUnableToFetchUrlError(msg);
|
|
280
|
+
}
|
|
281
|
+
throw new PermanentlyUnableToFetchUrlError(msg);
|
|
225
282
|
}
|
|
226
|
-
}
|
|
227
283
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
const contentType = split0(value, ';').trim().toLowerCase();
|
|
232
|
-
if (!allowedContentTypes.includes(contentType)) {
|
|
284
|
+
if (downloadLimit && downloadLimit > 0) {
|
|
285
|
+
const contentLength = response.headers.get('content-length');
|
|
286
|
+
if (contentLength && contentLength > downloadLimit) {
|
|
233
287
|
const err = new PermanentlyUnableToFetchUrlError(
|
|
234
|
-
`
|
|
288
|
+
`Exceeded size limit when fetching url ${url} (${contentLength} > ${downloadLimit})`,
|
|
235
289
|
);
|
|
236
290
|
controller.abort(err);
|
|
237
291
|
throw err;
|
|
238
292
|
}
|
|
239
|
-
} else {
|
|
240
|
-
logger.warn(
|
|
241
|
-
'The URL',
|
|
242
|
-
url,
|
|
243
|
-
'did not return a "Content-Type" HTTP header.',
|
|
244
|
-
'Continue and assume the types are matching...',
|
|
245
|
-
);
|
|
246
293
|
}
|
|
247
|
-
}
|
|
248
294
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
295
|
+
if (allowedContentTypes) {
|
|
296
|
+
const value = response.headers.get('content-type');
|
|
297
|
+
if (value) {
|
|
298
|
+
const contentType = split0(value, ';').trim().toLowerCase();
|
|
299
|
+
if (!allowedContentTypes.includes(contentType)) {
|
|
300
|
+
const err = new PermanentlyUnableToFetchUrlError(
|
|
301
|
+
`Unexpected "Content-Type" <${contentType}> (<${value}> not in {${allowedContentTypes}})`,
|
|
302
|
+
);
|
|
303
|
+
controller.abort(err);
|
|
304
|
+
throw err;
|
|
305
|
+
}
|
|
306
|
+
} else {
|
|
307
|
+
logger.warn(
|
|
308
|
+
'The URL',
|
|
309
|
+
url,
|
|
310
|
+
'did not return a "Content-Type" HTTP header.',
|
|
311
|
+
'Continue and assume the types are matching...',
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
try {
|
|
317
|
+
return await response.text();
|
|
318
|
+
} catch (e) {
|
|
319
|
+
if (timeoutTriggered) {
|
|
320
|
+
throw new TemporarilyUnableToFetchUrlError(
|
|
321
|
+
`Failed to fetch url ${url} because the request timed out.`,
|
|
322
|
+
);
|
|
323
|
+
}
|
|
253
324
|
throw new TemporarilyUnableToFetchUrlError(
|
|
254
|
-
`Failed to fetch url ${url}
|
|
325
|
+
`Failed to fetch url ${url} (${e.message})`,
|
|
326
|
+
{ cause: e },
|
|
255
327
|
);
|
|
256
328
|
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
{ cause: e },
|
|
260
|
-
);
|
|
329
|
+
} finally {
|
|
330
|
+
clearTimeout(timeoutTimer);
|
|
261
331
|
}
|
|
262
|
-
}
|
|
263
|
-
clearTimeout(timeoutTimer);
|
|
264
|
-
}
|
|
332
|
+
});
|
|
265
333
|
}
|
|
@@ -304,7 +304,7 @@ export default class JobScheduler {
|
|
|
304
304
|
// "min" value and an optional "max" value (if you want randomness).
|
|
305
305
|
//
|
|
306
306
|
// Examples:
|
|
307
|
-
// - { min: 2000} will delay by (at least) 2 seconds
|
|
307
|
+
// - { min: 2000 } will delay by (at least) 2 seconds
|
|
308
308
|
// - { min: 2000, max: 3000 } will delay by (at least) 2-3 seconds
|
|
309
309
|
//
|
|
310
310
|
// Note: the real delay can be higher, since there is no guarantee
|
|
@@ -29,6 +29,7 @@ import PersistedHashes from './persisted-hashes';
|
|
|
29
29
|
import AliveCheck from './alive-check';
|
|
30
30
|
import AliveMessageGenerator from './alive-message-generator';
|
|
31
31
|
import SessionStorageWrapper from './session-storage';
|
|
32
|
+
import AttrackMessageHandler from './communication-proxy/attrack-message-handler';
|
|
32
33
|
import logger from './logger';
|
|
33
34
|
import SelfCheck from './self-check';
|
|
34
35
|
import { BloomFilter } from './bloom-filters';
|
|
@@ -152,6 +153,11 @@ export default class Reporting {
|
|
|
152
153
|
storage,
|
|
153
154
|
storageKey: 'alive_check',
|
|
154
155
|
});
|
|
156
|
+
|
|
157
|
+
this.attrackMessageHandler = new AttrackMessageHandler({
|
|
158
|
+
communication,
|
|
159
|
+
jobScheduler: this.jobScheduler,
|
|
160
|
+
});
|
|
155
161
|
}
|
|
156
162
|
|
|
157
163
|
async init() {
|
|
@@ -270,6 +276,13 @@ export default class Reporting {
|
|
|
270
276
|
await this.patternsUpdater.update();
|
|
271
277
|
}
|
|
272
278
|
|
|
279
|
+
// Drop-in replacement for "communication.send". To be used by the
|
|
280
|
+
// request reporter. It is a synchronous fire-and-forget API. The
|
|
281
|
+
// message delivery will be best-effort. Also, it may be delayed.
|
|
282
|
+
forwardRequestReporterMessage(msg) {
|
|
283
|
+
this.attrackMessageHandler.sendInBackground(msg);
|
|
284
|
+
}
|
|
285
|
+
|
|
273
286
|
async selfChecks(check = new SelfCheck()) {
|
|
274
287
|
if (!this.isActive) {
|
|
275
288
|
check.warn('reporting is not enabled');
|
|
@@ -47,7 +47,7 @@ export default class RequestReporter {
|
|
|
47
47
|
{
|
|
48
48
|
trustedClock,
|
|
49
49
|
countryProvider,
|
|
50
|
-
|
|
50
|
+
onMessageReady,
|
|
51
51
|
getBrowserInfo,
|
|
52
52
|
onTrackerInteraction = (event, state) => {
|
|
53
53
|
logger.info('Tracker', event, 'with url:', state.url);
|
|
@@ -57,7 +57,7 @@ export default class RequestReporter {
|
|
|
57
57
|
},
|
|
58
58
|
) {
|
|
59
59
|
this.settings = settings;
|
|
60
|
-
this.
|
|
60
|
+
this.onMessageReady = onMessageReady;
|
|
61
61
|
this.trustedClock = trustedClock;
|
|
62
62
|
this.countryProvider = countryProvider;
|
|
63
63
|
this.onTrackerInteraction = onTrackerInteraction;
|
|
@@ -150,11 +150,6 @@ export default class RequestReporter {
|
|
|
150
150
|
}
|
|
151
151
|
|
|
152
152
|
telemetry(message) {
|
|
153
|
-
if (!this.communication) {
|
|
154
|
-
logger.error('No provider provider loaded');
|
|
155
|
-
return;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
153
|
message.type = 'wtm.request';
|
|
159
154
|
message.userAgent = this.userAgent;
|
|
160
155
|
message.ts = this.trustedClock.getTimeAsYYYYMMDD();
|
|
@@ -168,8 +163,7 @@ export default class RequestReporter {
|
|
|
168
163
|
message.payload.ctry = this.countryProvider.getSafeCountryCode();
|
|
169
164
|
|
|
170
165
|
logger.debug('report', message);
|
|
171
|
-
|
|
172
|
-
this.communication.send(message);
|
|
166
|
+
this.onMessageReady(message);
|
|
173
167
|
}
|
|
174
168
|
|
|
175
169
|
/** Global module initialisation.
|
|
@@ -717,6 +717,48 @@ describe('#titlesMatchAfterDoublefetch', function () {
|
|
|
717
717
|
}
|
|
718
718
|
});
|
|
719
719
|
});
|
|
720
|
+
|
|
721
|
+
describe('should tolerate removing a limited amount of chars in a longer title', function () {
|
|
722
|
+
describe('if most of the text is still preserved', function () {
|
|
723
|
+
for (const { before, after } of [
|
|
724
|
+
{
|
|
725
|
+
before:
|
|
726
|
+
'Darts-WM 2025: Alle Spiele heute live in TV und Stream schauen - COMPUTER BILD',
|
|
727
|
+
after:
|
|
728
|
+
'Darts-WM 2025: Alle Spiele live in TV und Stream schauen - COMPUTER BILD',
|
|
729
|
+
},
|
|
730
|
+
{
|
|
731
|
+
before:
|
|
732
|
+
'Darts-WM 2025: Alle Spiele heute live in TV und Stream schauen - COMPUTER BILD',
|
|
733
|
+
after:
|
|
734
|
+
'Darts-WM: Alle Spiele heute live in TV und Stream schauen - COMPUTER BILD',
|
|
735
|
+
},
|
|
736
|
+
]) {
|
|
737
|
+
it(`- ${before} --> ${after}`, function () {
|
|
738
|
+
shouldMatch(before, after);
|
|
739
|
+
});
|
|
740
|
+
}
|
|
741
|
+
});
|
|
742
|
+
|
|
743
|
+
describe('but reject if too much is removed', function () {
|
|
744
|
+
for (const { before, after } of [
|
|
745
|
+
{
|
|
746
|
+
before:
|
|
747
|
+
'Darts-WM 2025: Alle Spiele heute live in TV und Stream schauen - COMPUTER BILD',
|
|
748
|
+
after: 'Darts-WM 2025',
|
|
749
|
+
},
|
|
750
|
+
{
|
|
751
|
+
before:
|
|
752
|
+
'Darts-WM 2025: Alle Spiele heute live in TV und Stream schauen - COMPUTER BILD',
|
|
753
|
+
after: 'COMPUTER BILD',
|
|
754
|
+
},
|
|
755
|
+
]) {
|
|
756
|
+
it(`- ${before} --> ${after}`, function () {
|
|
757
|
+
shouldNotMatch(before, after);
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
});
|
|
761
|
+
});
|
|
720
762
|
});
|
|
721
763
|
|
|
722
764
|
describe('#sanitizeActivity', function () {
|
|
@@ -87,7 +87,7 @@ describe('RequestReporter', function () {
|
|
|
87
87
|
context('with pre-recorded events', function () {
|
|
88
88
|
let reporter;
|
|
89
89
|
let clock;
|
|
90
|
-
const
|
|
90
|
+
const communicationEmitter = new EventEmitter();
|
|
91
91
|
|
|
92
92
|
beforeEach(async function () {
|
|
93
93
|
globalThis.indexedDB = new IDBFactory();
|
|
@@ -100,18 +100,11 @@ describe('RequestReporter', function () {
|
|
|
100
100
|
return '';
|
|
101
101
|
},
|
|
102
102
|
};
|
|
103
|
-
|
|
104
|
-
const communication = {
|
|
105
|
-
send(msg) {
|
|
106
|
-
communicationEmiter.emit('send', msg);
|
|
107
|
-
},
|
|
108
|
-
sendInstant(msg) {
|
|
109
|
-
communicationEmiter.emit('sendInstant', msg);
|
|
110
|
-
},
|
|
111
|
-
trustedClock,
|
|
112
|
-
};
|
|
103
|
+
communicationEmitter.removeAllListeners();
|
|
113
104
|
reporter = new RequestReporter(config, {
|
|
114
|
-
|
|
105
|
+
onMessageReady: (msg) => {
|
|
106
|
+
communicationEmitter.emit('send', msg);
|
|
107
|
+
},
|
|
115
108
|
trustedClock,
|
|
116
109
|
getBrowserInfo: () => ({ name: 'xx' }),
|
|
117
110
|
countryProvider: { getSafeCountryCode: () => 'en' },
|
|
@@ -129,11 +122,11 @@ describe('RequestReporter', function () {
|
|
|
129
122
|
|
|
130
123
|
context('synthetic events', function () {
|
|
131
124
|
it('records stats from redirects', async function () {
|
|
132
|
-
const events = once(
|
|
125
|
+
const events = once(communicationEmitter, 'send');
|
|
133
126
|
|
|
134
127
|
const tab = { id: 1, url: 'https://www.onet.pl/' };
|
|
135
128
|
// creates a page
|
|
136
|
-
chrome.tabs.onCreated.dispatch(tab)
|
|
129
|
+
chrome.tabs.onCreated.dispatch(tab);
|
|
137
130
|
// changes state to 'completed'
|
|
138
131
|
chrome.webNavigation.onCompleted.dispatch({ tabId: tab.id, frameId: 0, });
|
|
139
132
|
|
|
@@ -221,7 +214,7 @@ describe('RequestReporter', function () {
|
|
|
221
214
|
});
|
|
222
215
|
await clock.runToLast();
|
|
223
216
|
const eventPromise = new Promise((resolve) =>
|
|
224
|
-
|
|
217
|
+
communicationEmitter.once('send', resolve),
|
|
225
218
|
);
|
|
226
219
|
// force stage all pages
|
|
227
220
|
seenTabIds.forEach((tabId) => chrome.tabs.onRemoved.dispatch(tabId));
|
|
@@ -237,7 +230,7 @@ describe('RequestReporter', function () {
|
|
|
237
230
|
context('0004-ping', function () {
|
|
238
231
|
it('reports pings', async function () {
|
|
239
232
|
const eventPromise = new Promise((resolve) =>
|
|
240
|
-
|
|
233
|
+
communicationEmitter.once('send', resolve),
|
|
241
234
|
);
|
|
242
235
|
await playScenario(chrome, {
|
|
243
236
|
scenarioName: this.test.parent.title,
|
|
@@ -261,7 +254,7 @@ describe('RequestReporter', function () {
|
|
|
261
254
|
});
|
|
262
255
|
await clock.runToLast();
|
|
263
256
|
const eventPromise = new Promise((resolve) =>
|
|
264
|
-
|
|
257
|
+
communicationEmitter.once('send', resolve),
|
|
265
258
|
);
|
|
266
259
|
// force stage all pages
|
|
267
260
|
seenTabIds.forEach((tabId) => chrome.tabs.onRemoved.dispatch(tabId));
|
|
@@ -277,7 +270,7 @@ describe('RequestReporter', function () {
|
|
|
277
270
|
context('0008-navigation', function () {
|
|
278
271
|
it('reports 3rd parties', async function () {
|
|
279
272
|
const eventPromise1 = new Promise((resolve) =>
|
|
280
|
-
|
|
273
|
+
communicationEmitter.once('send', resolve),
|
|
281
274
|
);
|
|
282
275
|
const { seenTabIds } = await playScenario(chrome, {
|
|
283
276
|
scenarioName: this.test.parent.title,
|
|
@@ -290,7 +283,7 @@ describe('RequestReporter', function () {
|
|
|
290
283
|
});
|
|
291
284
|
expect(event1.payload.data[0].tps).to.have.keys(['script1.localhost']);
|
|
292
285
|
const eventPromise2 = new Promise((resolve) =>
|
|
293
|
-
|
|
286
|
+
communicationEmitter.once('send', resolve),
|
|
294
287
|
);
|
|
295
288
|
// force stage all pages
|
|
296
289
|
seenTabIds.forEach((tabId) => chrome.tabs.onRemoved.dispatch(tabId));
|
|
@@ -329,7 +322,7 @@ describe('RequestReporter', function () {
|
|
|
329
322
|
for (const snapshotName of ['0001', '0002', '0003', '0004', '0005', '0006']) {
|
|
330
323
|
it(snapshotName, async function () {
|
|
331
324
|
const messages = [];
|
|
332
|
-
|
|
325
|
+
communicationEmitter.addListener('send', (message) =>
|
|
333
326
|
messages.push(cleanupMessage(message)),
|
|
334
327
|
);
|
|
335
328
|
playSnapshotScenario(chrome, snapshotName);
|