@@ -263,6 +263,19 @@ function commands(node: Node) {
263263 return node . descendantsOfType ( "command" ) . filter ( ( child ) : child is Node => Boolean ( child ) )
264264}
265265
266+ // A function body is defined before the assignment its later call resolves
267+ // (`f(){ cat $x; }; x=/etc/hostname; f`), so an offset-keyed scope excludes it. The
268+ // body is classified with the fully-merged scope instead: conservative, and correct for
269+ // the call that actually runs it.
270+ function inFunction ( node : Node ) {
271+ let current : Node | null = node . parent
272+ while ( current ) {
273+ if ( current . type === "function_definition" ) return true
274+ current = current . parent
275+ }
276+ return false
277+ }
278+
266279// Bash deletes `\<newline>` continuations before parsing, so `ca\<newline>t` runs `cat`.
267280// Single quotes keep the backslash literal; escapes inside double quotes are preserved.
268281function stripLineContinuations ( text : string ) {
@@ -592,22 +605,31 @@ function auto(key: string, cwd: string, shell: string) {
592605}
593606
594607function expand ( text : string , cwd : string , shell : string , vars ?: Map < string , string > ) {
595- let unquoted = unquote ( text )
596- // `${NAME:-word}` / `${NAME:=word}` yields `word` when NAME is unset. Resolving the
597- // default keeps `cat ${O:-/etc/hostname}` visible to the scan instead of leaving an
598- // unresolvable expansion that the path model skips.
599- unquoted = unquoted . replace (
600- / \$ \{ ( [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * ) (?: : - | : = | - ) ( [ ^ } ] * ) \} / g,
601- ( _whole , name : string , fallback : string ) => vars ?. get ( name ) ?? fallback ,
602- )
603- if ( vars && vars . size > 0 ) {
604- // One assignment can name another (`A=$B`), so resolve transitively until the
605- // text stops changing instead of leaving a `$B` that hides the real path.
606- for ( let pass = 0 ; pass < 8 ; pass ++ ) {
607- const next = unquoted . replace ( / \$ \{ ? ( [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * ) \} ? / g, ( whole , name : string ) => vars . get ( name ) ?? whole )
608- if ( next === unquoted ) break
609- unquoted = next
610- }
608+ // All parameter expansions are resolved together and repeatedly rather than in a
609+ // fixed order at one offset: a default introduced *by* a variable's value
610+ // (`C="${X:-cat /etc/hostname}"; bash -c "$C"`) is not visible until the variable
611+ // itself is substituted, so the pass must run again on its own output.
612+ const substitute = ( value : string ) =>
613+ value
614+ // `${NAME:+word}` yields `word` when NAME is set. The classifier cannot observe an
615+ // environment variable's state, so it resolves to the word: the conservative
616+ // direction, and the real read when the command sets the variable itself
617+ // (`O=notes.txt; cat ${O:+/etc/hostname}`).
618+ . replace ( / \$ \{ ( [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * ) : \+ ( [ ^ } ] * ) \} / g, ( _whole , _name : string , alternate : string ) => alternate )
619+ // `${NAME:-word}` / `${NAME:=word}` yields `word` when NAME is unset. Resolving the
620+ // default keeps `cat ${O:-/etc/hostname}` visible to the scan instead of leaving an
621+ // unresolvable expansion that the path model skips.
622+ . replace (
623+ / \$ \{ ( [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * ) (?: : - | : = | - ) ( [ ^ } ] * ) \} / g,
624+ ( _whole , name : string , fallback : string ) => vars ?. get ( name ) ?? fallback ,
625+ )
626+ // One assignment can name another (`A=$B`), so resolve transitively.
627+ . replace ( / \$ \{ ? ( [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * ) \} ? / g, ( whole , name : string ) => vars ?. get ( name ) ?? whole )
628+ let unquoted = substitute ( unquote ( text ) )
629+ for ( let pass = 0 ; pass < 8 ; pass ++ ) {
630+ const next = substitute ( unquoted )
631+ if ( next === unquoted ) break
632+ unquoted = next
611633 }
612634 const out = unquoted
613635 . replace ( / \$ \{ e n v : ( [ ^ } ] + ) \} / gi, ( _ , key : string ) => envValue ( key ) || "" )
@@ -1073,12 +1095,15 @@ function attachedOptionValue(text: string, name?: string) {
10731095// The operands a fail-closed scan must inspect for one token: the token itself plus
10741096// the value of an `if=`/`of=`-style assignment, with attached option paths split out.
10751097function operandTargets ( text : string , name ?: string ) {
1076- if ( text . startsWith ( "-" ) && text !== "-" ) {
1077- const value = attachedOptionValue ( text , name )
1098+ // Quotes hide an option from the `startsWith("-")` test (`gcc "-I/etc"`), so the
1099+ // attached value is extracted from the unquoted token the shell actually runs.
1100+ const bare = unquote ( text )
1101+ if ( bare . startsWith ( "-" ) && bare !== "-" ) {
1102+ const value = attachedOptionValue ( bare , name )
10781103 return value === undefined ? [ ] : [ value ]
10791104 }
1080- const operand = / ^ [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * = ( .* ) $ / . exec ( text )
1081- return operand ? [ text , operand [ 1 ] ] : [ text ]
1105+ const operand = / ^ [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * = ( .* ) $ / . exec ( bare )
1106+ return operand ? [ bare , operand [ 1 ] ] : [ text ]
10821107}
10831108
10841109// Local-path option positions of a remote CLI. Everything else on those command lines
@@ -1227,22 +1252,33 @@ function remotePaths(name: string, command: Part[]) {
12271252 const globalLong = REMOTE_GLOBAL_LONG [ name ] ?? new Set < string > ( )
12281253 const subcommands = REMOTE_SUBCOMMANDS [ name ]
12291254 const out : string [ ] = [ ]
1230- const positionals : string [ ] = [ ]
1255+ // A positional can arrive as the value of an unmodelled global option
1256+ // (`docker --tlscacert ps cp …`). It must still be scanned, but it must never be
1257+ // mistaken for the subcommand.
1258+ const positionals : { text : string ; optionValue : boolean } [ ] = [ ]
1259+ let plain = 0
12311260 let index = 0
12321261 const addValue = ( volume : boolean , value : string ) => {
12331262 out . push ( ...( volume ? mountLocal ( value ) : [ stripKeyPrefix ( value ) ] ) )
12341263 }
12351264 while ( index < args . length ) {
12361265 const text = args [ index ] . text
12371266 if ( text === "--" ) {
1238- for ( const item of args . slice ( index + 1 ) ) positionals . push ( item . text )
1267+ for ( const item of args . slice ( index + 1 ) ) {
1268+ positionals . push ( { text : item . text , optionValue : false } )
1269+ plain ++
1270+ }
12391271 break
12401272 }
12411273 if ( text . startsWith ( "--" ) ) {
12421274 const equals = text . indexOf ( "=" )
12431275 if ( equals !== - 1 ) {
12441276 const key = text . slice ( 0 , equals )
12451277 if ( pathLong . has ( key ) ) addValue ( VOLUME_LONG . has ( key ) , text . slice ( equals + 1 ) )
1278+ // `docker --config=<dir>` names a local directory whose `<dir>/config.json` the
1279+ // runtime opens. The space form is a declared residual (it would prompt on the
1280+ // routine `--config ~/.docker`); the attached `=` form is consumed and scanned.
1281+ else if ( key === "--config" ) out . push ( stripKeyPrefix ( text . slice ( equals + 1 ) ) )
12461282 index ++
12471283 continue
12481284 }
@@ -1252,13 +1288,22 @@ function remotePaths(name: string, command: Part[]) {
12521288 index += 2
12531289 continue
12541290 }
1255- // A known global value-option before the subcommand consumes its value. An
1256- // unknown long option is deliberately left unconsumed: its value falls into
1257- // `positionals` and cannot displace the subcommand, which is located by word.
1258- if ( positionals . length === 0 && globalLong . has ( text ) ) {
1291+ // A known global value-option before the subcommand consumes its value.
1292+ if ( plain === 0 && globalLong . has ( text ) ) {
12591293 index += 2
12601294 continue
12611295 }
1296+ // An unknown long option in the global region may still consume the next token
1297+ // (`docker --tlscacert ps cp …`). Keep that token in `positionals` so it is
1298+ // scanned, but mark it so `findIndex` cannot lock onto it as the subcommand.
1299+ if ( plain === 0 ) {
1300+ const value = args [ index + 1 ] ?. text
1301+ if ( value !== undefined && ! value . startsWith ( "-" ) ) {
1302+ positionals . push ( { text : value , optionValue : true } )
1303+ index += 2
1304+ continue
1305+ }
1306+ }
12621307 index ++
12631308 continue
12641309 }
@@ -1277,52 +1322,64 @@ function remotePaths(name: string, command: Part[]) {
12771322 }
12781323 break
12791324 }
1280- if ( positionals . length === 0 && globalChars . includes ( flag ) ) {
1325+ if ( plain === 0 && globalChars . includes ( flag ) ) {
12811326 if ( at === body . length - 1 ) consumeNext = true
12821327 break
12831328 }
12841329 }
12851330 index += consumeNext ? 2 : 1
12861331 continue
12871332 }
1288- positionals . push ( text )
1333+ positionals . push ( { text, optionValue : false } )
1334+ plain ++
12891335 index ++
12901336 }
12911337 // Locate the runtime subcommand as a known subcommand word rather than the first
12921338 // positional: an unmodelled global value-option (`docker --tlscacert <f> cp …`) pushes
12931339 // its value in front, and keying the operand rules off `positionals[0]` then missed
1294- // `cp`/`build` entirely.
1295- const subIndex = subcommands ? positionals . findIndex ( ( item ) => subcommands . has ( item ) ) : - 1
1296- const sub = subIndex === - 1 ? positionals [ 0 ] : positionals [ subIndex ]
1340+ // `cp`/`build` entirely. Option values are not candidates: a CA bundle named `ps` must
1341+ // not displace the real `cp`.
1342+ const subIndex = subcommands
1343+ ? positionals . findIndex ( ( item ) => ! item . optionValue && subcommands . has ( item . text ) )
1344+ : - 1
1345+ const sub = subIndex === - 1 ? positionals [ 0 ] ?. text : positionals [ subIndex ] . text
12971346 // A `container:/path` operand resolves inside the runtime; a bare one is local.
12981347 const local = ( operand : string ) => ! / ^ [ ^ / \\ : ] + : / . test ( operand )
12991348 // Values of unmodelled global options sit in front of the subcommand. They are local
13001349 // files (a CA bundle, a connection name), not remote operands, so scan them; without
13011350 // this a stored `docker --tlscacert *` grant would absorb the read.
13021351 if ( subcommands && subIndex > 0 ) {
1303- for ( const operand of positionals . slice ( 0 , subIndex ) ) if ( local ( operand ) ) out . push ( operand )
1352+ for ( const operand of positionals . slice ( 0 , subIndex ) ) if ( local ( operand . text ) ) out . push ( operand . text )
13041353 }
13051354 if ( subcommands && subIndex === - 1 ) {
13061355 // No known subcommand word: fail closed and scan every local positional.
1307- for ( const operand of positionals ) if ( local ( operand ) ) out . push ( operand )
1356+ for ( const operand of positionals ) if ( local ( operand . text ) ) out . push ( operand . text )
13081357 } else if ( sub === "cp" ) {
1309- for ( const operand of positionals . slice ( subIndex + 1 ) ) if ( local ( operand ) ) out . push ( operand )
1358+ for ( const operand of positionals . slice ( subIndex + 1 ) ) if ( local ( operand . text ) ) out . push ( operand . text )
13101359 } else if ( ( name === "docker" || name === "podman" ) && sub === "build" ) {
1311- const context = positionals [ positionals . length - 1 ]
1360+ const context = positionals [ positionals . length - 1 ] ?. text
13121361 if ( context !== undefined && context !== sub && context !== "." ) out . push ( context )
13131362 } else if ( sub === "load" || sub === "import" ) {
13141363 // `load -i FILE` / `import FILE` read a local archive or image.
1315- for ( const operand of positionals . slice ( subIndex + 1 ) ) if ( local ( operand ) ) out . push ( operand )
1316- } else if ( ( name === "docker" || name === "podman" ) && sub === "play" && positionals [ subIndex + 1 ] === "kube" ) {
1317- for ( const operand of positionals . slice ( subIndex + 2 ) ) if ( local ( operand ) ) out . push ( operand )
1318- } else if ( ( name === "docker" || name === "podman" ) && sub === "kube" && positionals [ subIndex + 1 ] === "play" ) {
1319- for ( const operand of positionals . slice ( subIndex + 2 ) ) if ( local ( operand ) ) out . push ( operand )
1364+ for ( const operand of positionals . slice ( subIndex + 1 ) ) if ( local ( operand . text ) ) out . push ( operand . text )
1365+ } else if (
1366+ ( name === "docker" || name === "podman" ) &&
1367+ sub === "play" &&
1368+ positionals [ subIndex + 1 ] ?. text === "kube"
1369+ ) {
1370+ for ( const operand of positionals . slice ( subIndex + 2 ) ) if ( local ( operand . text ) ) out . push ( operand . text )
1371+ } else if (
1372+ ( name === "docker" || name === "podman" ) &&
1373+ sub === "kube" &&
1374+ positionals [ subIndex + 1 ] ?. text === "play"
1375+ ) {
1376+ for ( const operand of positionals . slice ( subIndex + 2 ) ) if ( local ( operand . text ) ) out . push ( operand . text )
13201377 } else if (
13211378 ( name === "docker" || name === "podman" ) &&
13221379 sub === "context" &&
1323- positionals [ subIndex + 1 ] === "import"
1380+ positionals [ subIndex + 1 ] ?. text === "import"
13241381 ) {
1325- for ( const operand of positionals . slice ( subIndex + 3 ) ) if ( local ( operand ) ) out . push ( operand )
1382+ for ( const operand of positionals . slice ( subIndex + 3 ) ) if ( local ( operand . text ) ) out . push ( operand . text )
13261383 }
13271384 return out
13281385}
@@ -1331,12 +1388,13 @@ function pathArgs(list: Part[], ps: boolean, cmd = false) {
13311388 if ( ! ps ) {
13321389 const name = list [ 0 ] ? bareName ( commandName ( list [ 0 ] . text ) ) : undefined
13331390 return list . slice ( 1 ) . flatMap ( ( item ) : string [ ] => {
1334- if ( item . text . startsWith ( "-" ) && item . text !== "-" ) {
1335- const value = attachedOptionValue ( item . text , name )
1391+ const bare = unquote ( item . text )
1392+ if ( bare . startsWith ( "-" ) && bare !== "-" ) {
1393+ const value = attachedOptionValue ( bare , name )
13361394 return value === undefined ? [ ] : [ value ]
13371395 }
1338- if ( cmd && item . text . startsWith ( "/" ) ) return [ ]
1339- if ( list [ 0 ] ?. text === "chmod" && item . text . startsWith ( "+" ) ) return [ ]
1396+ if ( cmd && bare . startsWith ( "/" ) ) return [ ]
1397+ if ( name === "chmod" && bare . startsWith ( "+" ) ) return [ ]
13401398 return [ item . text ]
13411399 } )
13421400 }
@@ -1685,7 +1743,8 @@ export const ShellTool = Tool.define(
16851743 // A bare `$(...)` parses as a command whose name is the substitution itself;
16861744 // its real program is the descendant command, which `commands` also returns.
16871745 if ( nested . length === 0 || dynamic ( unquote ( nested [ 0 ] . text ) , ps ) ) continue
1688- const result = yield * classify ( nested , depth + 1 , true , scoped ( node . startIndex ) )
1746+ const scope = scoped ( inFunction ( node ) ? Number . MAX_SAFE_INTEGER : node . startIndex )
1747+ const result = yield * classify ( nested , depth + 1 , true , scope )
16891748 if ( result . conservative ) conservative = true
16901749 }
16911750 // Redirect destinations inside the re-parsed payload (`echo 'true > /tmp/out'`)
@@ -1695,11 +1754,12 @@ export const ShellTool = Tool.define(
16951754 const destination = redirect . childForFieldName ( "destination" )
16961755 if ( ! destination ) continue
16971756 const target = destination . text
1698- if ( yield * addPath ( yield * argPath ( target , cwd , ps , shell , scoped ( redirect . startIndex ) ) ) ) {
1757+ const scope = scoped ( inFunction ( redirect ) ? Number . MAX_SAFE_INTEGER : redirect . startIndex )
1758+ if ( yield * addPath ( yield * argPath ( target , cwd , ps , shell , scope ) ) ) {
16991759 external = true
17001760 continue
17011761 }
1702- if ( target . includes ( "$(" ) || target . includes ( "`" ) ) yield * reparse ( target , scoped ( redirect . startIndex ) )
1762+ if ( target . includes ( "$(" ) || target . includes ( "`" ) ) yield * reparse ( target , scope )
17031763 }
17041764 tree . delete ( )
17051765 } )
@@ -1760,9 +1820,13 @@ export const ShellTool = Tool.define(
17601820 // (`C='cat /etc/hostname'; bash -c "$C"`) becomes the command the shell runs.
17611821 // An unassigned `$HOME`/`$VAR` is left intact so the cwd/`dynamic` handling
17621822 // (`eval "cd $HOME"`) still sees it as dynamic instead of a literal path.
1763- const known = [ ...info . evalScript . matchAll ( / \$ \{ ? ( [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * ) / g) ] . some ( ( match ) =>
1764- vars . has ( match [ 1 ] ) ,
1765- )
1823+ // A `${…:-word}`/`${…:+word}` default is resolved even without a known variable:
1824+ // `expand` substitutes the word, so `bash -c "${X:-cat notes.txt}"` stays an
1825+ // in-tree read rather than a literal script the fail-closed anchor would prompt.
1826+ const defaulted = / \$ \{ [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * (?: : - | : = | - | \+ : ) / . test ( info . evalScript )
1827+ const known =
1828+ defaulted ||
1829+ [ ...info . evalScript . matchAll ( / \$ \{ ? ( [ A - Z a - z _ ] [ A - Z a - z 0 - 9 _ ] * ) / g) ] . some ( ( match ) => vars . has ( match [ 1 ] ) )
17661830 const script = known ? expand ( info . evalScript , cwd , shell , vars ) : info . evalScript
17671831 // A `$(...)`/backtick inside the extracted string is not data: the substitution
17681832 // result is what the shell executes, so its content must never hit `DATA`.
@@ -1771,15 +1835,40 @@ export const ShellTool = Tool.define(
17711835 if ( Exit . isSuccess ( exit ) ) {
17721836 const tree = exit . value
17731837 const nested = commands ( tree . rootNode )
1838+ // The payload carries its own assignments (`C='x=/etc/hostname; cat $x'`); they
1839+ // are live for the commands that follow them, so build the same offset-keyed
1840+ // view `reparse` uses, merged over the caller's scope.
1841+ const local : { at : number ; name : string ; value : string } [ ] = [ ]
1842+ for ( const node of tree . rootNode . descendantsOfType ( "variable_assignment" ) ) {
1843+ const name = node ?. childForFieldName ( "name" ) ?. text
1844+ const value = node ?. childForFieldName ( "value" ) ?. text
1845+ if ( name && value !== undefined ) local . push ( { at : node . startIndex , name, value : unquote ( value ) } )
1846+ }
1847+ for ( const node of tree . rootNode . descendantsOfType ( "for_statement" ) ) {
1848+ const name = node ?. childForFieldName ( "variable" ) ?. text
1849+ const value = node ?. childForFieldName ( "value" ) ?. text
1850+ if ( name && value !== undefined ) local . push ( { at : node . startIndex , name, value : unquote ( value ) } )
1851+ }
1852+ const scoped = ( at : number ) => {
1853+ const merged = new Map ( vars )
1854+ for ( const item of local ) if ( item . at <= at ) merged . set ( item . name , item . value )
1855+ return merged
1856+ }
1857+ let classified = 0
17741858 for ( const node of nested ) {
17751859 const item = parts ( node )
17761860 // A bare `$(...)` string parses as a command named by the substitution; its
17771861 // program is the descendant command, so skip the wrapper to avoid a false root anchor.
17781862 if ( item . length === 0 || dynamic ( unquote ( item [ 0 ] . text ) , ps ) ) continue
1779- const result = yield * classify ( item , depth + 1 , nestedSubstituted , vars )
1863+ classified ++
1864+ const scope = scoped ( inFunction ( node ) ? Number . MAX_SAFE_INTEGER : node . startIndex )
1865+ const result = yield * classify ( item , depth + 1 , nestedSubstituted , scope )
17801866 if ( result . conservative ) conservative = true
17811867 }
1782- if ( nested . length === 0 && tree . rootNode . hasError && script . trim ( ) . length > 0 ) {
1868+ // Every command was rejected by `dynamic()` (an unassigned `$Q` prefix, or an
1869+ // unresolvable `${X:-…}` default): the shell still runs that program text, so
1870+ // fail closed rather than drop it unscanned with no anchor.
1871+ if ( classified === 0 && script . trim ( ) . length > 0 ) {
17831872 state . dynamic = true
17841873 scan . dirs . add ( path . parse ( cwd ) . root )
17851874 }
@@ -1847,7 +1936,10 @@ export const ShellTool = Tool.define(
18471936 // anywhere in the invocation (including through a wrapper or `eval`) suppresses
18481937 // it for all of them, not just commands that happen to parse after it.
18491938 const resolved : { name ?: string ; info : Effective ; conservative ?: boolean } [ ] = [ ]
1850- for ( const entry of entries ) resolved . push ( yield * classify ( entry . command , 0 , false , varsAt ( entry . node . startIndex ) ) )
1939+ for ( const entry of entries ) {
1940+ const scope = varsAt ( inFunction ( entry . node ) ? Number . MAX_SAFE_INTEGER : entry . node . startIndex )
1941+ resolved . push ( yield * classify ( entry . command , 0 , false , scope ) )
1942+ }
18511943
18521944 for ( let index = 0 ; index < entries . length ; index ++ ) {
18531945 const entry = entries [ index ]
0 commit comments