@whotracksme/reporting 6.0.0 → 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 +12 -0
- package/package.json +1 -1
- package/reporting/src/http.js +235 -167
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
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
|
+
|
|
1
13
|
# v6.0.0 (Wed Jan 15 2025)
|
|
2
14
|
|
|
3
15
|
#### 💥 Breaking Change
|
package/package.json
CHANGED
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
|
}
|