Skip to content

sbom: populate root component license from manifests - #310

Merged
andrew merged 5 commits into
git-pkgs:mainfrom
abhinavgautam01:feature/sbom-root-license-303
Aug 14, 2026
Merged

andrew merged 5 commits into
git-pkgs:mainfrom
abhinavgautam01:feature/sbom-root-license-303

Conversation

@abhinavgautam01

Copy link
Copy Markdown
Contributor

Closes #303

Summary

  • Parse top-level package manifests at the requested Git revision to collect the project's declared license values.
  • Populate the root component license in CycloneDX JSON/XML and the root package license in SPDX JSON.
  • Deduplicate multiple declared licenses and produce deterministic output.
  • Preserve historical --commit behavior and avoid treating nested package manifests as the repository root.
  • Add regression coverage for revision selection and all supported SBOM output formats.

Verification

  • go test ./...
  • go mod tidy -diff
  • go tool golangci-lint run ./...
  • git diff --check

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 sbom command 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 andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@abhinavgautam01

Copy link
Copy Markdown
Contributor Author

Thanks, Fixed.

Manifest license values are now normalized and validated with git-pkgs/spdx before being written to SPDX expression fields.

Values that cannot be represented as SPDX are handled separately:

  • CycloneDX emits them as free-form license names.
  • SPDX emits stable LicenseRef identifiers with corresponding hasExtractedLicensingInfos entries, keeping licenseDeclared valid while preserving the original declaration.

I added regression coverage for the Python classifier example (BSD-3-Clause alongside License :: OSI Approved :: BSD License) and for unrecognized license names across CycloneDX JSON/XML and SPDX JSON.

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@abhinavgautam01

Copy link
Copy Markdown
Contributor Author

Fixed all three requested items.

  • CycloneDX output now follows the 1.5 license choice schema: expression-only licenses emit exactly one expression, while mixed declared licenses use only license entries. I also validated generated JSON and XML using the official CycloneDX CLI against the v1.5 schemas; both pass.
  • projectLicensesAtRevision now resolves and reads LicenseFile from the requested Git revision. Declared files are represented as named licenses in CycloneDX and as SPDX LicenseRef entries with the file contents in extractedText.
  • Split the previous combined test into separate CycloneDX JSON, CycloneDX XML and SPDX JSON tests. Added regression coverage proving that license-file contents come from the requested revision.

@andrew andrew left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The CycloneDX schema handling, LicenseFile reading, and test split all look right. Two further changes:

  • readProjectLicenseFile returns an error when the declared file is missing or empty, and runSBOM propagates it, so a Cargo.toml with license-file = "LICENSE" where LICENSE does not exist at the target revision now aborts git pkgs sbom entirely. 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.Component has no license fields. github.com/git-pkgs/sbom is 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 to Component (SPDX expression plus non-SPDX names plus extracted-text entries, enough to carry what projectLicenses holds) and make Encode write it for CycloneDX JSON/XML and SPDX JSON. Then bump the dependency here and set the fields in buildSBOM. That removes encodeSBOMWithRootLicenses, addRootLicensesJSON, addCycloneDXRootLicensesXML, isCycloneDXOptionalContainer, and the hardcoded SPDXRef-Package-root; normalizeProjectLicenses, projectLicensesAtRevision, and readProjectLicenseFile stay 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.

@abhinavgautam01

Copy link
Copy Markdown
Contributor Author

Thanks. I’ve addressed the requested changes locally.

I moved root declared-license serialization into github.com/git-pkgs/sbom, including SPDX expressions, non-SPDX names, extracted license text and CycloneDX JSON/XML plus SPDX JSON encoding. I’ve opened the prerequisite SBOM library PR here: git-pkgs/sbom#12

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 tree.Entries and the encode/patch/re-encode layer has been removed.

Once the SBOM PR is merged and tagged, I’ll bump the dependency here, rerun CI and push the rebased branch.

@andrew

andrew commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

github.com/git-pkgs/sbom v0.1.4 is now available. It includes the root-component license fields and CycloneDX JSON/XML and SPDX JSON encoding from git-pkgs/sbom#12, so this branch can now update the module dependency and remove the encode/patch/re-encode layer.

@abhinavgautam01
abhinavgautam01 force-pushed the feature/sbom-root-license-303 branch from cb27480 to 79b14eb Compare August 10, 2026 11:58
@abhinavgautam01

Copy link
Copy Markdown
Contributor Author

Updated now that github.com/git-pkgs/sbom v0.1.4 is available.

This branch now:

  • depends on github.com/git-pkgs/sbom v0.1.4
  • sets LicenseExpression, LicenseNames and ExtractedLicenses directly in buildSBOM
  • uses sbom.Encode without the previous encode/patch/re-encode layer
  • treats missing or empty declared license files as warnings
  • keeps unsafe absolute or escaping paths as hard errors
  • scans only root-level entries through tree.Entries
  • combines independent root-manifest license expressions with AND, while preserving manifest-level OR expressions

I also rebased the branch onto current main.

@andrew
andrew merged commit 71306cd into git-pkgs:main Aug 14, 2026
4 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.

sbom: populate root component license from parsed manifests

3 participants