TExcelTAD

Converting & Querying SBOMs

An SBOM is a JSON document, so the most useful SBOM tool is often jq. This lesson converts between formats with syft convert, then answers the questions people actually ask of SBOMs: is X in there, what changed between two releases, which licenses do we ship?

Converting with syft convert

syft convert reads an SBOM in one format and writes another, without rescanning. It is marked experimental and prints a warning, because no conversion is perfectly lossless.

bash · try it yourself
cp /opt/samples/sbom/legacy-service.cdx.json .
syft convert legacy-service.cdx.json -o spdx-json=legacy-service.spdx.json
jq -r '.packages[] | "\(.name) \(.versionInfo)  \(.externalRefs[]? | select(.referenceType == "purl") | .referenceLocator)"' legacy-service.spdx.json
syft convert legacy-service.cdx.json -o syft-table 2> /dev/null
[0000]  WARN convert is an experimental feature, run `syft convert -h` for help
jackson-databind 2.9.10  pkg:maven/com.fasterxml.jackson.core/jackson-databind@2.9.10
log4j-core 2.14.1  pkg:maven/org.apache.logging.log4j/log4j-core@2.14.1
NAME              VERSION  TYPE
jackson-databind  2.9.10   java-archive
legacy-service    0.9.0    UnknownPackage
log4j-core        2.14.1   java-archive

The sample is a CycloneDX SBOM of a Java service, written by hand in 2021 style: no tool name, just components with purls. After conversion, the purls survive (they are the part scanners need), and the warning reminds you to keep the original.

Conversion What usually survives What may be lost
CycloneDX → SPDX names, versions, purls, CPEs, licenses, dependencies VEX/vulnerabilities, services, formulation
SPDX → CycloneDX names, versions, purls, licenses file-level elements, snippets, some relationship types

Is a package in there?

The Log4Shell question. With one SBOM per release stored somewhere, it is a loop over files:

bash · try it yourself
mkdir sboms && cd sboms
cp /opt/samples/sbom/legacy-service.cdx.json legacy-service-0.9.0.cdx.json
syft scan /opt/samples/node-app -q --source-name node-app --source-version 1.0.0 -o cyclonedx-json=node-app-1.0.0.cdx.json
syft scan /opt/samples/images/alpine-3.18.0.tar -q -o cyclonedx-json=base-alpine-3.18.0.cdx.json
for f in *.cdx.json; do
  jq -r --arg f "$f" '.components[]? | select(.purl? // "" | test("^pkg:maven/org.apache.logging.log4j/log4j-core@2\\.(0|[0-9]\\.|1[0-6]\\.)")) | "\($f): \(.name) \(.version)"' "$f"
done
echo "--- every openssl-derived package"
jq -r '.components[] | select(.purl? // "" | test("upstream=openssl")) | "\(.name) \(.version)"' base-alpine-3.18.0.cdx.json
legacy-service-0.9.0.cdx.json: log4j-core 2.14.1
--- every openssl-derived package
libcrypto3 3.1.0-r4
libssl3 3.1.0-r4

Matching on the purl is more reliable than on name: two ecosystems can have packages with the same name, but the purl type and namespace are unambiguous. Alpine's purls carry an upstream= qualifier, so "everything built from OpenSSL" is one query.

What changed between two releases?

Diffing SBOMs is the cheapest way to review dependency changes, including transitive ones a pull request diff hides. Compare purls, not files (serial numbers and timestamps change every run). Here release 1.0.1 upgrades lodash and, quietly, adds a new package:

bash · try it yourself
cp -r /opt/samples/node-app v1.0.0
cp -r /opt/samples/node-app v1.0.1 && cd v1.0.1
jq '.version = "1.0.1"
  | .packages[""].version = "1.0.1"
  | .packages[""].dependencies.lodash = "4.17.21"
  | .packages["node_modules/lodash"].version = "4.17.21"
  | .packages["node_modules/lodash"].resolved = "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz"
  | .packages[""].dependencies["left-pad"] = "1.3.0"
  | .packages["node_modules/left-pad"] = {"version": "1.3.0", "resolved": "https://registry.npmjs.org/left-pad/-/left-pad-1.3.0.tgz"}' \
  package-lock.json > lock.tmp && mv lock.tmp package-lock.json
cd ..
for v in v1.0.0 v1.0.1; do
  syft scan $v -q -o cyclonedx-json | jq -r '.components[] | .purl // empty' | sed 's/?.*//' | sort > $v.purls
done
diff v1.0.0.purls v1.0.1.purls | grep '^[<>]'
< pkg:npm/lodash@4.17.15
> pkg:npm/left-pad@1.3.0
> pkg:npm/lodash@4.17.21
< pkg:npm/node-app@1.0.0
> pkg:npm/node-app@1.0.1

< lines left, > lines arrived. A new direct dependency (left-pad) is the line a reviewer should look at; the lodash upgrade is expected. Running this diff as a pull-request comment is a small CI job with a large effect.

Licenses

License data comes from package metadata, so it is only as good as what the package declares. Alpine's database records licenses for every package:

bash · try it yourself
syft scan /opt/samples/images/alpine-3.18.0.tar -q -o cyclonedx-json \
  | jq -r '.components[] | select(.type == "library") | .licenses[0] | .expression // .license.id // .license.name' \
  | sort | uniq -c | sort -k1,1nr -k2
      7 GPL-2.0-only
      2 Apache-2.0
      2 MIT
      1 BSD-2-Clause AND BSD-3-Clause
      1 MIT AND BSD-2-Clause AND GPL-2.0-or-later
      1 MPL-2.0 AND MIT
      1 Zlib

A license is either a single SPDX id (license.id) or an SPDX expression combining several (MPL-2.0 AND MIT); a policy check must parse expressions, not compare strings. The npm lockfile in the earlier sessions records no licenses, so the same query on that SBOM returns nothing; scan the installed node_modules (whose package.json files declare licenses) when you need them.

Dependency graph queries

CycloneDX's dependencies section is a graph you can walk. "Who pulls in minimist?":

bash · try it yourself
cp -r /opt/samples/node-app . && cd node-app
syft scan . -q -o cyclonedx-json > sbom.json
jq -r '.dependencies[] | select(.dependsOn[]? | startswith("pkg:npm/minimist@")) | .ref | sub("\\?.*"; "")' sbom.json
pkg:npm/node-app@1.0.0

At scale, people load SBOMs into a database or a graph tool such as GUAC (Toolkit) rather than looping over files, but the questions stay the same.

Test yourself

Why diff SBOMs by sorted purls rather than with diff a.json b.json?

  1. jq is faster than diff
  2. Serial numbers, timestamps and ordering differ between runs even when components don't
  3. JSON can't be diffed
  4. purls are shorter

Why does syft convert print a warning?

  1. It needs the network
  2. It is experimental, and conversions can lose data that has no equivalent in the other format
  3. It changes component versions
  4. It only works for SPDX

Which qualifier in an Alpine purl lets you find every package built from OpenSSL?

  1. arch=
  2. distro=
  3. upstream=
  4. repository_url=