fix: guard against panic on malformed SPDX license expression#548
Open
arpitjain099 wants to merge 1 commit into
Open
fix: guard against panic on malformed SPDX license expression#548arpitjain099 wants to merge 1 commit into
arpitjain099 wants to merge 1 commit into
Conversation
spdxexp.ExtractLicenses can panic (nil pointer dereference) on some
malformed expressions, for example one with a dangling opening
parenthesis like "MIT AND (". SBOM license fields are decoded straight
into pkg.License.SPDXExpression without revalidation, so a single
malformed value in an input SBOM crashes the whole scan.
Wrap the call in a small helper that recovers from the panic and
returns an error, letting the existing error path fall back to treating
the value as a non-SPDX license. syft guards its own call to the same
parser for this reason (anchore/syft#1837).
Adds a test that drives ConvertSyftLicenses with several malformed
expressions and asserts it no longer panics.
Signed-off-by: Arpit Jain <[email protected]>
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.
What
spdxexp.ExtractLicensescan panic (nil pointer dereference) on some malformed SPDX expressions. The one I hit is an expression with a dangling opening parenthesis, for exampleMIT AND ((also plain(,(((((,MIT OR ().handleSPDXLicenseingrant/license.gocalls it and only handles the returnederror, not a panic. Because SBOM license fields are decoded straight intopkg.License.SPDXExpressionwithout re-validation (syft's json decoder copies the field through as-is), a single malformed license value in an input SBOM takes down the whole scan rather than being skipped.Fix
Wrap the call in a small
safeExtractLicenseshelper that recovers from the panic and returns an error, so the existing error branch falls back to treating the value as a non-SPDX license. Minimal change, existing behavior for valid expressions is unchanged.syft guards its own call to this same parser for exactly this reason, see the recover in
license.ParseExpressionand anchore/syft#1837. This just brings grant in line.Test
Added
TestConvertSyftLicenses_MalformedSPDXExpressionDoesNotPanic, which drives the realConvertSyftLicensesentry point with the malformed expressions above and asserts they fall back to a non-SPDX license instead of panicking.Before the guard (reverting just the one call back to
spdxexp.ExtractLicenses):After:
go build ./...andgo test ./grant/both pass.This is a robustness fix for malformed input, not a security report.