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
4753const 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 = / \. ( t e s t | i n t e g r a t i o n | t e s t - h e l p e r s ) \. t s x ? $ /
57+
58+ /** A shared test fixture, which production code may not load. */
59+ const TEST_HELPER_SPECIFIER = / \. t e s t - h e l p e r s ( \. t s x ? ) ? $ /
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. */
5066const 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 ( / \. ( t s | t s x ) $ / . test ( entry . name ) && ! / \. ( t e s t | i n t e g r a t i o n ) \. t s x ? $ / . test ( entry . name ) )
99- out . push ( full )
114+ else if ( / \. ( t s | t s x ) $ / . 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