-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSyncMessage.js
549 lines (506 loc) · 18.7 KB
/
SyncMessage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
* NoScript Commons Library
* Reusable building blocks for cross-browser security/privacy WebExtensions.
* Copyright (C) 2020-2024 Giorgio Maone <https://maone.net>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
// depends on /nscl/common/uuid.js
"use strict";
if (!["onSyncMessage", "sendSyncMessage"].some((m) => browser.runtime[m])) {
const MOZILLA =
self.XMLHttpRequest && "mozSystem" in self.XMLHttpRequest.prototype;
const ENDPOINT_ORIGIN = "https://[ff00::]";
const ENDPOINT_PREFIX = `${ENDPOINT_ORIGIN}/nscl/${browser.runtime.getURL(
"syncMessage"
)}?`;
const msgUrl = (msgId) => `${ENDPOINT_PREFIX}id=${encodeURIComponent(msgId)}`;
// https://github.com/w3c/webappsec-permissions-policy/blob/main/permissions-policy-explainer.md#appendix-big-changes-since-this-was-called-feature-policy
const allowSyncXhr = (policy) =>
policy
.replace(/(?:[,;]\s*)?\b(?:sync-xhr\b[^;,]*)/gi, "")
.replace(/^\s*[;,]\s*/, "");
if (browser.webRequest) {
// Background script / event page / service worker
let anyMessageYet = false;
const retries = new Set();
// we don't care this is async, as long as it get called before the
// sync XHR (we are not interested in the response on the content side)
browser.runtime.onMessage.addListener((m, sender) => {
let wrapper = m.__syncMessage__;
if (!wrapper) return;
if(wrapper.retry) {
const retryKey = `${sender.tab.id}:${sender.frameId}:${sender.url}`;
let retried = retries.has(retryKey);
if (retried) {
retries.delete(retryKey);
} else {
retries.add(retryKey);
}
console.debug(`SyncMessage retry ${retried ? "(giving up)" : "now" }.`, retryKey); // DEV_ONLY
return Promise.resolve(!retried);
}
if (wrapper.release) {
suspender.release(wrapper.id);
} else if ("payload" in wrapper) {
anyMessageYet = true;
wrapper.result = Promise.resolve(
notifyListeners(JSON.stringify(wrapper.payload), sender)
);
suspender.hold(wrapper);
}
return Promise.resolve(null);
});
const asyncResults = new Map();
const ret = (r) => ({
redirectUrl: `data:application/json,${
encodeURIComponent(JSON.stringify(r))}`,
});
const res = (payload) => ({ payload });
const err = (e) => ({ error: { message: e.message, stack: e.stack } });
const LOOP_RET = ret({ loop: 1 });
const asyncRet = (msgId) => {
let chunks = asyncResults.get(msgId);
let chunk = chunks.shift();
let more = chunks.length;
if (more === 0) {
asyncResults.delete(msgId);
suspender.release(msgId);
}
return ret({ chunk, more });
};
const CHUNK_SIZE = 500000; // Work around any browser-dependent URL limit
const storeAsyncRet = (msgId, r) => {
r = JSON.stringify(r);
const len = r === undefined ? 0 : r.length;
const chunksCount = Math.ceil(len / CHUNK_SIZE);
const chunks = [];
for (let j = 0; j < chunksCount; j++) {
chunks.push(r.substr(j * CHUNK_SIZE, CHUNK_SIZE));
}
asyncResults.set(msgId, chunks);
};
const listeners = new Set();
function notifyListeners(msg, sender) {
// Just like in the async runtime.sendMessage() API,
// we process the listeners in order until we find a not undefined
// result, then we return it (or undefined if none returns anything).
for (let l of listeners) {
try {
let result = l(JSON.parse(msg), sender);
if (result !== undefined) return result;
} catch (e) {
console.error("%o processing message %o from %o", e, msg, sender);
}
}
}
const suspender = (
browser.declarativeNetRequest && !MOZILLA
? () => {
// MV3
const DNR_BASE_ID = 65535;
const DNR_BASE_PRIORITY = 1000;
let lastRuleId = DNR_BASE_ID;
const msg2redirector = new Map();
const { redirectUrl } = LOOP_RET;
const resourceTypes = ["xmlhttprequest"];
const createRedirector = async (
urlFilter,
redirectUrl,
options
) => {
const DEFAULT_OPTIONS = {
ruleSet: "Session",
priority: DNR_BASE_PRIORITY + 10,
addRules: [],
removeRuleIds: []
}
let { ruleSet, priority, addRules, removeRuleIds } = Object.assign(
{},
DEFAULT_OPTIONS,
options
);
const rule = {
id: ++lastRuleId,
priority,
action: {
type: "redirect",
redirect: { url: redirectUrl },
},
condition: {
urlFilter,
resourceTypes,
},
};
console.debug("Creating rule ", rule); // DEV_ONLY
addRules.push(rule);
const method = `update${ruleSet}Rules`;
await browser.declarativeNetRequest[method]({
addRules,
removeRuleIds,
});
return lastRuleId;
};
const removeRedirector = (redirId) => {
browser.declarativeNetRequest.updateSessionRules({
removeRuleIds: [redirId],
});
};
(async () => {
const allowSyncXhrRules = [
{
id: ++lastRuleId,
priority: DNR_BASE_PRIORITY,
action: {
type: "modifyHeaders",
// Note: notwithstanding poor documentation, looks like in modern browsers
// permissions-policy overrides (document|feature)-policy, & DNR appending
// to the header overrides the restrictive token despite inheritance rules,
// making the following hack work, quite surprisingly and nicely (i.e.
// other policies, if present, remain effective).
responseHeaders: [
{
header: "permissions-policy",
operation: "append",
value: "sync-xhr=*",
},
],
},
condition: {
resourceTypes: ["main_frame", "sub_frame"],
},
},
];
for (const ruleSet of ["Dynamic", "Session"]) {
try {
const removeRuleIds = (
await browser.declarativeNetRequest[`get${ruleSet}Rules`]()
)
.map((r) => r.id)
.filter((id) => id >= DNR_BASE_ID);
const options = {
ruleSet,
priority: DNR_BASE_PRIORITY,
addRules: allowSyncXhrRules,
removeRuleIds,
};
await createRedirector(
`|${ENDPOINT_PREFIX}*`,
redirectUrl,
options
);
} catch (e) {
console.error(e, "Error initializing SyncMessage DNR responders.");
}
}
})();
return {
async hold(wrapper) {
let result;
try {
result = ret(res(await wrapper.result));
} catch (e) {
result = ret(err(e));
}
const { id } = wrapper;
const urlFilter = `|${msgUrl(wrapper.id)}`;
const redirId = await createRedirector(urlFilter, result.redirectUrl);
msg2redirector.set(id, redirId);
},
release(id) {
const redirId = msg2redirector.get(id);
if (!redirId) return;
msg2redirector.delete(id);
removeRedirector(redirId);
},
};
}
: () => {
// MV2
const pending = new Map();
const CANCEL = { cancel: true };
const onBeforeRequest = (request) => {
try {
const { url } = request;
const shortUrl = url.replace(ENDPOINT_PREFIX, "");
const params = new URLSearchParams(url.split("?")[1]);
const msgId = params.get("id");
const chromeRet = (resultReady) => {
const r = resultReady
? asyncRet(msgId) // promise was already resolved
: LOOP_RET;
console.debug("SyncMessage XHR->webRequest %s returning %o", shortUrl, r, request); // DEV_ONLY
return r;
};
if (asyncResults.has(msgId)) {
return chromeRet(true);
}
const wrapper = pending.get(msgId);
console.debug(`PENDING ${shortUrl}: ${JSON.stringify(wrapper)}`, request); // DEV_ONLY
if (!wrapper) {
return anyMessageYet
? CANCEL // cannot reconcile with any pending message, abort
: LOOP_RET; // never received any message yet, retry
}
if (MOZILLA) {
// this should be a mozilla suspension request
return (async () => {
try {
return ret(res(await wrapper.result));
} catch (e) {
return ret(err(e));
} finally {
pending.delete(msgId);
}
})();
}
// CHROMIUM from now on
// On Chromium, if the promise is not resolved yet,
// we redirect the XHR to the same URL (hence same msgId)
// while the result get cached for asynchronous retrieval
wrapper.result.then(
(r) => storeAsyncRet(msgId, res(r)),
(e) => storeAsyncRet(msgId, err(e))
);
return chromeRet(asyncResults.has(msgId));
} catch (e) {
console.error(e);
return CANCEL;
}
};
const NOP = () => {};
let bug1899786 = NOP;
if (browser.webRequest.filterResponseData) {
bug1899786 = (request) => {
// work-around for https://bugzilla.mozilla.org/show_bug.cgi?id=1899786
let compressed = false,
xml = false;
for (const { name, value } of request.responseHeaders) {
switch (name.toLowerCase()) {
case "content-encoding":
if (
compressed ||
!(compressed =
/^(?:gzip|compress|deflate|br|zstd)$/i.test(value))
) {
continue;
}
break;
case "content-type":
if (xml || !(xml = /\bxml\b/i.test(value))) {
continue;
}
break;
default:
continue;
}
if (compressed && xml) {
console.log("Applying mozbug 1899786 work-around", request);
const filter = browser.webRequest.filterResponseData(
request.requestId
);
filter.ondata = (e) => {
filter.write(e.data);
};
filter.onstop = () => {
filter.close();
};
break;
}
}
};
(async () => {
const version = parseInt(
(await browser.runtime.getBrowserInfo()).version
);
if (version < 126) bug1899786 = NOP;
})();
}
const onHeadersReceived = (request) => {
let replaced = false;
let { responseHeaders } = request;
let rxPolicy = /^(?:feature|permissions|document)-policy$/i;
for (let h of request.responseHeaders) {
if (rxPolicy.test(h.name)) {
const value = allowSyncXhr(h.value);
if (value !== h.value) {
replaced = true;
h.value = value;
}
}
}
bug1899786(request);
return replaced ? { responseHeaders } : null;
};
browser.webRequest.onBeforeRequest.addListener(
onBeforeRequest,
{
urls: [`${ENDPOINT_PREFIX}*`],
types: ["xmlhttprequest"],
},
["blocking"]
);
browser.webRequest.onHeadersReceived.addListener(
onHeadersReceived,
{
urls: ["<all_urls>"],
types: ["main_frame", "sub_frame"],
},
["blocking", "responseHeaders"]
);
return {
hold(wrapper) {
pending.set(wrapper.id, wrapper);
},
release(id) {
pending.delete(id);
},
};
}
)();
browser.runtime.onSyncMessage = Object.freeze({
ENDPOINT_PREFIX,
addListener(l) {
listeners.add(l);
},
removeListener(l) {
listeners.delete(l);
},
hasListener(l) {
return listeners.has(l);
},
isMessageRequest(request) {
return (
request.type === "xmlhttprequest" &&
request.url.startsWith(ENDPOINT_PREFIX)
);
},
});
} else {
// Content Script side
{
// re-enable Sync XHR if disabled by featurePolicy
const allow = f => {
if (f.allow) {
const allowingValue = allowSyncXhr(f.allow);
if (f.allow != allowingValue) {
f.allow = allowingValue;
console.debug("Allowing Sync XHR on ", f, f.allow); // DEV_ONLY
f.src = f.src;
}
}
};
try {
// this is probably useless, but nontheless...
window.frameElement && allow(window.frameElement);
} catch (e) {
// SOP violation?
console.error(e); // DEV_ONLY
}
const mutationsCallback = records => {
for (var r of records) {
switch (r.type) {
case "attributes":
allow(r.target);
break;
case "childList":
[...r.addedNodes].forEach(allow);
break;
}
}
};
const observer = new MutationObserver(mutationsCallback);
observer.observe(document.documentElement, {
childList: true,
subtree: true,
attributeFilter: ["allow"],
});
}
const docId = uuid();
browser.runtime.sendSyncMessage = (msg) => {
let msgId = `${uuid()}:${docId}`;
let url = msgUrl(msgId);
const preSend = __syncMessage__ => browser.runtime.sendMessage({__syncMessage__});
// We first need to send an async message with both the payload
// and "trusted" sender metadata, along with an unique msgId to
// reconcile with in the retrieval phase via synchronous XHR
const preflight = preSend({ id: msgId, payload: msg });
// Now go retrieve the result!
const MAX_LOOPS = 1000;
let r = new XMLHttpRequest();
let result;
let chunks = [];
for (let loop = 0; ; ) {
try {
r.open("GET", url, false);
r.send(null);
result = JSON.parse(r.responseText);
if ("chunk" in result) {
let { chunk, more } = result;
chunks.push(chunk);
if (more) {
continue;
}
result = JSON.parse(chunks.join(""));
} else if (result.loop) {
if (++loop > MAX_LOOPS) {
console.debug(
"Too many loops (%s), look for deadlock conditions.",
loop
);
throw new Error("Too many SyncMessage loops!");
}
console.debug(`SyncMessage ${msgId} waiting for main process asynchronous processing, loop ${loop}/${MAX_LOOPS}.`); // DEV_ONLY
continue;
} else if (result.error) {
result.error = new Error(result.error.message, result.error);
}
} catch (e) {
console.error(e,
`SyncMessage ${msgId} error in ${document.URL}: ${e.message} (response ${r.responseURL} ${r.responseText})`
);
result = {
error: new Error(`SyncMessage Error ${e.message}`, { cause: e }),
};
}
break;
}
preSend({ id: msgId, release: true });
console.debug(`SyncMessage ${msgId}, state ${ document.readyState }, result: ${JSON.stringify(result)}`); // DEV_ONLY
if (result.error) {
if (document.featurePolicy && !document.featurePolicy?.allowsFeature("sync-xhr")) {
throw new Error(`SyncMessage fails on ${document.URL} because sync-xhr is not allowed!`);
}
if (document.readyState == "loading" && /Failed to load/.test(result.error.message)) {
window.stop();
(async () => {
try {
await preflight;
browser.runtime.sendSyncMessage(msg);
} catch (e) {
console.error(e, `SyncMessage immediate retry failed on ${document.URL}!`);
if (!(await preSend({retry: true}))) {
return;
}
}
history.go(0);
})();
}
throw result.error;
}
return result.payload;
};
}
}