Skip to content

Commit 3dbfa4e

Browse files
committed
fix(audits): skip test-helper fixtures in the egress scan and bar production code from loading them
1 parent 5d8f939 commit 3dbfa4e

2 files changed

Lines changed: 45 additions & 15 deletions

File tree

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
import { describe, expect, it } from 'vitest'
2-
import { mayLoadTransport } from './check-egress-boundary'
2+
import { findForbiddenLoads, mayLoadForbiddenModule } from './check-egress-boundary'
33

44
describe('egress transport candidate scan', () => {
55
it('finds literal transport modules', () => {
6-
expect(mayLoadTransport("import { request } from 'node:https'")).toBe(true)
6+
expect(mayLoadForbiddenModule("import { request } from 'node:https'")).toBe(true)
77
})
88

99
it('decodes escaped transport module literals', () => {
10-
expect(mayLoadTransport(String.raw`const http = require('node:\x68ttp')`)).toBe(true)
10+
expect(mayLoadForbiddenModule(String.raw`const http = require('node:\x68ttp')`)).toBe(true)
1111
})
1212

1313
it('ignores transport names outside string tokens', () => {
14-
expect(mayLoadTransport('const https = createClient()')).toBe(false)
14+
expect(mayLoadForbiddenModule('const https = createClient()')).toBe(false)
15+
})
16+
17+
it('flags production code that loads a test fixture, which the scan skips', () => {
18+
const source = "import { startFixture } from '@/lib/core/network/gateway-tls.test-helpers'"
19+
expect(mayLoadForbiddenModule(source)).toBe(true)
20+
expect(findForbiddenLoads('lib/x.ts', source)).toEqual([
21+
{
22+
line: 1,
23+
kind: 'import',
24+
specifier: '@/lib/core/network/gateway-tls.test-helpers',
25+
},
26+
])
1527
})
1628
})

‎scripts/check-egress-boundary.ts‎

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
* containing a quote hid a real one — which is what a scanner that does not
1919
* understand the grammar will keep doing.
2020
*
21+
* Test files (`*.test.ts`, `*.integration.ts`) and shared test fixtures
22+
* (`*.test-helpers.ts`) are not scanned: they run only under the test runner and
23+
* often stand up a loopback server to test against. Skipping a fixture would let
24+
* production code reach a transport through it, so a scanned module may not load
25+
* a `*.test-helpers` module at all.
26+
*
2127
* Not checked: bare `fetch()`. It is used constantly for same-origin and
2228
* server-action calls where the guard does not apply, so flagging it would be
2329
* noise. The transports it can reach are covered by the rules below.
@@ -46,6 +52,16 @@ const SCAN_DIRS = [
4652

4753
const SKIP_DIRS = new Set(['node_modules', 'dist', '.next', '.turbo', 'coverage'])
4854

55+
/** Test code, which runs only under the test runner and is not scanned. */
56+
const TEST_FILE = /\.(test|integration|test-helpers)\.tsx?$/
57+
58+
/** A shared test fixture, which production code may not load. */
59+
const TEST_HELPER_SPECIFIER = /\.test-helpers(\.tsx?)?$/
60+
61+
function isForbiddenSpecifier(specifier: string): boolean {
62+
return TRANSPORTS.has(specifier) || TEST_HELPER_SPECIFIER.test(specifier)
63+
}
64+
4965
/** Modules that can open a socket directly. */
5066
const TRANSPORTS = new Set([
5167
'http',
@@ -95,8 +111,7 @@ function walk(dir: string, out: string[] = []): string[] {
95111
if (SKIP_DIRS.has(entry.name)) continue
96112
const full = path.join(dir, entry.name)
97113
if (entry.isDirectory()) walk(full, out)
98-
else if (/\.(ts|tsx)$/.test(entry.name) && !/\.(test|integration)\.tsx?$/.test(entry.name))
99-
out.push(full)
114+
else if (/\.(ts|tsx)$/.test(entry.name) && !TEST_FILE.test(entry.name)) out.push(full)
100115
}
101116
return out
102117
}
@@ -145,15 +160,15 @@ function isElidedExport(node: ts.ExportDeclaration): boolean {
145160
}
146161

147162
/**
148-
* Every runtime reference to a transport module. An import TypeScript elides is
149-
* skipped: it has no runtime presence and cannot open anything.
163+
* Every runtime reference to a transport or test-helper module. An import
164+
* TypeScript elides is skipped: it has no runtime presence and cannot open anything.
150165
*/
151-
function findTransportLoads(file: string, source: string): Array<Omit<Violation, 'file'>> {
166+
export function findForbiddenLoads(file: string, source: string): Array<Omit<Violation, 'file'>> {
152167
const sourceFile = ts.createSourceFile(file, source, ts.ScriptTarget.Latest, false)
153168
const found: Array<Omit<Violation, 'file'>> = []
154169

155170
const record = (node: ts.Node, specifier: string, kind: string) => {
156-
if (!TRANSPORTS.has(specifier)) return
171+
if (!isForbiddenSpecifier(specifier)) return
157172
const { line } = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile))
158173
found.push({ line: line + 1, kind, specifier })
159174
}
@@ -185,7 +200,8 @@ function findTransportLoads(file: string, source: string): Array<Omit<Violation,
185200
return found
186201
}
187202

188-
export function mayLoadTransport(source: string): boolean {
203+
/** Cheap token prefilter: whether any string literal names a forbidden module. */
204+
export function mayLoadForbiddenModule(source: string): boolean {
189205
const scanner = ts.createScanner(
190206
ts.ScriptTarget.Latest,
191207
true,
@@ -196,7 +212,7 @@ export function mayLoadTransport(source: string): boolean {
196212
if (
197213
(token === ts.SyntaxKind.StringLiteral ||
198214
token === ts.SyntaxKind.NoSubstitutionTemplateLiteral) &&
199-
TRANSPORTS.has(scanner.getTokenValue())
215+
isForbiddenSpecifier(scanner.getTokenValue())
200216
) {
201217
return true
202218
}
@@ -214,8 +230,8 @@ function main() {
214230
if (ALLOWED.has(rel)) continue
215231
scanned++
216232
const source = readFileSync(file, 'utf8')
217-
if (!mayLoadTransport(source)) continue
218-
for (const load of findTransportLoads(rel, source)) {
233+
if (!mayLoadForbiddenModule(source)) continue
234+
for (const load of findForbiddenLoads(rel, source)) {
219235
violations.push({ file: rel, ...load })
220236
}
221237
}
@@ -236,7 +252,9 @@ function main() {
236252
' destination first, so a user- or model-supplied URL reaches the network\n' +
237253
' unchecked. Use secureFetchWithValidation (or secureFetchWithPinnedIP with\n' +
238254
' a validated address) from @/lib/core/security/input-validation.server and\n' +
239-
' pass the egress profile describing where the URL came from.\n'
255+
' pass the egress profile describing where the URL came from. A\n' +
256+
' *.test-helpers module is a test fixture outside this scan; production\n' +
257+
' code must not load it.\n'
240258
)
241259
process.exit(1)
242260
}

0 commit comments

Comments
 (0)