Skip to content

fix(spdx): bound recursion depth when parsing license expressions - #2995

Closed
Amey-Thakur wants to merge 1 commit into
google:mainfrom
Amey-Thakur:fix/spdx-recursion-depth-limit
Closed

Amey-Thakur wants to merge 1 commit into
google:mainfrom
Amey-Thakur:fix/spdx-recursion-depth-limit

Conversation

@Amey-Thakur

Copy link
Copy Markdown
Contributor

Overview

Fixes #2993.

spdx.Satisfies parses SPDX license expressions with an unbounded recursive
descent. 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 recover cannot
catch, 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 there
does not bound recursion depth.

Details

The recursion runs parseOr -> parseAnd -> parseExpression, and
parseExpression calls back into parseOr on every (, with no limit on
nesting 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:

  • a depth field is added to the tokens struct, incremented when
    parseExpression recurses into a bracketed sub-expression, and
  • when the depth exceeds maxDepth (1000), the parser returns a normal error
    rather 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

  • Added TestSatisfies_DeeplyNested in internal/spdx/satisfies_test.go, which
    drives 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.
  • Ran the full package suite: go test ./internal/spdx/... passes.
  • gofmt and go vet ./internal/spdx/ are clean.

Checklist

  • I have signed the Contributor License Agreement.
  • I have run the linter (gofmt, go vet) on the changed package.
  • I have run the unit tests for the changed package and all tests pass.
  • I have made my commits and PR title follow the Conventional Commits specification.

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.

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.
@G-Rath

G-Rath commented Aug 15, 2026

Copy link
Copy Markdown
Collaborator

Please read our contributing guide before opening pull requests

@G-Rath G-Rath closed this Aug 15, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

spdx.Satisfies: unbounded recursion causes uncatchable stack overflow on deeply nested license expressions

2 participants