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