AtlatestRepositorysigil-quic

sigil-quic / tree / scriptsinject-event-spy.mjs

1// Remote-only observation and subsequent-operation faults. Keep input/output
2// distinct and install the result only in an isolated test source tree.
3import { readFileSync, writeFileSync } from 'node:fs';
4
5const [input, output] = process.argv.slice(2);
6if (!input || !output || input === output) {
7 throw new Error('Usage: inject-event-spy.mjs INPUT OUTPUT');
8}
9let source = readFileSync(input, 'utf8');
10const imported = '(import (sigil crypto)';
11const start = source.indexOf(' (define (secrets! engine level client server read? write?)');
12const end = source.indexOf(' (define (derive-handshake! engine peer-public)');
13if (start < 0 || end <= start || source.includes('event-spy') ||
14 source.split(imported).length !== 2) {
15 throw new Error('Expected an uninstrumented TLS handshake');
17let body = source.slice(start, end);
18for (const [direction, value] of [['read', 'server client'], ['write', 'client server']]) {
19 const site = `(bytevector-copy (if client? ${value}))`;
20 if (body.split(site).length !== 2) throw new Error('Secret-copy site changed');
21 body = body.replace(site,
22 `(event-spy-copy (tls-role engine) level '${direction} (if client? ${value}))`);
24const queue = "(queue! engine 'secrets level";
25if (body.split(queue).length !== 2) throw new Error('Secret-queue site changed');
26body = body.replace(queue, "(event-spy-queue! queue! engine (tls-role engine) 'secrets level");
27source = source.slice(0, start) + body + source.slice(end);
28source = source.replace(imported,
29 '(import (sigil quic tests event spy)\n (sigil crypto)');
30writeFileSync(output,
31 ';;; TEST ONLY: observed secret-event copies; never ship this variant.\n' + source);