Container image scanning

An old base image, a current one, and a gate on criticals.

Intermediate12 min · lesson 14 of 17
Lesson files
The scripts, test data and local test servers this lesson uses, exactly as they ran on the lab machine (16 files, 7 KB): imagescan.tar.gz. The lab VM shares no folders with your computer, so fetch them inside the VM: cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-gitlab/imagescan.tar.gz && tar -xzf imagescan.tar.gz, which creates ~/lab/imagescan/. SHA-256: 32afeb5867ff4964cac5f47edd161cd3dca89f3a92cd6b96fbfb9f57fca03a5a

Your container image ships your code and a whole Linux distribution under it: a C library, OpenSSL, compression libraries, a shell. Most of the vulnerabilities in a typical image are in that base layer, which nobody on the team wrote or reads. Container scanning lists the packages in an image and checks each against vulnerability data. GitLab's container scanning template runs on the Free tier, and like the other templates it reports without failing anything.

The template

app/.gitlab-ci.yml
# GitLab's container scanning template (Trivy inside), pointed at an image by digest.
# CS_IMAGE is the image to scan; in a real project it is the image this pipeline just built.
include:
- template: Jobs/Container-Scanning.gitlab-ci.yml
stages: [test, gate]
variables:
CS_IMAGE: python:3.10.0-slim@sha256:ad540a471260fee5e5e1a99ee2acf142efe8c279a7a54315160d8033ba88f0d8
image-gate:
stage: gate
image: secopslog-py:3.15
needs:
- job: container_scanning
artifacts: true
script:
- python gate.py gl-container-scanning-report.json Critical | tail -3
- python gate.py gl-container-scanning-report.json Critical > /dev/null

CS_IMAGE names the image to scan. In a real project it's the image the pipeline just built and pushed. Here it's an old Python base image, pinned by digest so the scan always looks at the same bytes. image-gate is the gate.py from the SAST lesson, failing on any Critical. Treat it as a starting point: the hands-on shows why a production gate counts only Criticals that have a fix. In production, scan the image you built by its digest, and deploy that same digest, so the scanned bytes are the shipped bytes.

ubuntu@secopslog-cicd:~/lab/imagescan · GitLab 19.5.3
$ ./up.sh
runner lab-runner: online (tags: lab,docker) GitLab 19.5.3 is up on http://127.0.0.1:8929
$ ./seed.sh app app/ >/dev/null && ./gl.sh '/projects/root%2Fapp/ci/lint' | jq -r '.merged_yaml' > /tmp/merged.yml && grep -E '^ +(CS_ANALYZER_IMAGE|CS_IMAGE|CS_SCHEMA_MODEL):' /tmp/merged.yml | sort -u; rm -f /tmp/merged.yml
CS_ANALYZER_IMAGE: "$CI_TEMPLATE_REGISTRY_HOST/security-products/container-scanning:8" CS_IMAGE: python:3.10.0-slim@sha256:ad540a471260fee5e5e1a99ee2acf142efe8c279a7a54315160d8033ba88f0d8 CS_SCHEMA_MODEL: 15

The template runs GitLab's container scanning analyzer, which uses Trivy inside, from the tag container-scanning:8, a major version that moves with every release, as the SAST analyzer did. CS_IMAGE is our pinned image. The job also writes a CycloneDX SBOM of the image next to its report.

An old base image

ubuntu@secopslog-cicd:~/lab/imagescan · GitLab 19.5.3
$ PIPE_WAIT=900 ./pipeline.sh app/ | sed -n "/--- job image-gate/,\$p"; exit ${PIPESTATUS[0]}
--- job image-gate (gate): failed Downloading artifacts $ python gate.py gl-container-scanning-report.json Critical | tail -3 High zlib1g High zlib1g 748 findings, 31 at Critical ERROR: Job failed: exit code 1 pipeline #1: failed
$ ./gl.sh "/projects/root%2Fapp/jobs/$(./gl.sh '/projects/root%2Fapp/jobs?per_page=10' | jq -r '[.[] | select(.name=="container_scanning")][0].id')/artifacts/gl-container-scanning-report.json" | jq -r '[.vulnerabilities[].severity] | group_by(.) | map("\(.[0]): \(length)") | join(", ")'
Critical: 31, High: 228, Low: 185, Medium: 270, Unknown: 34

748 findings in a base image nobody on the team wrote a line of: 31 Critical, 228 High, and hundreds more. The gate failed on the 31 Criticals. tail -3 shows only the end of the list, here two High findings in zlib1g, the compression library, one of dozens of packages that came with the base image. The full list is in the job log and the report. Python 3.10.0 slim is from 2021, and its Debian packages have been fixed many times since.

A current base image

The quickest fix for base-layer findings is usually a newer base image. Scan the current python:3.15-slim, again by digest, by overriding CS_IMAGE for one pipeline:

ubuntu@secopslog-cicd:~/lab/imagescan · GitLab 19.5.3
$ ./run-pipeline.sh app CS_IMAGE=python:3.15-slim@sha256:65a65fc7b30b06cb6df4930fd4650697cb23f4c2698ab81fc87e4b031bc36928 >/dev/null && sleep 3 && PIPE_WAIT=900 ./pipeline.sh app/ | sed -n "/--- job image-gate/,\$p"
--- job image-gate (gate): success Removing gl-container-scanning-report.json Removing gl-sbom-report.cdx.json Downloading artifacts $ python gate.py gl-container-scanning-report.json Critical | tail -3 High zlib1g Medium zlib1g 189 findings, 0 at Critical $ python gate.py gl-container-scanning-report.json Critical > /dev/null pipeline #2: success
$ ./gl.sh "/projects/root%2Fapp/jobs/$(./gl.sh '/projects/root%2Fapp/jobs?per_page=10' | jq -r '[.[] | select(.name=="container_scanning")][0].id')/artifacts/gl-container-scanning-report.json" | jq -r '[.vulnerabilities[].severity] | group_by(.) | map("\(.[0]): \(length)") | join(", ")'
High: 49, Low: 65, Medium: 75

No Criticals, and the gate passed. The current image still has 189 findings, 49 of them High. Many of them are in Debian packages with no fixed version released yet. For those, a newer image doesn't help today, and a gate that failed on them would block every pipeline with nothing anyone could do about it.

Watch out
A scan is a snapshot: it reports what the database knew about these packages on the day it ran. An image that passed at build time can have a Critical next month without a single byte changing. Rescan the images you run, not just the ones you build, on a schedule, and rebuild on a fresh base image regularly so fixes arrive even without a finding.
GateEffect
Fail on every findingFails on almost every image, so it gets switched off
Fail on CriticalCatches the worst, but can still fail on things nobody can fix yet
Fail on Critical with a fix availableFails only when an upgrade would help. Track the rest
No gate, read the reportFindings pile up unread

Hands-on

Take the report of the first scan, the old image. It belongs to the older of the two container_scanning jobs, so pick it with sort_by(.id)[0] instead of the [0] the counts step used, and for each severity count how many findings have a fix available. In GitLab's report format, a finding with a fix has a non-empty solution. How many Criticals would a "Critical with a fix available" gate fail on? Finish with ./down.sh.

What you should see: in the old image, 26 of the 31 Criticals have a fix available, and so do 103 of the 228 Highs. A "Critical with a fix available" gate would fail on those 26, every one solved by upgrading a package or the base image. The other five are known and unfixed, worth tracking, and nobody can fix them yet.

Remember

Scan the image you ship, gate on Critical findings that have a fix, rebuild on current base images, and rescan running images on a schedule, because a scan only reflects the day it ran.

Quick check
01An image passed container scanning at build time. A month later, a new Critical CVE is published for one of its packages. What does the original scan tell you?
Incorrect — It passed against the database of that day.
Correct — A scan is a snapshot of what was known when it ran.
Incorrect — Nothing in the image changes by itself.
Incorrect — The CVE was not in the database then.
02Why gate on Critical findings that have a fix available, rather than on every Critical?
Incorrect — Many are real. There is just no upgrade yet.
Incorrect — The report lists them, with no solution.
Incorrect — Severity and fix availability are separate.
Correct — A gate like that is one people keep switched on. Track the unfixable ones separately.
03In the lab, where did most findings come from?
Correct — The operating system packages of the base image produced them. The application added nothing; the old base image brought the findings.
Incorrect — The scan looks at installed packages, not source code.
Incorrect — The scanner reports on CS_IMAGE, not on itself.
Incorrect — The runner is not part of the scanned image.

Related