AtlatestRepositorysigil-quic

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

1// Test-only instrumentation. Run remotely with explicit input/output paths;
2// never use this file as a package hook or modify the shipped module in place.
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-secret-spy.mjs INPUT OUTPUT');
8}
9let source = readFileSync(input, 'utf8');
10if (!source.includes('(define-library (sigil quic tls schedule)') ||
11 source.includes('secret-spy')) {
12 throw new Error('Expected an uninstrumented TLS schedule');
14const imported = '(import (sigil crypto)';
15if (source.split(imported).length !== 2 ||
16 [...source.matchAll(/\(hkdf-sha256-extract(?=\s)/g)].length !== 3 ||
17 [...source.matchAll(/\(hmac-sha256-bytes(?=\s)/g)].length !== 1) {
18 throw new Error('Schedule primitive/import sites changed');
20source = source.replace(imported,
21 '(import (sigil quic tests secret spy)\n (sigil crypto)');
22source = source.replace(/\(hkdf-sha256-extract(?=\s)/g, '(secret-spy-extract')
23 .replace(/\(hmac-sha256-bytes(?=\s)/g, '(secret-spy-hmac');
24writeFileSync(output,
25 ';;; TEST ONLY: retained primitive inputs; never ship this variant.\n' + source);