|
| 1 | +/** |
| 2 | + * Runtime parity test for the controlled-egress fetch engine. |
| 3 | + * |
| 4 | + * `external-fetch-boundary.test.ts` runs the same real-DNS/real-TLS boundary |
| 5 | + * fixture under Bun. Production does NOT run Bun: the gateway ships as a |
| 6 | + * pre-bundled CJS artifact executed by Node (service/Dockerfile.egress-gateway |
| 7 | + * `production` stage), because the /external-fetch route depends on two |
| 8 | + * behaviours Bun's node:http/node:https compat layer does not carry |
| 9 | + * faithfully — HTTP response trailers and the connection-pinning |
| 10 | + * `https.request({ lookup })` hook that IS the SSRF defence. |
| 11 | + * |
| 12 | + * So the SSRF-critical path is exercised twice: once under Bun (developer |
| 13 | + * runtime) and once here, bundled exactly the way the image bundles it and |
| 14 | + * executed by Node, so a Bun/Node divergence in DNS pinning, redirect |
| 15 | + * revalidation, header allow-listing, timeouts, or streaming ceilings fails a |
| 16 | + * check instead of shipping. |
| 17 | + * |
| 18 | + * Skipped automatically when `node` isn't on PATH or the platform isn't Linux |
| 19 | + * (the fixture needs `unshare --net` to own its loopback addresses). |
| 20 | + */ |
| 21 | +import { afterAll, expect, test } from 'bun:test'; |
| 22 | +import { spawnSync } from 'node:child_process'; |
| 23 | +import fs from 'node:fs'; |
| 24 | +import os from 'node:os'; |
| 25 | +import path from 'node:path'; |
| 26 | + |
| 27 | +const NODE_AVAILABLE = (() => { |
| 28 | + try { |
| 29 | + return spawnSync('node', ['--version'], { stdio: 'ignore' }).status === 0; |
| 30 | + } catch { |
| 31 | + return false; |
| 32 | + } |
| 33 | +})(); |
| 34 | +const SHOULD_RUN = NODE_AVAILABLE && process.platform === 'linux'; |
| 35 | +const testIfRuntime = SHOULD_RUN ? test : test.skip; |
| 36 | + |
| 37 | +const workdir = fs.mkdtempSync( |
| 38 | + path.join(os.tmpdir(), 'external-fetch-runtime-'), |
| 39 | +); |
| 40 | + |
| 41 | +afterAll(() => { |
| 42 | + fs.rmSync(workdir, { recursive: true, force: true }); |
| 43 | +}); |
| 44 | + |
| 45 | +testIfRuntime( |
| 46 | + 'crosses the same real boundaries when bundled and run under the production Node runtime', |
| 47 | + async () => { |
| 48 | + const serviceDir = path.resolve(__dirname, '..'); |
| 49 | + const fixture = path.resolve( |
| 50 | + __dirname, |
| 51 | + 'external-fetch-boundary.fixture.ts', |
| 52 | + ); |
| 53 | + const fixtureDir = path.resolve(serviceDir, 'test-fixtures/external-fetch'); |
| 54 | + const bundle = path.join(workdir, 'external-fetch-boundary.cjs'); |
| 55 | + |
| 56 | + const build = Bun.spawnSync( |
| 57 | + [ |
| 58 | + 'bun', |
| 59 | + 'build', |
| 60 | + fixture, |
| 61 | + '--target=node', |
| 62 | + '--format=cjs', |
| 63 | + `--outfile=${bundle}`, |
| 64 | + ], |
| 65 | + { cwd: serviceDir, stdout: 'pipe', stderr: 'pipe' }, |
| 66 | + ); |
| 67 | + expect(build.exitCode, build.stderr.toString()).toBe(0); |
| 68 | + expect(fs.existsSync(bundle)).toBe(true); |
| 69 | + |
| 70 | + const child = Bun.spawn( |
| 71 | + ['unshare', '--user', '--map-root-user', '--net', 'node', bundle], |
| 72 | + { |
| 73 | + cwd: serviceDir, |
| 74 | + env: { |
| 75 | + ...process.env, |
| 76 | + NODE_EXTRA_CA_CERTS: path.join(fixtureDir, 'cert.pem'), |
| 77 | + EXTERNAL_FETCH_TLS_FIXTURE_DIR: fixtureDir, |
| 78 | + }, |
| 79 | + stdout: 'pipe', |
| 80 | + stderr: 'pipe', |
| 81 | + }, |
| 82 | + ); |
| 83 | + const [status, stdout, stderr] = await Promise.all([ |
| 84 | + child.exited, |
| 85 | + new Response(child.stdout).text(), |
| 86 | + new Response(child.stderr).text(), |
| 87 | + ]); |
| 88 | + expect(status, stderr).toBe(0); |
| 89 | + expect(stdout).toContain('EXTERNAL_FETCH_TLS_BOUNDARY_OK'); |
| 90 | + }, |
| 91 | + 60_000, |
| 92 | +); |
0 commit comments