fix(spdx): guard against panic on empty license expressions - #3023
Open
sl4x0 wants to merge 1 commit into
Open
Conversation
G-Rath
requested changes
Aug 27, 2026
G-Rath
left a comment
Collaborator
There was a problem hiding this comment.
can you tone the comments down a bit? we don't need to be describing the past behaviour because that's what git and commit messages are for
Author
|
Note on the checklist: the linter (./scripts/run_lints.sh\ / golangci-lint v2.11.4) flags 19 files in the \osvscanner\ package as gofmt-unformatted on my Windows checkout, including files this PR does not touch (e.g. \pkg/osvscanner/stats.go, \pkg/osvscanner/vulnerability_result.go). Root cause is git's CRLF autocrlf conversion on Windows, not the diff. I verified the changed files (and an untouched control file) are gofmt-clean by running gofmt on LF-normalized copies, and CI on Linux will confirm. |
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 #2968
osv-scanner scan --licenses <allowlist>panics withindex out of rangewhen any scanned package has an empty license field. An empty license string reachesspdx.Satisfies("", allowlist): the tokenizer produces zero tokens, andtokens.next()dereferencests.tokens[0]without a bounds check, terminating the whole scan.Details
Two changes in
internal/spdx/satisfies.go:nextAndIsNextNextValidnow returns an error when the token stream is exhausted instead of callingnext()on an empty list, so an empty expression can never reach the index-out-of-range dereference. This is the root-cause guard (peek()already protected itself;next()did not).Satisfieshandles empty/whitespace-only expressions explicitly, returningfalse, nilso an empty license is treated as "not satisfied by the allowlist" and recorded as a violation (viapkg/osvscanner/vulnerability_result.go) rather than aborting the scan.Regression tests:
internal/spdx/satisfies_test.go: empty ("") and whitespace-only (" ") license expressions added toTestSatisfies, asserting they evaluate to not-satisfied without an error (these panicked at HEAD).pkg/osvscanner/vulnerability_result_internal_test.go:TestBuildVulnerabilityResultsWithEmptyLicensedrives a package withLicenses: []string{""}throughbuildVulnerabilityResultswith aMITallowlist and asserts the empty license is reported as a violation and the scan completes.Testing
go test ./internal/spdx -run TestSatisfiesbefore the fix.go test ./internal/spdx/... ./pkg/osvscanner/...passes after the fix (including the new regression tests).go vet ./internal/spdx/... ./pkg/osvscanner/...reports no issues.go build ./...succeeds.Checklist
./scripts/run_lints.sh../scripts/run_tests.sh.