// The test nginx's TLS material (#66, DESIGN 11.1 / TESTING 11.1). A self-signed certificate is // GENERATED at run time, never committed: browsers only negotiate HTTP/2 over TLS, and the session // cookie is `Secure`, so the browser e2e and the harnesses have to speak https. // // One directory per run (`run/tls`, gitignored) is shared by every harness file in the run: the // certificate is a trust anchor for the Node side through NODE_EXTRA_CA_CERTS (set by tools/test.sh // and client/e2e/run.sh), and nginx loads the same pair. The SANs are what a client checks on loopback. import { spawnSync } from 'node:child_process' import fs from 'node:fs' import path from 'node:path' const SUBJECT = '/CN=fff-vtt test nginx' const SAN = 'subjectAltName=DNS:localhost,IP:127.0.0.1' function generate(dir) { fs.mkdirSync(dir, { recursive: true }) const certPath = path.join(dir, 'cert.pem') const keyPath = path.join(dir, 'key.pem') const r = spawnSync('openssl', [ 'req', '-x509', '-newkey', 'ec', '-pkeyopt', 'ec_paramgen_curve:prime256v1', '-nodes', '-sha256', '-days', '3', '-keyout', keyPath, '-out', certPath, '-subj', SUBJECT, // CA:TRUE so the pair is usable as a trust anchor (NODE_EXTRA_CA_CERTS) as well as a leaf; // serverAuth + digitalSignature so a browser/h2 client accepts it as a TLS server certificate. '-addext', SAN, '-addext', 'basicConstraints=critical,CA:TRUE', '-addext', 'keyUsage=critical,digitalSignature,keyCertSign', '-addext', 'extendedKeyUsage=serverAuth', ], { encoding: 'utf8' }) if (r.status !== 0) { throw new Error(`openssl could not generate the test certificate (is the openssl devbox ` + `package installed?):\n${r.stdout}\n${r.stderr}`) } return { certPath, keyPath } } // Ensures /{cert,key}.pem exist and returns their paths. Idempotent: an existing pair is // reused, so a run's harness files start from the same anchor. export function ensureDevCert(dir) { const certPath = path.join(dir, 'cert.pem') const keyPath = path.join(dir, 'key.pem') if (fs.existsSync(certPath) && fs.existsSync(keyPath)) return { certPath, keyPath } return generate(dir) }