-
Notifications
You must be signed in to change notification settings - Fork 506
/
Copy pathrequests.js
607 lines (581 loc) · 20 KB
/
requests.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import {
blob2base64, buffer2string, getUniqId, request, i18n, isEmpty, noop, sendTabCmd,
string2uint8array,
} from '#/common';
import { forEachEntry, objectPick } from '#/common/object';
import ua from '#/common/ua';
import cache from './cache';
import { isUserScript, parseMeta } from './script';
import { extensionRoot } from './init';
import { commands } from './message';
const VM_VERIFY = 'VM-Verify';
/** @type {Object<string,VMHttpRequest>} */
const requests = {};
const verify = {};
const tabRequests = {};
let encoder;
Object.assign(commands, {
ConfirmInstall: confirmInstall,
/** @return {void} */
HttpRequest(opts, src) {
const { tab: { id: tabId }, frameId } = src;
const { id, eventsToNotify } = opts;
requests[id] = {
id,
tabId,
eventsToNotify,
xhr: new XMLHttpRequest(),
};
(tabRequests[tabId] || (tabRequests[tabId] = {}))[id] = 1;
httpRequest(opts, src, res => requests[id] && (
sendTabCmd(tabId, 'HttpRequested', res, { frameId })
));
},
/** @return {void} */
AbortRequest(id) {
const req = requests[id];
if (req) {
req.xhr.abort();
clearRequest(req);
}
},
RevokeBlob(url) {
const timer = cache.pop(`xhrBlob:${url}`);
if (timer) {
clearTimeout(timer);
URL.revokeObjectURL(url);
}
},
});
const specialHeaders = [
'user-agent',
// https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name
// https://cs.chromium.org/?q=file:cc+symbol:IsForbiddenHeader%5Cb
'accept-charset',
'accept-encoding',
'access-control-request-headers',
'access-control-request-method',
'connection',
'content-length',
'cookie',
'cookie2',
'date',
'dnt',
'expect',
'host',
'keep-alive',
'origin',
'referer',
'te',
'trailer',
'transfer-encoding',
'upgrade',
'via',
];
// const tasks = {};
const HeaderInjector = (() => {
/** @type chrome.webRequest.RequestFilter */
const apiFilter = {
urls: ['<all_urls>'],
types: ['xmlhttprequest'],
};
const EXTRA_HEADERS = [
browser.webRequest.OnBeforeSendHeadersOptions.EXTRA_HEADERS,
].filter(Boolean);
const headersToInject = {};
/** @param {chrome.webRequest.HttpHeader} header */
const isVmVerify = header => header.name === VM_VERIFY;
const isNotCookie = header => !/^cookie2?$/i.test(header.name);
const isSendable = header => header.name !== VM_VERIFY;
const isSendableAnon = header => isSendable(header) && isNotCookie(header);
const RE_SET_COOKIE = /^set-cookie2?$/i;
const RE_SET_COOKIE_VALUE = /^\s*(?:__(Secure|Host)-)?([^=\s]+)\s*=\s*(")?([!#-+\--:<-[\]-~]*)\3(.*)/;
const RE_SET_COOKIE_ATTR = /\s*;?\s*(\w+)(?:=(")?([!#-+\--:<-[\]-~]*)\2)?/y;
const SAME_SITE_MAP = {
strict: 'strict',
lax: 'lax',
none: 'no_restriction',
};
/**
* @param {string} headerValue
* @param {VMHttpRequest} req
* @param {string} url
*/
const setCookieInStore = (headerValue, req, url) => {
let m = RE_SET_COOKIE_VALUE.exec(headerValue);
if (m) {
const [, prefix, name, , value, optStr] = m;
const opt = {};
const isHost = prefix === 'Host';
RE_SET_COOKIE_ATTR.lastIndex = 0;
while ((m = RE_SET_COOKIE_ATTR.exec(optStr))) {
opt[m[1].toLowerCase()] = m[3];
}
const sameSite = opt.sameSite?.toLowerCase();
browser.cookies.set({
url,
name,
value,
domain: isHost ? undefined : opt.domain,
expirationDate: Math.max(0, +new Date(opt['max-age'] * 1000 || opt.expires)) || undefined,
httpOnly: 'httponly' in opt,
path: isHost ? '/' : opt.path,
sameSite: SAME_SITE_MAP[sameSite],
secure: url.startsWith('https:') && (!!prefix || sameSite === 'none' || 'secure' in opt),
storeId: req.storeId,
});
}
};
const apiEvents = {
onBeforeSendHeaders: {
options: ['requestHeaders', 'blocking', ...EXTRA_HEADERS],
/** @param {chrome.webRequest.WebRequestHeadersDetails} details */
listener({ requestHeaders: headers, requestId }) {
// only the first call during a redirect/auth chain will have VM-Verify header
const reqId = headers.find(isVmVerify)?.value || verify[requestId];
const req = reqId && requests[reqId];
if (reqId && req) {
verify[requestId] = reqId;
req.coreId = requestId;
headers = (req.noNativeCookie ? headers.filter(isNotCookie) : headers)
.concat(headersToInject[reqId] || [])
.filter(req.anonymous ? isSendableAnon : isSendable);
}
return { requestHeaders: headers };
},
},
onHeadersReceived: {
options: ['responseHeaders', 'blocking', ...EXTRA_HEADERS],
/** @param {chrome.webRequest.WebRequestHeadersDetails} details */
listener({ responseHeaders: headers, requestId, url }) {
const req = requests[verify[requestId]];
if (req) {
if (req.anonymous || req.storeId) {
headers = headers.filter(h => (
!RE_SET_COOKIE.test(h.name)
|| !req.storeId
|| setCookieInStore(h.value, req, url)
));
}
req.responseHeaders = headers.map(encodeWebRequestHeader).join('');
return { responseHeaders: headers };
}
},
},
};
// Chrome 74-91 needs an extraHeaders listener at tab load start, https://crbug.com/1074282
// We're attaching a no-op in non-blocking mode so it's very lightweight and fast.
if (ua.isChrome >= 74 && ua.isChrome <= 91) {
browser.webRequest.onBeforeSendHeaders.addListener(noop, apiFilter, ['extraHeaders']);
}
return {
add(reqId, headers) {
// need to set the entry even if it's empty [] so that 'if' check in del() runs only once
headersToInject[reqId] = headers;
// need the listener to get the requestId
apiEvents::forEachEntry(([name, { listener, options }]) => {
browser.webRequest[name].addListener(listener, apiFilter, options);
});
},
del(reqId) {
if (reqId in headersToInject) {
delete headersToInject[reqId];
if (isEmpty(headersToInject)) {
apiEvents::forEachEntry(([name, { listener }]) => {
browser.webRequest[name].removeListener(listener);
});
}
}
},
};
})();
/* 1MB takes ~20ms to encode/decode so it doesn't block the process of the extension and web page,
* which lets us and them be responsive to other events or user input. */
const CHUNK_SIZE = 1e6;
async function blob2chunk(response, index) {
return blob2base64(response, index * CHUNK_SIZE, CHUNK_SIZE);
}
function blob2objectUrl(response) {
const url = URL.createObjectURL(response);
cache.put(`xhrBlob:${url}`, setTimeout(commands.RevokeBlob, 60e3, url), 61e3);
return url;
}
/** @param {VMHttpRequest} req */
function xhrCallbackWrapper(req) {
let lastPromise = Promise.resolve();
let contentType;
let dataSize;
let numChunks;
let response;
let responseText;
let responseHeaders;
let sent = false;
const { id, blobbed, chunked, xhr } = req;
// Chrome encodes messages to UTF8 so they can grow up to 4x but 64MB is the message size limit
const getChunk = blobbed && blob2objectUrl || chunked && blob2chunk;
const getResponseHeaders = () => {
const headers = req.responseHeaders || xhr.getAllResponseHeaders();
if (responseHeaders !== headers) {
responseHeaders = headers;
return { responseHeaders };
}
};
return (evt) => {
const type = evt.type;
if (!contentType) {
contentType = xhr.getResponseHeader('Content-Type') || 'application/octet-stream';
}
if (xhr.response !== response) {
response = xhr.response;
sent = false;
try {
responseText = xhr.responseText;
if (responseText === response) responseText = ['same'];
} catch (e) {
// ignore if responseText is unreachable
}
if ((blobbed || chunked) && response) {
dataSize = response.size;
numChunks = chunked && Math.ceil(dataSize / CHUNK_SIZE) || 1;
}
}
const shouldNotify = req.eventsToNotify.includes(type);
// only send response when XHR is complete
const shouldSendResponse = xhr.readyState === 4 && shouldNotify && !sent;
lastPromise = lastPromise.then(async () => {
await req.cb({
blobbed,
chunked,
contentType,
dataSize,
id,
numChunks,
type,
data: shouldNotify && {
finalUrl: xhr.responseURL,
...getResponseHeaders(),
...objectPick(xhr, ['readyState', 'status', 'statusText']),
...('loaded' in evt) && objectPick(evt, ['lengthComputable', 'loaded', 'total']),
response: shouldSendResponse
? numChunks && await getChunk(response, 0) || response
: null,
responseText: shouldSendResponse
? responseText
: null,
},
});
if (shouldSendResponse) {
for (let i = 1; i < numChunks; i += 1) {
await req.cb({
id,
chunk: {
pos: i * CHUNK_SIZE,
data: await getChunk(response, i),
last: i + 1 === numChunks,
},
});
}
}
if (type === 'loadend') {
clearRequest(req);
}
});
};
}
function isSpecialHeader(lowerHeader) {
return specialHeaders.includes(lowerHeader)
|| lowerHeader.startsWith('proxy-')
|| lowerHeader.startsWith('sec-');
}
/**
* @param {Object} opts
* @param {chrome.runtime.MessageSender | browser.runtime.MessageSender} src
* @param {function} cb
*/
async function httpRequest(opts, src, cb) {
const { tab } = src;
const { incognito } = tab;
const { anonymous, id, overrideMimeType, responseType, url } = opts;
const req = requests[id];
if (!req || req.cb) return;
req.cb = cb;
req.anonymous = anonymous;
const { xhr } = req;
const vmHeaders = [];
const FF = ua.isFirefox;
// Firefox can send Blob/ArrayBuffer directly
const chunked = !FF && incognito;
const blobbed = responseType && !FF && !incognito;
const [body, contentType] = decodeBody(opts.data);
// Chrome can't fetch Blob URL in incognito so we use chunks
req.blobbed = blobbed;
req.chunked = chunked;
// Firefox doesn't send cookies, https://github.com/violentmonkey/violentmonkey/issues/606
// Both Chrome & FF need explicit routing of cookies in containers or incognito
let shouldSendCookies = !anonymous && (incognito || FF);
xhr.open(opts.method || 'GET', url, true, opts.user || '', opts.password || '');
xhr.setRequestHeader(VM_VERIFY, id);
if (contentType) xhr.setRequestHeader('Content-Type', contentType);
opts.headers::forEachEntry(([name, value]) => {
const lowerName = name.toLowerCase();
if (isSpecialHeader(lowerName)) {
vmHeaders.push({ name, value });
} else if (!lowerName.startsWith('vm-')) {
// `VM-` headers are reserved
xhr.setRequestHeader(name, value);
}
if (lowerName === 'cookie') {
shouldSendCookies = false;
}
});
xhr.responseType = (chunked || blobbed) && 'blob' || responseType || 'text';
xhr.timeout = Math.max(0, Math.min(0x7FFF_FFFF, opts.timeout)) || 0;
if (overrideMimeType) xhr.overrideMimeType(overrideMimeType);
if (shouldSendCookies) {
req.noNativeCookie = true;
for (const store of await browser.cookies.getAllCookieStores()) {
if (store.tabIds.includes(tab.id)) {
if (FF ? store.id !== 'firefox-default' : store.id !== '0') {
/* Cookie routing. For the main store we rely on the browser.
* The ids are hard-coded as `stores` may omit the main store if no such tabs are open. */
req.storeId = store.id;
}
break;
}
}
const now = Date.now() / 1000;
const cookies = (await browser.cookies.getAll({
url,
storeId: req.storeId,
...FF >= 59 && { firstPartyDomain: null },
})).filter(c => c.session || c.expirationDate > now); // FF reports expired cookies!
if (cookies.length) {
vmHeaders.push({
name: 'cookie',
value: cookies.map(c => `${c.name}=${c.value};`).join(' '),
});
}
}
HeaderInjector.add(id, vmHeaders);
const callback = xhrCallbackWrapper(req);
req.eventsToNotify.forEach(evt => { xhr[`on${evt}`] = callback; });
xhr.onloadend = callback; // always send it for the internal cleanup
xhr.send(body);
}
/** @param {VMHttpRequest} req */
function clearRequest(req) {
if (req.coreId) delete verify[req.coreId];
delete requests[req.id];
delete (tabRequests[req.tabId] || {})[req.id];
HeaderInjector.del(req.id);
}
/** Polyfill for Chrome's inability to send complex types over extension messaging */
function decodeBody([body, type, wasBlob]) {
if (type === 'query') {
type = 'application/x-www-form-urlencoded';
} else if (type) {
// 5x times faster than fetch() which wastes time on inter-process communication
const res = string2uint8array(atob(body.slice(body.indexOf(',') + 1)));
if (!wasBlob) {
type = body.match(/^data:(.+?);base64/)[1].replace(/(boundary=)[^;]+/,
// using a function so it runs only if "boundary" was found
(_, p1) => p1 + String.fromCharCode(...res.slice(2, res.indexOf(13))));
}
body = res;
}
return [body, type];
}
// Watch URL redirects
// browser.webRequest.onBeforeRedirect.addListener(details => {
// const reqId = verify[details.requestId];
// if (reqId) {
// const req = requests[reqId];
// if (req) req.finalUrl = details.redirectUrl;
// }
// }, {
// urls: ['<all_urls>'],
// types: ['xmlhttprequest'],
// });
// tasks are not necessary now, turned off
// Stop redirects
// browser.webRequest.onHeadersReceived.addListener(details => {
// const task = tasks[details.requestId];
// if (task) {
// delete tasks[details.requestId];
// if (task === 'Get-Location' && [301, 302, 303].includes(details.statusCode)) {
// const locationHeader = details.responseHeaders.find(
// header => header.name.toLowerCase() === 'location');
// const base64 = locationHeader && locationHeader.value;
// return {
// redirectUrl: `data:text/plain;charset=utf-8,${base64 || ''}`,
// };
// }
// }
// }, {
// urls: ['<all_urls>'],
// types: ['xmlhttprequest'],
// }, ['blocking', 'responseHeaders']);
// browser.webRequest.onCompleted.addListener(details => {
// delete tasks[details.requestId];
// }, {
// urls: ['<all_urls>'],
// types: ['xmlhttprequest'],
// });
// browser.webRequest.onErrorOccurred.addListener(details => {
// delete tasks[details.requestId];
// }, {
// urls: ['<all_urls>'],
// types: ['xmlhttprequest'],
// });
async function confirmInstall({ code, from, url }, { tab = {} }) {
if (!code) code = (await request(url)).data;
// TODO: display the error in UI
if (!isUserScript(code)) throw i18n('msgInvalidScript');
cache.put(url, code, 3000);
const confirmKey = getUniqId();
const { active, id: tabId, incognito } = tab;
// Not testing tab.pendingUrl because it will be always equal to `url`
const canReplaceCurTab = (!incognito || ua.isFirefox) && (
url === from
|| cache.has(`autoclose:${tabId}`)
|| /^(chrome:\/\/(newtab|startpage)\/|about:(home|newtab))$/.test(from));
cache.put(`confirm-${confirmKey}`, { incognito, url, from, tabId });
const confirmUrl = `/confirm/index.html#${confirmKey}`;
const { windowId } = canReplaceCurTab
? await browser.tabs.update(tabId, { url: confirmUrl })
: await commands.TabOpen({ url: confirmUrl, active: !!active }, { tab });
if (active && windowId !== tab.windowId) {
await browser.windows.update(windowId, { focused: true });
}
}
const whitelistRe = new RegExp(`^https://(${
[
'greasyfork\\.org/scripts/%/code/',
'openuserjs\\.org/install/%/',
'github\\.com/%/%/raw/%/',
'github\\.com/%/%/releases/%/download/',
'raw\\.githubusercontent\\.com(/%){3}/',
'gist\\.github\\.com/.*?/',
].join('|')
})%?\\.user\\.js([?#]|$)`.replace(/%/g, '[^/]*'));
const blacklistRe = new RegExp(`^https://(${
[
'(gist\\.)?github\\.com',
'greasyfork\\.org',
'openuserjs\\.org',
].join('|')
})/`);
const resolveVirtualUrl = url => (
`${extensionRoot}options/index.html#scripts/${+url.split('#')[1]}`
);
// FF can't intercept virtual .user.js URL via webRequest, so we redirect it explicitly
const virtualUrlRe = ua.isFirefox && new RegExp((
`^(view-source:)?(${extensionRoot.replace('://', '$&)?')}[^/]*\\.user\\.js#\\d+`
));
const maybeRedirectVirtualUrlFF = virtualUrlRe && ((tabId, src) => {
if (virtualUrlRe.test(src)) {
browser.tabs.update(tabId, { url: resolveVirtualUrl(src) });
}
});
if (virtualUrlRe) {
const listener = (tabId, { url }) => url && maybeRedirectVirtualUrlFF(tabId, url);
const apiEvent = browser.tabs.onUpdated;
const addListener = apiEvent.addListener.bind(apiEvent, listener);
try { addListener({ properties: ['url'] }); } catch (e) { addListener(); }
}
browser.tabs.onCreated.addListener((tab) => {
const { id, title, url } = tab;
/* Determining if this tab can be auto-closed (replaced, actually).
FF>=68 allows reading file: URL only in the tab's content script so the tab must stay open. */
if ((!url.startsWith('file:') || ua.isFirefox < 68)
&& /\.user\.js([?#]|$)/.test(tab.pendingUrl || url)) {
cache.put(`autoclose:${id}`, true, 10e3);
}
if (virtualUrlRe && url === 'about:blank') {
maybeRedirectVirtualUrlFF(id, title);
}
});
browser.webRequest.onBeforeRequest.addListener((req) => {
const { method, tabId, url } = req;
if (method !== 'GET') {
return;
}
// open a real URL for simplified userscript URL listed in devtools of the web page
if (url.startsWith(extensionRoot)) {
return { redirectUrl: resolveVirtualUrl(url) };
}
if (!cache.has(`bypass:${url}`)
&& (!blacklistRe.test(url) || whitelistRe.test(url))) {
maybeInstallUserJs(tabId, url);
return { redirectUrl: 'javascript:void 0' }; // eslint-disable-line no-script-url
}
}, {
urls: [
// 1. *:// comprises only http/https
// 2. the API ignores #hash part
// 3. Firefox: onBeforeRequest does not work with file:// or moz-extension://
'*://*/*.user.js',
'*://*/*.user.js?*',
'file://*/*.user.js',
'file://*/*.user.js?*',
`${extensionRoot}*.user.js`,
],
types: ['main_frame'],
}, ['blocking']);
async function maybeInstallUserJs(tabId, url) {
const { data: code } = await request(url).catch(noop) || {};
if (code && parseMeta(code).name) {
const tab = tabId >= 0 && await browser.tabs.get(tabId) || {};
confirmInstall({ code, url, from: tab.url }, { tab });
} else {
cache.put(`bypass:${url}`, true, 10e3);
if (tabId >= 0) browser.tabs.update(tabId, { url });
}
}
// In Firefox with production code of Violentmonkey, scripts can be injected before `tabs.onUpdated` is fired.
// Ref: https://github.com/violentmonkey/violentmonkey/issues/1255
browser.tabs.onRemoved.addListener((tabId) => {
clearRequestsByTabId(tabId);
});
export function clearRequestsByTabId(tabId) {
const set = tabRequests[tabId];
if (set) {
delete tabRequests[tabId];
set::forEachEntry(([id]) => commands.AbortRequest(id));
}
}
/**
* Imitating https://developer.mozilla.org/docs/Web/API/XMLHttpRequest/getAllResponseHeaders
* Per the specification https://tools.ietf.org/html/rfc7230 the header name is within ASCII,
* but we'll try encoding it, if necessary, to handle invalid server responses.
*/
function encodeWebRequestHeader({ name, value, binaryValue }) {
return `${string2byteString(name)}: ${
binaryValue
? buffer2string(binaryValue)
: string2byteString(value)
}\r\n`;
}
/**
* Returns a UTF8-encoded binary string i.e. one byte per character.
* Returns the original string in case it was already within ASCII.
*/
function string2byteString(str) {
if (!/[\u0080-\uFFFF]/.test(str)) return str;
if (!encoder) encoder = new TextEncoder();
return buffer2string(encoder.encode(str));
}
/** @typedef {{
anonymous: boolean
blobbed: boolean
cb: function(Object)
chunked: boolean
coreId: number
eventsToNotify: string[]
id: number
noNativeCookie: boolean
responseHeaders: string
storeId: string
tabId: number
xhr: XMLHttpRequest
}} VMHttpRequest */