repoburg 1.3.289 → 1.3.291

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.
Files changed (40) hide show
  1. package/agent-pane/backend/dist/index.js +59 -9
  2. package/agent-pane/backend/dist/server.js +59 -9
  3. package/agent-pane/frontend/dist/assets/{TerminalView-BIN1AVRE.js → TerminalView-8SYwvwRI.js} +1 -1
  4. package/agent-pane/frontend/dist/assets/index-Dp-5bP4E.js +667 -0
  5. package/agent-pane/frontend/dist/assets/index-lBXohPJP.css +1 -0
  6. package/agent-pane/frontend/dist/index.html +2 -2
  7. package/daemon/dist/index.js +1 -0
  8. package/daemon/dist/tabtape/api/router.js +36 -1
  9. package/daemon/dist/tabtape/api/router.spec.js +47 -0
  10. package/daemon/dist/tabtape/e2e-server.js +1 -0
  11. package/daemon/dist/tabtape/index.js +10 -0
  12. package/daemon/dist/tabtape/index.spec.js +35 -2
  13. package/daemon/dist/tabtape/mcp/definitions.js +14 -3
  14. package/daemon/dist/tabtape/mcp/server.js +2 -1
  15. package/daemon/dist/tabtape/mcp/server.spec.js +50 -2
  16. package/daemon/dist/tabtape/mcp/tools.js +19 -0
  17. package/daemon/dist/tabtape/proxy/proxy.js +361 -0
  18. package/daemon/dist/tabtape/proxy/proxy.spec.js +497 -0
  19. package/daemon/dist/tabtape/proxy/rewrite.js +176 -0
  20. package/daemon/dist/tabtape/proxy/rewrite.spec.js +140 -0
  21. package/package.json +1 -1
  22. package/tabtape/extension/dist/background.js +4 -4
  23. package/tabtape/extension/dist/background.js.map +4 -4
  24. package/tabtape/extension/dist/bridge.js +2 -2
  25. package/tabtape/extension/dist/bridge.js.map +2 -2
  26. package/tabtape/extension/dist/build-info.json +4 -4
  27. package/tabtape/extension/dist/manifest.json +1 -1
  28. package/tabtape/extension/dist/popup.js +1 -1
  29. package/tabtape/extension/dist/popup.js.map +2 -2
  30. package/tabtape/extension/dist/proxy-bridge.js +3 -0
  31. package/tabtape/extension/dist/proxy-bridge.js.map +1 -0
  32. package/tabtape/extension/dist/recorder.js +20 -20
  33. package/tabtape/extension/dist/recorder.js.map +1 -1
  34. package/tabtape/extension/dist/state.js +1 -1
  35. package/tabtape/extension/dist/state.js.map +1 -1
  36. package/tabtape/playwright/tabtape.d.mts +17 -0
  37. package/tabtape/playwright/tabtape.mjs +40 -2
  38. package/tabtape/ui/dist/build-info.json +4 -4
  39. package/agent-pane/frontend/dist/assets/index-DumuprBy.js +0 -667
  40. package/agent-pane/frontend/dist/assets/index-sz14_w3J.css +0 -1
@@ -0,0 +1,497 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ const fs_1 = __importDefault(require("fs"));
40
+ const http_1 = __importDefault(require("http"));
41
+ const net_1 = __importDefault(require("net"));
42
+ const path_1 = __importDefault(require("path"));
43
+ const ws_1 = __importStar(require("ws"));
44
+ const zlib_1 = __importDefault(require("zlib"));
45
+ const log_1 = require("../log");
46
+ const qt = __importStar(require("../testing/querytest"));
47
+ const tmp_1 = require("../testing/tmp");
48
+ const proxy_1 = require("./proxy");
49
+ const PAGE = '<!doctype html><html><head><title>App</title></head><body>hello</body></html>';
50
+ const DOCUMENT = { 'Sec-Fetch-Dest': 'document' };
51
+ const LINE = (url) => JSON.stringify({ ts: qt.T0, type: 'event', data: { name: 'page_view', url, title: 'App' } });
52
+ const cleanups = [];
53
+ afterEach(async () => {
54
+ while (cleanups.length)
55
+ await cleanups.pop()();
56
+ await qt.closeTestStores();
57
+ (0, tmp_1.cleanupTempDirs)();
58
+ });
59
+ async function serveApp(app) {
60
+ const server = http_1.default.createServer(app);
61
+ await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
62
+ cleanups.push(() => new Promise((resolve) => {
63
+ server.closeAllConnections();
64
+ server.close(() => resolve());
65
+ }));
66
+ return { server, origin: `http://127.0.0.1:${server.address().port}` };
67
+ }
68
+ function scriptsDir() {
69
+ const dir = (0, tmp_1.tempDir)();
70
+ for (const f of ['proxy-bridge.js', 'state.js', 'recorder.js'])
71
+ fs_1.default.writeFileSync(path_1.default.join(dir, f), `// ${f}`);
72
+ fs_1.default.writeFileSync(path_1.default.join(dir, 'recorder.js.map'), '{}');
73
+ fs_1.default.writeFileSync(path_1.default.join(dir, 'background.js'), '// not for pages');
74
+ return dir;
75
+ }
76
+ function proxiesFor(st, opts = {}) {
77
+ const proxies = new proxy_1.Proxies({ st, baseURL: 'http://127.0.0.1:9998/tabtape', scriptsDir: scriptsDir(), logger: log_1.silentLogger, stopGraceMs: 0, ...opts });
78
+ cleanups.push(() => proxies.close());
79
+ return proxies;
80
+ }
81
+ async function setup(app, opts = {}) {
82
+ const st = qt.testStore();
83
+ const { origin: target } = await serveApp(app);
84
+ const proxies = proxiesFor(st, opts);
85
+ const rec = await proxies.start({ url: `${target}/start?a=1#top`, name: ' my check ' });
86
+ return { st, proxies, rec, origin: new URL(rec.url).origin, target };
87
+ }
88
+ function request(url, opts = {}) {
89
+ return new Promise((resolve, reject) => {
90
+ // A connection each: a kept one would hide that the proxy closed its port.
91
+ const req = http_1.default.request(url, { agent: false, method: opts.method ?? 'GET', headers: opts.headers }, (res) => {
92
+ const parts = [];
93
+ res.on('data', (c) => parts.push(c));
94
+ res.on('end', () => resolve({ status: res.statusCode ?? 0, headers: res.headers, body: Buffer.concat(parts) }));
95
+ res.on('error', reject);
96
+ });
97
+ req.on('error', reject);
98
+ req.end(opts.body);
99
+ });
100
+ }
101
+ const html = (res, body = PAGE, headers = {}) => {
102
+ res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', ...headers });
103
+ res.end(body);
104
+ };
105
+ const injected = (body) => body.toString().includes('<head><script data-tabtape src="/__tabtape/proxy-bridge.js"></script>');
106
+ describe('Proxies: pages', () => {
107
+ it('returns the page on the proxy and serves it with the recorder', async () => {
108
+ const { rec, target } = await setup((_req, res) => html(res));
109
+ const url = new URL(rec.url);
110
+ expect(url.hostname).toBe('127.0.0.1');
111
+ expect(url.pathname + url.search + url.hash).toBe('/start?a=1#top');
112
+ expect(rec.target).toBe(`${target}/start?a=1#top`);
113
+ expect(rec.replay).toBe(`http://127.0.0.1:9998/tabtape/#/s/${rec.id}`);
114
+ const res = await request(rec.url, { headers: DOCUMENT });
115
+ expect(res.status).toBe(200);
116
+ expect(injected(res.body)).toBe(true);
117
+ expect(res.body.toString()).toContain('<body>hello</body>');
118
+ });
119
+ it.each([
120
+ ['gzip', zlib_1.default.gzipSync(PAGE)],
121
+ ['deflate', zlib_1.default.deflateSync(PAGE)],
122
+ ['br', zlib_1.default.brotliCompressSync(PAGE)],
123
+ ])('reads %s pages', async (encoding, body) => {
124
+ const { rec } = await setup((_req, res) => html(res, body, { 'Content-Encoding': encoding, 'Content-Length': body.length }));
125
+ const res = await request(rec.url, { headers: DOCUMENT });
126
+ expect(injected(res.body)).toBe(true);
127
+ expect(res.headers['content-encoding']).toBeUndefined();
128
+ // The length of the rewritten page, not the target's.
129
+ expect(res.headers['content-length']).toBeUndefined();
130
+ expect(res.body.toString()).toContain('<body>hello</body>');
131
+ });
132
+ it('leaves a page in an unknown encoding as it is', async () => {
133
+ const { rec, st, proxies } = await setup((_req, res) => html(res, 'zzz', { 'Content-Encoding': 'zstd', 'Content-Length': 3 }));
134
+ const res = await request(rec.url, { headers: DOCUMENT });
135
+ expect(res.body.toString()).toBe('zzz');
136
+ expect([res.headers['content-encoding'], res.headers['content-length']]).toEqual(['zstd', '3']);
137
+ // Without the recorder there is nothing to replay.
138
+ expect(st.has(rec.id)).toBe(false);
139
+ expect(await proxies.stop(rec.id)).toMatchObject({ recorded: false });
140
+ });
141
+ it('touches nothing but pages', async () => {
142
+ const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0, 1, 2, 255]);
143
+ const gz = zlib_1.default.gzipSync('{"a":1}');
144
+ const { origin } = await setup((req, res) => {
145
+ if (req.url === '/logo.png') {
146
+ res.writeHead(200, { 'Content-Type': 'image/png', 'Content-Length': png.length });
147
+ res.end(png);
148
+ }
149
+ else if (req.url === '/api') {
150
+ res.writeHead(200, { 'Content-Type': 'application/json', 'Content-Encoding': 'gzip', 'Content-Length': gz.length });
151
+ res.end(gz);
152
+ }
153
+ else if (req.url === '/gone') {
154
+ res.writeHead(304);
155
+ res.end();
156
+ }
157
+ else {
158
+ html(res, PAGE, { 'Content-Length': Buffer.byteLength(PAGE) });
159
+ }
160
+ });
161
+ const image = await request(`${origin}/logo.png`, { headers: { 'Sec-Fetch-Dest': 'image' } });
162
+ expect([image.body, image.headers['content-length']]).toEqual([png, String(png.length)]);
163
+ const api = await request(`${origin}/api`, { headers: { 'Sec-Fetch-Dest': 'empty' } });
164
+ expect([api.body, api.headers['content-encoding']]).toEqual([gz, 'gzip']);
165
+ expect((await request(`${origin}/gone`, { headers: DOCUMENT })).status).toBe(304);
166
+ // HTML, but no page in a tab: a fragment for fetch, an iframe the parent's rrweb reads.
167
+ expect((await request(`${origin}/x`, { headers: { 'Sec-Fetch-Dest': 'empty' } })).body.toString()).toBe(PAGE);
168
+ expect((await request(`${origin}/x`, { headers: { 'Sec-Fetch-Dest': 'iframe' } })).body.toString()).toBe(PAGE);
169
+ expect((await request(`${origin}/x`, { method: 'HEAD', headers: DOCUMENT })).headers['content-length']).toBe(String(Buffer.byteLength(PAGE)));
170
+ });
171
+ it('forwards the method and body, and takes pages from POST too', async () => {
172
+ let seen = '';
173
+ const { origin } = await setup((req, res) => {
174
+ let body = '';
175
+ req.on('data', (c) => (body += c.toString()));
176
+ req.on('end', () => {
177
+ seen = `${req.method} ${req.url} ${body}`;
178
+ html(res);
179
+ });
180
+ });
181
+ const res = await request(`${origin}/form?x=1`, { method: 'POST', headers: DOCUMENT, body: 'a=1' });
182
+ expect(seen).toBe('POST /form?x=1 a=1');
183
+ expect(injected(res.body)).toBe(true);
184
+ });
185
+ it('keeps pages and responses arriving in pieces', async () => {
186
+ let more;
187
+ const { origin } = await setup((req, res) => {
188
+ if (req.url === '/events') {
189
+ res.writeHead(200, { 'Content-Type': 'text/event-stream' });
190
+ res.write('data: 1\n\n');
191
+ }
192
+ else {
193
+ res.writeHead(200, { 'Content-Type': 'text/html' });
194
+ res.write('<head></head><body>first');
195
+ }
196
+ more = () => res.end('second');
197
+ });
198
+ for (const [url, dest, first] of [
199
+ ['/events', 'empty', 'data: 1\n\n'],
200
+ ['/page', 'document', 'first'],
201
+ ]) {
202
+ const chunks = [];
203
+ await new Promise((resolve, reject) => {
204
+ const req = http_1.default.get(`${origin}${url}`, { headers: { 'Sec-Fetch-Dest': dest } }, (res) => {
205
+ res.on('data', (c) => {
206
+ chunks.push(c.toString());
207
+ if (chunks.join('').endsWith(first))
208
+ more();
209
+ });
210
+ res.on('end', resolve);
211
+ });
212
+ req.on('error', reject);
213
+ });
214
+ expect(chunks.join('').endsWith(`${first}second`)).toBe(true);
215
+ }
216
+ });
217
+ it('says so when the target does not answer', async () => {
218
+ const st = qt.testStore();
219
+ const { server, origin } = await serveApp((_req, res) => html(res));
220
+ server.closeAllConnections();
221
+ await new Promise((r) => server.close(r));
222
+ const rec = await proxiesFor(st).start({ url: origin });
223
+ const res = await request(rec.url);
224
+ expect(res.status).toBe(502);
225
+ expect(res.body.toString()).toContain(`tabtape proxy: ${origin} did not answer`);
226
+ });
227
+ it('survives what a target should not send', async () => {
228
+ const target = net_1.default.createServer((socket) => {
229
+ socket.on('error', () => socket.destroy());
230
+ socket.once('data', () => socket.end('HTTP/1.1 000 Nope\r\nContent-Length: 0\r\n\r\n'));
231
+ });
232
+ await new Promise((resolve) => target.listen(0, '127.0.0.1', resolve));
233
+ cleanups.push(() => void target.close());
234
+ const rec = await proxiesFor(qt.testStore()).start({ url: `http://127.0.0.1:${target.address().port}` });
235
+ expect((await request(rec.url)).status).toBe(502);
236
+ expect((await request(rec.url)).status).toBe(502);
237
+ });
238
+ });
239
+ describe('Proxies: what the target sees', () => {
240
+ it('gets its own origin, and foreign pages stay foreign', async () => {
241
+ const seen = [];
242
+ const { origin, target } = await setup((req, res) => {
243
+ seen.push(req.headers);
244
+ res.end('ok');
245
+ });
246
+ await request(`${origin}/a`, { headers: { Origin: origin, Referer: `${origin}/page` } });
247
+ await request(`${origin}/a`, { headers: { Origin: 'https://evil.test' } });
248
+ expect(seen[0]).toMatchObject({ host: new URL(target).host, origin: target, referer: `${target}/page` });
249
+ expect(seen[1].origin).toBe('https://evil.test');
250
+ });
251
+ it('keeps redirects and cookies on the proxy', async () => {
252
+ let appOrigin = '';
253
+ const app = await serveApp((_req, res) => {
254
+ res.writeHead(302, { Location: `${appOrigin}/next`, 'Set-Cookie': ['a=1; Secure; SameSite=None', 'b=2; Domain=127.0.0.1'] });
255
+ res.end();
256
+ });
257
+ appOrigin = app.origin;
258
+ const rec = await proxiesFor(qt.testStore()).start({ url: appOrigin });
259
+ const res = await request(rec.url);
260
+ expect(res.headers.location).toBe('/next');
261
+ expect(res.headers['set-cookie']).toEqual(['a=1; SameSite=Lax', 'b=2']);
262
+ });
263
+ it('drops CSPs', async () => {
264
+ const { rec } = await setup((_req, res) => html(res, '<head><meta http-equiv="Content-Security-Policy" content="script-src \'none\'"></head>', {
265
+ 'Content-Security-Policy': "script-src 'none'",
266
+ 'Content-Security-Policy-Report-Only': "script-src 'none'",
267
+ }));
268
+ const res = await request(rec.url, { headers: DOCUMENT });
269
+ expect(res.headers['content-security-policy']).toBeUndefined();
270
+ expect(res.headers['content-security-policy-report-only']).toBeUndefined();
271
+ expect(res.body.toString()).not.toContain('Content-Security-Policy');
272
+ });
273
+ });
274
+ describe('Proxies: the page scripts', () => {
275
+ it('serves them from the build, fresh each time, and nothing else there', async () => {
276
+ const { origin } = await setup((_req, res) => html(res));
277
+ const res = await request(`${origin}/__tabtape/recorder.js`);
278
+ expect([res.status, res.body.toString()]).toEqual([200, '// recorder.js']);
279
+ expect(res.headers).toMatchObject({ 'content-type': 'text/javascript; charset=utf-8', 'cache-control': 'no-store' });
280
+ expect((await request(`${origin}/__tabtape/recorder.js.map`)).status).toBe(200);
281
+ for (const name of ['background.js', '../background.js', '..%2fbackground.js', '', 'state.js?x=1']) {
282
+ const status = await new Promise((resolve, reject) => {
283
+ // Not through a URL, which would resolve the dots.
284
+ const req = http_1.default.request({ host: '127.0.0.1', port: new URL(origin).port, path: `/__tabtape/${name}`, agent: false }, (res) => {
285
+ res.resume();
286
+ resolve(res.statusCode);
287
+ });
288
+ req.on('error', reject);
289
+ req.end();
290
+ });
291
+ expect([name, status]).toEqual([name, 404]);
292
+ }
293
+ });
294
+ it('never forwards the path it keeps', async () => {
295
+ const seen = [];
296
+ const { origin } = await setup((req, res) => {
297
+ seen.push(req.url ?? '');
298
+ res.end('ok');
299
+ });
300
+ await request(`${origin}/__tabtape/nope`);
301
+ await request(`${origin}/__tabtape/events`);
302
+ expect(seen).toEqual([]);
303
+ });
304
+ });
305
+ describe('Proxies: the lines of a page', () => {
306
+ const post = (origin, body, headers = { 'X-Tabtape-Client': 'proxy' }) => request(`${origin}/__tabtape/events`, { method: 'POST', headers, body });
307
+ async function lines(st, id) {
308
+ const parts = [];
309
+ await st.writeSession(id, (c) => {
310
+ parts.push(Buffer.from(c));
311
+ });
312
+ return Buffer.concat(parts).toString();
313
+ }
314
+ it('writes them into the recording, with the app in place of the proxy', async () => {
315
+ const { st, rec, origin, target } = await setup((_req, res) => html(res));
316
+ await request(rec.url, { headers: DOCUMENT });
317
+ expect((await post(origin, `${LINE(`${origin}/a`)}\n${LINE(`${origin}/b?x=${origin}/c`)}`)).status).toBe(204);
318
+ const stored = await lines(st, rec.id);
319
+ expect(stored).toContain(`"url":"${target}/a"`);
320
+ expect(stored).toContain(`"url":"${target}/b?x=${target}/c"`);
321
+ expect(stored).not.toContain(origin);
322
+ });
323
+ it('takes the origin the page has, also through localhost', async () => {
324
+ const { st, rec, origin, target } = await setup((_req, res) => html(res));
325
+ await request(rec.url, { headers: DOCUMENT });
326
+ const local = origin.replace('127.0.0.1', 'localhost');
327
+ expect((await post(local, LINE(`${local}/a`))).status).toBe(204);
328
+ expect(await lines(st, rec.id)).toContain(`"url":"${target}/a"`);
329
+ });
330
+ it('refuses them without the client header, and other methods', async () => {
331
+ const { st, rec, origin } = await setup((_req, res) => html(res));
332
+ await request(rec.url, { headers: DOCUMENT });
333
+ expect((await post(origin, LINE('x'), {})).status).toBe(403);
334
+ expect((await request(`${origin}/__tabtape/events`, { headers: { 'X-Tabtape-Client': 'proxy' } })).status).toBe(403);
335
+ expect((await request(`${origin}/__tabtape/events`, { method: 'OPTIONS', headers: { Origin: 'https://evil.test' } })).status).toBe(403);
336
+ expect(await lines(st, rec.id)).not.toContain('page_view');
337
+ });
338
+ it('refuses what is not a line, and too much', async () => {
339
+ const { rec, origin } = await setup((_req, res) => html(res), { maxBatchBytes: 1000 });
340
+ await request(rec.url, { headers: DOCUMENT });
341
+ expect((await post(origin, '{"no":"ts"}')).status).toBe(400);
342
+ expect((await post(origin, 'x'.repeat(2000))).status).toBe(413);
343
+ });
344
+ it('serves loopback Host names only', async () => {
345
+ const { rec, origin } = await setup((_req, res) => html(res));
346
+ for (const host of ['evil.test', 'evil.test:80', '127.0.0.1.evil.test']) {
347
+ expect([host, (await request(rec.url, { headers: { Host: host } })).status]).toEqual([host, 403]);
348
+ expect((await post(origin, LINE('x'), { Host: host, 'X-Tabtape-Client': 'proxy' })).status).toBe(403);
349
+ }
350
+ });
351
+ });
352
+ describe('Proxies: the recording', () => {
353
+ it('starts with the first page, and ends with stop', async () => {
354
+ const { st, proxies, rec, target } = await setup((_req, res) => html(res));
355
+ expect(st.has(rec.id)).toBe(false);
356
+ await request(rec.url, { headers: { ...DOCUMENT, 'User-Agent': 'browser/1' } });
357
+ await request(rec.url, { headers: { ...DOCUMENT, 'User-Agent': 'other/2' } });
358
+ const sess = st.get(rec.id);
359
+ expect(sess).toMatchObject({ title: 'my check', url: `${target}/start?a=1`, userAgent: 'browser/1', source: 'automation' });
360
+ expect(sess.endedAt ?? 0).toBeFalsy();
361
+ expect(await proxies.stop(rec.id)).toEqual({ id: rec.id, recorded: true, replay: rec.replay });
362
+ expect(st.get(rec.id).endedAt).toBeGreaterThan(0);
363
+ await expect(request(rec.url)).rejects.toThrow(/ECONNREFUSED/);
364
+ });
365
+ it('leaves nothing behind when nobody came', async () => {
366
+ const { st, proxies, rec } = await setup((_req, res) => html(res));
367
+ await request(rec.url, { headers: { 'Sec-Fetch-Dest': 'image' } });
368
+ expect(await proxies.stop(rec.id)).toMatchObject({ recorded: false });
369
+ expect(st.has(rec.id)).toBe(false);
370
+ });
371
+ it('waits for the lines still on their way', async () => {
372
+ const { st, proxies, rec, origin } = await setup((_req, res) => html(res), { stopGraceMs: 100 });
373
+ await request(rec.url, { headers: DOCUMENT });
374
+ const stopping = proxies.stop(rec.id);
375
+ await new Promise((r) => setTimeout(r, 20));
376
+ const res = await request(`${origin}/__tabtape/events`, { method: 'POST', headers: { 'X-Tabtape-Client': 'proxy' }, body: LINE('late') });
377
+ expect(res.status).toBe(204);
378
+ await stopping;
379
+ expect(st.get(rec.id).endedAt).toBeGreaterThan(0);
380
+ });
381
+ it('answers a second stop, and refuses an unknown id', async () => {
382
+ const { proxies, rec } = await setup((_req, res) => html(res));
383
+ await request(rec.url, { headers: DOCUMENT });
384
+ await proxies.stop(rec.id);
385
+ expect(await proxies.stop(rec.id)).toMatchObject({ recorded: true });
386
+ await expect(proxies.stop('nope')).rejects.toThrow(proxy_1.ProxyInputError);
387
+ });
388
+ it('closes on its own when nobody uses it', async () => {
389
+ const { st, rec } = await setup((_req, res) => html(res), { idleMs: 50 });
390
+ await request(rec.url, { headers: DOCUMENT });
391
+ await new Promise((r) => setTimeout(r, 150));
392
+ await expect(request(rec.url)).rejects.toThrow(/ECONNREFUSED/);
393
+ expect(st.get(rec.id).endedAt).toBeGreaterThan(0);
394
+ });
395
+ it('ends what runs when the daemon stops', async () => {
396
+ const { st, proxies, rec } = await setup((_req, res) => html(res));
397
+ await request(rec.url, { headers: DOCUMENT });
398
+ await proxies.close();
399
+ expect(st.get(rec.id).endedAt).toBeGreaterThan(0);
400
+ });
401
+ it('refuses what it cannot record', async () => {
402
+ const st = qt.testStore();
403
+ const proxies = proxiesFor(st);
404
+ await expect(proxies.start({ url: 'not a url' })).rejects.toThrow(proxy_1.ProxyInputError);
405
+ await expect(proxies.start({ url: 'file:///tmp/a.html' })).rejects.toThrow(/only http and https/);
406
+ await expect(proxiesFor(st, { scriptsDir: null }).start({ url: 'http://localhost:3000' })).rejects.toThrow(/not built/);
407
+ await expect(proxiesFor(st, { scriptsDir: (0, tmp_1.tempDir)() }).start({ url: 'http://localhost:3000' })).rejects.toThrow(/not built/);
408
+ });
409
+ it('limits how many run at once', async () => {
410
+ const proxies = proxiesFor(qt.testStore());
411
+ for (let i = 0; i < 16; i++)
412
+ await proxies.start({ url: 'http://localhost:3000' });
413
+ await expect(proxies.start({ url: 'http://localhost:3000' })).rejects.toThrow(/too many/);
414
+ });
415
+ it('never gives two recordings one id in the same second', async () => {
416
+ const proxies = proxiesFor(qt.testStore());
417
+ const ids = await Promise.all([1, 2, 3, 4].map(() => proxies.start({ url: 'http://localhost:3000' })));
418
+ expect(new Set(ids.map((r) => r.id)).size).toBe(4);
419
+ for (const r of ids)
420
+ expect(r.id).toMatch(/^\d{8}-\d{6}-[0-9a-f]{6}$/);
421
+ });
422
+ });
423
+ describe('Proxies: WebSockets', () => {
424
+ async function wsApp() {
425
+ const { server, origin } = await serveApp((_req, res) => html(res));
426
+ const wss = new ws_1.WebSocketServer({ noServer: true });
427
+ const origins = [];
428
+ const hosts = [];
429
+ server.on('upgrade', (req, socket, head) => {
430
+ origins.push(req.headers.origin);
431
+ hosts.push(req.headers.host);
432
+ if (req.url === '/refused') {
433
+ socket.end('HTTP/1.1 401 Unauthorized\r\n\r\n');
434
+ return;
435
+ }
436
+ wss.handleUpgrade(req, socket, head, (ws) => {
437
+ ws.on('message', (m, binary) => ws.send(m, { binary }));
438
+ });
439
+ });
440
+ cleanups.push(() => {
441
+ wss.close();
442
+ });
443
+ return { origin, origins, hosts };
444
+ }
445
+ const open = (url, opts = {}, protocols = []) => new Promise((resolve, reject) => {
446
+ const ws = new ws_1.default(url, protocols, opts);
447
+ ws.once('open', () => resolve(ws));
448
+ ws.once('error', reject);
449
+ ws.once('unexpected-response', (_req, res) => reject(new Error(`status ${res.statusCode}`)));
450
+ });
451
+ it('carries them to the target, as the page on its own origin', async () => {
452
+ const app = await wsApp();
453
+ const proxies = proxiesFor(qt.testStore());
454
+ const rec = await proxies.start({ url: app.origin });
455
+ const origin = new URL(rec.url).origin;
456
+ const ws = await open(`${origin.replace('http', 'ws')}/hmr?token=1`, { origin }, ['vite-hmr']);
457
+ expect(ws.protocol).toBe('vite-hmr');
458
+ const echo = new Promise((resolve) => ws.once('message', (m) => resolve(m.toString())));
459
+ ws.send('ping');
460
+ expect(await echo).toBe('ping');
461
+ ws.close();
462
+ expect(app.origins).toEqual([app.origin]);
463
+ expect(app.hosts).toEqual([new URL(app.origin).host]);
464
+ });
465
+ it('keeps foreign pages foreign, and refuses foreign Host names', async () => {
466
+ const app = await wsApp();
467
+ const rec = await proxiesFor(qt.testStore()).start({ url: app.origin });
468
+ const url = new URL(rec.url).origin.replace('http', 'ws');
469
+ (await open(url, { origin: 'https://evil.test' })).close();
470
+ expect(app.origins).toEqual(['https://evil.test']);
471
+ await expect(open(url, { headers: { Host: 'evil.test' } })).rejects.toThrow(/status 403/);
472
+ expect(app.origins).toHaveLength(1);
473
+ });
474
+ it('passes on a refusal', async () => {
475
+ const app = await wsApp();
476
+ const rec = await proxiesFor(qt.testStore()).start({ url: app.origin });
477
+ await expect(open(`${new URL(rec.url).origin.replace('http', 'ws')}/refused`)).rejects.toThrow(/status 401/);
478
+ });
479
+ it('closes them with the recording', async () => {
480
+ const app = await wsApp();
481
+ const proxies = proxiesFor(qt.testStore());
482
+ const rec = await proxies.start({ url: app.origin });
483
+ const ws = await open(new URL(rec.url).origin.replace('http', 'ws'));
484
+ const closed = new Promise((r) => ws.once('close', r));
485
+ await proxies.stop(rec.id);
486
+ await closed;
487
+ });
488
+ it('survives a client that goes away', async () => {
489
+ const app = await wsApp();
490
+ const proxies = proxiesFor(qt.testStore());
491
+ const rec = await proxies.start({ url: app.origin });
492
+ const ws = await open(new URL(rec.url).origin.replace('http', 'ws'));
493
+ ws._socket.destroy();
494
+ await new Promise((r) => setTimeout(r, 20));
495
+ expect((await request(rec.url)).status).toBe(200);
496
+ });
497
+ });
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.PageRewriter = exports.PAGE_SCRIPTS = exports.PROXY_PATH = void 0;
7
+ exports.opensPage = opensPage;
8
+ exports.upstreamHeaders = upstreamHeaders;
9
+ exports.downstreamHeaders = downstreamHeaders;
10
+ exports.localLocation = localLocation;
11
+ exports.localCookie = localCookie;
12
+ exports.isPage = isPage;
13
+ exports.decoderFor = decoderFor;
14
+ exports.rewriteHead = rewriteHead;
15
+ const stream_1 = require("stream");
16
+ const zlib_1 = __importDefault(require("zlib"));
17
+ /** Answered by the proxy, never forwarded (PROXY_PATH in tabtape/extension/src/protocol.ts). */
18
+ exports.PROXY_PATH = '/__tabtape/';
19
+ /** In order: the bridge first (it answers the recorder), the state hook before the recorder. */
20
+ exports.PAGE_SCRIPTS = ['proxy-bridge.js', 'state.js', 'recorder.js'];
21
+ /** The bridge removes the tags again once they ran. */
22
+ const SCRIPT_TAGS = exports.PAGE_SCRIPTS.map((s) => `<script data-tabtape src="${exports.PROXY_PATH}${s}"></script>`).join('');
23
+ const HOP_BY_HOP = new Set([
24
+ 'connection',
25
+ 'keep-alive',
26
+ 'proxy-authenticate',
27
+ 'proxy-authorization',
28
+ 'proxy-connection',
29
+ 'te',
30
+ 'trailer',
31
+ 'transfer-encoding',
32
+ 'upgrade',
33
+ ]);
34
+ /** The recorder is a script of the proxy's and sends lines to it: a page's CSP would stop both. */
35
+ const CSP_HEADERS = new Set(['content-security-policy', 'content-security-policy-report-only']);
36
+ /** What decoderFor can undo; asked for instead of the browser's list, which may name more. */
37
+ const DECODABLE = 'gzip, deflate, br';
38
+ /** Whether the request opens a page in a tab. Iframes are not: their parent's rrweb reads them. */
39
+ function opensPage(headers) {
40
+ const dest = headers['sec-fetch-dest'];
41
+ if (dest !== undefined)
42
+ return dest === 'document';
43
+ return /\btext\/html\b/i.test(headers.accept ?? '');
44
+ }
45
+ /**
46
+ * The request as the target gets it. Only the proxy's own origin turns into
47
+ * the target's: what a foreign page sends stays foreign, for the target to
48
+ * refuse as it would without the proxy.
49
+ */
50
+ function upstreamHeaders(headers, proxyOrigin, target) {
51
+ const out = {};
52
+ for (const [k, v] of Object.entries(headers)) {
53
+ if (!HOP_BY_HOP.has(k))
54
+ out[k] = v;
55
+ }
56
+ out.host = target.host;
57
+ if (out.origin === proxyOrigin)
58
+ out.origin = target.origin;
59
+ const referer = out.referer;
60
+ if (typeof referer === 'string' && (referer === proxyOrigin || referer.startsWith(`${proxyOrigin}/`))) {
61
+ out.referer = target.origin + referer.slice(proxyOrigin.length);
62
+ }
63
+ if (opensPage(headers))
64
+ out['accept-encoding'] = DECODABLE;
65
+ return out;
66
+ }
67
+ /** The response as the browser gets it. */
68
+ function downstreamHeaders(headers, target) {
69
+ const out = {};
70
+ for (const [k, v] of Object.entries(headers)) {
71
+ if (!HOP_BY_HOP.has(k) && !CSP_HEADERS.has(k))
72
+ out[k] = v;
73
+ }
74
+ if (headers.location !== undefined)
75
+ out.location = localLocation(headers.location, target);
76
+ if (headers['set-cookie'] !== undefined)
77
+ out['set-cookie'] = headers['set-cookie'].map(localCookie);
78
+ return out;
79
+ }
80
+ /** A redirect within the target stays on the proxy; other origins are left, and leave the recording. */
81
+ function localLocation(location, target) {
82
+ if (!/^(https?:)?\/\//i.test(location))
83
+ return location;
84
+ try {
85
+ const u = new URL(location, target);
86
+ return u.origin === target.origin ? u.pathname + u.search + u.hash : location;
87
+ }
88
+ catch {
89
+ return location;
90
+ }
91
+ }
92
+ /**
93
+ * A cookie as the proxy's origin can keep it: plain http on a loopback host,
94
+ * so without the target's domain and Secure, which SameSite=None requires.
95
+ */
96
+ function localCookie(cookie) {
97
+ const [pair, ...attrs] = cookie.split(';').map((p) => p.trim());
98
+ const kept = attrs.filter((a) => !/^(domain|secure)\s*(=|$)/i.test(a)).map((a) => (/^samesite\s*=\s*none$/i.test(a) ? 'SameSite=Lax' : a));
99
+ return [pair, ...kept].join('; ');
100
+ }
101
+ /** Whether the response is a page to put the recorder into. */
102
+ function isPage(method, status, headers) {
103
+ return method !== 'HEAD' && status >= 200 && status < 300 && status !== 204 && /\btext\/html\b/i.test(headers['content-type'] ?? '');
104
+ }
105
+ /**
106
+ * What undoes a Content-Encoding: null when the body is plain, undefined for
107
+ * an encoding not known here.
108
+ */
109
+ function decoderFor(encoding) {
110
+ switch ((encoding ?? '').trim().toLowerCase()) {
111
+ case '':
112
+ case 'identity':
113
+ return null;
114
+ case 'gzip':
115
+ case 'x-gzip':
116
+ case 'deflate':
117
+ return zlib_1.default.createUnzip();
118
+ case 'br':
119
+ return zlib_1.default.createBrotliDecompress();
120
+ default:
121
+ return undefined;
122
+ }
123
+ }
124
+ const HEAD_END = /<\/head\s*>|<body[\s>]/i;
125
+ const INSERT_AFTER = [/<head(?:\s[^>]*)?>/i, /<html(?:\s[^>]*)?>/i, /<!doctype[^>]*>/i];
126
+ const META_CSP = /<meta\b[^>]*\bhttp-equiv\s*=\s*["']?content-security-policy(?:-report-only)?\b[^>]*>/gi;
127
+ /** A page whose head does not end by here gets the recorder anyway. */
128
+ const MAX_HELD_BYTES = 256 << 10;
129
+ /** The start of a page, with the recorder in it and without <meta> CSPs. */
130
+ function rewriteHead(html) {
131
+ const clean = html.replace(META_CSP, '');
132
+ for (const tag of INSERT_AFTER) {
133
+ const m = tag.exec(clean);
134
+ if (m) {
135
+ const at = m.index + m[0].length;
136
+ return clean.slice(0, at) + SCRIPT_TAGS + clean.slice(at);
137
+ }
138
+ }
139
+ return SCRIPT_TAGS + clean;
140
+ }
141
+ /**
142
+ * Passes a page through, rewriting its start. It holds the page back until
143
+ * its head is complete and streams the rest, so pages sent in chunks keep
144
+ * arriving that way. latin1 keeps the bytes as they are in any charset.
145
+ */
146
+ class PageRewriter extends stream_1.Transform {
147
+ constructor() {
148
+ super(...arguments);
149
+ this.held = [];
150
+ this.size = 0;
151
+ this.done = false;
152
+ }
153
+ _transform(chunk, _enc, cb) {
154
+ if (this.done) {
155
+ cb(null, chunk);
156
+ return;
157
+ }
158
+ this.held.push(chunk);
159
+ this.size += chunk.length;
160
+ const html = Buffer.concat(this.held, this.size).toString('latin1');
161
+ if (this.size >= MAX_HELD_BYTES || HEAD_END.test(html))
162
+ this.release(html);
163
+ cb();
164
+ }
165
+ _flush(cb) {
166
+ if (!this.done)
167
+ this.release(Buffer.concat(this.held, this.size).toString('latin1'));
168
+ cb();
169
+ }
170
+ release(html) {
171
+ this.done = true;
172
+ this.held = [];
173
+ this.push(Buffer.from(rewriteHead(html), 'latin1'));
174
+ }
175
+ }
176
+ exports.PageRewriter = PageRewriter;