SBOMs in the Pipeline
A software bill of materials (SBOM) lists every component in an artifact: name, version, package URL (purl), licence, and how they relate. A pipeline is the right place to produce it, because the pipeline is the only place that knows exactly what went into a build. Software Supply Chain Security covers the formats (SPDX vs CycloneDX), Syft's sources and converting and querying SBOMs in depth. This lesson wires SBOMs into CI: generate once, scan from the SBOM, diff it in pull requests, and keep it with the release.
Generate, then scan the SBOM
Syft writes the SBOM; Grype can scan the SBOM instead of the artifact, which is faster and lets you re-scan old releases later without their images:
cp -r /opt/samples/node-app shop-web
syft scan dir:shop-web -q --source-name shop-web --source-version 1.0.0 -o cyclonedx-json=sbom-1.0.0.cdx.json
jq -r '"\(.bomFormat) \(.specVersion), subject \(.metadata.component.name) \(.metadata.component.version)"' sbom-1.0.0.cdx.json
jq -r '.components[] | select(.purl != null) | " \(.purl)"' sbom-1.0.0.cdx.json
grype sbom:sbom-1.0.0.cdx.json -q --fail-on high -o json > vulns.json
echo "grype gate on the SBOM: exit $?"
jq -r '[.matches[] | .vulnerability.severity] | group_by(.) | map("\(.[0])=\(length)") | join(" ")' vulns.jsonCycloneDX 1.7, subject shop-web 1.0.0
pkg:npm/lodash@4.17.15
pkg:npm/minimist@1.2.5
pkg:npm/node-app@1.0.0
grype gate on the SBOM: exit 2
Critical=1 High=3 Medium=3
--source-name/--source-version matter: without them the SBOM's subject is a temporary directory path, and nobody can tell later which release it describes.
Diff the SBOM in pull requests
The most useful SBOM in daily work is the difference between the main branch and a pull request: which components does this change add, remove or upgrade? That is a review question ("why does a CSS fix add 40 packages?") and a security question at the same time. The session simulates a PR that upgrades lodash, drops minimist and adds a new dependency, and compares the two SBOMs by package URL:
cp -r /opt/samples/node-app main && cp -r /opt/samples/node-app pr
cd pr
jq '.packages["node_modules/lodash"].version = "4.17.21"
| .packages[""].dependencies = {"lodash": "4.17.21", "ms": "2.1.3"}
| del(.packages["node_modules/minimist"])
| .packages["node_modules/ms"] = {"version": "2.1.3"}' package-lock.json > lock.tmp && mv lock.tmp package-lock.json
cd ..
for side in main pr; do
syft scan dir:$side -q -o cyclonedx-json | jq -r '.components[] | select(.type == "library") | "\(.name) \(.version)"' | sort > $side.txt
done
echo "### SBOM diff (main -> pr)"
join -a1 -a2 -e '-' -o 0,1.2,2.2 main.txt pr.txt | awk '
$2 == "-" { print "added " $1 " " $3; next }
$3 == "-" { print "removed " $1 " " $2; next }
$2 != $3 { print "changed " $1 " " $2 " -> " $3 }'
for side in main pr; do
syft scan dir:$side -q -o cyclonedx-json > $side.cdx.json
echo "$side: $(grype sbom:$side.cdx.json -q -o json | jq '[.matches[] | select(.vulnerability.severity == "Critical" or .vulnerability.severity == "High")] | length') high/critical findings"
done### SBOM diff (main -> pr)
changed lodash 4.17.15 -> 4.17.21
removed minimist 1.2.5
added ms 2.1.3
main: 4 high/critical findings
pr: 1 high/critical findings
The PR removes three of the four high or critical findings; the one left is a newer advisory against lodash 4.17.21 itself, which the reviewer now sees before merging. A CI job can post exactly this as a pull-request comment. GitHub's dependency review action does a similar diff for supported ecosystems and can fail PRs that add vulnerable or disallowed-licence packages; GitLab shows dependency changes in the merge request's security widget (Ultimate).
Keep the SBOM with the release
An SBOM sitting in a CI log is useless a year later. Store it where it will be found:
| Where | How |
|---|---|
| CI artifact | actions/upload-artifact / artifacts: with long retention; easy but tied to the CI system |
| Release asset | attach sbom.cdx.json to the GitHub/GitLab release (Release Engineering) |
| Registry, next to the image | cosign attest --type cyclonedx (a signed attestation) or oras attach; consumers fetch it by the image digest (Attestations) |
| SBOM platform | Dependency-Track and similar tools ingest SBOMs and re-check all of them as new CVEs appear |
# the SBOM step of a release job (non-run)
- name: SBOM for the pushed image
run: |
syft scan "registry:$IMAGE@$DIGEST" --source-name shop-web --source-version "$VERSION" \
-o cyclonedx-json=sbom.cdx.json -o spdx-json=sbom.spdx.json
cosign attest --yes --type cyclonedx --predicate sbom.cdx.json "$IMAGE@$DIGEST" # keyless, via OIDCThe last row of the table is where SBOMs pay off: when the next log4shell-scale vulnerability is announced, "which of our deployed releases contain this package?" becomes a query instead of a week of archaeology.
Test yourself
Why pass --source-name and --source-version to Syft in CI?
- They are required
- So the SBOM's subject identifies the product and release instead of a temporary path
- To speed up the scan
- To sign the SBOM
What is the advantage of grype sbom:sbom.cdx.json over scanning the image again?
- It finds more vulnerabilities
- It's fast and works later without the image, e.g. to re-check old releases against new CVEs
- It signs the result
- It needs no database
What does an SBOM diff in a pull request show?
- Code changes
- Which components the change adds, removes or upgrades
- Test coverage
- Commit authors