Sub topic 8
Attesting and verifying container images
This chapter assumes you can write a GitHub Actions workflow that builds and pushes an image to a registry, and that you have read the earlier chapters on provenance attestations, GitHub Actions OIDC identity and Sigstore signing. Here you apply that machinery to container images. Images differ from plain files in two ways that cause most mistakes: an image is identified by a digest rather than a file path, and a registry gives you a second place to store attestations.
Why images need a different approach
For a file such as dist/app, the attest action hashes the file on the runner. An image is not a file on the runner. It lives in a registry as a manifest plus layers, and the registry computes the identity of the image. That identity is the manifest digest, a SHA-256 hash written as sha256: followed by 64 hex characters.
Tags are different. A tag such as v1.2.0 is a mutable pointer, and anyone with push access can repoint it to another image. An attestation therefore never binds to a tag. It binds to a digest. When you attest an image you give the action two pieces of information:
subject-name: the fully qualified image name without a tag, for exampleghcr.io/octo-org/octo-app.subject-digest: the digest of the pushed manifest, including thesha256:prefix.
Both are required for images. Without a digest, the action has nothing stable to sign.
What happens during an image attestation
The sequence below shows who talks to whom. The workflow pushes first, because only the registry knows the final digest. It then asks the attest action to sign a provenance statement about that digest.
The signing certificate is valid for minutes and carries the workflow identity, so no long-lived key exists to steal. The statement says "this digest was produced by this repository, this workflow, this commit."
Generating the attestation
The job needs three permissions beyond reading the code:
packages: writeto push to GitHub Container Registry (GHCR).id-token: writeso the job can request an OIDC token.attestations: writeso the action can store the attestation.
The complete workflow below builds an image on a version tag, pushes it and attests it.
name: image
on:
push:
tags: ['v*']
jobs:
image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
attestations: write
steps:
- uses: actions/checkout@v4
- name: Compute lowercase image name
run: echo "IMAGE=ghcr.io/${GITHUB_REPOSITORY,,}" >> "$GITHUB_ENV"
- name: Log in to GHCR
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push image
id: push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ${{ env.IMAGE }}:${{ github.ref_name }}
- name: Attest image provenance
uses: actions/attest-build-provenance@v2
with:
subject-name: ${{ env.IMAGE }}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: trueThree details deserve attention.
- Lowercase names. Registries require lowercase repository names, but
github.repositorykeeps the capitals from your account or repository name. The${GITHUB_REPOSITORY,,}expansion in Bash lowercases it. Without this step, an owner likeOcto-Orgmakes the push or the attestation fail. - The digest comes from the push step.
docker/build-push-actionexposes the pushed manifest digest as thedigestoutput. Do not compute a digest yourself from a local build, because it may not match what the registry stored. push-to-registry: true. This stores the attestation in the registry next to the image, in addition to the copy GitHub keeps. It needspackages: write. The action ignoressubject-namemismatches poorly, so keep the same name in the push tag and the attestation.
Note that subject-name has no tag. If you write ghcr.io/octo-org/octo-app:v1.2.0 there, the attestation will not match what consumers look up by digest.
Verifying an image
Consumers verify with the GitHub CLI. The oci:// prefix tells gh to treat the argument as an image reference.
# Verify by tag. gh resolves the tag to a digest, then looks up attestations for that digest
gh attestation verify oci://ghcr.io/OWNER/IMAGE:TAG --owner OWNER
# Verify a specific repository built it, and a specific workflow signed it
gh attestation verify oci://ghcr.io/OWNER/IMAGE:TAG \
--repo OWNER/REPO \
--signer-workflow OWNER/REPO/.github/workflows/image.yml
# Verify by digest, which cannot be repointed
gh attestation verify oci://ghcr.io/OWNER/IMAGE@sha256:DIGEST --repo OWNER/REPO
# Print verified details as JSON
gh attestation verify oci://ghcr.io/OWNER/IMAGE:TAG --owner OWNER --format jsonA successful run checks several things in order:
- It resolves the image reference to a digest by contacting the registry.
- It finds attestation bundles whose subject matches that digest.
- It validates each signature and the Fulcio certificate chain against the Sigstore trusted root.
- It checks the transparency log entry.
- It compares the certificate identity to the owner, repository or workflow you passed.
If you pass neither --owner nor --repo, the command refuses to run, because "signed by somebody" proves nothing. Choose the narrowest flag you can. --owner accepts any repository in the organization. --repo limits to one repository. --signer-workflow limits to one workflow file, which matters when several workflows in a repository can attest.
By default, gh fetches bundles from GitHub's attestation store. If you pushed with push-to-registry: true, you can fetch them from the registry instead with --bundle-from-oci. This helps in environments that can reach the registry but not the GitHub API. For private images, log in first with docker login ghcr.io, because the CLI needs registry credentials to resolve the digest.
Tags move, digests do not
The most common mistake is verifying a tag and then deploying a tag. Consider this timeline:
- At 10:00 you verify
app:v1.2.0. It resolves to digestsha256:aaa...and passes. - At 10:05 someone with push rights repoints
v1.2.0tosha256:bbb.... - At 10:10 your cluster pulls
app:v1.2.0and gets the unverified image.
Verification proved something about aaa, but you ran bbb. This is a time-of-check to time-of-use gap. Close it by resolving the digest once, verifying it, and deploying by digest:
DIGEST=$(docker buildx imagetools inspect ghcr.io/OWNER/IMAGE:TAG \
--format '{{json .Manifest.Digest}}' | tr -d '"')
gh attestation verify "oci://ghcr.io/OWNER/IMAGE@${DIGEST}" --owner OWNER
docker pull "ghcr.io/OWNER/IMAGE@${DIGEST}"In Kubernetes manifests, write image: ghcr.io/owner/image@sha256:... instead of a tag. A later chapter on policy enforcement builds on this.
Multi-platform images and BuildKit attestations
Two situations change which digest you attest.
Multi-platform builds. If you build for linux/amd64 and linux/arm64, the registry holds an image index that points to one manifest per platform. The digest output of build-push-action is the digest of the index. Attest that digest. Users pulling the tag on any platform get the index, so the attestation covers what they request. The per-platform manifests are referenced by the index but are not separately attested unless you do it.
BuildKit provenance. Recent Docker builds can attach their own provenance and SBOM attestations to the image, controlled by the provenance and sbom inputs of build-push-action. These are stored inside the image index and are not signed with the workflow identity. They are a different mechanism from the GitHub attestation, and gh attestation verify does not treat them as proof. Do not assume a BuildKit provenance record means your image is attested. If you want to avoid confusion, or if the extra entries change your index digest in ways your tooling does not expect, set provenance: false on the build step.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Attest step fails with a permissions message | Missing `id-token: write` or `attestations: write` | Add both to the job's `permissions` block |
| `push-to-registry` fails with 403 | Missing `packages: write`, or the package belongs to another owner | Add the permission and check package access settings |
| Name rejected as invalid | Uppercase letters in the repository name | Lowercase the name as shown above |
| Verify finds no attestations | `subject-name` included a tag, or the digest came from a different build | Use the untagged name and `steps.push.outputs.digest` |
| Verify fails with an identity mismatch | `--repo` or `--signer-workflow` names a different repository or workflow | Compare against the JSON output from `--format json` |
| Verify cannot read a private image | The CLI has no registry credentials | Run `docker login ghcr.io` first |
When a mismatch is hard to diagnose, run verification with --format json and read the certificate fields in the output. They show the repository, workflow path, ref and runner type that actually signed.
Where this fits
At this point your image has a signed statement: this digest came from this repository and workflow. That is build provenance and meets SLSA Build Level 2 when the build runs on GitHub-hosted runners. It does not by itself prove the image is free of vulnerabilities, or that the source code was reviewed. The later chapters cover Level 3 with reusable workflows, SBOM attestations, enforcement in a cluster, and the limits of what provenance proves.
Exercise
A teammate's workflow pushes ghcr.io/Acme/Widget:v2 and then runs actions/attest-build-provenance@v2 with subject-name: ghcr.io/Acme/Widget:v2 and subject-digest set to a digest computed from a local docker build. Verification with gh attestation verify oci://ghcr.io/acme/widget:v2 --owner Acme finds nothing. List the problems and how to fix each.
Show answer
There are three problems. First, the name has capitals, and registries need lowercase, so the name must be ghcr.io/acme/widget. Second, subject-name includes the tag :v2. It must be the name only, because the attestation binds to the digest. Third, the digest came from a local build, not from the registry, so it may differ from the pushed manifest. Give the build step an id and use ${{ steps.<id>.outputs.digest }} from docker/build-push-action. The job also needs packages: write, id-token: write and attestations: write, and push-to-registry: true if the attestation should be stored in the registry.
Exercise
Why should a deployment pipeline resolve a tag to a digest before verifying, and what does it deploy afterwards?
Show answer
A tag can be repointed after verification, so the image pulled later may not be the one that was checked. Resolving the tag to a digest once, verifying that digest, and then deploying by digest (image@sha256:...) ensures the verified image is the one that runs.