All guides

SBOMs: generate one for every build

An SBOM lists the software inside an image. Syft, Trivy and Docker BuildKit each generate one, GitHub's attest action signs it against the image digest, and Grype scans it for known vulnerabilities.

A laptop screen showing JavaScript in a code editor

What an SBOM holds

SBOM stands for software bill of materials. Docker's documentation explains that an SBOM attestation records two things: the software artifacts inside an image and the artifacts used to build it. The metadata for each artifact may include its name, version, license type, authors and a unique package identifier.

SPDX and CycloneDX

Syft, Trivy and GitHub's attest action all work with two formats. Syft's documentation describes them this way:

  • SPDX (Software Package Data Exchange) is an SBOM format and an industry standard, published as ISO/IEC 5962:2021.
  • CycloneDX is an SBOM format and an industry standard that OWASP maintains.

Syft and Trivy write both as JSON. BuildKit writes SPDX. Pick the format the consumers of your SBOM ask for. Syft writes both in one run when you pass -o twice.

Three ways to generate one

Shell

# Syft: one scan, two files
syft ghcr.io/my-org/my-app:1.4.2 \
  -o spdx-json=sbom.spdx.json -o cyclonedx-json=sbom.cdx.json

# Trivy
trivy image --format cyclonedx --output sbom.cdx.json ghcr.io/my-org/my-app:1.4.2

# Docker BuildKit: build, push and attach an SPDX SBOM to the image
docker buildx build --tag ghcr.io/my-org/my-app:1.4.2 --attest type=sbom --push .

Syft accepts a schema version after the format name, such as [email protected]. With no version, it uses the latest one it supports. With --format cyclonedx, Trivy writes the SBOM and leaves vulnerabilities out unless you add --scanners vuln.

BuildKit generates the SBOM during the build and attaches it to the image as an in-toto attestation. --sbom=true is shorthand for --attest type=sbom. By default BuildKit scans the final stage only. To scan earlier stages too, declare ARG BUILDKIT_SBOM_SCAN_STAGE=true in the Dockerfile. Read the result back from the registry:

Shell

docker buildx imagetools inspect ghcr.io/my-org/my-app:1.4.2 \
  --format "{{ json .SBOM.SPDX }}"

Attest the SBOM to the image

An SBOM attestation binds the SBOM to a subject, which is an artifact name together with its digest, in the in-toto format. The action signs it with a short-lived certificate issued by Sigstore.

On GitHub, use actions/attest with the sbom-path input. The older actions/attest-sbom is being deprecated. As of version 4 it is a wrapper around actions/attest. For creating the SBOM, GitHub's documentation points to anchore/sbom-action, which runs Syft.

YAML

permissions:
  id-token: write
  contents: read
  attestations: write
  packages: write

steps:
  - id: push
    uses: docker/build-push-action@v7
    with:
      context: .
      push: true
      tags: ghcr.io/my-org/my-app:1.4.2
  - uses: anchore/sbom-action@v0
    with:
      image: ghcr.io/my-org/my-app:1.4.2
      format: cyclonedx-json
      output-file: sbom.cdx.json
  - uses: actions/attest@v4
    with:
      subject-name: ghcr.io/my-org/my-app
      subject-digest: ${{ steps.push.outputs.digest }}
      sbom-path: sbom.cdx.json
      push-to-registry: true

The checkout and registry login steps are left out here. subject-name is the fully qualified image name with no tag, because the digest identifies the image. The SBOM must be SPDX or CycloneDX JSON, and the file cannot exceed 16 MB. Artifact attestations are available in public repositories on all current GitHub plans. Private and internal repositories need GitHub Enterprise Cloud.

To verify an SBOM attestation, pass --predicate-type, because the default predicate is build provenance:

Shell

gh attestation verify oci://ghcr.io/my-org/my-app:1.4.2 -R my-org/my-repo \
  --predicate-type https://cyclonedx.org/bom

The CycloneDX value comes from the source of actions/attest, which sets https://cyclonedx.org/bom for every CycloneDX document. For SPDX the action builds the type from the document's version, and GitHub's documentation shows https://spdx.dev/Document/v2.3.

Scan the SBOM with Grype

Shell

grype sbom:sbom.cdx.json --fail-on high

Grype detects the SBOM format by itself. Its documentation lists Syft JSON, SPDX and CycloneDX as supported. --fail-on sets the return code to 2 when Grype finds a vulnerability at or above the given severity. The benefits its documentation gives for scanning an SBOM in place of the image include faster scans, because package cataloging is already complete, and the ability to cache and reuse SBOMs.

Trivy takes an SBOM as input as well, with trivy sbom.

Keep it with the release

Shell

gh release upload v1.4.2 sbom.spdx.json sbom.cdx.json

gh release upload uploads asset files to a GitHub release. anchore/sbom-action also does this for you: by default it uploads the SBOM as a workflow artifact, and when it runs during a GitHub release it uploads the SBOM as a release asset. For that upload its README asks for actions: read and contents: write on the job.

What to check

  • The SBOM before you push. Docker's documentation says to validate the generated SBOM before you push the image. docker buildx build --sbom=true --output type=local,dest=out . writes sbom.spdx.json to the out directory in place of creating an image.
  • Build stages. By default the BuildKit SBOM leaves out build-time dependencies installed in earlier stages. Docker's documentation warns that this may cause you to overlook vulnerabilities in them.
  • The format. actions/attest rejects a file that is not SPDX or CycloneDX JSON.
  • The predicate type. gh attestation verify enforces the https://slsa.dev/provenance/v1 predicate type unless you pass --predicate-type.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides