Skip to content

feat(spdx): add SHA-512 hash algorithm support to SPDX serializer #9130

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

DicksenT
Copy link

@DicksenT DicksenT commented Jul 3, 2025

Description

This PR adds SHA-512 hash algorithm support to the SPDX SBOM serializer.

Specifically, it updates spdxChecksums() in marshal.go to handle the digest.SHA512 case, so Trivy can correctly include SHA-512 checksums in the SPDX output (used in tools like npm sbom). This complements #9126, which already added digest and CycloneDX support.

A unit test was also added to ensure SHA-512 is handled correctly in SPDX.

Related issues

Related PRs

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

@CLAassistant
Copy link

CLAassistant commented Jul 3, 2025

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Collaborator

@knqyf263 knqyf263 left a comment

Choose a reason for hiding this comment

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

I think we also need to update unmarshaling. #9126 will probably help your understanding.

@@ -37,6 +38,38 @@ func annotation(t *testing.T, comment string) spdx.Annotation {
}
}

func Test_spdxChecksums_SHA512(t *testing.T) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test doesn't appear to make sense. You can refer to other tests.

Copy link
Author

Choose a reason for hiding this comment

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

you are right, i will remove the additional function

@DicksenT DicksenT force-pushed the feat/add-spdx-sha512 branch 2 times, most recently from c14561b to ca2c4df Compare July 4, 2025 11:09
- Added SHA512 case to marshal.go
- Added new test case for SHA512 digest in marshal_test.go
- Fixed typo in SHA512 algorithm switch
@DicksenT DicksenT force-pushed the feat/add-spdx-sha512 branch from ca2c4df to 204a126 Compare July 4, 2025 15:01
@DicksenT
Copy link
Author

DicksenT commented Jul 4, 2025

Hi @knqyf263 ,

I’ve addressed the following:

Fixed the digest.SHA512 typo (was mistakenly written as spdx.SHA512)

Removed the incorrect test case and added the correct one for SHA512 support in SPDX

however, I looked into unmarshal.go as suggested, but I couldn’t find explicit handling for SHA1 or SHA256 there.
Should I add new logic specifically for SHA512, or is the current unmarshal logic expected to cover it already?

@DicksenT DicksenT requested a review from knqyf263 July 4, 2025 17:35
@knqyf263
Copy link
Collaborator

knqyf263 commented Jul 7, 2025

@DicksenT Could you fix the lint error?
https://trivy.dev/latest/community/contribute/pr/#lint

@DicksenT
Copy link
Author

DicksenT commented Jul 7, 2025

on it

@DicksenT
Copy link
Author

DicksenT commented Jul 8, 2025

@knqyf263 I've fixed the lint issues. Let me know if there's anything else I should address. Thanks again!

@knqyf263
Copy link
Collaborator

knqyf263 commented Jul 8, 2025

Do you mind signing our CLA?
#9130 (comment)

@DicksenT
Copy link
Author

DicksenT commented Jul 8, 2025

signed, but somehow there 2 cla in here, one is signed, another one still pending despite i already agreed

@knqyf263
Copy link
Collaborator

knqyf263 commented Jul 8, 2025

signed, but somehow there 2 cla in here, one is signed, another one still pending despite i already agreed

No worries. It's correctly recognized that you signed.

@@ -1448,6 +1448,125 @@ func TestMarshaler_Marshal(t *testing.T) {
},
},
},
{
name: "happy path for SHA512 digest",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Adding a test case specifically for SHA-512 may be excessive. Can we merge this case into the existing case somehow?

Copy link
Author

Choose a reason for hiding this comment

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

i think it's possible, but maybe i need to tweak marshal.go abit(?), or should i create the test in testdata/happy just like how it done in cycloneDx implementation?

Copy link
Collaborator

Choose a reason for hiding this comment

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

but maybe i need to tweak marshal.go a bit(?)

I don't think so. You can just modify the existing case. For example:

Digest: "md5:483792b8b5f9eb8be7dc4407733118d0",

Copy link
Author

Choose a reason for hiding this comment

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

I’ve added a sha512: Digest to the input report and also updated the PackageChecksums in the expected wantSBOM(both in "happy path for local container scan" test case), but the test fails, is there anything i've missed in the test case implementation?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is it failing? What does it say?

Copy link
Author

Choose a reason for hiding this comment

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

hello, is there any update on the issue?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't have time to debug it right now.

Copy link
Author

Choose a reason for hiding this comment

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

understandable, should we keep using spesific sha512 test case or you want me keep trying to merge the testcase?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@DmitriyLewen Do you have time to take a look?

Copy link
Contributor

Choose a reason for hiding this comment

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

simplified the test example for sha512 - 6ee1bc9

@DicksenT DicksenT requested a review from knqyf263 July 15, 2025 16:03
@DmitriyLewen
Copy link
Contributor

Hello @DicksenT
you also need update unmarshal logic:

// Files
// TODO: handle checksums as well
if path, ok := s.pkgFilePaths[spdxPkg.PackageSPDXIdentifier]; ok {
component.Files = []core.File{
{Path: path},
}
} else if len(spdxPkg.Files) > 0 {
component.Files = []core.File{
{Path: spdxPkg.Files[0].FileName}, // Take the first file name
}
}

You need to add testcase (as in https://github.com/aquasecurity/trivy/pull/9126/files#diff-b17fc40bdfbe923285c5951b6ff7243b43dddedfa22eb0a5967d695268096a87).
This test case will help you to update logic.

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.

feat: add SHA-512 hash support for SBOM formats (CycloneDX and SPDX)
4 participants