Repository navigation
sbom: populate root component license from manifests - #310
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fills a gap in SBOM generation by extracting the project’s declared license(s) from top-level manifests at a specified Git revision and injecting that license into the root component/package of generated SBOMs (CycloneDX JSON/XML and SPDX JSON).
Changes:
- Load the project’s declared license at the requested revision during
sbomcommand execution and include it in the emitted SBOM. - Add a new implementation for collecting/deduplicating licenses from root manifests and for post-processing encoded SBOM output to set root license fields across supported formats.
- Add regression tests covering revision selection (e.g., historical
--commit) and license injection for CycloneDX JSON/XML and SPDX JSON outputs.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| cmd/sbom.go | Loads the project license for the requested revision and routes encoding through the new root-license-aware encoder. |
| cmd/sbom_root_license.go | Implements root manifest license extraction at a revision and injects the root license into CycloneDX/SPDX outputs. |
| cmd/sbom_test.go | Adds tests for revision-based license selection and for root license encoding across all supported SBOM formats. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
andrew
left a comment
There was a problem hiding this comment.
ParseResult.Licenses contains verbatim manifest values, but projectLicenseAtRevision joins them and writes the result to CycloneDX expression and SPDX licenseDeclared fields without validation. Supported Python manifests can return BSD-3-Clause plus License :: OSI Approved :: BSD License, producing an invalid SPDX expression. Please normalize and validate each value, handle names that cannot be expressed as SPDX separately, and add a regression test.
|
Thanks, Fixed. Manifest license values are now normalized and validated with Values that cannot be represented as SPDX are handled separately:
I added regression coverage for the Python classifier example ( |
andrew
left a comment
There was a problem hiding this comment.
Three changes are still needed:
- CycloneDX 1.5 permits either a list of license entries or exactly one expression. cmd/sbom_root_license.go currently emits an expression alongside named license entries in both JSON and XML, making mixed-license output schema-invalid. Use one permitted representation and validate both outputs against the official 1.5 schemas.
- projectLicensesAtRevision discards result.LicenseFile. Cargo license-file and Python license.file declarations therefore produce no root license. Read the declared file from the same revision and represent it as a named or custom license.
- Split TestEncodeSBOMWithRootLicense into separate format tests. The new test has cognitive complexity 37, above the configured limit of 30.
|
Fixed all three requested items.
|
andrew
left a comment
There was a problem hiding this comment.
The CycloneDX schema handling, LicenseFile reading, and test split all look right. Two further changes:
-
readProjectLicenseFilereturns an error when the declared file is missing or empty, andrunSBOMpropagates it, so aCargo.tomlwithlicense-file = "LICENSE"whereLICENSEdoes not exist at the target revision now abortsgit pkgs sbomentirely. That is a regression against main. Keep the hard error for absolute paths and paths that escape the tree, but treat a missing or empty file as a warning and continue with whatever other licenses were collected. Add a regression test for a manifest pointing at a nonexistent file. -
The encode/decode/patch/re-encode layer exists because
sbom.Componenthas no license fields.github.com/git-pkgs/sbomis a git-pkgs package, so the fields belong there rather than being spliced into serialised output here. Please open a PR against that repo adding declared-license support toComponent(SPDX expression plus non-SPDX names plus extracted-text entries, enough to carry whatprojectLicensesholds) and makeEncodewrite it for CycloneDX JSON/XML and SPDX JSON. Then bump the dependency here and set the fields inbuildSBOM. That removesencodeSBOMWithRootLicenses,addRootLicensesJSON,addCycloneDXRootLicensesXML,isCycloneDXOptionalContainer, and the hardcodedSPDXRef-Package-root;normalizeProjectLicenses,projectLicensesAtRevision, andreadProjectLicenseFilestay in this repo.
Minor, can fold in with the above: tree.Files().ForEach walks the entire tree recursively and filters to path.Dir == "."; iterate tree.Entries and read only root-level files instead.
|
Thanks. I’ve addressed the requested changes locally. I moved root declared-license serialization into On this branch, missing and empty declared license files now emit warnings instead of aborting SBOM generation, unsafe paths remain hard errors, root manifest discovery uses Once the SBOM PR is merged and tagged, I’ll bump the dependency here, rerun CI and push the rebased branch. |
|
|
cb27480 to
79b14eb
Compare
|
Updated now that This branch now:
I also rebased the branch onto current |
Closes #303
Summary
--commitbehavior and avoid treating nested package manifests as the repository root.Verification
go test ./...go mod tidy -diffgo tool golangci-lint run ./...git diff --check