bug(sbom): vulnerabilities are attached to a random package when scanning an SPDX SBOM with SBOM output
Description
When an SPDX SBOM is scanned (trivy sbom file.spdx.json) and the result is written in an SBOM format (--format cyclonedx, spdx or spdx-json), vulnerabilities are attached to the wrong packages.
All detected vulnerabilities land on one random component, and which one changes from run to run.
In addition, in CycloneDX output every component has an empty bom-ref, and dependencies and affects point to "".
Every kind of SPDX input is affected: JSON, tag-value, in-toto attestations and Sigstore bundles.
The table and json outputs are correct, and CycloneDX SBOM input is handled correctly.
Reproduction
Trivy v0.74.0, fixture from the repository: integration/testdata/fixtures/sbom/centos-7-spdx.json.
Correct attribution (json output):
trivy sbom --scanners vuln --format json -q centos-7-spdx.json \
| jq -r '[.Results[].Vulnerabilities[]? | .PkgName] | group_by(.) | map("\(.[0])=\(length)") | join(" ")'bash=3 openssl-libs=50spdx-json output, three runs in a row (number of advisory references per package):
trivy sbom --scanners vuln --format spdx-json -q centos-7-spdx.json \
| jq -r '[.packages[] | {n: .name, a: ([.externalRefs[]? | select(.referenceCategory == "SECURITY")] | length)} | select(.a > 0) | "\(.n)=\(.a)"] | join(" ")'
bash=53
openssl-libs=53Every time, all 53 vulnerabilities end up on a single random component. The first run prints nothing: the vulnerabilities were attached to the root component, for which the SPDX marshaler writes no advisory references, so they disappeared from the output entirely. In other runs they also landed on the centos OS component.
cyclonedx output:
trivy sbom --scanners vuln --format cyclonedx -q centos-7-spdx.json \
| jq -c '{components: [.components[] | {name, "bom-ref"}], dependencies: .dependencies[0:2], affects: [.vulnerabilities[0:3][] | .affects[0].ref]}'{"components":[{"name":"centos","bom-ref":null},{"name":"bash","bom-ref":null},{"name":"openssl-libs","bom-ref":null}],"dependencies":[{"ref":"","dependsOn":["",""]},{"ref":"","dependsOn":[""]}],"affects":["","",""]}The same scan from centos-7-cyclonedx.json is correct:
trivy sbom --scanners vuln --format cyclonedx -q centos-7-cyclonedx.json \
| jq -r '. as $b | [.vulnerabilities[] | .affects[0].ref as $r | ($b.components[] | select(."bom-ref" == $r) | .name)] | group_by(.) | map("\(.[0])=\(length)") | join(" ")'bash=3 openssl-libs=50Root cause
- SPDX documents are decoded into
core.BOMwithout BOM-Ref generation (sbom.go#L293, #L315, #L323, #L327, spdx/unmarshal.go#L56), and the SPDX decoder does not fillBOMRefitself. Every component has an emptyPkgIdentifier.BOMRef. CycloneDX usesGenerateBOMRef: true(sbom.go#L265). - For any SBOM input, the original BOM is kept in the report (artifact/sbom/sbom.go#L101, scan/service.go#L121).
- When writing an SBOM format, the encoder reuses that BOM (encode.go#L60-L65).
reuseExistingBOMbuilds the component map and groups vulnerabilities byPkgIdentifier.BOMRef(encode.go#L307-L316). For SPDX input every key is"": one random component survives in the component map (map iteration order), and all vulnerabilities are attached to it. The "component not found" warning does not fire, because the""key exists in the map. - The comment on
reuseExistingBOMsays this path is meant for CycloneDX, because SPDX 2.3 does not include vulnerabilities (encode.go#L298-L300). However, SPDX output does carry vulnerabilities implicitly (as advisory references on packages), so SPDX was deliberately left out. VEX is not affected: it forces the BOM to be regenerated (vex.go#L79).
Source: aquasecurity/trivy