|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Is the scanner image on this runner the one this repository recorded. |
| 4 | +# |
| 5 | +# The OSV actions are pinned by commit, and each of them names its Docker image |
| 6 | +# by a tag, ghcr.io/google/osv-scanner-action:vX.Y.Z, which whoever owns it can |
| 7 | +# push again. Pinning the action does not stop that. The runner pulls the image |
| 8 | +# while it sets the job up, before the first step, so by the time this runs the |
| 9 | +# image is here and has not run yet. If its digest is not the one recorded in |
| 10 | +# the file, this fails, and the scan steps after it do not start. Measured on |
| 11 | +# the runner 2026-10-15, docs/OSV-2-2026-10-15.md. |
| 12 | +# |
| 13 | +# Fails rather than passes when there is no image of the scanner at all. That |
| 14 | +# would mean the runner stopped pulling it during set-up, and a check that found |
| 15 | +# nothing to check has not checked anything. |
| 16 | +# |
| 17 | +# A Dependabot update of google/osv-scanner-action brings a new image and stops |
| 18 | +# here until its digest is written to the file. That is on purpose: it is the |
| 19 | +# moment somebody reads what the new release changed. |
| 20 | +# |
| 21 | +# Usage: osv_scanner_image.sh [file with the digest] |
| 22 | +set -euo pipefail |
| 23 | + |
| 24 | +file="${1:-.github/osv-scanner-digest}" |
| 25 | +repository="ghcr.io/google/osv-scanner-action" |
| 26 | + |
| 27 | +# One digest and nothing else, white space allowed around it and none inside |
| 28 | +# it - the shape TestTheScannerImageIsCheckedBeforeItRuns asks of the file. |
| 29 | +# Taking every space out first, as this once did, joined a digest split in two |
| 30 | +# into one that passed (review of #187). |
| 31 | +raw=$(cat -- "$file" 2>/dev/null || true) |
| 32 | +if [[ "$raw" =~ ^[[:space:]]*(sha256:[0-9a-f]{64})[[:space:]]*$ ]]; then |
| 33 | + expected="${BASH_REMATCH[1]}" |
| 34 | +else |
| 35 | + echo "::error::$file does not hold one sha256 digest, so there is nothing to compare the scanner image with. Write one line to it: sha256: and the 64 lowercase hexadecimal characters of the image the OSV actions run, which the Set up job section of a run prints after Digest." |
| 36 | + exit 1 |
| 37 | +fi |
| 38 | + |
| 39 | +ids=$(docker image ls --no-trunc --quiet "$repository") |
| 40 | +if [ -z "$ids" ]; then |
| 41 | + echo "::error::No image of $repository is on this runner. The runner used to pull it while setting the job up, before any step, and this check relies on that. Read the Set up job section of this run." |
| 42 | + exit 1 |
| 43 | +fi |
| 44 | + |
| 45 | +status=0 |
| 46 | +for id in $(printf '%s\n' "$ids" | sort -u); do |
| 47 | + digests=$(docker image inspect --format '{{range .RepoDigests}}{{println .}}{{end}}' "$id") |
| 48 | + if printf '%s\n' "$digests" | grep -qxF "$repository@$expected"; then |
| 49 | + echo "osv_scanner_image: $id is $repository@$expected" |
| 50 | + continue |
| 51 | + fi |
| 52 | + echo "::error::The scanner image $id is not the one recorded in $file." |
| 53 | + echo "It was pulled as:" |
| 54 | + printf '%s\n' "$digests" | sed 's/^/ /' |
| 55 | + echo "If google/osv-scanner-action was just updated, read what its new release changed and write the new digest to $file." |
| 56 | + echo "If it was not, the image behind the tag has changed, and nothing should run it." |
| 57 | + status=1 |
| 58 | +done |
| 59 | +exit "$status" |
0 commit comments