Skip to content

fix: guard against panic on malformed SPDX license expression#548

Open
arpitjain099 wants to merge 1 commit into
anchore:mainfrom
arpitjain099:fix/guard-spdx-expression-panic
Open

fix: guard against panic on malformed SPDX license expression#548
arpitjain099 wants to merge 1 commit into
anchore:mainfrom
arpitjain099:fix/guard-spdx-expression-panic

Conversation

@arpitjain099

Copy link
Copy Markdown
Contributor

What

spdxexp.ExtractLicenses can panic (nil pointer dereference) on some malformed SPDX expressions. The one I hit is an expression with a dangling opening parenthesis, for example MIT AND ( (also plain (, (((((, MIT OR ().

handleSPDXLicense in grant/license.go calls it and only handles the returned error, not a panic. Because SBOM license fields are decoded straight into pkg.License.SPDXExpression without 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 safeExtractLicenses helper 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.ParseExpression and anchore/syft#1837. This just brings grant in line.

Test

Added TestConvertSyftLicenses_MalformedSPDXExpressionDoesNotPanic, which drives the real ConvertSyftLicenses entry 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):

--- FAIL: TestConvertSyftLicenses_MalformedSPDXExpressionDoesNotPanic/MIT_AND_(
panic: runtime error: invalid memory address or nil pointer dereference [recovered, repanicked]

After:

--- PASS: TestConvertSyftLicenses_MalformedSPDXExpressionDoesNotPanic
ok      github.com/anchore/grant/grant

go build ./... and go test ./grant/ both pass.

This is a robustness fix for malformed input, not a security report.

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]>
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.

1 participant