Repository navigation
fix(spdx): bound recursion depth when parsing license expressions - #2995
Closed
Amey-Thakur wants to merge 1 commit into
Closed
Amey-Thakur wants to merge 1 commit into
Amey-Thakur wants to merge 1 commit into
Conversation
License expressions come from scanned package metadata, which is untrusted. The recursive-descent parser in Satisfies recursed once per nested bracket with no depth limit, so a deeply nested expression exhausted the goroutine stack and triggered a fatal stack overflow that recover cannot catch, aborting the whole scan. Thread a depth counter through the descent and return a parse error past a generous ceiling, so an over-nested expression is rejected like any other invalid input. Real SPDX expressions nest only a few levels, so no valid input is affected. This is distinct from the empty-token index panic in #2968; the next() bounds check there does not bound recursion depth.
Collaborator
|
Please read our contributing guide before opening pull requests |
4 tasks done
another-rex
added a commit
that referenced
this pull request
Sep 2, 2026
) ## Overview Fixes #2993. `spdx.Satisfies` parses SPDX license expressions with an unbounded recursive descent. License strings come from the metadata of scanned packages, so a crafted expression with deeply nested brackets recurses until the goroutine stack overflows, a fatal error that `recover` cannot catch. Reopens #2995, which was closed when its source fork was deleted. The change is identical, rebased on current `main`. Thank you @another-rex for the go-ahead. ## Details The descent runs `parseOr -> parseAnd -> parseExpression`, and `parseExpression` recurses into `parseOr` on every `(` with no bound. The fix: - a `depth` field on the `tokens` struct, incremented on each bracketed sub-expression and decremented on the way out - past `maxDepth` (1000), the parser returns an ordinary parse error instead of recursing Real SPDX expressions nest only a few levels, so the ceiling rejects nothing legitimate. An over-nested expression now fails like any other invalid input rather than terminating the process. CWE-674. ## Testing - `TestSatisfies_DeeplyNested` drives a 2,000,000-deep expression and asserts a parse error. It crashes with a fatal stack overflow without this change and passes with it. - `go test ./internal/spdx/` passes; `gofmt` and `go vet ./internal/spdx/` are clean. ## Checklist - [x] I have signed the [Contributor License Agreement](https://cla.developers.google.com/). - [x] I have run the linter (`gofmt`, `go vet`) on the changed package. - [x] I have run the unit tests for the changed package and all tests pass. - [x] I have made my commits and PR title follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) specification. Co-authored-by: Rex P <106129829+another-rex@users.noreply.github.com>
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.
Overview
Fixes #2993.
spdx.Satisfiesparses SPDX license expressions with an unbounded recursivedescent. Because license strings come from the metadata of packages in the
scanned target, a crafted expression with deeply nested brackets recurses until
the goroutine stack overflows. That is a Go fatal error which
recovercannotcatch, so the whole scan process aborts.
This is a single, self-contained change addressing the specific issue I filed
(#2993), which I intend to shepherd through review myself. It is distinct from
the empty-token index panic in #2968; the
next()bounds check discussed theredoes not bound recursion depth.
Details
The recursion runs
parseOr -> parseAnd -> parseExpression, andparseExpressioncalls back intoparseOron every(, with no limit onnesting depth. On this machine the crash threshold sits between 1,000,000
nested brackets (survives) and 1,500,000 (overflows), roughly a 3 MB license
string.
The fix threads a nesting-depth counter through the descent and returns an
ordinary parse error once it passes a generous ceiling:
depthfield is added to thetokensstruct, incremented whenparseExpressionrecurses into a bracketed sub-expression, andmaxDepth(1000), the parser returns a normal errorrather than recursing further.
Real SPDX expressions nest only a few levels, so the ceiling rejects nothing
legitimate. An over-nested expression is now rejected the same way as any other
invalid input, instead of terminating the process. CWE-674 (Uncontrolled
Recursion).
Testing
TestSatisfies_DeeplyNestedininternal/spdx/satisfies_test.go, whichdrives a deeply nested expression and asserts a parse error rather than a
crash. It triggers a fatal stack overflow without this change and passes with
it.
go test ./internal/spdx/...passes.gofmtandgo vet ./internal/spdx/are clean.Checklist
gofmt,go vet) on the changed package.If the maintainers would prefer to assign #2993 before review, I am happy to
wait; opening the pull request here to make the proposed fix concrete.