Skip to content

Commit 839f190

Browse files
committed
fix(core): keep arithmetic taint when a substitution can build the subscript
1 parent 02d45a5 commit 839f190

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

‎packages/core/src/shell/scan.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,10 +1209,12 @@ function bashAppend(text: BashText, value: string) {
12091209
function bashEvaluatesExpansion(text: BashText, evaluation: BashEvaluation, context: BashContext) {
12101210
const combined = `${text.literal} ${text.output}`
12111211
const hasExpansion = BASH_EXPANSION_RE.test(combined)
1212-
const hasEscape = BASH_ESCAPE_RE.test(text.literal)
1212+
// Escapes reach the value through $'...' or a substitution's printf, echo -e, or tr.
1213+
const hasEscape = BASH_ESCAPE_RE.test(combined)
12131214
if (hasExpansion || hasEscape || /[$`]/.test(text.literal)) context.boundDollar = true
12141215
if (hasExpansion || hasEscape || /[({`]/.test(text.literal)) context.boundOpen = true
1215-
if (hasEscape || combined.includes("[")) context.boundBracket = true
1216+
// A substitution's output can hold `[` that the script never spells (printf '\x5b', tr, base64 -d).
1217+
if (hasExpansion || hasEscape || text.output.trim() || combined.includes("[")) context.boundBracket = true
12161218
if (evaluation === "deferred") return false
12171219
if (text.word.includes("[") || combined.includes("[")) context.sinkBracket = true
12181220
if (

‎packages/core/test/shell-parse-syntax.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe("native shell syntax compatibility", () => {
6262
"((count++))",
6363
"for ((i=0; i<2; i++)); do printf ok; done",
6464
"echo `printf \\2`",
65-
"F='$HOME/bin'; SIZE=$(stat -f %z file); N=$(( (SIZE + 1023) / 1024 )); echo $N",
65+
"F='$HOME/bin'; SIZE=4096; N=$(( (SIZE + 1023) / 1024 )); echo $N",
6666
])(`${shell} extracts commands without rejecting ordinary syntax: %s`, async (command) => {
6767
const scanned = await Effect.runPromise(ShellParse.scan(command, shell, "/workspace"))
6868
expect(await Effect.runPromise(ShellParse.scanPortable(command, shell, "/workspace"))).toEqual(scanned)
@@ -166,6 +166,19 @@ describe("native shell syntax compatibility", () => {
166166
}
167167
})
168168

169+
test.each([
170+
["bash", "x=\"a$(printf '\\x5b')\\$(touch pwn)]\"; echo $((x))"],
171+
["bash", "x=$(printf 'a\\x5b\\x24\\x28touch pwn\\x29]'); echo $((x))"],
172+
["bash", "x=$(echo 'a{$(touch pwn)]' | tr '{' '\\133'); (( x ))"],
173+
["bash", "F='$HOME/bin'; SIZE=$(stat -f %z file); N=$(( (SIZE + 1023) / 1024 )); echo $N"],
174+
["zsh", "r=\"a$(printf '\\x5b')\\$(touch pwn)]\"; echo ${(P)r}"],
175+
])("%s rejects variable sinks when a substitution can build the subscript: %s", async (shell, command) => {
176+
expect(await Effect.runPromise(Effect.result(ShellParse.scan(command, shell, "/workspace")))).toMatchObject({
177+
_tag: "Failure",
178+
failure: { message: "Portable shell scanner cannot analyze command: dynamic-execution" },
179+
})
180+
})
181+
169182
test.each([
170183
'Write-Output "$(Get-Location)"',
171184
"$value = Get-Date; Write-Output $value",

‎packages/core/test/shell-scan/oracle.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,15 @@ const fixtures = [
412412
"a=(1); echo ${a[$'a\\x5b\\x24\\x28scan_probe\\x29\\x5d']}",
413413
"a=(1); declare \"${x:-$'a\\x5b\\x24\\x28scan_probe\\x29\\x5d=1'}\"",
414414
"a=(1); unset \"${x:-$'a\\x5b\\x24\\x28scan_probe\\x29\\x5d'}\"",
415+
// A substitution can produce the `[` (or the whole payload) that the script never spells.
416+
"a=(1); x=\"a$(printf '\\x5b')\\$(scan_probe)]\"; echo $((x))",
417+
"a=(1); b=$(printf '\\x5b'); x=\"a${b}\\$(scan_probe)]\"; echo $((x))",
418+
"a=(1); x=$(echo 'a{$(scan_probe)]' | tr '{' '\\133'); echo $((x))",
419+
"a=(1); x='a{$(scan_probe)]'; y=${x/\\{/$(printf '\\x5b')}; (( y ))",
420+
"a=(1); x='a{$(scan_probe)]'; y=$(printf %s \"$x\" | tr '{' '\\133'); [[ y -eq 0 ]]",
421+
"a=(1); x=$(printf 'a\\x5b\\x24\\x28scan_probe\\x29]'); echo $((x))",
422+
"a=(1); r=\"a$(printf '\\x5b')\\$(scan_probe)]\"; echo ${!r}",
423+
"a=(1); r=\"a$(printf '\\x5b')\\$(scan_probe)]\"; echo ${(P)r}",
415424
"(( $'\\' )) # ' )); scan_probe",
416425
"declare -A a; a[$'\\']=1 # ']=1; scan_probe",
417426
"a=(1); echo ${a[$'\\']} # ']} $(scan_probe)",

0 commit comments

Comments
 (0)