Repository navigation
fix(syntax): stop crashes and hangs from deep or malformed shell input - #876
Merged
Merged
Conversation
Deeply nested input such as $(( followed by 100k parentheses, or long a && b && ... lists, overflowed the goroutine stack in the parser, in Walk or in the interpreter, a fatal error recover() cannot catch. The parser now fails with a ParseError past MaxNestingDepth (1000) nested constructs, including shell parts re-parsed by pattern and heredoc sub-parsers, and past MaxTreeDepth (10000) tree levels built by operator lists. Walk keeps its own stack. StmtsSeq no longer yields again after its consumer stops.
With extglob on, a pattern such as "*(" or "@(a|" from a variable made
pattern.Regexp emit an invalid regular expression that leaked its NUL
end marker, and callers panicked in regexp.MustCompile, aborting the
whole execution. Like bash, an unterminated group and everything after
it now match literally, in both pattern package copies and in the
negated-extglob matcher, and callers compile with regexp.Compile.
The parser keeps ${%} with a nil Param so it fails at runtime like bash.
Minify dereferenced that Param (found by FuzzParsePrint), the printer
rendered it as ${}, for example in declare -f output, and quoted
expansion panicked in the array-field path. The printer now writes the
original source and expansion reports "${%}: bad substitution".
After scanning a case pattern, the parser skipped tokens up to the end of the pattern, but the lexer never moves past a backquote token on its own, so any backquote substitution in a case pattern, and even "case i in `;", spun forever (found by FuzzParsePrint). Backquote substitutions are now consumed as a whole, and the skip loop stops when the lexer stalls.
ewhauser
enabled auto-merge (squash)
October 19, 2026 20:39
…hes-review-7f23ca
The interpreter budgets merged from main stop deep subshell nesting at run time and expect 10000 nested subshells and 5000-stage pipelines to parse. Raise MaxNestingDepth to 16000 and MaxTreeDepth to 50000, which probing shows stay far from the goroutine stack limit, and test that pattern and heredoc sub-parsers inherit the depth by starting near the limit instead of parsing very deep input.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the parser, AST-walk, arithmetic and pattern findings from the security review against 83b31f1. Two of them were fatal Go stack overflows:
recover()can't catch those, so they killed the embedding process.What changed
Arithmetic recursion (critical)
Arithmagain, and each call restarted its depth count at 0. Self-referencing values recursed until the process died, e.g.:x="x[x]"; echo $((x))a=(); i="a[i]"; echo ${a[i]}x='y=x'; echo $((x))expand.Confignow stops these with bash's error, e.g.x[x]: expression recursion level exceeded (error token is "x[x]").Parser and
Walknesting (critical)$((followed by 100k(, or deep( (,{,$(,${x:-,[[ ! …or unary-, overflowed the stack in the parser or inWalk. Tiny scripts could reach this througheval.ParseError(nesting exceeds the maximum depth of N) in two cases:syntax.MaxNestingDepth(16000) nested constructs;syntax.MaxTreeDepth(50000) tree levels. Operator lists likea && b && …are built in a loop but still add a level per operator, and the interpreter recursed on them.[[ x == $([[ x == $(…got past the limit.syntax.Walkkeeps an explicit stack and visits nodes in exactly the old order; a test checks it against a copy of the old recursive version.StmtsSeqno longer yields again after its consumer stops (that panicked with range-over-func).Unbalanced extglob from variables (medium)
"*(","@(a|"or"+(a"madepattern.Regexpemit an invalid regex that leaked its NUL end marker. Callers then panicked inregexp.MustCompile, aborting the whole execution.regexp.Compile.${%}(low)${%}instead of${}(e.g. indeclare -f), and Minify no longer dereferences the nilParam."${%}"panicked at expansion; it now reports${%}: bad substitution.casepatterns with backquotes (found by FuzzParsePrint)casepattern made the parser loop forever, evencase i in `;. Backquote substitutions are now parsed in full, as bash does, and the token-skip loop stops if the lexer stops advancing.For reviewers
Walkis about 20% slower on nvm.sh, with fewer allocations. The extra tree-depth walk after parsing only runs when a parse saw at least 256 levels of nesting or chained operators.Testing
go test ./shell/... ./internal/shellpattern/...passes.make lintreports 0 issues.make conformance-testhad 0 failures. It ran before thecasefix; the case, arith, glob and dbracket conformance files pass on top of it.go test ./...failures are unrelated:scripts/bench-comparefails the same way on main;tail -fCLI tests failed under load and pass on rerun.Known follow-ups (not in this PR, and none crash)
[[ ]]and nested$(case …)incasepatterns still parse in exponential time. A separate change is in progress.a[a[…]]and deep[[ == ]]patterns re-scan the rest of the input at every level, so input near the limit can take tens of seconds to reject.$((1+1+…))with 100k terms is slow to evaluate.