-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpatchWorkers.js
304 lines (280 loc) · 10.6 KB
/
patchWorkers.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
/*
* 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/>.
*/
"use strict";
{
let patchesByTab = new Map();
let debugging;
let cleanup = tabId => {
if (tabId === -1) return;
let byOrigin = patchesByTab.get(tabId);
if (!byOrigin) return;
patchesByTab.delete(tabId);
if (debugging) {
debugging.dispose(tabId);
}
let serviceWorkers = patchesByTab.get(-1);
if (serviceWorkers) {
for (let origin of byOrigin.keys()) {
serviceWorkers.delete(origin);
}
}
};
browser.tabs.onRemoved.addListener(tab => {
cleanup(tab.id);
});
browser.webNavigation.onCommitted.addListener(({tabId, frameId}) => {
if (frameId === 0) cleanup(tabId);
});
browser.runtime.onMessage.addListener(({__patchWorkers__}, {tab, url: documentUrl}) => {
if (!__patchWorkers__) return;
try {
let {url, patch, isServiceOrShared} = __patchWorkers__;
let tabId = isServiceOrShared && !chrome.debugger ? -1 : tab.id;
let byOrigin = patchesByTab.get(tabId);
if (!byOrigin) patchesByTab.set(tabId, byOrigin = new Map());
if (tabId == -1) {
documentUrl = new URL(documentUrl).origin;
}
let patchInfo = byOrigin.get(documentUrl);
if (!patchInfo) byOrigin.set(documentUrl, patchInfo = {patch, urls: new Set()});
else {
patchInfo.patch = patch;
}
patchInfo.urls.add(url);
return Promise.resolve(init(tab.id, url, patchInfo));
} catch (e) {
console.error("Error on __patchWorkers__ message", e);
return Promise.reject(e);
}
});
let init = browser.webRequest.filterResponseData ? () => {
// Firefox, filter the source from the network
init = () => {}; // attach the listener just once
browser.webRequest.onBeforeRequest.addListener(request => {
let {tabId, url, documentUrl, requestId} = request;
console.debug("patchesByTab", patchesByTab, request); // DEV_ONLY REMOVEME
let byOrigin = patchesByTab.get(tabId);
if (!byOrigin) return;
if (tabId == -1) {
documentUrl = new URL(documentUrl).origin;
}
let patchInfo = byOrigin.get(documentUrl);
if (!(patchInfo && patchInfo.urls.has(url))) return;
console.debug(`Patching ${tabId == -1 ? 'service' : ''}worker`, requestId, url, documentUrl); // DEV_ONLY REMOVEME
let filter = browser.webRequest.filterResponseData(requestId);
filter.onstart = () => {
console.debug("filter.onstart", requestId); // DEV_ONLY REMOVEME
filter.write(new TextEncoder().encode(patchInfo.patch));
};
filter.ondata = e => {
filter.write(e.data);
};
filter.onstop = () => {
filter.close();
};
}, {
urls: ["<all_urls>"],
types: ["script"],
}, ["blocking"]);
} : async (...args) => {
// Section 1, run once
// Chromium, use debugger breakpoints
if (!chrome.debugger) {
throw new Error("patchWorker.js - no debugger API: missing permission?");
}
const patchedTargets = new Set();
let makeAsync = (api, method) => {
return new Proxy(api[method], {
apply(target, thisArg, args) {
return new Promise((resolve, reject) => {
let callback = (...r) => {
console.debug("%s ret", target.name, args, r); // DEV_ONLY
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(r.length > 1 ? r : r[0]);
}
};
target.call(api, ...args, callback);
});
}
});
};
const dbg = {};
for (let [key, prop] of Object.entries(chrome.debugger)) {
if (typeof prop === "function") dbg[key] = makeAsync(chrome.debugger, key);
}
debugging = new Map();
debugging.dispose = async function(tabId) {
let dbgInfo = await this.get(tabId);
if (dbgInfo) {
try {
return await dbgInfo.dispose();
} catch (e) {
console.error(e);
}
}
return false;
}
chrome.debugger.onEvent.addListener(async (source, method, params) => {
let {tabId} = source;
let dbgInfo = await debugging.get(tabId);
if (!dbgInfo || method === "Debugger.scriptParsed") return; // shouldn't happen
console.debug("Debugger event", method, params, source); // DEV_ONLY
switch(method) {
case "Debugger.scriptFailedToParse":
return await dbgInfo.dispose(params.url);
case "Target.attachedToTarget": {
return dbgInfo.handleWorker(source, params);
}
}
});
chrome.debugger.onDetach.addListener((source, reason) => {
console.log("Detached debugger from", source, reason);
if (source.tabId) debugging.dispose(source.tabId);
});
// Section 2, run from now on
// see https://chromedevtools.github.io/devtools-protocol/tot/Target/#type-TargetFilter
// and https://source.chromium.org/chromium/chromium/src/+/main:content/browser/devtools/devtools_agent_host_impl.cc?ss=chromium&q=f:devtools%20-f:out%20%22::kTypeTab%5B%5D%22
const targetFilter = ["worker", "shared_worker", "service_worker"].map(type => ({type, exclude: false}));
return await (init = async (tabId, url, {patch}) => {
const dbgTarget = {tabId};
let dbgInfo = await debugging.get(tabId);
if (!dbgInfo) {
const cmd = async (command, params) => await dbg.sendCommand(dbgTarget, command, params);
const startingDebugger = (async () => {
try {
console.debug("Attaching debugger to", tabId); // DEV_ONLY
try {
await dbg.attach(dbgTarget, "1.3");
} catch (e) {
// might just be because we're already attached
console.error(e);
}
await cmd("Debugger.enable");
await cmd("Target.setAutoAttach", {
autoAttach: true,
waitForDebuggerOnStart: true,
flatten: true,
targetFilter,
});
} catch (e) {
console.error(e);
throw e;
}
console.debug("NoScript's patchWorker started debugger on ", tabId);
return {
sessions: 0,
patches: new Map(),
async handleWorker(source, {sessionId, targetInfo, waitingForDebugger}) {
const {url, type, targetId} = targetInfo;
const targetKey = `${type}:${url}|T${tabId}:${targetId}`;
if (patchedTargets.has(targetKey)) return;
const {patches} = this;
const session = {...source, sessionId};
try {
if (!(patches.has(url) && /worker$/.test(type))) return;
patchedTargets.add(targetKey);
this.sessions++;
const expression = patches.get(url).code;
console.debug("Session %s (%s, %s), patching %s", sessionId, this.sessions, waitingForDebugger, url/*, expression */); // DEV_ONLY
if (waitingForDebugger) {
try {
await dbg.sendCommand(session, "Runtime.runIfWaitingForDebugger");
} catch (e) {
console.error(e);
}
}
try {
await dbg.sendCommand(session, "Runtime.evaluate", {
expression,
silent: true,
allowUnsafeEvalBlockedByCSP: true,
});
} catch (e) {
console.error("Runtime.evaluate failed", e);
}
} catch (e) {
console.error("Attaching failed", e);
} finally {
if (waitingForDebugger) {
try {
await dbg.sendCommand(session, "Runtime.runIfWaitingForDebugger");
} catch (e) {
console.error(e);
}
} else {
try {
await dbg.sendCommand(session, "Target.detachFromTarget", {sessionId});
} catch (e) {
console.error(e);
}
}
}
this.sessions--;
console.debug("Done with session %s (%s, %s)", sessionId, this.sessions, waitingForDebugger);
await this.dispose(url);
},
patch(url, code) {
let patch = this.patches.get(url);
if (!patch) {
this.patches.set(url, patch = {url, code, count: 1});
} else {
patch.code = code;
patch.count++;
}
},
async dispose(url) {
let {patches} = this;
if (!url) {
return await Promise.all([...patches.keys()].map(u => this.dispose(u)));
}
if (patches.has(url) && patches.get(url).count-- <= 1) {
patches.delete(url);
}
if (patches.size === 0 && this.sessions == 0) {
console.debug("Detaching debugger from tab", dbgTarget.tabId); // DEV_ONLY
try {
await cmd("Target.setAutoAttach", {autoAttach: false, waitForDebuggerOnStart: false});
} catch (e) {
console.error(e);
}
try {
await cmd("Debugger.disable");
} catch (e) {
console.error(e);
}
try {
await dbg.detach(dbgTarget);
} catch (e) {
console.error(e);
}
debugging.delete(dbgTarget.tabId);
}
}
};
})();
debugging.set(tabId, startingDebugger);
dbgInfo = await startingDebugger;
}
dbgInfo.patch(url, patch);
})(...args);
};
}