Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix(spdx): save text licenses into otherLicenses without normalize #8502

Merged

Conversation

DmitriyLewen
Copy link
Contributor

@DmitriyLewen DmitriyLewen commented Mar 6, 2025

Description

Save text licenses in otherLicenses without normalization.
This is necessary to avoid errors when parsing the license text.
Also, we did not include these "incorrect" licenses in the report.

See #8465 for more details.

Before (report doesn't contain this license):

➜  trivy image 8449 -f spdx-json | grep Redistribution 
...
2025-03-06T13:33:03+06:00       WARN    [spdx] Unable to marshal SPDX licenses  license="(GPL-2.0-only) AND (text://Redistribution and use in source and binary forms, with or without) AND (text://By obtaining, using, and/or copying this software and/or its) AND (text://Permission to use, copy, modify, and distribute this software and) AND (text://This software is provided 'as-is', without any express or implied) AND (text://Permission  is  hereby granted,  free  of charge,  to  any person) AND (text://Permission is hereby granted, free of charge, to any person obtaining) AND (text://Permission to use, copy, modify, and distribute this software and its) AND (text://This software is provided as-is, without express or implied) AND (text://Permission to use, copy, modify, and distribute this software for any) AND (text://* Permission to use this software in any way is granted without)"

After:

➜ ./trivy image 8449 -f spdx-json | grep Redistribution
...
      "extractedText": "Redistribution and use in source and binary forms, with or without",

Related issues

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@DmitriyLewen DmitriyLewen self-assigned this Mar 6, 2025
@DmitriyLewen DmitriyLewen marked this pull request as ready for review March 6, 2025 08:31
@DmitriyLewen DmitriyLewen requested a review from knqyf263 as a code owner March 6, 2025 08:31
licenseNames = append(licenseNames, license)
}

license := strings.Join(lo.Map(licenseNames, func(license string, index int) string {
Copy link
Collaborator

@knqyf263 knqyf263 Mar 6, 2025

Choose a reason for hiding this comment

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

Do we need to iterate over licenses twice? Can't we normalize the text license in lo.Map?

Suggested change
license := strings.Join(lo.Map(licenseNames, func(license string, index int) string {
license := strings.Join(lo.Map(licenseNames, func(license string, index int) string {
// We need to save text licenses before normalization,
// because it is impossible to handle all cases possible in the text.
// as an example, parse a license with 2 consecutive tokens (see https://github.com/aquasecurity/trivy/issues/8465)
if strings.HasPrefix(license, licensing.LicenseTextPrefix) {
license = strings.TrimPrefix(license, licensing.LicenseTextPrefix)
otherLicense := m.newOtherLicense(license, true)
otherLicenses[otherLicense.LicenseIdentifier] = otherLicense
return otherLicense.LicenseIdentifier
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about it when I started working. I don't know why I didn't implement it right away...
thanks!

Updated in befbe8e

@knqyf263 knqyf263 added this pull request to the merge queue Mar 6, 2025
Merged via the queue into aquasecurity:main with commit e5072f1 Mar 6, 2025
12 checks passed
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.

bug(spdx): Trivy can't parse text licenses with 2 consecutive tokens
2 participants