Public: anyone can read this; the crew can change it.
A guard test: every raw TLS connection verifies its peer, and turning verification off needs a listed reason #16920
Check · Review · Land · Live done — Landed and live in production.
egress-tls → main 0 commits
1 file
changed +69
−0
test/tls-verified.test.ts added +69
| @@ -0,0 +1,69 @@ | ||
| 1 | /** | |
| 2 | * Every TLS connection the forge opens verifies its peer. `Bun.connect` with `tls` completes a | |
| 3 | * handshake with any certificate unless told otherwise, so a raw TLS connect must ask for | |
| 4 | * `rejectUnauthorized: true` and check the name itself (`checkServerIdentity`), as | |
| 5 | * `kernel/mail.ts` does; and `rejectUnauthorized: false` appears only where a reason is listed. | |
| 6 | * | |
| 7 | * Audited 2026-11-30 after an inventory named `apps/egress.ts:812` and `:1115`: neither is TLS. | |
| 8 | * The first is the cell proxy's TCP tunnel (an app's own TLS passes through it end to end, and | |
| 9 | * the forge must not terminate it); the second dials a loopback port or a cell's Unix socket. This | |
| 10 | * test keeps it so: a TLS option added to any raw connect without verification fails here. | |
| 11 | */ | |
| 12 | import { expect, test } from "bun:test"; | |
| 13 | import { readdirSync, readFileSync, statSync } from "node:fs"; | |
| 14 | import { join, relative } from "node:path"; | |
| 15 | ||
| 16 | const ROOT = join(import.meta.dir, ".."); | |
| 17 | ||
| 18 | /** Where verification is off on purpose, and why. */ | |
| 19 | const UNVERIFIED_ON_PURPOSE: Readonly<Record<string, string>> = { | |
| 20 | "src/apps/certificate.ts": "reads a served certificate's expiry, an expired one included, and sends nothing over the connection", | |
| 21 | }; | |
| 22 | ||
| 23 | function sources(dir: string, out: string[] = []): string[] { | |
| 24 | for (const name of readdirSync(dir)) { | |
| 25 | const path = join(dir, name); | |
| 26 | if (name === "vendor" || name === "node_modules") continue; | |
| 27 | if (statSync(path).isDirectory()) sources(path, out); | |
| 28 | else if (name.endsWith(".ts")) out.push(path); | |
| 29 | } | |
| 30 | return out; | |
| 31 | } | |
| 32 | ||
| 33 | /** Each `Bun.connect(` call in `text` that asks for TLS without verifying it, by line. */ | |
| 34 | function unverifiedConnects(text: string): number[] { | |
| 35 | const out: number[] = []; | |
| 36 | for (let at = text.indexOf("Bun.connect"); at !== -1; at = text.indexOf("Bun.connect", at + 1)) { | |
| 37 | // The options up to the socket handlers: where `tls` and its settings are named. | |
| 38 | const end = text.indexOf("socket:", at); | |
| 39 | const options = text.slice(at, end === -1 ? at + 2_000 : end); | |
| 40 | if (!/\btls\b/.test(options)) continue; | |
| 41 | if (/rejectUnauthorized:\s*true/.test(options) && text.includes("checkServerIdentity")) continue; | |
| 42 | out.push(text.slice(0, at).split("\n").length); | |
| 43 | } | |
| 44 | return out; | |
| 45 | } | |
| 46 | ||
| 47 | test("the check sees a raw TLS connect that does not verify, and passes one that does", () => { | |
| 48 | expect(unverifiedConnects(`Bun.connect({ hostname: h, port: 443, tls: true, socket: {} })`)).toEqual([1]); | |
| 49 | expect(unverifiedConnects(`Bun.connect({ hostname: h, port: 25, socket: {} })`)).toEqual([]); | |
| 50 | expect( | |
| 51 | unverifiedConnects(`import { checkServerIdentity } from "node:tls";\nBun.connect({ tls: { serverName: h, rejectUnauthorized: true }, socket: {} })`), | |
| 52 | ).toEqual([]); | |
| 53 | }); | |
| 54 | ||
| 55 | test("every raw TLS connect in src verifies its peer, and verification is off only where listed", () => { | |
| 56 | const unverified: string[] = []; | |
| 57 | const off: string[] = []; | |
| 58 | for (const path of sources(join(ROOT, "src"))) { | |
| 59 | const text = readFileSync(path, "utf8"); | |
| 60 | const rel = relative(ROOT, path); | |
| 61 | for (const line of unverifiedConnects(text)) unverified.push(`${rel}:${line}`); | |
| 62 | if (/rejectUnauthorized:\s*false/.test(text) && UNVERIFIED_ON_PURPOSE[rel] === undefined) off.push(rel); | |
| 63 | } | |
| 64 | expect(unverified).toEqual([]); | |
| 65 | expect(off).toEqual([]); | |
| 66 | // A listed file that no longer turns verification off is a stale reason. | |
| 67 | for (const rel of Object.keys(UNVERIFIED_ON_PURPOSE)) | |
| 68 | expect(/rejectUnauthorized:\s*false/.test(readFileSync(join(ROOT, rel), "utf8"))).toBe(true); | |
| 69 | }); |
new above
offline