Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix: add panic recovery for license parse #1839

Merged
merged 2 commits into from
May 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion syft/license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/github/go-spdx/v2/spdxexp"

"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/internal/spdxlicense"
)

Expand All @@ -17,15 +18,25 @@ const (
)

func ParseExpression(expression string) (string, error) {
// https://github.com/anchore/syft/issues/1837
// The current spdx library can panic when parsing some expressions
// This is a temporary fix to recover and patch until we can investigate and contribute
// a fix to the upstream github library
defer func() {
if r := recover(); r != nil {
log.Trace("recovered in parseExpression", r)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of logging, should we change the return error wrapping the stack trace and let the caller decide the correct action to take? We have a similar pattern in the runCataloger() call

if e := recover(); e != nil {

}
}()

licenseID, exists := spdxlicense.ID(expression)
if exists {
return licenseID, nil
}

// If it doesn't exist initially in the SPDX list it might be a more complex expression
// ignored variable is any invalid expressions
// TODO: contribute to spdxexp to expose deprecated license IDs
// https://github.com/anchore/syft/issues/1814

valid, _ := spdxexp.ValidateLicenses([]string{expression})
if !valid {
return "", fmt.Errorf("failed to validate spdx expression: %s", expression)
Expand Down