Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(docker): pin Python 3.14.8, retire the zipfile backport, and enforce interpreter and expat floors (#7077)

LearningCircuitb9fb829f69b9
diff --git a/.grype.yaml b/.grype.yaml
index b21b90f01b..7cf8dd1aa9 100644
--- a/.grype.yaml
+++ b/.grype.yaml
@@ -262,46 +262,11 @@ ignore:
# the 2026-07-11 release-gate scan still flagged it with the auto-rule
# active, contrary to the assumption in PR #5032).
# ==========================================================================
- - vulnerability: CVE-2026-6879
- package: { name: python, type: binary }
- reason: "O(n^2) DoS (Low, CVSS 2.0) in stdlib xml.etree.ElementTree XPath positional predicates. Not exploitable here: grep confirms zero [N]/[last()] positional predicates on any ElementTree consumer in src/. defusedxml only hardens DTD/entity handling, not the XPath engine. Fix only in CPython 3.15.0 beta, outside requires-python <3.15; drop when 3.15.0 is stable."
- - vulnerability: CVE-2026-4360
- package: { name: python, type: binary }
- reason: "Low tarfile uid/gid extraction-filter gap (GHSA-gf2w-jqmq-fcm8). No fixed release exists yet: Python 3.14.7 is scheduled 2026-08-04 (PEP 745). NVD-CPE binary match carries fix-state unknown, so the not-fixed auto-rule misses it. Remove when the base image ships Python 3.14.7+."
- - vulnerability: CVE-2026-15308
- package: { name: python, type: binary }
- reason: "High (PSF CVSS4 8.7 / NVD 7.5) html.parser quadratic CPU DoS needing unterminated constructs across many incremental feed() calls (PSF-2026-33). Not reachable: all html.parser use is single-shot BeautifulSoup feed(markup); lxml uses libxml2. No fixed release yet: 3.14.7 due 2026-08-04 (PEP 745). NVD-CPE binary match has fix-state unknown. Remove when the base image ships 3.14.7+."
- # 2026-08-18 CPython security batch (alerts #8143/#8144/#8148): all three
- # fixes landed on the 3.14 branch AFTER the v3.14.7 tag was cut (verified
- # 2026-08-27 via python/cpython compare: the [3.14] backport of gh-155694 is
- # ahead of v3.14.7; the tarfile fix PR #156000 merged to main 2026-08-19),
- # so the first release carrying them is 3.14.8 — not yet released, no Docker
- # image exists. NVD-CPE binary match with fix-state unknown, so the
- # not-fixed auto-rule misses them (same class as the entries above).
- # Remove all three when the base image ships Python 3.14.8+.
- - vulnerability: CVE-2026-19672
- package: { name: python, type: binary }
- reason: "Medium tarfile extraction-filter gap: intermediate dirs are created outside the destination for members whose name leaves and re-enters it. Not reachable: zero tarfile/extractall/unpack_archive usage in src/ — the only .extract() calls are trafilatura/extruct HTML content extractors. Fix post-3.14.7; remove when base image ships 3.14.8+."
- - vulnerability: CVE-2026-15806
- package: { name: python, type: binary }
- reason: "Medium urllib.request HTTPPasswordMgr cross-scheme credential reuse. Not reachable: urllib.request never used as an HTTP client — sole import is url2pathname for file:// paths (zotero/client.py); no password manager or opener constructed; all HTTP uses requests via security/safe_requests.py. Fix post-3.14.7; remove when base image ships 3.14.8+."
- - vulnerability: CVE-2026-17084
- package: { name: python, type: binary }
- reason: "Medium stringprep processes RFC 3454 B.2/B.3 tables with current Unicode instead of 3.2.0, causing IDNA-2003 mismatches. Not reachable: zero stringprep/encodings.idna usage in src/ (dns_pinning.py deliberately avoids the idna codec); hostname encoding goes through the third-party idna package (IDNA 2008) via requests/urllib3. Fix post-3.14.7; remove when base image ships 3.14.8+."
-
- # CVE-2026-15310 is tracked separately from the 2026-08-18 batch above: it is
- # not merely unreachable/unfixed-upstream, it is actively patched in the
- # image, and its removal condition is its own.
- - vulnerability: CVE-2026-15310
- package: { name: python, type: binary }
- reason: >-
- The official CPython 3.14 zipfile fix (31980e84b9a708424a0a1dfecde3fc991e313f89)
- is backported by scripts/patch_cpython_zipfile_cve_2026_15310.py in both stages
- built directly from the pinned base image (builder-base, ldr); the other two
- stages (builder, ldr-test) inherit it from builder-base. Verified during the
- build. The binary CPE remains 3.14.7, so Grype cannot observe the patched
- pure-Python stdlib file. Remove this entry and the backport when a fixed
- Python 3.14 image is released.
+ # Fixed in the Python 3.14.8 base image, so their entries were removed:
+ # CVE-2026-4360, CVE-2026-6879, and CVE-2026-15308 (fixed in 3.14.7), and
+ # CVE-2026-15310, CVE-2026-15806, CVE-2026-17084, and CVE-2026-19672
+ # (fixed in 3.14.8). CVE-2026-87910 remains visible to the gate: its 3.14
+ # backport landed after the 3.14.8 release tag.
# ==========================================================================
# NO FIX AVAILABLE IN DEBIAN TRIXIE
@@ -363,55 +328,26 @@ ignore:
reason: "Assertion failure in font handling. Debian trixie no-dsa. Crash, no security impact."
# --- libexpat1 ---
- - vulnerability: CVE-2025-59375
- reason: "Memory amplification via small XML. Needs expat 2.7.2, Trixie has 2.7.1. Debian no-dsa."
+ # Debian trixie ships libexpat1 2.8.3-1~deb13u1 (DSA-6404-1, DSA-6446-1),
+ # which fixes every libexpat1 CVE previously listed here except
+ # CVE-2025-66382. It does not include fixes from later expat releases, for
+ # example CVE-2026-66046 (2.8.4) and CVE-2026-93990 (2.8.5). Debian has no
+ # fixed trixie package for those yet, so the fix-state rules at the top of
+ # this file keep them out of the Grype results until it does.
+ # Python's pyexpat does not link this package: it embeds CPython's bundled
+ # expat, which Grype cannot see. The builder-base and ldr stages of the
+ # Dockerfile fail the build if that bundled copy is older than 2.8.5, the
+ # expat that the pinned Python 3.14.8 bundles. BUNDLED_EXPAT in
+ # tests/deployment/test_deployment_surface_contracts.py maps each CPython
+ # release to its bundled expat, and the contract test fails while the
+ # minimum differs from the entry for the release in the base image tag, so
+ # a change of that release has to move the minimum with it. Expat 2.8.5 has
+ # the fixes of expat 2.8.4 and 2.8.5, but not those of expat 2.9.0:
+ # CVE-2026-77214, and CVE-2026-102633, which affects 32-bit platforms only.
+ # No expat release fixes CVE-2025-66382 yet.
- vulnerability: CVE-2025-66382
- reason: "2 MiB XML causes DoS via slow parsing. Debian: minor, postponed. Needs expat >2.7.3."
- - vulnerability: CVE-2026-24515
- reason: "External entity parser missing encoding handler copy. Debian trixie no-dsa. Sid 2.7.4-1."
- - vulnerability: CVE-2026-25210
- reason: "Integer overflow in doContent tag buffer realloc. Debian trixie no-dsa. Sid 2.7.4-1."
- - vulnerability: CVE-2026-32776
- reason: "NULL deref in empty external parameter entity. Needs expat 2.7.5, not in Trixie or sid. DoS only (CVSS 4.0). API XML uses defusedxml; XML upload uses lxml/libxml2 (not expat)."
- - vulnerability: CVE-2026-32777
- reason: "Infinite loop in DTD parsing. Needs expat 2.7.5, not in Trixie or sid. Local attack vector, DoS only (CVSS 4.0). API XML uses defusedxml; XML upload uses lxml/libxml2 (not expat)."
- - vulnerability: CVE-2026-32778
- reason: "NULL deref in setContext after OOM. Needs expat 2.7.5, not in Trixie or sid. CNA CVSS 2.9 Low / NIST CVSS 5.5 Medium. Requires OOM precondition."
- - vulnerability: CVE-2026-50219
- reason: "UAF via reentrant parser calls from handlers in expat <2.8.2. Unfixed upstream and in sid (Debian bug 1138862). API XML uses defusedxml; XML upload uses lxml/libxml2 (not expat)."
- # libexpat <2.8.2 integer-overflow batch. Fixed upstream in expat 2.8.2
- # (released 2026-06-25) but not yet packaged in Debian trixie (2.7.1-2) or
- # sid (2.8.1-1) — both still vulnerable, nothing to apt-upgrade to. These
- # overflows fire during normal attribute/namespace/CDATA parsing, so
- # defusedxml's entity/DTD hardening does NOT mitigate them; the real
- # mitigation is that expat only parses TLS-fetched XML from hardcoded trusted
- # upstreams — arXiv (export.arxiv.org, via the `arxiv` lib's feedparser ->
- # xml.sax -> expat) and PubMed/NCBI (eutils.ncbi.nlm.nih.gov, via
- # defusedxml). No attacker-controlled XML reaches expat; document uploads
- # use lxml/libxml2, not expat. NVD: AV:L, medium.
- - vulnerability: CVE-2026-56403
- reason: "Integer overflow in storeAtts (attribute parsing), expat <2.8.2. Trixie 2.7.1-2 / sid 2.8.1-1 vulnerable; expat 2.8.2 (2026-06-25) not yet in Debian. Expat parses only TLS-fetched trusted arXiv/PubMed XML; uploads use lxml not expat. AV:L medium."
- - vulnerability: CVE-2026-56404
- reason: "Integer overflow in addBinding (namespace binding), expat <2.8.2. Trixie 2.7.1-2 / sid 2.8.1-1 vulnerable; expat 2.8.2 (2026-06-25) not yet in Debian. Expat parses only TLS-fetched trusted arXiv/PubMed XML; uploads use lxml not expat. AV:L medium."
- - vulnerability: CVE-2026-56405
- reason: "Integer overflow in getAttributeId, expat <2.8.2. Trixie 2.7.1-2 / sid 2.8.1-1 vulnerable; expat 2.8.2 (2026-06-25) not yet in Debian. Expat parses only TLS-fetched trusted arXiv/PubMed XML; uploads use lxml not expat. AV:L medium."
- - vulnerability: CVE-2026-56406
- reason: "Integer overflow in XML_ParseBuffer (missing length check), expat <2.8.2. Trixie 2.7.1-2 / sid 2.8.1-1 vulnerable; expat 2.8.2 (2026-06-25) not yet in Debian. Expat parses only TLS-fetched trusted arXiv/PubMed XML; uploads use lxml not expat. AV:L medium."
- - vulnerability: CVE-2026-56407
- reason: "Integer overflow in doProlog (storeEntityValue), expat <2.8.2. Trixie 2.7.1-2 / sid 2.8.1-1 vulnerable; expat 2.8.2 (2026-06-25) not yet in Debian. Expat parses only TLS-fetched trusted arXiv/PubMed XML; uploads use lxml not expat. AV:L medium."
- - vulnerability: CVE-2026-56408
- reason: "Integer overflow in copyString, expat <2.8.2. Trixie 2.7.1-2 / sid 2.8.1-1 vulnerable; expat 2.8.2 (2026-06-25) not yet in Debian. Expat parses only TLS-fetched trusted arXiv/PubMed XML; uploads use lxml not expat. AV:L medium."
- - vulnerability: CVE-2026-56412
- reason: "UAF in doCdataSection (missing XML_TOK_DATA_CHARS check; incomplete fix for CVE-2026-50219), expat <2.8.2. Trixie 2.7.1-2 / sid 2.8.1-1 vulnerable; expat 2.8.2 (2026-06-25) not yet in Debian. Expat parses only TLS-fetched trusted arXiv/PubMed XML; uploads use lxml not expat. AV:L medium."
- # CVE-2026-56409/-56410/-56411 are in the xmlwf CLI tool, which ships in the
- # `expat` package (not the installed libexpat1 runtime) and is never invoked
- # in this container — no runtime attack surface.
- - vulnerability: CVE-2026-56409
- reason: "Integer overflow in the xmlwf CLI tool (-d outputDir output filename), expat <2.8.2. Bug is in the xmlwf binary, not the libexpat1 runtime, and xmlwf is never invoked in this container. Trixie/sid vulnerable; expat 2.8.2 (2026-06-25) not yet in Debian."
- - vulnerability: CVE-2026-56410
- reason: "Integer overflow in the xmlwf CLI tool (resolveSystemId), expat <2.8.2. Bug is in the xmlwf binary, not the libexpat1 runtime, and xmlwf is never invoked in this container. Trixie/sid vulnerable; expat 2.8.2 (2026-06-25) not yet in Debian."
- - vulnerability: CVE-2026-56411
- reason: "Integer overflow in the xmlwf CLI tool (endDoctypeDecl via NOTATION declarations), expat <2.8.2. Bug is in the xmlwf binary, not the libexpat1 runtime, and xmlwf is never invoked in this container. Trixie/sid vulnerable; expat 2.8.2 (2026-06-25) not yet in Debian."
+ package: { name: libexpat1, type: deb }
+ reason: "No fixed expat release yet (libexpat#1076); Debian trixie: postponed. Impact is CPU time only (CWE-407). Remove when a fixed libexpat1 reaches trixie."
# --- libgraphite2-3 (font shaping, transitive via harfbuzz for WeasyPrint) ---
- vulnerability: CVE-2026-50593
@@ -510,10 +446,16 @@ ignore:
- vulnerability: CVE-2023-37769
reason: "FPE in stress-test tool. Debian: unimportant. Crash in test tool, no security impact."
- # --- python3.14 (base image python:3.14.6-slim) ---
+ # --- python3.14 (base image python:3.14.8-slim) ---
# 3.14.5 fixed CVE-2026-6100, -4786, -1502, -6019; 3.14.6 (2026-06-11)
# fixed CVE-2026-8328, -9669, -7774, -3276 — those entries were removed.
- # The entries below remain unfixed upstream (no CPython release has them).
+ # Also removed: CVE-2025-13462 (tarfile, fixed in 3.14.4), CVE-2025-15366
+ # (imaplib, fixed in 3.14.7) and CVE-2025-12781 (base64 altchars). The
+ # CVE-2025-12781 record lists 3.14 as affected only below 3.14.1, so it
+ # does not match 3.14.8, although 3.14 keeps the behaviour: its 3.14
+ # change added tests only, and 3.15 deprecates the behaviour.
+ # The entry below has no fix in any 3.14 release: the poplib fix
+ # (gh-143923) is only in 3.15.0a6 and later.
#
# NOTE on Grype DB lag: Grype matches the python *binary* against NVD/CPE
# ranges, which can trail an upstream patch release by days to weeks. A
@@ -522,14 +464,8 @@ ignore:
# alerts via the code-scanning API as false positives (they self-heal
# when the DB updates) instead of adding entries here — a suppression in
# this file would silently outlive the DB lag it papered over.
- - vulnerability: CVE-2025-12781
- reason: "base64 altchars accepts +/ regardless. Debian: minor. No alt-alphabet base64 usage."
- - vulnerability: CVE-2025-15366
- reason: "imaplib newline injection. Debian trixie no-dsa. No IMAP usage in container."
- vulnerability: CVE-2025-15367
- reason: "poplib newline injection. Debian trixie no-dsa. No POP3 usage in container."
- - vulnerability: CVE-2025-13462
- reason: "tarfile AREGTYPE/DIRTYPE normalization in multi-block members. Low (CVSS 2.0). No untrusted tar extraction."
+ reason: "poplib newline injection (gh-143923). No 3.14 release has the fix; it is only in 3.15.0a6 and later. No POP3 usage in container."
# --- coreutils ---
- vulnerability: CVE-2025-5278
@@ -572,7 +508,7 @@ ignore:
# partition_md() (fetched via requests.get with no host validation).
# Critical per the advisory. NO LOCKABLE FIX: every fixed release (0.24.0
# through 0.27.6) requires Python <3.14, while this project pins
- # requires-python >=3.12,<3.15 with a Python 3.14.7 production image.
+ # requires-python >=3.12,<3.15 with a Python 3.14.8 production image.
# MITIGATED — the URL sinks are unreachable in this codebase:
# es_utils.index_file calls partition(filename=<PathValidator-vetted local
# path>); every other touchpoint is langchain-community's file-based
@@ -610,6 +546,6 @@ ignore:
- vulnerability: CVE-2026-2219
reason: "High vuln in dpkg 1.22.22. No fix in Trixie yet. Base image python:3.14-slim ships this version."
- # --- sed (GNU sed from Debian base image python:3.14.6-slim) ---
+ # --- sed (GNU sed from Debian base image python:3.14.8-slim) ---
- vulnerability: CVE-2026-5958
reason: "Low CVE in GNU sed from base image. No public info — may be very new, disputed, or not yet indexed. sed not explicitly installed in Dockerfile; comes from the python slim base image. Container does not process untrusted input with sed. No fix in Trixie."
diff --git a/.trivyignore b/.trivyignore
index 1085688a81..89beae6583 100644
--- a/.trivyignore
+++ b/.trivyignore
@@ -15,7 +15,8 @@
# MITIGATED: This vulnerability only affects pip's fallback tar extraction
# on Python versions that don't implement PEP 706. Safe versions:
# Python >= 3.9.17, >= 3.10.12, >= 3.11.4, or >= 3.12 (all versions).
-# This project uses Python 3.13 which implements PEP 706, so the vulnerable
+# This project requires Python 3.12 or newer and its production image ships
+# Python 3.14.8, so PEP 706 is always implemented and the vulnerable
# fallback code path is never executed.
#
# Fix available in pip 25.3+, but not needed for PEP 706-compliant Python.
@@ -37,25 +38,16 @@ CVE-2025-8869
# Tracking: https://security-tracker.debian.org/tracker/CVE-2025-14104
CVE-2025-14104 exp:2026-07-01
-# CVE-2025-59375: libexpat memory allocation DoS via small crafted XML
-# Severity: HIGH (CVSS 7.5)
-# Review: 2026-07-01
-#
-# DEBIAN IGNORED: Classified as "Minor issue", no backport planned.
-# Allows disproportionately large memory allocations via small XML documents.
-# App doesn't process untrusted XML from external sources. DoS only.
-# Fixed in: libexpat 2.7.2 (bookworm has 2.5.0)
-# Tracking: https://security-tracker.debian.org/tracker/CVE-2025-59375
-CVE-2025-59375 exp:2026-07-01
-
-# CVE-2025-66382: libexpat DoS via 2MB crafted XML
-# Severity: LOW (CVSS 2.9)
-# Review: 2026-07-01
+# CVE-2025-66382: libexpat CPU-time denial of service
+# Severity: LOW per CNA (CVSS 2.9) / MEDIUM per NIST (CVSS 5.5)
+# Review: 2027-01-06
#
-# NOT FIXED ANYWHERE: No upstream fix available yet. Debian marked "postponed".
-# App doesn't process large untrusted XML from external sources.
+# NOT FIXED ANYWHERE: no expat release fixes it yet (libexpat#1076), and
+# Debian trixie lists it as postponed. Impact is CPU time only (CWE-407).
+# The other libexpat entries were removed: Debian trixie's libexpat1
+# 2.8.3-1~deb13u1 (DSA-6404-1, DSA-6446-1) fixes them.
# Tracking: https://security-tracker.debian.org/tracker/CVE-2025-66382
-CVE-2025-66382 exp:2026-07-01
+CVE-2025-66382 exp:2027-01-06
# CVE-2025-7709: SQLite FTS5 integer overflow
# Severity: MEDIUM (CVSS 6.9)
@@ -201,35 +193,6 @@ CVE-2026-5435 exp:2026-10-01
# Tracking: https://security-tracker.debian.org/tracker/CVE-2026-6238
CVE-2026-6238 exp:2026-10-01
-# CVE-2026-32776: libexpat NULL deref in empty external parameter entity
-# Severity: MEDIUM (CVSS 4.0)
-# Review: 2026-09-01
-#
-# UNFIXABLE: Needs expat 2.7.5, not available in Trixie or sid.
-# DoS only via DTD processing. API XML (PubMed/arXiv) uses defusedxml;
-# XML file upload uses lxml/libxml2 (not expat). Low exploitability.
-# Tracking: https://security-tracker.debian.org/tracker/CVE-2026-32776
-CVE-2026-32776 exp:2026-09-01
-
-# CVE-2026-32777: libexpat infinite loop in DTD parsing
-# Severity: MEDIUM (CVSS 4.0)
-# Review: 2026-09-01
-#
-# UNFIXABLE: Needs expat 2.7.5, not available in Trixie or sid.
-# Local attack vector, DoS only. API XML (PubMed/arXiv) uses defusedxml;
-# XML file upload uses lxml/libxml2 (not expat). Low exploitability.
-# Tracking: https://security-tracker.debian.org/tracker/CVE-2026-32777
-CVE-2026-32777 exp:2026-09-01
-
-# CVE-2026-32778: libexpat NULL deref in setContext after OOM
-# Severity: LOW per CNA (CVSS 2.9) / MEDIUM per NIST (CVSS 5.5)
-# Review: 2026-09-01
-#
-# UNFIXABLE: Needs expat 2.7.5, not available in Trixie or sid.
-# Requires OOM precondition to trigger. Crash only, no code execution.
-# Tracking: https://security-tracker.debian.org/tracker/CVE-2026-32778
-CVE-2026-32778 exp:2026-09-01
-
# CVE-2019-1010023: libc6 — library remapping via ldd
# Severity: HIGH
# Review: 2026-09-01
@@ -247,14 +210,6 @@ CVE-2019-1010023 exp:2026-09-01
# libncursesw6, libtinfo6). No fix available in Trixie.
CVE-2025-69720 exp:2026-09-01
-# CVE-2026-3479: Python 3.14.3 low vulnerability
-# Severity: LOW
-# Review: 2026-09-01
-#
-# UNFIXABLE: No fix available — latest python:3.14-slim still ships 3.14.3.
-# Awaiting Python 3.14.4.
-CVE-2026-3479 exp:2026-09-01
-
# CVE-2026-27456: util-linux low-severity vulnerability
# Severity: LOW
# Review: 2026-10-01
@@ -274,7 +229,7 @@ CVE-2026-27456 exp:2026-10-01
# partition_html()/partition_md() is fetched with no host validation
# (full-read SSRF). Fixed only in unstructured 0.24.0+, and EVERY fixed
# release (0.24.0..0.27.6) declares Requires-Python <3.14 while this project
-# pins requires-python >=3.12,<3.15 with a Python 3.14.7 production image —
+# pins requires-python >=3.12,<3.15 with a Python 3.14.8 production image —
# no lockable fixed version exists until upstream ships 3.14 support.
#
# MITIGATED — the URL sinks are unreachable: es_utils.index_file calls
diff --git a/Dockerfile b/Dockerfile
index d34ed3a651..1a8304c223 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -1,19 +1,32 @@
####
# Used for building the LDR service dependencies.
####
-FROM python:3.14.7-slim@sha256:cad9a2c871761c413caa6fdd6441c783451e740a48aaeba60ae62a8b53525ef6 AS builder-base
-
-# CVE-2026-15310 is fixed on CPython's 3.14 maintenance branch but no patched
-# 3.14 image has been released yet. Apply the reviewed pure-Python backport;
-# the script fails closed if a future base image no longer matches its anchors.
-COPY scripts/patch_cpython_zipfile_cve_2026_15310.py /tmp/patch_cpython_zipfile.py
-RUN python3 /tmp/patch_cpython_zipfile.py && rm /tmp/patch_cpython_zipfile.py
+FROM python:3.14.8-slim@sha256:89fb7d3da20043c370643435258bdd7ab755d326d359001d02988ed15ae5219e AS builder-base
# Set shell to bash with pipefail for safer pipe handling
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
+# Fail the build if the base image's interpreter is older than 3.14.8, the
+# first 3.14 release with the upstream CVE-2026-15310 zipfile fix. Docker
+# pulls a `name:tag@sha256:...` reference by its digest and ignores the tag,
+# so this checks the interpreter the digest actually contains. It calls
+# sys.exit rather than assert: python -O and PYTHONOPTIMIZE strip assert
+# statements, and the check must fail the build in either case.
+# See .grype.yaml (CPYTHON).
+RUN python3 -c "import sys; v = sys.version.split()[0]; print(f'base image Python {v}'); sys.exit(0 if sys.version_info >= (3, 14, 8) else f'base image Python {v} is older than 3.14.8, the first 3.14 release with the CVE-2026-15310 zipfile fix')"
+
ARG DEBIAN_FRONTEND=noninteractive
+# Fail the build if pyexpat embeds an expat older than 2.8.5, the expat that
+# CPython 3.14.8 bundles. The official python images build CPython with its
+# bundled expat instead of Debian's libexpat1, so Grype sees libexpat1 but not
+# the copy that pyexpat uses. Keep this floor, and the floor of the ldr
+# stage's copy of this check, at the bundled expat version of the pinned base
+# image. It calls sys.exit rather than assert: python -O and
+# PYTHONOPTIMIZE strip assert statements, and the check must fail the build in
+# either case. See .grype.yaml (libexpat1).
+RUN python3 -c "import pyexpat, sys; v = pyexpat.version_info; print(f'pyexpat uses expat {v}'); sys.exit(0 if v >= (2, 8, 5) else 'pyexpat embeds an expat older than 2.8.5; expected the CPython-bundled expat 2.8.5 or newer')"
+
# `apt-get upgrade -y` is INTENTIONAL — we want every build to pull the
# latest patched Debian packages so security fixes flow into the image.
# This trades bit-for-bit reproducibility (two rebuilds of the same source
@@ -308,18 +321,21 @@ RUN groupadd -r ldruser && useradd -r -g ldruser -u 1000 -m -d /home/ldruser ldr
####
# Runs the LDR service.
###
-FROM python:3.14.7-slim@sha256:cad9a2c871761c413caa6fdd6441c783451e740a48aaeba60ae62a8b53525ef6 AS ldr
-
-# Keep the production stage's independent stdlib in sync with the builder/test
-# stages until the base image ships the upstream CVE-2026-15310 fix.
-COPY scripts/patch_cpython_zipfile_cve_2026_15310.py /tmp/patch_cpython_zipfile.py
-RUN python3 /tmp/patch_cpython_zipfile.py && rm /tmp/patch_cpython_zipfile.py
+FROM python:3.14.8-slim@sha256:89fb7d3da20043c370643435258bdd7ab755d326d359001d02988ed15ae5219e AS ldr
# Set shell to bash with pipefail for safer pipe handling
SHELL ["/bin/bash", "-o", "pipefail", "-c"]
+# Same interpreter check as builder-base: this stage starts from the base
+# image again, and its interpreter is the one the image ships.
+RUN python3 -c "import sys; v = sys.version.split()[0]; print(f'base image Python {v}'); sys.exit(0 if sys.version_info >= (3, 14, 8) else f'base image Python {v} is older than 3.14.8, the first 3.14 release with the CVE-2026-15310 zipfile fix')"
+
ARG DEBIAN_FRONTEND=noninteractive
+# Same pyexpat check as builder-base: this stage starts from the base image
+# again, and its interpreter is the one the image ships.
+RUN python3 -c "import pyexpat, sys; v = pyexpat.version_info; print(f'pyexpat uses expat {v}'); sys.exit(0 if v >= (2, 8, 5) else 'pyexpat embeds an expat older than 2.8.5; expected the CPython-bundled expat 2.8.5 or newer')"
+
# Pin pip to 26.2 for the CVE-2026-13346 malicious-index fix and the earlier
# CVE-2026-8643, CVE-2026-1703, and GHSA-jp4c-xjxw-mgf9 fixes.
# Hash-locked (Scorecard Pinned-Dependencies, alert #8207): same rationale as
diff --git a/changelog.d/+cpython-zipfile-cve-2026-15310.security.md b/changelog.d/+cpython-zipfile-cve-2026-15310.security.md
index a1cd0ad909..79b661e2d7 100644
--- a/changelog.d/+cpython-zipfile-cve-2026-15310.security.md
+++ b/changelog.d/+cpython-zipfile-cve-2026-15310.security.md
@@ -1,7 +1,6 @@
-Bound the Python ZIP reader's per-call decompression output while awaiting the
-next patched Python 3.14 base image, so a crafted bzip2/lzma/zstd member can no
-longer expand into one unexpectedly large allocation behind a small buffered
-read. Upstream deliberately leaves whole-member reads unbounded, so
-`ZipFile.read()` and `read(-1)` still return the full decompressed member;
-`extractall()` is now bounded by `copyfileobj()`'s chunking on top of the
-per-read bound.
+Upgrade the Docker base image to Python 3.14.8, which includes the upstream
+`zipfile` fix for CVE-2026-15310 and the `tarfile`, `urllib`, and `stringprep`
+security fixes released with it. The image build now fails if the base image's
+Python is older than 3.14.8, or if the expat that Python bundles and `pyexpat`
+uses is older than 2.8.5, the version Python 3.14.8 ships. Scanner exceptions
+for CPython and libexpat1 CVEs that no longer match the image are removed.
diff --git a/scripts/patch_cpython_zipfile_cve_2026_15310.py b/scripts/patch_cpython_zipfile_cve_2026_15310.py
deleted file mode 100644
index 504ce0677c..0000000000
--- a/scripts/patch_cpython_zipfile_cve_2026_15310.py
+++ /dev/null
@@ -1,286 +0,0 @@
-"""Backport the CPython 3.14 fix for CVE-2026-15310.
-
-The production image is pinned to Python 3.14.7. The upstream fix is merged
-on the 3.14 maintenance branch but is not part of a released 3.14 image yet.
-This script applies the pure-Python ``zipfile`` portion of that backport and
-fails closed if the pinned base image no longer matches the reviewed source.
-
-Upstream backport: https://github.com/python/cpython/commit/31980e84b9a708424a0a1dfecde3fc991e313f89
-"""
-
-from __future__ import annotations
-
-import argparse
-import hashlib
-import io
-import subprocess
-import sys
-import sysconfig
-from pathlib import Path
-
-
-_PATCHES = (
- (
- """ def decompress(self, data):
- if self._decomp is None:
-""",
- """ @property
- def _needs_input(self):
- # While the LZMA properties header is still being buffered, more input
- # is required; afterwards defer to the wrapped decompressor so a bounded
- # decompress() call can be drained across reads.
- if self._decomp is None:
- return True
- return self._decomp.needs_input
-
- def decompress(self, data, max_length=-1):
- if self._decomp is None:
-""",
- ),
- (
- """ result = self._decomp.decompress(data)
- self.eof = self._decomp.eof
-""",
- """ result = self._decomp.decompress(data, max_length)
- self.eof = self._decomp.eof
-""",
- ),
- (
- """def _get_decompressor(compress_type):
-""",
- """def _decompressor_needs_input(decompressor):
- # bz2/zstd expose the stdlib decompressor's public needs_input; the LZMA
- # wrapper keeps it private (_needs_input) to avoid adding public API.
- needs_input = getattr(decompressor, "needs_input", None)
- return decompressor._needs_input if needs_input is None else needs_input
-
-
-def _get_decompressor(compress_type):
-""",
- ),
- (
- """ else:
- data = self._read2(n)
-
- if self._compress_type == ZIP_STORED:
-""",
- """ elif self._compress_type == ZIP_STORED:
- data = self._read2(n)
- else:
- # bzip2/lzma/zstd: a bounded decompress() call may leave input
- # buffered inside the decompressor; drain that before reading more.
- if _decompressor_needs_input(self._decompressor):
- data = self._read2(n)
- else:
- data = b''
-
- if self._compress_type == ZIP_STORED:
-""",
- ),
- (
- """ else:
- data = self._decompressor.decompress(data)
- self._eof = self._decompressor.eof or self._compress_left <= 0
-""",
- """ else:
- # Bound the output of a single decompress() call (mirroring the
- # DEFLATE path above) so that a small compressed member cannot
- # expand into one unbounded read.
- data = self._decompressor.decompress(data, max(n, self.MIN_READ_SIZE))
- self._eof = (self._decompressor.eof or
- self._compress_left <= 0 and
- _decompressor_needs_input(self._decompressor))
-""",
- ),
-)
-
-# One marker per hunk in ``_PATCHES``, in the same order, each absent from the
-# unpatched 3.14.7 source. ``main()`` requires all of them in the written file,
-# so a hunk dropped from ``_PATCHES`` fails the build even when
-# ``verify_runtime()`` cannot observe its absence: the ``_read1()`` branch-chain
-# restructure has no public-API-visible effect on its own, because ``_read2()``
-# clamps the compressed input it hands to the decompressor.
-_PATCH_MARKERS = (
- "def _needs_input(self):",
- "result = self._decomp.decompress(data, max_length)",
- "def _decompressor_needs_input(decompressor):",
- "if _decompressor_needs_input(self._decompressor):",
- "self._decompressor.decompress(data, max(n, self.MIN_READ_SIZE))",
-)
-
-
-def default_target() -> Path:
- """Return the active interpreter's ``zipfile`` source path."""
- return Path(sysconfig.get_path("stdlib")) / "zipfile" / "__init__.py"
-
-
-def apply_patch(target: Path) -> bool:
- """Apply the reviewed backport to *target* and return whether it changed."""
- source = target.read_text(encoding="utf-8")
- if all(marker in source for marker in _PATCH_MARKERS):
- return False
-
- patched = source
- for old, new in _PATCHES:
- occurrences = patched.count(old)
- if occurrences != 1:
- raise RuntimeError(
- f"refusing to patch {target}: expected one reviewed source anchor, "
- f"found {occurrences}"
- )
- patched = patched.replace(old, new, 1)
-
- compile(patched, str(target), "exec")
- target.write_text(patched, encoding="utf-8")
- return True
-
-
-def _incompressible(size: int) -> bytes:
- """Return *size* deterministic, effectively incompressible bytes.
-
- A SHA-256 chain rather than ``random`` so the build check is reproducible
- (and so it does not trip lint rules about non-cryptographic RNGs).
- """
- out = bytearray()
- digest = b"cve-2026-15310"
- while len(out) < size:
- digest = hashlib.sha256(digest).digest()
- out += digest
- return bytes(out[:size])
-
-
-def verify_runtime() -> None:
- """Check that one small read stays bounded and that content round-trips.
-
- Two payloads are used per codec. A highly compressible one proves the
- bound: on an unpatched interpreter a single ``_read1(100)`` call expands
- the whole member. An incompressible one proves the bounded path still
- returns every byte -- a correctly patched ``_read1()`` may legitimately
- return ``b''`` mid-stream while the decompressor drains buffered input,
- so the drain loop keys off ``_eof`` rather than on a falsy chunk.
-
- STORED and DEFLATE are included because the fourth hunk restructures the
- ``_read1()`` branch chain that those codecs also flow through.
-
- This always exercises the ambient interpreter's ``zipfile``; ``--target``
- selects what is patched, not what is verified.
- """
- import zipfile
-
- compressible = b"\0" * (4 * 1024 * 1024)
- incompressible = _incompressible(100 * 1024)
-
- compressions = [zipfile.ZIP_STORED, zipfile.ZIP_DEFLATED]
- # zipfile sets these attributes to None when the codec's support module is
- # missing. bz2 and lzma are the codecs this CVE is about, so a base image
- # without them cannot verify the fix at all: say so plainly instead of
- # letting writestr() raise something that reads like a broken patch.
- for module_name, compression in (
- ("bz2", zipfile.ZIP_BZIP2),
- ("lzma", zipfile.ZIP_LZMA),
- ):
- if getattr(zipfile, module_name, None) is None:
- raise RuntimeError(
- f"codec unavailable: this interpreter's zipfile has no "
- f"{module_name} support, so the bounded-decompression "
- "backport cannot be verified for a codec the CVE covers "
- "(base-image problem, not a failed patch)"
- )
- compressions.append(compression)
- # zstd is genuinely optional: unlike bz2/lzma it is absent from older
- # interpreters entirely, so its absence is skipped rather than fatal.
- if getattr(zipfile, "zstd", None) is not None:
- compressions.append(zipfile.ZIP_ZSTANDARD)
-
- for compression in compressions:
- for payload in (compressible, incompressible):
- archive = io.BytesIO()
- with zipfile.ZipFile(
- archive, "w", compression=compression
- ) as output:
- output.writestr("payload", payload)
- raw = archive.getvalue()
-
- # Bound: a single small read must not exceed MIN_READ_SIZE.
- with zipfile.ZipFile(io.BytesIO(raw)) as incoming:
- with incoming.open("payload") as member:
- first = member._read1(100)
- if len(first) > member.MIN_READ_SIZE:
- raise RuntimeError(
- "zipfile decompression output is not bounded by "
- f"MIN_READ_SIZE (compression={compression}, "
- f"one read returned {len(first)} bytes)"
- )
-
- # Correctness: the ordinary read path is lossless.
- with zipfile.ZipFile(io.BytesIO(raw)) as incoming:
- with incoming.open("payload") as member:
- if member.read() != payload:
- raise RuntimeError(
- "zipfile.read() changed archive contents "
- f"(compression={compression})"
- )
-
- # Correctness: draining in bounded steps is also lossless.
- with zipfile.ZipFile(io.BytesIO(raw)) as incoming:
- with incoming.open("payload") as member:
- chunks = []
- budget = 4 * (len(payload) // member.MIN_READ_SIZE + 16)
- while not member._eof:
- chunks.append(member._read1(100))
- budget -= 1
- if budget < 0:
- raise RuntimeError(
- "zipfile bounded reads do not terminate "
- f"(compression={compression})"
- )
- if b"".join(chunks) != payload:
- raise RuntimeError(
- "zipfile bounded reads changed archive contents "
- f"(compression={compression})"
- )
-
-
-def main() -> None:
- parser = argparse.ArgumentParser()
- parser.add_argument(
- "--target",
- type=Path,
- default=default_target(),
- help=(
- "zipfile source to rewrite (apply-only: --verify-runtime always "
- "imports the ambient interpreter's zipfile, so pointing --target "
- "at another interpreter's stdlib patches that copy but still "
- "verifies this one; import the patched copy via PYTHONPATH in a "
- "subprocess to check it)"
- ),
- )
- parser.add_argument("--verify-runtime", action="store_true")
- args = parser.parse_args()
-
- if args.verify_runtime:
- verify_runtime()
- return
-
- apply_patch(args.target)
-
- # Fail the build if any hunk is missing from the written file. Runtime
- # verification alone cannot catch every partial application: see the
- # comment on _PATCH_MARKERS.
- written = args.target.read_text(encoding="utf-8")
- missing = [marker for marker in _PATCH_MARKERS if marker not in written]
- if missing:
- raise RuntimeError(
- f"incomplete backport in {args.target}: the patched file is "
- f"missing {len(missing)} of {len(_PATCH_MARKERS)} reviewed hunks "
- f"{missing}"
- )
-
- subprocess.run(
- [sys.executable, str(Path(__file__).resolve()), "--verify-runtime"],
- check=True,
- )
-
-
-if __name__ == "__main__":
- main()
diff --git a/tests/ci/standalone/test_semgrep_report.py b/tests/ci/standalone/test_semgrep_report.py
index f3e1ed4af5..5e6782d65a 100644
--- a/tests/ci/standalone/test_semgrep_report.py
+++ b/tests/ci/standalone/test_semgrep_report.py
@@ -817,7 +817,7 @@ class ReportTests(unittest.TestCase):
the C JSON decoder bounds nesting by a fixed recursion count, so it
raises ``RecursionError``. From 3.14 the bound is the C stack
size, so with a large or unlimited ``ulimit -s`` (as in the CI test
- image, Python 3.14.7) the 100,000-deep list parses and is then
+ image, Python 3.14.8) the 100,000-deep list parses and is then
rejected as not being SARIF. No depth is deterministic there, since
an unlimited stack parses any depth that fits in memory. Either way
the validator must report an error and exit 1; that is what matters.
diff --git a/tests/deployment/test_cpython_zipfile_backport.py b/tests/deployment/test_cpython_zipfile_backport.py
deleted file mode 100644
index 53eab32dde..0000000000
--- a/tests/deployment/test_cpython_zipfile_backport.py
+++ /dev/null
@@ -1,146 +0,0 @@
-"""Contract tests for the temporary CPython zipfile security backport.
-
-The regression must hold on hosts where the interpreter's own ``zipfile`` has
-already been patched (the ``ldr-test`` Docker stage inherits ``builder-base``,
-which runs the patch script) as well as on unpatched hosts. So the test never
-trusts the ambient stdlib: it copies the module, reverse-applies the reviewed
-hunks to synthesise a guaranteed-unpatched baseline, and drives every
-assertion against that copy through ``PYTHONPATH``.
-"""
-
-# allow: no-sut-import — the system under test is the build-time patch script
-# scripts/patch_cpython_zipfile_cve_2026_15310.py, which rewrites the
-# interpreter's stdlib during the Docker build; it is deliberately outside the
-# local_deep_research package so the image can be patched before install.
-
-from __future__ import annotations
-
-import importlib.util
-import os
-import shutil
-import subprocess
-import sys
-import zipfile
-from pathlib import Path
-
-REPO_ROOT = Path(__file__).resolve().parents[2]
-SCRIPT = REPO_ROOT / "scripts" / "patch_cpython_zipfile_cve_2026_15310.py"
-
-_SPEC = importlib.util.spec_from_file_location(
- "patch_cpython_zipfile_cve_2026_15310", SCRIPT
-)
-assert _SPEC is not None and _SPEC.loader is not None
-patcher = importlib.util.module_from_spec(_SPEC)
-_SPEC.loader.exec_module(patcher)
-
-apply_patch = patcher.apply_patch
-
-# The hunk that actually bounds the decompressor's per-call output. Removing
-# it must make verify_runtime() fail, otherwise the check cannot catch a
-# revert of the security fix.
-BOUND_HUNK_MARKER = (
- "self._decompressor.decompress(data, max(n, self.MIN_READ_SIZE))"
-)
-
-
-def _copy_stdlib_zipfile(destination: Path) -> Path:
- """Copy the live ``zipfile`` package into *destination* and return it."""
- source_package = Path(zipfile.__file__).resolve().parent
- copied_package = destination / "zipfile"
- shutil.copytree(source_package, copied_package)
- return copied_package / "__init__.py"
-
-
-def _unapply_patch(target: Path) -> bool:
- """Reverse the reviewed hunks so *target* is a known-unpatched baseline.
-
- Returns whether anything changed (False when the source package the host
- provided was not patched to begin with).
- """
- source = target.read_text(encoding="utf-8")
- if not all(marker in source for marker in patcher._PATCH_MARKERS):
- return False
-
- reverted = source
- for old, new in patcher._PATCHES:
- assert reverted.count(new) == 1, f"expected one anchor for {new!r}"
- reverted = reverted.replace(new, old, 1)
- compile(reverted, str(target), "exec")
- target.write_text(reverted, encoding="utf-8")
- return True
-
-
-def _apply_hunks(target: Path, hunks) -> None:
- """Apply an explicit subset of ``_PATCHES`` to *target*."""
- patched = target.read_text(encoding="utf-8")
- for old, new in hunks:
- assert patched.count(old) == 1, f"expected one anchor for {old!r}"
- patched = patched.replace(old, new, 1)
- compile(patched, str(target), "exec")
- target.write_text(patched, encoding="utf-8")
-
-
-def _run_runtime_check(package_root: Path) -> subprocess.CompletedProcess[str]:
- """Run ``verify_runtime()`` against the ``zipfile`` under *package_root*."""
- env = os.environ.copy()
- env["PYTHONPATH"] = os.pathsep.join(
- (str(package_root), env.get("PYTHONPATH", ""))
- )
- return subprocess.run(
- [
- sys.executable,
- "-c",
- (
- "import importlib.util, sys;"
- "spec = importlib.util.spec_from_file_location("
- "'patcher', sys.argv[1]);"
- "mod = importlib.util.module_from_spec(spec);"
- "spec.loader.exec_module(mod);"
- "mod.verify_runtime()"
- ),
- str(SCRIPT),
- ],
- env=env,
- capture_output=True,
- text=True,
- check=False,
- )
-
-
-def test_backport_bounds_zipfile_decompression(tmp_path: Path) -> None:
- baseline = tmp_path / "baseline"
- baseline.mkdir()
- module = _copy_stdlib_zipfile(baseline)
- _unapply_patch(module)
-
- # Negative control: the unpatched module has the unbounded read, so the
- # runtime check must reject it. This is synthesised rather than taken
- # from the ambient interpreter, which may already be patched.
- unpatched = _run_runtime_check(baseline)
- assert unpatched.returncode != 0, unpatched.stdout + unpatched.stderr
- assert "MIN_READ_SIZE" in unpatched.stderr
-
- assert apply_patch(module) is True
- assert apply_patch(module) is False
- patched = _run_runtime_check(baseline)
- assert patched.returncode == 0, patched.stdout + patched.stderr
-
-
-def test_runtime_check_catches_a_revert_of_the_bound(tmp_path: Path) -> None:
- """Dropping the bounding hunk must reopen the finding, not pass quietly."""
- without_bound = tmp_path / "without-bound"
- without_bound.mkdir()
- module = _copy_stdlib_zipfile(without_bound)
- _unapply_patch(module)
-
- hunks = [
- hunk for hunk in patcher._PATCHES if BOUND_HUNK_MARKER not in hunk[1]
- ]
- assert len(hunks) == len(patcher._PATCHES) - 1, (
- "the bounding hunk is no longer identifiable in _PATCHES"
- )
- _apply_hunks(module, hunks)
-
- reverted = _run_runtime_check(without_bound)
- assert reverted.returncode != 0, reverted.stdout + reverted.stderr
- assert "MIN_READ_SIZE" in reverted.stderr
diff --git a/tests/deployment/test_deployment_surface_contracts.py b/tests/deployment/test_deployment_surface_contracts.py
index 4c7a5e8b86..af694756d6 100644
--- a/tests/deployment/test_deployment_surface_contracts.py
+++ b/tests/deployment/test_deployment_surface_contracts.py
@@ -10,8 +10,11 @@ on, cross-checking every documented claim against the constant in ``src/``
that actually implements it.
Nothing here builds or runs a container: these are static contracts over
-shipped files plus one execution-verified check of the uvicorn launch
-kwargs (``TestRunCommand::test_uvicorn_launch_kwargs``).
+shipped files plus two execution-verified checks: the uvicorn launch
+kwargs (``TestRunCommand::test_uvicorn_launch_kwargs``) and the Python
+code of the Dockerfile's pyexpat checks, run against a stub ``pyexpat`` as
+a control on the exact check line that ``TestPyexpatMinimumCheck``
+accepts.
Confirmed defects are pinned with ``xfail(strict=True)`` rather than
deleted, so the suite stays green today and turns red the moment someone
@@ -25,12 +28,15 @@ import ast
import json
import re
import shutil
+import subprocess
+import sys
from pathlib import Path
from types import SimpleNamespace
import pytest
import yaml
from defusedxml import ElementTree as DefusedET
+from packaging.version import Version
from local_deep_research.security.file_upload_validator import (
_DEFAULT_MAX_FILE_SIZE_MB,
@@ -45,8 +51,6 @@ COMPOSE_UNRAID = REPO_ROOT / "docker-compose.unraid.yml"
COMPOSE_GPU = REPO_ROOT / "docker-compose.gpu.override.yml"
ENTRYPOINT = REPO_ROOT / "scripts" / "ldr_entrypoint.sh"
DOCKER_TESTS_YML = REPO_ROOT / ".github" / "workflows" / "docker-tests.yml"
-ZIPFILE_PATCH_SCRIPT_REL = "scripts/patch_cpython_zipfile_cve_2026_15310.py"
-ZIPFILE_PATCH_SCRIPT = REPO_ROOT / ZIPFILE_PATCH_SCRIPT_REL
COOKIECUTTER_DIR = REPO_ROOT / "cookiecutter-docker"
UNRAID_TEMPLATE = REPO_ROOT / "unraid-templates" / "local-deep-research.xml"
REVERSE_PROXY_DOC = REPO_ROOT / "docs" / "deployment" / "reverse-proxy.md"
@@ -132,52 +136,307 @@ DOCKER_INSTRUCTIONS = {
# ---------------------------------------------------------------------------
-def _logical_lines(text: str) -> list[str]:
- """Join backslash continuations; drop comment-only lines."""
- out: list[str] = []
- buf = ""
- for raw in text.splitlines():
- stripped = raw.strip()
- if stripped.startswith("#"):
- # Docker strips comment lines even inside a continuation.
- continue
- if not stripped and not buf:
+# BuildKit's line continuation for the default escape character, a
+# backslash: lineContinuationRegex in frontend/dockerfile/parser/parser.go,
+# which BuildKit replaces with its first group. A backslash followed by
+# nothing but spaces and tabs continues the line, unless a backslash comes
+# right before it. The backslash and those spaces and tabs are removed, and
+# the character before the backslash is kept; a line of only a backslash,
+# spaces and tabs becomes empty. BuildKit releases before v0.8.0 continued a
+# line at any final backslash, an escaped one too.
+LINE_CONTINUATION = re.compile(r"([^\\])\\[ \t]*\Z|\A\\[ \t]*\Z")
+
+
+def _trim_continuation(line: str) -> tuple[str, bool]:
+ """Remove a LINE_CONTINUATION from ``line``, as BuildKit does.
+
+ The second value is True when ``line`` has none, so it ends its
+ logical line.
+ """
+ match = LINE_CONTINUATION.search(line)
+ if match is None:
+ return line, True
+ return line[: match.start()] + (match.group(1) or ""), False
+
+
+def _numbered_logical_lines(text: str) -> list[tuple[int, str]]:
+ """Return ``_logical_lines(text)``, each with the number of its first line."""
+ lines = text.split("\n")
+ if lines[-1] == "":
+ lines.pop()
+ logical: list[tuple[int, str]] = []
+ index = 0
+ while index < len(lines):
+ number = index + 1
+ line = lines[index].rstrip("\r\n").lstrip(" \t")
+ index += 1
+ if line.startswith("#"):
continue
- buf = f"{buf} {stripped}" if buf else stripped
- if buf.endswith("\\"):
- buf = buf[:-1].rstrip()
+ line, ended = _trim_continuation(line)
+ if ended and not line:
continue
- out.append(buf)
- buf = ""
- if buf:
- out.append(buf)
- return out
+ parts = [line]
+ while not ended and index < len(lines):
+ continuation = lines[index].rstrip("\r\n")
+ index += 1
+ content = continuation.lstrip(" \t")
+ if not content or content.startswith("#"):
+ continue
+ continuation, ended = _trim_continuation(continuation)
+ parts.append(continuation)
+ logical.append((number, "".join(parts)))
+ return logical
+
+
+def _logical_lines(text: str) -> list[str]:
+ """Join a Dockerfile's lines into logical lines the way BuildKit does.
+
+ This is the line loop of Parse in BuildKit's
+ frontend/dockerfile/parser/parser.go, for the default escape character,
+ a backslash. A line ends at a line feed and loses its trailing carriage
+ returns. The first line of a logical line loses its leading spaces and
+ tabs. It is skipped when it is then empty, and when it starts with
+ ``#``: it is a comment then, even if it ends in a backslash. While a
+ line ends in a LINE_CONTINUATION, the continuation is removed and the
+ next line is appended as it is: it keeps its leading whitespace, and
+ nothing is put between the two. A continuation line is skipped, without
+ ending the logical line, when it holds nothing but spaces and tabs or
+ its first other character is ``#``.
+
+ For a Dockerfile that misread_dockerfile_syntax accepts, each logical
+ line is, byte for byte, the line that BuildKit splits into an
+ instruction and its arguments. BuildKit also reads whitespace other than
+ spaces and tabs, a byte order mark, heredocs, an ``escape`` parser
+ directive and a ``syntax`` parser directive, which hands the file to
+ another frontend, in ways this function does not;
+ misread_dockerfile_syntax refuses each of them.
+ """
+ return [line for _, line in _numbered_logical_lines(text)]
+
+
+def _instruction(line: str) -> tuple[str, str] | None:
+ """Return ``(INSTRUCTION, argument)`` for a logical line, or None.
+
+ As in BuildKit's splitCommand, the instruction is the first word of the
+ trimmed line. The argument is the rest of the line, trimmed, with any
+ ``--flags`` left in. It is empty for a line of one word, such as a CMD
+ or ENTRYPOINT without an argument, which docker build accepts. A line
+ whose first word is not in DOCKER_INSTRUCTIONS gives None.
+ """
+ match = re.fullmatch(r"([A-Za-z]+)(?:\s+(.*))?", line.strip())
+ if not match:
+ return None
+ keyword = match.group(1).upper()
+ if keyword not in DOCKER_INSTRUCTIONS:
+ return None
+ return keyword, (match.group(2) or "").strip()
def parse_dockerfile(path: Path) -> list[tuple[str, str]]:
- """Return ``[(INSTRUCTION, argument), ...]`` for a Dockerfile."""
- result: list[tuple[str, str]] = []
- for line in _logical_lines(path.read_text(encoding="utf-8")):
- match = re.match(r"([A-Za-z]+)\s+(.*)", line)
- if not match:
- continue
- keyword = match.group(1).upper()
- if keyword not in DOCKER_INSTRUCTIONS:
+ """Return ``[(INSTRUCTION, argument), ...]`` for a Dockerfile.
+
+ A logical line that is not an instruction (``_instruction``) is left
+ out; misread_dockerfile_syntax reports it.
+ """
+ # read_text() would turn a lone "\r" into a line break; Docker does not.
+ text = path.read_bytes().decode("utf-8")
+ return [
+ instruction
+ for line in _logical_lines(text)
+ if (instruction := _instruction(line)) is not None
+ ]
+
+
+# Dockerfile lines and the logical lines that docker build 29.1.3 (classic
+# builder) printed for them, in its `Step N/M :` lines, after `FROM scratch`.
+DOCKER_LOGICAL_LINES = [
+ pytest.param(
+ 'LABEL x="a \\\n b"\n',
+ ['LABEL x="a b"'],
+ id="continuation-line-keeps-its-leading-whitespace",
+ ),
+ pytest.param(
+ 'LABEL x="a\\\nb"\n', ['LABEL x="ab"'], id="nothing-put-between"
+ ),
+ pytest.param(
+ 'LABEL x="a\\ \t\nb"\n',
+ ['LABEL x="ab"'],
+ id="spaces-and-tabs-after-the-backslash",
+ ),
+ pytest.param(
+ 'SHE\\\nLL ["/bin/true"]\n',
+ ['SHELL ["/bin/true"]'],
+ id="instruction-split-by-a-continuation",
+ ),
+ pytest.param(
+ 'LABEL x="a \\\n # b \\\n# c\n d"\n',
+ ['LABEL x="a d"'],
+ id="comment-lines-inside-a-continuation",
+ ),
+ pytest.param(
+ 'LABEL x="a \\\n\n \t\n b"\n',
+ ['LABEL x="a b"'],
+ id="blank-lines-inside-a-continuation",
+ ),
+ pytest.param(
+ 'LABEL x="a \\\n\\\n b"\n',
+ ['LABEL x="a b"'],
+ id="line-of-only-a-backslash",
+ ),
+ pytest.param(
+ "LABEL x=a\\\\\nLABEL y=b\n",
+ ["LABEL x=a\\\\", "LABEL y=b"],
+ id="escaped-backslash-ends-the-line",
+ ),
+ pytest.param(
+ "# a \\\n \tLABEL x=a\n",
+ ["LABEL x=a"],
+ id="comment-ending-in-a-backslash-then-indented-line",
+ ),
+ pytest.param(
+ 'LABEL x="a \\\r\n b"\r\n',
+ ['LABEL x="a b"'],
+ id="carriage-return-before-each-line-feed",
+ ),
+]
+
+
+def _flag_words(argument: str) -> list[str]:
+ """Return the leading ``--flags`` of an instruction's ``argument``.
+
+ They are the words of ``argument`` before its first word that does not
+ start with ``--``, such as ``--platform=$BUILDPLATFORM`` in a FROM line.
+ Every reader of flags in this module takes them from here: _from_words,
+ unreviewed_source_images, parse_healthcheck, secret_mounts and
+ misread_dockerfile_syntax. BuildKit reads the same words as the
+ instruction's flags (extractBuilderFlags in
+ frontend/dockerfile/parser/split_command.go) when none of them holds a
+ character that is_misread_word refuses, which misread_dockerfile_syntax
+ ensures, and none is a word of just ``--``. BuildKit reads no flags
+ after such a word, while this function returns it and reads on. Every
+ flag that BuildKit reads is then still among the words returned, so
+ unreviewed_source_images and secret_mounts can only report more, and
+ parse_healthcheck reads ``--`` as a flag with an empty name, which
+ test_healthcheck_shape refuses. In a FROM line, BuildKit reads a word
+ that starts with ``--`` after ``--`` as an argument, and docker build
+ 29.1.3 (classic builder) fails on such a line: an image name cannot
+ start with ``-``, and FROM takes one argument or three.
+ """
+ words = []
+ for word in argument.split():
+ if not word.startswith("--"):
+ break
+ words.append(word)
+ return words
+
+
+def _from_words(argument: str) -> list[str]:
+ """Return the words of a FROM argument after its leading ``--flags``.
+
+ The flags are _flag_words(argument), and the other words are the
+ arguments of the instruction.
+ """
+ return argument.split()[len(_flag_words(argument)) :]
+
+
+def _unwrap_onbuild(keyword: str, argument: str) -> tuple[str, str, str]:
+ """Return ``(label, keyword, argument)`` for an instruction.
+
+ An ONBUILD instruction whose argument is an instruction gives that
+ trigger, labelled ``ONBUILD <keyword>``. Any other instruction is
+ returned as it is, labelled with its keyword.
+
+ Docker does not run a trigger in the stage that holds it. That stage's
+ image keeps the trigger's text, values included, in its list of
+ ONBUILD triggers, and Docker runs the trigger in each image built from
+ that image. It also runs it in each stage built from that stage: the
+ classic builder does, and so does BuildKit since v0.17.1, which adds
+ the ONBUILD lines of a stage's base stage to the stage's commands
+ (toDispatchState in frontend/dockerfile/dockerfile2llb/convert.go).
+ docker build 29.1.3 (classic builder) runs a stage's ONBUILD ARG, ENV,
+ RUN, SHELL, USER, CMD, ENTRYPOINT, STOPSIGNAL and HEALTHCHECK triggers
+ in a stage built from it, and none of them in the stage itself, whose
+ settings they leave as they are.
+
+ The readers of flags, sources, secret mounts, build arguments and
+ credential names read triggers through this function, since these
+ matter in whichever stage a trigger runs in, and a credential also in
+ the text that an image keeps: misread_dockerfile_syntax,
+ unreviewed_source_images, secret_mounts, build_arg_names and
+ credential_bearing_args_and_env. The readers of what runs in a root
+ stage, and of the settings of the image that ldr ships, read a stage's
+ own instructions only. No trigger in the Dockerfile runs in a root
+ stage, which is built from the base image rather than from a stage,
+ and ldr is a root stage (test_all_root_stages_use_patched_python). So a
+ check in an ONBUILD RUN does not count (stages_checking_the_interpreter,
+ stages_checking_pyexpat), an ONBUILD SHELL in a root stage does not
+ change the shell that its checks run with, an ONBUILD CMD, ENTRYPOINT,
+ USER, HEALTHCHECK or STOPSIGNAL in ldr leaves that setting of the image
+ that ships as it is, and an ONBUILD RUN in ldr does not run in it. The
+ image that ships keeps a trigger of ldr in its list, though, and runs
+ it in each image built from that image, so
+ test_the_shipped_stage_holds_no_onbuild_trigger requires that ldr hold
+ none. Docker refuses an ONBUILD FROM, so the stage helpers read FROM
+ lines only.
+ """
+ if keyword == "ONBUILD" and (trigger := _instruction(argument)):
+ return f"ONBUILD {trigger[0]}", *trigger
+ return keyword, keyword, argument
+
+
+def dockerfile_stage_names(instructions: list[tuple[str, str]]) -> list[str]:
+ """Return the name of each build stage, in the order of the FROM lines.
+
+ As in BuildKit's parseBuildStageName, a FROM line names its stage only
+ when it has three words after its leading ``--flags`` (_from_words) and
+ the second is ``AS`` in any case; the third is the name. BuildKit
+ compares the second word with ``AS``, and lowercases the name, by Go's
+ Unicode case rules, which agree with str.lower() only in ASCII;
+ misread_dockerfile_syntax refuses a FROM line with a character outside
+ ASCII (is_misread_word). A stage without a name is named ``#<index>``,
+ from the index among the stages, counting from 0, by which Docker
+ refers to it in ``COPY --from=<index>``. Docker refuses a stage name
+ that does not start with a letter, so no named stage has that name. A
+ stage without a name is therefore never keyed by the image it is built
+ from, which would make a later FROM line or ``--from`` that names that
+ image read as built from or copying from that stage
+ (stage_names_before), and never merged into a named stage.
+ """
+ names = []
+ for keyword, argument in instructions:
+ if keyword != "FROM":
continue
- result.append((keyword, match.group(2).strip()))
- return result
+ words = _from_words(argument)
+ if len(words) == 3 and words[1].lower() == "as":
+ names.append(words[2])
+ else:
+ names.append(f"#{len(names)}")
+ return names
+
+
+def stage_built_without_a_target(instructions: list[tuple[str, str]]) -> str:
+ """Return the stage that ``docker build`` builds without ``--target``.
+
+ That is the last stage of the Dockerfile, named as in
+ dockerfile_stage_names.
+ """
+ return dockerfile_stage_names(instructions)[-1]
def dockerfile_stages(
instructions: list[tuple[str, str]],
) -> dict[str, list[tuple[str, str]]]:
- """Group instructions by build stage name (``FROM ... AS <name>``)."""
+ """Group instructions by build stage, under dockerfile_stage_names.
+
+ Two stages with one name are merged; misread_dockerfile_syntax refuses
+ them.
+ """
+ names = iter(dockerfile_stage_names(instructions))
stages: dict[str, list[tuple[str, str]]] = {}
current: str | None = None
for keyword, argument in instructions:
if keyword == "FROM":
- named = re.search(r"\bAS\s+(\S+)\s*$", argument, re.IGNORECASE)
- current = named.group(1) if named else argument.split()[0]
+ current = next(names)
stages.setdefault(current, [])
continue
if current is not None:
@@ -187,80 +446,214 @@ def dockerfile_stages(
def dockerfile_stage_parents(
instructions: list[tuple[str, str]],
-) -> dict[str, str]:
- """Return ``{stage name: the image or stage its FROM names}``.
-
- ``dockerfile_stages()`` drops the ``FROM`` line itself, so this is its
- companion for questions about what a stage is built *from*. Leading
- ``--flags`` (e.g. ``--platform=$BUILDPLATFORM``) are skipped the same
- way the COPY token filter does below, so a flagged FROM still resolves
- to the image/stage token rather than the flag itself.
+) -> list[tuple[str, str]]:
+ """Return ``(stage name, the word its FROM line builds it from)``.
+
+ There is one pair for each FROM line, in their order, with the stage
+ named as in dockerfile_stage_names. ``dockerfile_stages()`` drops the
+ ``FROM`` line itself, so this is its companion for questions about what
+ a stage is built *from*: the first word of its FROM line after the
+ leading ``--flags`` (_from_words). stages_from_base_image tells whether
+ that word names an image or a stage.
"""
- parents: dict[str, str] = {}
- for keyword, argument in instructions:
- if keyword != "FROM":
- continue
- named = re.search(r"\bAS\s+(\S+)\s*$", argument, re.IGNORECASE)
- tokens = [t for t in argument.split() if not t.startswith("--")]
- parent = tokens[0] if tokens else argument.split()[0]
- parents[named.group(1) if named else parent] = parent
+ froms = [
+ argument for keyword, argument in instructions if keyword == "FROM"
+ ]
+ parents = []
+ for name, argument in zip(
+ dockerfile_stage_names(instructions), froms, strict=True
+ ):
+ words = _from_words(argument)
+ parents.append((name, words[0] if words else argument))
return parents
+def stage_names_before(names: list[str], index: int) -> set[str]:
+ """Return the names of the stages before stage ``index``, lowercased.
+
+ ``names`` is dockerfile_stage_names. These are the only stages that a
+ FROM line, or a source of COPY or RUN, in stage ``index`` can use as a
+ stage (stages_from_base_image, unreviewed_source_images). BuildKit
+ lowercases a stage's name when it reads the FROM line
+ (parseBuildStageName in frontend/dockerfile/instructions/parse.go).
+ """
+ return {name.lower() for name in names[:index]}
+
+
def stages_from_base_image(
instructions: list[tuple[str, str]],
) -> tuple[str, list[str]]:
"""Return ``(base image ref, stages built directly from it)``.
- The base image is derived rather than hardcoded: it is the single ``FROM``
- target that does not name another stage in this Dockerfile.
+ The base image is derived rather than hardcoded: it is the single image
+ that the FROM lines build stages from. A FROM line builds its stage from
+ a stage when its first word after the ``--flags``
+ (dockerfile_stage_parents) is the lowercased name of a stage before it
+ (stage_names_before), and from an image of that name otherwise. Docker
+ reads such a word as that stage too: BuildKit's addState in
+ frontend/dockerfile/dockerfile2llb/convert.go looks the word up, after
+ expanding the build arguments in it, among the lowercased names of the
+ stages before it, and only then adds the stage's own name, and the
+ classic builder looks it up, in any case, among the stages it has
+ already built. So the stage's own name and the names of later stages
+ name images: ``FROM python AS python`` builds from the image
+ ``python``. A word with a build argument is read as an image here, and
+ so is a stage name in capitals, which BuildKit reads as an image name
+ and refuses. Every FROM line counts, with or without a name
+ (dockerfile_stage_names), so a stage built from another image fails the
+ assertion, whatever its name.
"""
parents = dockerfile_stage_parents(instructions)
- external = sorted({p for p in parents.values() if p not in parents})
+ names = [name for name, _ in parents]
+ built_from_images = [
+ (name, parent)
+ for index, (name, parent) in enumerate(parents)
+ if parent not in stage_names_before(names, index)
+ ]
+ external = sorted({image for _, image in built_from_images})
assert len(external) == 1, (
f"expected exactly one external base image, found {external}"
)
- base = external[0]
- return base, [name for name, parent in parents.items() if parent == base]
+ return external[0], [name for name, _ in built_from_images]
+
+# The images that COPY and RUN in the Dockerfile may take files from besides
+# its own earlier stages (unreviewed_source_images). Each is a reviewed
+# decision, as the base image is: the build pulls it, and what COPY or RUN
+# takes from it can reach the image that ships without the checks that the
+# root stages run. The Dockerfile takes files only from its own stages.
+REVIEWED_SOURCE_IMAGES: frozenset[str] = frozenset()
-def copy_argument_tokens(argument: str) -> list[str] | None:
- """Return a ``COPY`` argument's source/dest tokens, flags stripped.
- Handles ordinary shell-form (``COPY [--flag ...] src... dst``) plus the
- JSON exec-form (``COPY [--flag ...] ["src", ..., "dst"]``). Returns
- ``None`` for JSON-form text that fails to parse as a JSON list, so
- callers can fail loudly instead of silently matching nothing.
+def unreviewed_source_images(
+ instructions: list[tuple[str, str]],
+ reviewed: frozenset[str] = REVIEWED_SOURCE_IMAGES,
+) -> list[str]:
+ """Return the sources of COPY and RUN that are not earlier stages.
+
+ Besides a FROM line (stages_from_base_image), a Dockerfile names an
+ image or a stage in the ``--from`` flag of COPY and in the ``from``
+ field of a ``--mount`` flag of RUN, which COPY and RUN take files from.
+ An ONBUILD COPY or ONBUILD RUN runs in a stage built from the stage
+ that holds it; it is read here as an instruction of that stage.
+ BuildKit's ADD has no ``--from`` flag, and misread_dockerfile_syntax
+ refuses a ``syntax`` parser directive, which names the image of a
+ frontend.
+
+ A source counts as a stage when it is, in any case, the name of a stage
+ before the instruction's stage (stage_names_before), or, in COPY, the
+ index of one in decimal digits; Docker reads it as that stage. Every
+ other source is reported, unless it is in ``reviewed``. BuildKit looks
+ a name up among all the stages, in any case, and a COPY number among
+ their indexes (toCommand in
+ frontend/dockerfile/dockerfile2llb/convert.go and detectRunMount in
+ convert_runmount.go). It fails the build of a stage whose source is
+ that stage itself or a later one, and it reads every other source as an
+ image, a number in a ``from`` field too. The classic builder looks a
+ COPY name up, in any case, only among the stages it has already built,
+ so it reads the name of the instruction's own stage or of a later one
+ as an image; it refuses the index of its own stage or of a later one,
+ and RUN ``--mount``. A few of the reported sources name no image, such
+ as an empty one; reporting them too only refuses more.
+
+ The flags are _flag_words of the argument. A flag for which
+ is_misread_word is true, one with a quote, a backslash or a character
+ outside ASCII, is reported instead of read, so that this function also
+ refuses it without misread_dockerfile_syntax, which refuses it as well.
+ BuildKit reads no flags after a word of just ``--``; this function
+ reads on, which also only refuses more. A ``from`` field is a
+ comma-separated field of a ``--mount`` value whose key is ``from`` in
+ any case, as BuildKit reads it (parseMount in
+ frontend/dockerfile/instructions/commands_runmount.go).
"""
- remainder = argument
- while remainder.startswith("--"):
- _, _, rest = remainder.partition(" ")
- remainder = rest.lstrip()
- if remainder.startswith("["):
- try:
- parsed = json.loads(remainder)
- except (json.JSONDecodeError, ValueError):
- return None
- if not isinstance(parsed, list) or not all(
- isinstance(t, str) for t in parsed
- ):
- return None
- return parsed
- return [t for t in argument.split() if not t.startswith("--")]
+ names = dockerfile_stage_names(instructions)
+ unreviewed = []
+ index, before = -1, set()
+ for keyword, argument in instructions:
+ if keyword == "FROM":
+ index += 1
+ before = stage_names_before(names, index)
+ continue
+ where = f"stage '{names[index]}'" if index >= 0 else "before FROM"
+ label, keyword, argument = _unwrap_onbuild(keyword, argument)
+ for word in _flag_words(argument):
+ if is_misread_word(word):
+ unreviewed.append(
+ f"{where}: {label} {word}: a quote, a backslash or a "
+ "character outside ASCII in a flag"
+ )
+ continue
+ if keyword == "COPY" and word.startswith("--from="):
+ flag, sources = "--from", [word.removeprefix("--from=")]
+ if sources[0].isdigit() and int(sources[0]) < index:
+ continue
+ elif keyword == "RUN" and word.startswith("--mount="):
+ flag = "--mount from"
+ sources = [
+ field[5:]
+ for field in word.removeprefix("--mount=").split(",")
+ if field[:5].lower() == "from="
+ ]
+ else:
+ continue
+ unreviewed += [
+ f"{where}: {label} {flag}={source}"
+ for source in sources
+ if source.lower() not in before and source not in reviewed
+ ]
+ return unreviewed
+
+
+# FROM arguments, each for a stage built from `a`, and the name that
+# dockerfile_stage_names gives the stage. BuildKit names a stage only when
+# the line has three words after its leading --flags and the second is `AS`
+# in any case, and it refuses every other FROM line of more than one word,
+# such as the last two. A regular expression for `AS <name>` at the end of
+# the argument would read a name in the line with a flag that ends in
+# `$AS`, an ARG that Docker expands to the platform, and would key that
+# stage by the image `a`, and one for `AS <name>` at the end of the words
+# after the flags would read a name in the last line.
+FROM_STAGE_NAMES = [
+ pytest.param("a AS one", "one", id="named"),
+ pytest.param("a as one", "one", id="lowercase-as"),
+ pytest.param(
+ "--platform=$BUILDPLATFORM a AS one", "one", id="named-after-a-flag"
+ ),
+ pytest.param("a", "#0", id="unnamed"),
+ pytest.param(
+ "--platform=$AS a", "#0", id="unnamed-after-a-flag-that-ends-in-as"
+ ),
+ pytest.param("a AS one two", "#0", id="four-words"),
+ pytest.param("a b AS one", "#0", id="four-words-that-end-in-as-and-a-name"),
+]
def parse_healthcheck(argument: str) -> dict[str, object] | None:
- """Split a HEALTHCHECK argument into its flags and its exec-form CMD."""
- if argument.strip().upper() == "NONE":
+ """Read a HEALTHCHECK argument into its flags and its command.
+
+ As BuildKit reads it (parseHealthConfig in
+ frontend/dockerfile/parser/line_parsers.go and parseHealthcheck in
+ frontend/dockerfile/instructions/parse.go), the flags are the leading
+ ``--<name>=<value>`` words (_flag_words), the word after them is the
+ type, ``CMD`` or ``NONE`` in any case, and the rest of the argument is
+ the command. ``None`` stands for ``NONE``. A ``CMD`` whose command is a
+ JSON array, which BuildKit requires to hold only strings, runs in exec
+ form, given as ``cmd``. Any other command gives ``shell`` True and no
+ ``cmd``, and so does any other type, which BuildKit refuses. A word of
+ the command that looks like a flag is therefore not a flag, and
+ ``CMD [...]`` inside a shell command is not the exec form.
+ """
+ flags = _flag_words(argument)
+ words = argument.split(maxsplit=len(flags) + 1)[len(flags) :]
+ kind, command = [*words, "", ""][:2]
+ if kind.upper() == "NONE":
return None
- flags = dict(re.findall(r"--([\w-]+)=(\S+)", argument))
- exec_form = re.search(r"\bCMD\s+(\[.*\])\s*$", argument)
- if not exec_form:
- return {"flags": flags, "cmd": None, "shell": True}
+ cmd = _json_or_none(command) if kind.upper() == "CMD" else None
+ exec_form = isinstance(cmd, list)
return {
- "flags": flags,
- "cmd": json.loads(exec_form.group(1)),
- "shell": False,
+ "flags": dict(flag[2:].partition("=")[::2] for flag in flags),
+ "cmd": cmd if exec_form else None,
+ "shell": not exec_form,
}
@@ -416,6 +809,20 @@ class TestParserFloors:
assert {"builder-base", "builder", "ldr-test", "ldr"} <= set(stages)
assert len(stages["ldr"]) >= 10
+ @pytest.mark.parametrize(("argument", "name"), FROM_STAGE_NAMES)
+ def test_a_from_line_names_its_stage_as_buildkit_does(self, argument, name):
+ """Each FROM line starts a stage, named as BuildKit names it.
+
+ A stage without a name is keyed by its index in all three stage
+ helpers, never by the image it is built from, so its lines are not
+ merged into another stage, and stages_from_base_image counts its
+ image as a base image.
+ """
+ instructions = [("FROM", argument), ("RUN", "true")]
+ assert dockerfile_stage_names(instructions) == [name]
+ assert dockerfile_stages(instructions) == {name: [("RUN", "true")]}
+ assert dockerfile_stage_parents(instructions) == [(name, "a")]
+
def test_nginx_parse_floor(self, nginx_directives):
assert len(nginx_directives) >= 25, (
"nginx parser found too few directives -- it is probably "
@@ -460,6 +867,30 @@ class TestParserFloors:
):
assert required in names, f"{required} missing from Unraid parse"
+ @pytest.mark.parametrize(("text", "logical"), DOCKER_LOGICAL_LINES)
+ def test_logical_lines_are_the_lines_docker_reads(self, text, logical):
+ """_logical_lines joins each case into the lines docker build reads."""
+ assert _logical_lines(text) == logical
+
+ def test_an_instruction_without_an_argument_is_kept(self, tmp_path):
+ """Docker builds a CMD or ENTRYPOINT that has no argument.
+
+ The last CMD in a stage is the one the image keeps, so leaving out
+ an empty CMD after ``CMD ["ldr-web"]`` would hide that the
+ container no longer runs ldr-web.
+ """
+ dockerfile = tmp_path / "Dockerfile"
+ dockerfile.write_text(
+ 'FROM python:3-slim AS ldr\nCMD ["ldr-web"]\nCMD\nENTRYPOINT \n',
+ encoding="utf-8",
+ )
+ assert parse_dockerfile(dockerfile) == [
+ ("FROM", "python:3-slim AS ldr"),
+ ("CMD", '["ldr-web"]'),
+ ("CMD", ""),
+ ("ENTRYPOINT", ""),
+ ]
+
def test_dockerfile_parser_is_sensitive(self, tmp_path):
"""Negative control: the parser must lose HEALTHCHECK if removed."""
scratch = tmp_path / "dockerfile-healthcheck-negative-control"
@@ -782,10 +1213,72 @@ class TestProcessAndOwnership:
# 3. The health check
# ---------------------------------------------------------------------------
+# HEALTHCHECK arguments and what parse_healthcheck reads in each, as BuildKit
+# reads it. The flags are only the leading words that start with `--`, so the
+# flag-like word in the exec-form command of the second case is not a flag.
+# The type is the word after the flags, so the third, whose shell command
+# holds `CMD [...]`, is not the exec form, and neither is the last, whose
+# type BuildKit refuses. BuildKit reads the type in any case, and only a
+# JSON array is the exec form.
+HEALTHCHECK_ARGUMENTS = [
+ pytest.param(
+ '--interval=30s --timeout=10s CMD ["python", "-c", "x"]',
+ {
+ "flags": {"interval": "30s", "timeout": "10s"},
+ "cmd": ["python", "-c", "x"],
+ "shell": False,
+ },
+ id="exec-form",
+ ),
+ pytest.param(
+ '--timeout=10s CMD ["python", "-c", "x", "--timeout=99s y"]',
+ {
+ "flags": {"timeout": "10s"},
+ "cmd": ["python", "-c", "x", "--timeout=99s y"],
+ "shell": False,
+ },
+ id="flag-like-word-in-the-command",
+ ),
+ pytest.param(
+ 'CMD echo CMD ["python", "-c", "x"]',
+ {"flags": {}, "cmd": None, "shell": True},
+ id="exec-form-inside-a-shell-command",
+ ),
+ pytest.param(
+ '--retries=3 cmd ["true"]',
+ {"flags": {"retries": "3"}, "cmd": ["true"], "shell": False},
+ id="type-in-lowercase",
+ ),
+ pytest.param("--interval=5s none", None, id="none-after-a-flag"),
+ pytest.param(
+ 'CMD "python"',
+ {"flags": {}, "cmd": None, "shell": True},
+ id="json-string-that-is-not-an-array",
+ ),
+ pytest.param(
+ 'ECHO ["python"]',
+ {"flags": {}, "cmd": None, "shell": True},
+ id="type-other-than-cmd",
+ ),
+]
+
class TestHealthCheck:
"""Does the shipped probe detect an unhealthy app?"""
+ @pytest.mark.parametrize(("argument", "parsed"), HEALTHCHECK_ARGUMENTS)
+ def test_a_healthcheck_is_read_as_buildkit_reads_it(self, argument, parsed):
+ """parse_healthcheck reads each case as BuildKit reads it.
+
+ Without any one clause of parse_healthcheck, it reads at least one
+ case otherwise. The reading it replaced, a regular expression for
+ ``--<name>=<value>`` anywhere in the argument and a search for
+ ``CMD [...]`` at its end, read the second to the fifth case
+ otherwise, so a ``--timeout`` in the probe's own command could stand
+ in for the flag that Docker applies.
+ """
+ assert parse_healthcheck(argument) == parsed
+
def test_healthcheck_shape(self, ldr_stage):
raw = [arg for kw, arg in ldr_stage if kw == "HEALTHCHECK"]
assert len(raw) == 1
@@ -1281,29 +1774,221 @@ SECRET_NAME_RE = re.compile(
)
+def build_arg_names(instructions: list[tuple[str, str]]) -> set[str]:
+ """Return the names of the build arguments that the ARG lines declare.
+
+ BuildKit reads every word of an ARG line as a name or a
+ ``<name>=<value>`` pair (parseNameOrNameVal in
+ frontend/dockerfile/parser/line_parsers.go and parseArg in
+ frontend/dockerfile/instructions/parse.go), so one line can declare
+ several. It splits the line at whitespace outside quotes and escapes,
+ and expands the quotes, escapes and build arguments in a name. This
+ function splits at all whitespace and keeps each name as written, so
+ where the two differ it reads a name that BuildKit does not read, which
+ test_no_credential_bearing_build_args_or_env refuses. An ONBUILD ARG
+ trigger (_unwrap_onbuild) declares its names in each stage built from
+ the stage that holds it, and in each image built from that stage's
+ image, so its words are read too.
+ """
+ names = set()
+ for instruction in instructions:
+ _, keyword, argument = _unwrap_onbuild(*instruction)
+ if keyword == "ARG":
+ names |= {word.split("=")[0] for word in argument.split()}
+ return names
+
+
+def credential_bearing_args_and_env(
+ instructions: list[tuple[str, str]],
+) -> list[str]:
+ """Return the ARG and ENV lines whose argument names a credential.
+
+ A line counts when SECRET_NAME_RE matches anywhere in its argument, in
+ a name or in a value, and so does an ONBUILD ARG or ONBUILD ENV trigger
+ (_unwrap_onbuild): the image of the stage that holds it keeps its text,
+ values included, and Docker runs it in each stage built from that stage
+ and in each image built from that image. Each line is given with its
+ label, such as ``ONBUILD ENV``, and its argument. This is a text
+ tripwire: BuildKit expands quotes, escapes and build arguments in a
+ name, so a name deliberately spelled with them is not matched.
+ """
+ found = []
+ for instruction in instructions:
+ label, keyword, argument = _unwrap_onbuild(*instruction)
+ if keyword in {"ARG", "ENV"} and SECRET_NAME_RE.search(argument):
+ found.append(f"{label} {argument}")
+ return found
+
+
+def secret_mounts(instructions: list[tuple[str, str]]) -> list[str]:
+ """Return the flags in ``instructions`` that can mount a build secret.
+
+ BuildKit mounts a secret for a RUN ``--mount`` flag whose value has a
+ ``type`` field of ``secret``. The value is comma-separated
+ ``<key>=<value>`` fields, and BuildKit lowercases each key and the
+ type, and expands build arguments in a field's value (parseMount in
+ frontend/dockerfile/instructions/commands_runmount.go). This reads the
+ fields of the value of every flag (_flag_words), of ONBUILD triggers
+ too (_unwrap_onbuild), and reports a flag that has a ``type`` field of
+ ``secret`` in any case or of a value with a ``$``. Reading the fields of
+ flags other than ``--mount`` only reports more.
+ """
+ found = []
+ for keyword, argument in instructions:
+ _, _, argument = _unwrap_onbuild(keyword, argument)
+ for word in _flag_words(argument):
+ fields = word.partition("=")[2].split(",")
+ if any(
+ key.lower() == "type"
+ and (value.lower() == "secret" or "$" in value)
+ for key, _, value in (field.partition("=") for field in fields)
+ ):
+ found.append(word)
+ return found
+
+
+# Lines and the flags that secret_mounts reports in each. BuildKit reads the
+# fields of a --mount value in any order, and the key `type` and its value in
+# any case, so each of the first six mounts a secret: the fifth when the
+# build argument KIND is `secret`, and the sixth in each stage built from the
+# stage that holds it. Neither of the last two mounts one.
+SECRET_MOUNT_LINES = [
+ pytest.param(
+ "RUN --mount=type=secret,id=a cat /run/secrets/a",
+ ["--mount=type=secret,id=a"],
+ id="type-first",
+ ),
+ pytest.param(
+ "RUN --mount=id=a,type=secret cat /run/secrets/a",
+ ["--mount=id=a,type=secret"],
+ id="type-after-another-field",
+ ),
+ pytest.param(
+ "RUN --mount=TYPE=secret,id=a true",
+ ["--mount=TYPE=secret,id=a"],
+ id="key-in-capitals",
+ ),
+ pytest.param(
+ "RUN --mount=type=SECRET,id=a true",
+ ["--mount=type=SECRET,id=a"],
+ id="type-in-capitals",
+ ),
+ pytest.param(
+ "RUN --mount=type=$KIND,id=a true",
+ ["--mount=type=$KIND,id=a"],
+ id="type-from-a-build-argument",
+ ),
+ pytest.param(
+ "ONBUILD RUN --mount=id=a,type=secret true",
+ ["--mount=id=a,type=secret"],
+ id="onbuild-run",
+ ),
+ pytest.param(
+ "RUN --mount=type=cache,target=/root/.cache true", [], id="cache"
+ ),
+ pytest.param(
+ "RUN echo --mount=type=secret",
+ [],
+ id="command-word-that-looks-like-a-flag",
+ ),
+]
+
+# Lines, the lines that credential_bearing_args_and_env reports in each, and
+# the build arguments that build_arg_names reads in each. Docker runs an
+# ONBUILD trigger in each stage built from the stage that holds it, and the
+# image of that stage keeps its text (_unwrap_onbuild), so an ONBUILD ARG or
+# ENV trigger counts as the line it holds: docker build 29.1.3 (classic
+# builder) sets OPENAI_API_KEY in a stage built from one that holds the
+# fourth line, and keeps its trigger, `ENV OPENAI_API_KEY=sk-live-xyz`, in the
+# holding stage's image. A trigger of another instruction is read as that
+# instruction, so neither function reads the last line.
+ARG_AND_ENV_LINES = [
+ pytest.param(
+ "ARG GITHUB_TOKEN", ["ARG GITHUB_TOKEN"], {"GITHUB_TOKEN"}, id="arg"
+ ),
+ pytest.param(
+ "ENV OPENAI_API_KEY=sk-live-xyz",
+ ["ENV OPENAI_API_KEY=sk-live-xyz"],
+ set(),
+ id="env",
+ ),
+ pytest.param(
+ "ONBUILD ARG GITHUB_TOKEN",
+ ["ONBUILD ARG GITHUB_TOKEN"],
+ {"GITHUB_TOKEN"},
+ id="onbuild-arg",
+ ),
+ pytest.param(
+ "ONBUILD ENV OPENAI_API_KEY=sk-live-xyz",
+ ["ONBUILD ENV OPENAI_API_KEY=sk-live-xyz"],
+ set(),
+ id="onbuild-env",
+ ),
+ pytest.param(
+ "ONBUILD ARG DEBIAN_FRONTEND=noninteractive EXTRA=1",
+ [],
+ {"DEBIAN_FRONTEND", "EXTRA"},
+ id="onbuild-arg-without-a-credential",
+ ),
+ pytest.param("ONBUILD RUN echo $GITHUB_TOKEN", [], set(), id="onbuild-run"),
+]
+
+
class TestNoSecretsInImage:
"""No build arg, env default or copied file may carry a credential."""
def test_no_credential_bearing_build_args_or_env(
self, dockerfile_instructions
):
- offenders = [
- (kw, arg)
- for kw, arg in dockerfile_instructions
- if kw in {"ARG", "ENV"} and SECRET_NAME_RE.search(arg)
- ]
+ offenders = credential_bearing_args_and_env(dockerfile_instructions)
assert offenders == [], offenders
- args = {
- arg.split("=")[0].strip()
- for kw, arg in dockerfile_instructions
- if kw == "ARG"
- }
+ args = build_arg_names(dockerfile_instructions)
assert args == {"DEBIAN_FRONTEND"}, f"a new build arg appeared: {args}"
- def test_no_mount_type_secret_leaks_into_a_layer(self):
+ def test_every_word_of_an_arg_line_declares_a_build_arg(self):
+ """An ARG line can declare more than one build argument."""
+ assert build_arg_names(
+ [("ARG", "DEBIAN_FRONTEND=noninteractive EXTRA=1 MORE")]
+ ) == {"DEBIAN_FRONTEND", "EXTRA", "MORE"}
+
+ @pytest.mark.parametrize(
+ ("line", "credentials", "names"), ARG_AND_ENV_LINES
+ )
+ def test_an_onbuild_trigger_is_read_as_the_line_it_holds(
+ self, line, credentials, names
+ ):
+ """Both readers of ARG and ENV lines read ONBUILD triggers too.
+
+ Without the ONBUILD unwrap, or any other clause, of either
+ credential_bearing_args_and_env or build_arg_names, it reads at
+ least one case otherwise. Before they read triggers, both read only
+ lines whose keyword is ARG or ENV, so with `ONBUILD ARG GITHUB_TOKEN`
+ and `ONBUILD ENV OPENAI_API_KEY=sk-live-xyz` in builder-base, whose
+ triggers run in builder and ldr-test,
+ test_no_credential_bearing_build_args_or_env passed.
+ """
+ instructions = [_instruction(line)]
+ assert credential_bearing_args_and_env(instructions) == credentials
+ assert build_arg_names(instructions) == names
+
+ def test_no_mount_type_secret_leaks_into_a_layer(
+ self, dockerfile_instructions
+ ):
text = DOCKERFILE.read_text(encoding="utf-8")
assert "--mount=type=secret" not in text
assert "--secret" not in text
+ mounts = secret_mounts(dockerfile_instructions)
+ assert mounts == [], f"a RUN line mounts a build secret: {mounts}"
+
+ @pytest.mark.parametrize(("line", "mounts"), SECRET_MOUNT_LINES)
+ def test_a_secret_mount_is_read_as_buildkit_reads_it(self, line, mounts):
+ """secret_mounts reads each line's flags as BuildKit reads them.
+
+ Without any one clause of secret_mounts, it reports something else
+ for at least one case. The text check above misses the second case
+ and the four after it.
+ """
+ assert secret_mounts([_instruction(line)]) == mounts
def test_build_context_excludes_secrets_and_history(self):
patterns = {
@@ -1540,91 +2225,1289 @@ class TestUnraidTemplate:
# ---------------------------------------------------------------------------
-# 9. The temporary CPython zipfile backport (CVE-2026-15310)
+# 9. The CPython base image and fixed-vulnerability scan policy
# ---------------------------------------------------------------------------
+# The first CPython 3.14 release with the upstream CVE-2026-15310 zipfile fix.
+# The base image's tag and the floor of each root stage's build-time
+# interpreter check must both be this version or newer.
+PATCHED_PYTHON = (3, 14, 8)
+PATCHED_PYTHON_TEXT = ".".join(str(part) for part in PATCHED_PYTHON)
+
+# The build-time interpreter check, matched against a root stage's whole RUN
+# argument. A partial match would also accept forms that keep the check's
+# text but can no longer fail the build: `|| true` or a `;`-chained command
+# after it, `echo` or a shell comment in front of it, a `#` inside the
+# program, a weakened condition such as `or True`, or an expression in the
+# f-string message, which runs before sys.exit does. Environment prefixes
+# are refused as well; PYTHONOPTIMIZE=1 disabled the earlier assert-based
+# check. The check calls sys.exit, which python -O and PYTHONOPTIMIZE keep.
+# Only the floor and the failure message may differ from the Dockerfile's
+# check, and the message may hold no character of
+# INTERPRETER_MESSAGE_REFUSED except in `{v}`.
+INTERPRETER_CHECK_RUN = re.compile(
+ r'python3 -c "import sys; v = sys\.version\.split\(\)\[0\]; '
+ r"print\(f'base image Python \{v\}'\); "
+ r"sys\.exit\(0 if sys\.version_info >= \((\d+), (\d+), (\d+)\) "
+ r"else f'(?:[^'\"\\{}`$]|\{v\})*'\)\""
+)
+
-class TestCPythonZipfileBackportWiring:
- """The backport only exists in an image if the Dockerfile applies it.
-
- ``scripts/patch_cpython_zipfile_cve_2026_15310.py`` rewrites the
- interpreter's stdlib at build time, and its own tests resolve it by path,
- so deleting the ``COPY``/``RUN`` pair from a stage leaves every other
- check green while shipping an unpatched ``zipfile``. Grype will not
- notice either: ``.grype.yaml`` suppresses ``CVE-2026-15310`` for the
- ``python`` binary package unconditionally, because the binary CPE stays
- 3.14.7 whether or not the pure-Python fix was applied. This class is the
- only thing that fails when those four lines go away.
-
- The wiring check below only confirms the ``COPY``/``RUN`` pair is
- present; it does not run the patch. Runtime verification happens
- because the script's own ``main()`` re-execs itself with
- ``--verify-runtime`` after applying the patch -- that flag never
- appears on the Dockerfile's ``RUN`` line, so a refactor of ``main()``
- that drops the self re-exec would not be caught here.
+def interpreter_check_floor(run_argument: str) -> tuple[int, ...] | None:
+ """Return the version floor of an exact interpreter-check RUN argument.
+
+ Any other RUN argument, including a rewritten check, gives ``None``.
+ """
+ match = INTERPRETER_CHECK_RUN.fullmatch(run_argument)
+ if match is None:
+ return None
+ return tuple(int(part) for part in match.groups())
+
+
+# The characters that the shell or Python reads in the interpreter check's
+# failure message, an f-string inside the shell's double quotes. `'` ends the
+# message and `"` the shell's argument to python3. `$` and a backtick start a
+# command substitution, which the shell runs inside double quotes. A backslash
+# starts an escape, for the shell before some characters and for Python
+# before any. `{` and `}` delimit an expression, which runs before sys.exit
+# does. INTERPRETER_CHECK_RUN refuses each of them in the message, except in
+# `{v}`.
+INTERPRETER_MESSAGE_REFUSED = [
+ pytest.param("'", id="quote"),
+ pytest.param('"', id="double-quote"),
+ pytest.param("$", id="dollar-sign"),
+ pytest.param("`", id="backtick"),
+ pytest.param("\\", id="backslash"),
+ pytest.param("{", id="opening-brace"),
+ pytest.param("}", id="closing-brace"),
+]
+
+
+# The shell the root stages run their RUN lines with. Another SHELL in a root
+# stage changes what the check runs under; SHELL ["/bin/true"] makes it a
+# no-op.
+ROOT_STAGE_SHELL = ["/bin/bash", "-o", "pipefail", "-c"]
+
+# The only whitespace characters a Dockerfile may contain for the interpreter
+# and pyexpat check contracts. Docker and this module's parsers read these
+# alike: Docker splits a Dockerfile into lines at "\n" only, as _logical_lines
+# does, and _logical_lines treats spaces and tabs as BuildKit does. Other
+# whitespace is read differently. BuildKit removes every character for which
+# Go's unicode.IsSpace is true from the start of an instruction's first line,
+# and from the start of a line inside a continuation to tell whether that
+# line is a comment or empty, where _logical_lines removes only spaces and
+# tabs. BuildKit splits an instruction from its arguments at "\t", "\v",
+# "\f", "\r" and " " only.
+# parse_dockerfile and the stage helpers use str.strip(), str.split() and the
+# \s of re, which match every character for which str.isspace() is true, such
+# as "\x1f", U+00A0 and U+3000. In a line of `RUN`, U+00A0 and `true`, for
+# example, parse_dockerfile reads a RUN instruction, while Docker reads one
+# word, an unknown instruction. A "\r" directly before "\n" is read the same
+# way by both.
+DOCKERFILE_WHITESPACE = " \t\n"
+
+# Docker drops a byte order mark at the start of the file only, and
+# str.strip() keeps one anywhere, so a byte order mark before a parser
+# directive such as `# escape=` keeps misread_dockerfile_syntax from reading
+# the line as a comment, and so from reporting the directive. _instruction
+# keeps it too, so that first line is also not an instruction to this
+# module's parsers. Anywhere else, Docker keeps a byte order mark, as these
+# parsers do; it is refused there as well, so that a Dockerfile that the
+# contracts accept holds none.
+BYTE_ORDER_MARK = "\ufeff"
+
+
+def is_misread_character(char: str) -> bool:
+ """Whether Docker and this module's parsers can read ``char`` differently.
+
+ True for every character for which str.isspace() is true except those in
+ DOCKERFILE_WHITESPACE, and for BYTE_ORDER_MARK. Both split a Dockerfile
+ into lines, and a line into an instruction and its argument, alike at
+ every other character, including non-ASCII ones such as the em dashes in
+ the Dockerfile's comments. Within the leading flags of an instruction and
+ the words of a FROM line, BuildKit reads quotes, backslashes and
+ characters outside ASCII otherwise (is_misread_word).
"""
+ return char == BYTE_ORDER_MARK or (
+ char.isspace() and char not in DOCKERFILE_WHITESPACE
+ )
- def test_patch_script_is_present(self):
- assert ZIPFILE_PATCH_SCRIPT.is_file(), (
- f"{ZIPFILE_PATCH_SCRIPT_REL} is gone but the Dockerfile and the "
- "CVE-2026-15310 .grype.yaml suppression still reference it"
- )
- def test_every_stage_built_from_the_base_image_applies_the_backport(
- self, dockerfile_instructions
- ):
- """Deleting the COPY/RUN pair from any base-rooted stage must fail.
+def is_misread_word(word: str) -> bool:
+ """Whether BuildKit can read ``word`` otherwise than this module's parsers.
+
+ ``word`` is a leading flag of an instruction (_flag_words) or a word of a
+ FROM line. True when it holds a quote, a backslash or a character
+ outside ASCII. BuildKit reads quotes and backslashes in a flag as quoting
+ and escapes, so a quoted span of a flag can hold spaces and the words
+ after them, and it reads a character outside ASCII in a flag byte by
+ byte, with the bytes 0x85 and 0xA0, which UTF-8 uses in characters such
+ as ``Å``, as whitespace (extractBuilderFlags in
+ frontend/dockerfile/parser/split_command.go). It reads quotes and
+ backslashes in the image of a FROM line as quoting and escapes as well,
+ when it expands the build arguments there (dockerfile2llb/convert.go).
+ It compares the second word of a FROM line with ``AS``, and lowercases a
+ stage name, by Go's Unicode case rules, which agree with str.lower()
+ only in ASCII (parseBuildStageName in
+ frontend/dockerfile/instructions/parse.go): docker build 29.1.3 (classic
+ builder) reads ``Aſ``, with a long s, as ``AS``, and names a stage
+ ``İx``, with a dotted capital I, ``ix``.
+ """
+ return any(char in "'\"\\" or not char.isascii() for char in word)
- Derived from the parsed stages rather than from the names
- ``builder-base`` and ``ldr``, so a future stage rooted at the base
- image is held to the same requirement.
- """
+
+def _json_or_none(argument: str) -> object:
+ try:
+ return json.loads(argument)
+ except ValueError:
+ return None
+
+
+def misread_dockerfile_syntax(path: Path) -> list[str]:
+ """Return the constructs in ``path`` that this module's parsers misread.
+
+ ``_logical_lines`` joins lines with BuildKit's rule. A backslash that
+ ends a line, with nothing but spaces and tabs after it and no backslash
+ right before it (LINE_CONTINUATION), is removed together with those
+ spaces and tabs. The next line is then appended as it is, keeping its
+ leading whitespace, with nothing put between the two, and comment lines
+ inside the continuation are dropped. ``parse_dockerfile`` splits each
+ logical line into an instruction and its argument. Together they read a
+ Dockerfile as Docker does only without the constructs listed below.
+ Around each of them but a ``check`` parser directive, they can report a
+ RUN line that Docker never runs, runs with another shell, or runs with
+ other code, or one whose reading differs between Docker releases, or
+ read a stage as built from another image or stage than Docker does, a
+ stage's name otherwise, or a source of COPY or RUN otherwise. So the
+ interpreter and pyexpat check contracts refuse the construct instead of
+ parsing it, and the tests that read the Dockerfile with them require
+ that it has none. The contracts guard against accidental regressions in
+ reviewed Dockerfile changes; they are not a parser for adversarial
+ Dockerfiles.
+
+ - a heredoc: ``<<`` in a line, or a ``<`` before a backslash that ends
+ a line, which a continuation can join to a ``<`` at the start of the
+ next line. BuildKit looks for ``<<`` in the logical line, and every
+ logical line with ``<<`` comes from a line of one of these two kinds
+ or from lines with a character for which is_misread_character is
+ true. A heredoc's body lines are file content to Docker but
+ instructions to ``_logical_lines``.
+ - a blank line inside a backslash continuation. Docker skips it, as
+ ``_logical_lines`` does, but warns that empty continuation lines will
+ become errors in a future release.
+ - a character for which is_misread_character is true: whitespace other
+ than a space, a tab and a line feed, or a byte order mark.
+ - two backslashes at the end of a line, before any spaces and tabs.
+ Docker reads them as an escaped backslash and ends the instruction,
+ as ``_logical_lines`` does, but BuildKit releases before v0.8.0 join
+ the next line to it.
+ - a parser directive: ``escape``, ``syntax`` or ``check``, which
+ BuildKit reads from comment lines at the top of the file. ``escape``
+ can make a backtick the line continuation character, and ``syntax``
+ has another frontend parse the Dockerfile, such as an older release
+ or a custom image. ``check`` only configures BuildKit's build checks,
+ but it is refused with the other two, so that a Dockerfile that the
+ contracts accept has no parser directive.
+ - a logical line whose first word is not in DOCKER_INSTRUCTIONS, which
+ ``parse_dockerfile`` leaves out (``_instruction``). Docker does not
+ build a Dockerfile with an unknown instruction, so in one that builds
+ such a line means that this module's parsers joined or split a line
+ in another way than Docker.
+ - a word of a FROM line, or a leading flag (_flag_words) of any other
+ instruction, an ONBUILD trigger's included, that holds a quote, a
+ backslash or a character outside ASCII (is_misread_word). BuildKit
+ can read such a flag as another flag, or as one that takes in the
+ words after it, such as the image of a FROM line, and such a FROM
+ word as another image, stage or stage name. Without them, BuildKit
+ reads the flags that _flag_words returns, unless one of them is just
+ ``--`` (_flag_words), and splits a FROM line into the words that
+ _from_words returns and names its stage as dockerfile_stage_names
+ does.
+ - two stages with the same name. ``dockerfile_stages`` merges them, but
+ ``docker build --target`` builds only one: the first with the classic
+ builder, the last with BuildKit.
+ - a SHELL in a root stage other than ROOT_STAGE_SHELL.
+
+ All but the last are refused anywhere in the file: the lines a heredoc
+ or a continuation takes in can belong to a later stage. A comment line
+ inside a continuation is accepted. Docker drops it, as
+ ``_logical_lines`` does, and the package lists of both root stages use
+ such comments.
+ """
+ text = path.read_bytes().decode("utf-8")
+ # read_text() would turn a lone "\r" into a line break before the scan.
+ raw = text.replace("\r\n", "\n")
+ misread = [
+ f"character U+{ord(char):04X}"
+ for char in sorted(set(raw))
+ if is_misread_character(char)
+ ]
+ continued = False
+ lines = raw.removesuffix("\n").split("\n")
+ for number, line in enumerate(lines, start=1):
+ stripped = line.strip()
+ if stripped.startswith("#"):
+ directive = re.match(
+ r"#\s*(escape|syntax|check)\s*=", stripped, re.IGNORECASE
+ )
+ if directive:
+ misread.append(
+ f"line {number}: {directive.group(1).lower()} "
+ "parser directive"
+ )
+ continue
+ if not stripped:
+ if continued:
+ misread.append(
+ f"line {number}: blank line inside a continuation"
+ )
+ continue
+ if "<<" in line or re.search(r"<\\[ \t]*$", line):
+ misread.append(f"line {number}: heredoc")
+ if re.search(r"\\\\[ \t]*$", line):
+ misread.append(
+ f"line {number}: two backslashes at the end of the line"
+ )
+ continued = stripped.endswith("\\")
+
+ misread += [
+ f"line {number}: not an instruction: {line!r}"
+ for number, line in _numbered_logical_lines(text)
+ if _instruction(line) is None
+ ]
+ for number, line in _numbered_logical_lines(text):
+ if (instruction := _instruction(line)) is None:
+ continue
+ label, keyword, argument = _unwrap_onbuild(*instruction)
+ words = argument.split() if keyword == "FROM" else _flag_words(argument)
+ misread += [
+ f"line {number}: {label} {word}: a quote, a backslash or a "
+ "character outside ASCII"
+ for word in words
+ if is_misread_word(word)
+ ]
+
+ instructions = parse_dockerfile(path)
+ names = [name.lower() for name in dockerfile_stage_names(instructions)]
+ misread += [
+ f"stage '{name}' is defined more than once"
+ for name in sorted(set(names))
+ if names.count(name) > 1
+ ]
+ _, rooted = stages_from_base_image(instructions)
+ stages = dockerfile_stages(instructions)
+ misread += [
+ f"stage '{stage}': SHELL {argument}"
+ for stage in rooted
+ for keyword, argument in stages[stage]
+ if keyword == "SHELL" and _json_or_none(argument) != ROOT_STAGE_SHELL
+ ]
+ return misread
+
+
+def stages_checking_the_interpreter(path: Path) -> set[str]:
+ """Return the root stages of ``path`` whose build checks the interpreter.
+
+ A root stage counts when one of its RUN lines is exactly the interpreter
+ check (INTERPRETER_CHECK_RUN) with a floor of PATCHED_PYTHON or newer.
+ No stage counts while ``path`` uses syntax that misread_dockerfile_syntax
+ reports, because a check line found there may never run.
+ """
+ if misread_dockerfile_syntax(path):
+ return set()
+ instructions = parse_dockerfile(path)
+ _, rooted = stages_from_base_image(instructions)
+ stages = dockerfile_stages(instructions)
+ checked = set()
+ for stage in rooted:
+ floors = [
+ interpreter_check_floor(argument)
+ for keyword, argument in stages[stage]
+ if keyword == "RUN"
+ ]
+ if any(
+ floor is not None and floor >= PATCHED_PYTHON for floor in floors
+ ):
+ checked.add(stage)
+ return checked
+
+
+def _check_as_assert(check: str) -> str:
+ """Rewrite a check into the assert form that -O and PYTHONOPTIMIZE strip."""
+ return re.sub(
+ r"sys\.exit\(0 if (.+?) else (f?'.*')\)", r"assert \1, \2", check
+ )
+
+
+# A shell command that prints a `'`, the base64 decoding of `Jw==`, without
+# holding one.
+PRINT_A_QUOTE = "echo Jw== | base64 -d"
+
+
+def _message_ended_early(check: str, end: str) -> str:
+ """Return ``RUN <check>`` with its failure message ended at ``end``.
+
+ The rest of the line then passes on any version. ``end`` is ``'``,
+ ``"``, ``$(`` or a backtick. After ``'`` comes a conditional expression
+ that calls ``sys.exit(0)``. After ``"``, which ends the shell's argument
+ to python3, come ``|| true`` and a shell comment. ``$(`` and a backtick
+ each start a command substitution of PRINT_A_QUOTE, which the shell runs
+ inside its double quotes, so python3 reads a ``'`` in its place: the
+ line then ends as after ``'``, with each ``'`` spelled as such a
+ substitution.
+ """
+ quotes = {"'": "'", "$(": f"$({PRINT_A_QUOTE})", "`": f"`{PRINT_A_QUOTE}`"}
+ rest = {'"': '" || true # '}
+ for start, quote in quotes.items():
+ rest[start] = f"{quote} if False else sys.exit(0) or {quote}"
+ tail = "')\""
+ assert check.endswith(tail), check
+ return f"RUN {check.removesuffix(tail)}{rest[end]}{tail}"
+
+
+# The Dockerfile a rewrite below is placed in, unless the rewrite writes its
+# own FROM line. Its SHELL is the root stages' SHELL.
+REWRITE_STAGE = (
+ f"FROM python:3-slim AS stage\nSHELL {json.dumps(ROOT_STAGE_SHELL)}"
+)
+
+# Rewrites of the Dockerfile line ``RUN <check>``. All but the last, which
+# lowers the floor to 3.14.7, keep the check's text. On Python 3.14.4, below
+# the floor, none of the first ten fails a build (bash -o pipefail) except
+# two: the environment prefix, and the assert form, which also passes once
+# PYTHONOPTIMIZE=1 is set. The next sixteen leave the check line unchanged
+# but surround it with syntax that misread_dockerfile_syntax refuses. The
+# four after them end the failure message early (_message_ended_early): at
+# a `'`, at a `"`, and at a `'` that a command substitution prints, written
+# as `$(...)` or between backticks. docker build 29.1.3 (classic builder,
+# --target stage) completes each of the sixteen and each of the last five on
+# the python:3.14.7 image without the check failing the build. Without
+# misread_dockerfile_syntax the check would count in each of the sixteen
+# except four, which _logical_lines joins as Docker does: the blank line
+# inside a continuation, the line separator, the lone carriage return and
+# the control character. The duplicate-stage case keeps the check in the
+# middle one of three stages named `stage`, which neither the classic
+# builder nor BuildKit builds for that target. In the heredoc that a line of
+# `RUN` ends, Docker reads the check line as heredoc content, and only the
+# `<<` clause of the heredoc rule of misread_dockerfile_syntax refuses the
+# case; test_checks_in_a_split_heredoc_do_not_count needs its other clause.
+# In the case with a no-break space before `#`, Docker skips that line as a
+# comment and joins the check line to `RUN true`, and only the character
+# rule refuses the case.
+REWRITTEN_INTERPRETER_CHECKS = [
+ pytest.param(lambda check: f"RUN {check} || true", id="or-true"),
+ pytest.param(lambda check: f"RUN {check}; true", id="semicolon-chain"),
+ pytest.param(
+ lambda check: f"RUN PYTHONOPTIMIZE=1 {check}", id="environment-prefix"
+ ),
+ pytest.param(lambda check: f"RUN echo {check}", id="echo-wrapper"),
+ pytest.param(lambda check: f"RUN true # {check}", id="shell-comment"),
+ pytest.param(lambda check: f"# RUN {check}", id="commented-out"),
+ pytest.param(
+ lambda check: "RUN " + check.replace("; sys.exit(", "; # sys.exit(", 1),
+ id="python-comment",
+ ),
+ pytest.param(
+ lambda check: (
+ "RUN " + check.replace(") else f'", ") or True else f'", 1)
+ ),
+ id="or-true-condition",
+ ),
+ pytest.param(
+ lambda check: (
+ "RUN " + check.replace("else f'", "else f'{sys.exit(0)}", 1)
+ ),
+ id="message-expression",
+ ),
+ pytest.param(
+ lambda check: "RUN " + _check_as_assert(check), id="assert-statement"
+ ),
+ pytest.param(
+ lambda check: f"RUN cat > /dev/null <<'EOF'\nRUN {check}\nEOF",
+ id="heredoc-body",
+ ),
+ pytest.param(
+ lambda check: f"RUN cat > /dev/null <\\\n<'EOF'\nRUN {check}\nEOF",
+ id="heredoc-operator-split-by-continuation",
+ ),
+ pytest.param(
+ lambda check: f"RUN cat > /dev/null <<'RUN'\nRUN {check}\nRUN",
+ id="heredoc-ended-by-a-run-line",
+ ),
+ pytest.param(
+ lambda check: f"RUN true \\\n\nRUN {check}",
+ id="blank-line-inside-continuation",
+ ),
+ pytest.param(
+ lambda check: f'SHELL ["/bin/true"]\nRUN {check}', id="shell-override"
+ ),
+ pytest.param(
+ lambda check: f'SHE\\\nLL ["/bin/true"]\nRUN {check}',
+ id="shell-keyword-split-by-continuation",
+ ),
+ pytest.param(
+ lambda check: f"RUN true #\u2028RUN {check}", id="line-separator"
+ ),
+ pytest.param(
+ lambda check: f"RUN true #\rRUN {check}", id="lone-carriage-return"
+ ),
+ pytest.param(
+ lambda check: f"RUN {check} \\\n\x1f# || true",
+ id="control-character-before-comment",
+ ),
+ pytest.param(
+ lambda check: f'RUN true \\\u00a0\nSHELL ["/bin/true"]\nRUN {check}',
+ id="no-break-space-after-backslash",
+ ),
+ pytest.param(
+ lambda check: f'RUN true \\\u3000\nSHELL ["/bin/true"]\nRUN {check}',
+ id="ideographic-space-after-backslash",
+ ),
+ pytest.param(
+ lambda check: f"RUN true \\\n\u00a0# a comment to Docker\nRUN {check}",
+ id="no-break-space-before-comment-inside-continuation",
+ ),
+ pytest.param(
+ lambda check: f'RUN true \\\\\nSHELL ["/bin/true"]\nRUN {check}',
+ id="escaped-backslash-at-line-end",
+ ),
+ pytest.param(
+ lambda check: f"# escape=`\n{REWRITE_STAGE}\nRUN true `\nRUN {check}",
+ id="escape-directive",
+ ),
+ pytest.param(
+ lambda check: (
+ f"\ufeff# escape=`\n{REWRITE_STAGE}\nRUN true `\nRUN {check}"
+ ),
+ id="byte-order-mark-before-escape-directive",
+ ),
+ pytest.param(
+ lambda check: (
+ f"{REWRITE_STAGE}\nRUN true\n{REWRITE_STAGE}\nRUN {check}\n"
+ f"{REWRITE_STAGE}\nRUN true"
+ ),
+ id="duplicate-stage-name",
+ ),
+ pytest.param(
+ lambda check: _message_ended_early(check, "'"),
+ id="message-ended-by-a-quote",
+ ),
+ pytest.param(
+ lambda check: _message_ended_early(check, '"'),
+ id="argument-ended-by-a-double-quote",
+ ),
+ pytest.param(
+ lambda check: _message_ended_early(check, "$("),
+ id="message-ended-by-a-command-substitution",
+ ),
+ pytest.param(
+ lambda check: _message_ended_early(check, "`"),
+ id="message-ended-by-a-backtick-substitution",
+ ),
+ pytest.param(
+ lambda check: (
+ "RUN " + re.sub(r"(?<=>= )\(\d+, \d+, \d+\)", "(3, 14, 7)", check)
+ ),
+ id="floor-below-the-minimum",
+ ),
+]
+
+# Dockerfiles that each hold one construct that misread_dockerfile_syntax
+# refuses and nothing else that it refuses, with what it reports. BuildKit
+# matches parser directive names without regard to case, with any spaces
+# around them, and a syntax directive has another frontend parse the file.
+# BuildKit releases before v0.8.0 join the line after two backslashes at the
+# end of a line to it, also when spaces and tabs follow them. Docker warns
+# that a blank line inside a continuation will become an error, also when
+# comment lines come before it. Docker reads `AS` without regard to case and
+# lowercases stage names, so `stage` and `Stage` name one stage. Docker drops
+# a byte order mark at the start of the file only, and keeps one inside a
+# line, as this module's parsers do (BYTE_ORDER_MARK). BuildKit reads a
+# quote or a backslash in a flag as quoting or an escape, and a character
+# outside ASCII in a flag byte by byte, in the flags of every instruction,
+# an ONBUILD trigger's too, and it reads `Aſ`, with a long s, as `AS`
+# (is_misread_word): docker build 29.1.3 (classic builder) names the stage
+# of `FROM stage Aſ two` `two`, where this module's parsers read a stage
+# without a name.
+MISREAD_ON_ITS_OWN = [
+ pytest.param(
+ f"# syntax=docker/dockerfile:1.2\n{REWRITE_STAGE}\n",
+ ["line 1: syntax parser directive"],
+ id="syntax-directive",
+ ),
+ pytest.param(
+ f"# Syntax = docker/dockerfile:1.2\n{REWRITE_STAGE}\n",
+ ["line 1: syntax parser directive"],
+ id="syntax-directive-with-a-capital-and-spaces",
+ ),
+ pytest.param(
+ f"# check=error=true\n{REWRITE_STAGE}\n",
+ ["line 1: check parser directive"],
+ id="check-directive",
+ ),
+ pytest.param(
+ f"{REWRITE_STAGE}\nRUN true \\\\\nRUN true\n",
+ ["line 3: two backslashes at the end of the line"],
+ id="two-backslashes-at-the-end-of-a-line",
+ ),
+ pytest.param(
+ f"{REWRITE_STAGE}\nRUN true \\\\ \t\nRUN true\n",
+ ["line 3: two backslashes at the end of the line"],
+ id="two-backslashes-before-spaces-and-tabs",
+ ),
+ pytest.param(
+ f"{REWRITE_STAGE}\nRUN true \\\n\n true\n",
+ ["line 4: blank line inside a continuation"],
+ id="blank-line-inside-a-continuation",
+ ),
+ pytest.param(
+ f"{REWRITE_STAGE}\nRUN true \\\n # a comment\n\n true\n",
+ ["line 5: blank line inside a continuation"],
+ id="blank-line-after-a-comment-inside-a-continuation",
+ ),
+ pytest.param(
+ f"{REWRITE_STAGE}\n{REWRITE_STAGE.replace(' AS stage', ' AS Stage')}\n",
+ ["stage 'stage' is defined more than once"],
+ id="stage-names-that-differ-in-case",
+ ),
+ pytest.param(
+ f"{REWRITE_STAGE}\n{REWRITE_STAGE.replace(' AS stage', ' as stage')}\n",
+ ["stage 'stage' is defined more than once"],
+ id="stage-defined-again-with-a-lowercase-as",
+ ),
+ pytest.param(
+ f"{REWRITE_STAGE}\nLABEL x=a\ufeffb\n",
+ ["character U+FEFF"],
+ id="byte-order-mark-inside-a-line",
+ ),
+ pytest.param(
+ f"{REWRITE_STAGE}\nFROM --platform=linux/amd'64' stage AS two\n",
+ [
+ "line 3: FROM --platform=linux/amd'64': a quote, a backslash or "
+ "a character outside ASCII"
+ ],
+ id="quote-in-a-from-flag",
+ ),
+ pytest.param(
+ f'{REWRITE_STAGE}\nFROM --platform="linux/amd64" stage AS two\n',
+ [
+ 'line 3: FROM --platform="linux/amd64": a quote, a backslash or '
+ "a character outside ASCII"
+ ],
+ id="double-quote-in-a-from-flag",
+ ),
+ pytest.param(
+ f"{REWRITE_STAGE}\nFROM --platform=linux/amd\\64 stage AS two\n",
+ [
+ "line 3: FROM --platform=linux/amd\\64: a quote, a backslash or "
+ "a character outside ASCII"
+ ],
+ id="backslash-in-a-from-flag",
+ ),
+ pytest.param(
+ f"{REWRITE_STAGE}\nFROM --platform=linux/amd64\u00c5 stage AS two\n",
+ [
+ "line 3: FROM --platform=linux/amd64\u00c5: a quote, a backslash "
+ "or a character outside ASCII"
+ ],
+ id="character-outside-ascii-in-a-from-flag",
+ ),
+ pytest.param(
+ f"{REWRITE_STAGE}\nFROM stage A\u017f two\n",
+ [
+ "line 3: FROM A\u017f: a quote, a backslash or a character "
+ "outside ASCII"
+ ],
+ id="character-outside-ascii-in-a-word-of-a-from-line",
+ ),
+ pytest.param(
+ f'{REWRITE_STAGE}\nHEALTHCHECK --timeout="10s" CMD ["true"]\n',
+ [
+ 'line 3: HEALTHCHECK --timeout="10s": a quote, a backslash or a '
+ "character outside ASCII"
+ ],
+ id="quote-in-a-flag-of-another-instruction",
+ ),
+ pytest.param(
+ f"{REWRITE_STAGE}\nONBUILD COPY --chown='1000' a b\n",
+ [
+ "line 3: ONBUILD COPY --chown='1000': a quote, a backslash or a "
+ "character outside ASCII"
+ ],
+ id="quote-in-a-flag-of-an-onbuild-trigger",
+ ),
+]
+
+# The python:3.14.7-slim manifest digest that both root stages were pinned to
+# before Python 3.14.8. Its interpreter predates the CVE-2026-15310 fix, and
+# its bundled expat is 2.8.2 (BUNDLED_EXPAT).
+PYTHON_3_14_7_IMAGE = (
+ "python:3.14.7-slim@sha256:"
+ "cad9a2c871761c413caa6fdd6441c783451e740a48aaeba60ae62a8b53525ef6"
+)
+
+
+def _with_stage_before_ldr(text: str, stage: str) -> str:
+ """Return the Dockerfile ``text`` with ``stage`` before the ldr stage.
+
+ ``{base}`` in ``stage`` stands for the image that ldr is built from.
+ """
+ ldr = re.search(r"(?m)^FROM (\S+) AS ldr$", text)
+ assert ldr is not None, "the Dockerfile has no `FROM <image> AS ldr` line"
+ start = ldr.start()
+ return f"{text[:start]}{stage.format(base=ldr.group(1))}{text[start:]}"
+
+
+# Rewrites of the Dockerfile that each add a stage or a line, and the rules
+# that refuse the result (test_an_added_stage_is_held_to_the_contract):
+# "base images" when stages_from_base_image does not find exactly one image
+# that is not a stage, "unchecked root stage" when a stage built from the
+# base image does not run both checks, "shipped stage" when the last stage,
+# which a build without --target builds, is not ldr, and "unreviewed image"
+# when COPY or RUN takes files from an image that is not in
+# REVIEWED_SOURCE_IMAGES (unreviewed_source_images). The first is a final
+# gate's probe: a last stage without a name, built from PYTHON_3_14_7_IMAGE.
+# docker build 29.1.3 (classic builder, no --target) completes a Dockerfile
+# of the ldr stage's SHELL and two check lines and such a stage, and its
+# image runs Python 3.14.7 with expat 2.8.2. The next three add a stage
+# without a name that is built from another image, the third after a flag
+# that ends in `$AS`, in which a regular expression for `AS <name>` at the
+# end of the FROM argument would read a name (FROM_STAGE_NAMES). The fifth
+# adds a root stage without a name and without the checks, and the next two
+# a stage built from ldr after it: that stage inherits ldr's checks, but it
+# would ship instead of ldr. The next two are a later final gate's probes:
+# a stage named after the image `python` that it is built from, whose files
+# a COPY in ldr takes, and a stage built from a stage defined after it. A
+# FROM line names a stage only when that stage is defined before it
+# (stages_from_base_image): with the classic builder, docker build 29.1.3
+# answers a FROM line that names its own stage or a later one with "pull
+# access denied" for an image of that name that no registry has. The next
+# FROM line names a stage in capitals, which BuildKit reads as an image name
+# and refuses, and the one after it a stage whose name is in capitals,
+# which Docker lowercases. The next copies CPython's zipfile package from
+# PYTHON_3_14_7_IMAGE into ldr. The last is the probe of the final gate
+# after those: a stage `old`, built from PYTHON_3_14_7_IMAGE, whose
+# --platform flag holds ` builder-base ` in a quoted span, and a COPY
+# --from=3, its index, that copies its zipfile package into ldr. This
+# module's parsers split the flag at the spaces inside the quotes and take
+# `builder-base` for the image, so they read stage 3 as a stage without a
+# name built from builder-base, and the COPY as one from an earlier stage.
+# docker build 29.1.3 (classic builder) reads the flag as one: given ARG
+# BUILDPLATFORM before the first FROM line, as here, it builds a stage
+# written so from Python 3.14.7, and a COPY --from of that stage's index
+# copies its files. misread_dockerfile_syntax refuses the quotes
+# (is_misread_word), so neither check counts.
+REWRITTEN_STAGE_GRAPHS = [
+ pytest.param(
+ lambda text: (
+ f"{text}\nFROM {PYTHON_3_14_7_IMAGE}\n"
+ "COPY --from=ldr /etc/os-release /ldr-os-release\n"
+ ),
+ {"base images", "shipped stage"},
+ id="unnamed-last-stage-from-python-3.14.7",
+ ),
+ pytest.param(
+ lambda text: _with_stage_before_ldr(
+ text, f"FROM {PYTHON_3_14_7_IMAGE}\nRUN true\n"
+ ),
+ {"base images"},
+ id="unnamed-stage-from-python-3.14.7-before-ldr",
+ ),
+ pytest.param(
+ lambda text: _with_stage_before_ldr(
+ text, "FROM python:3.14-slim\nRUN true\n"
+ ),
+ {"base images"},
+ id="unnamed-stage-from-an-unpinned-tag-before-ldr",
+ ),
+ pytest.param(
+ lambda text: (
+ "ARG AS=linux/amd64\n"
+ + _with_stage_before_ldr(
+ text, f"FROM --platform=$AS {PYTHON_3_14_7_IMAGE}\nRUN true\n"
+ )
+ ),
+ {"base images"},
+ id="unnamed-stage-after-a-flag-that-ends-in-as",
+ ),
+ pytest.param(
+ lambda text: _with_stage_before_ldr(text, "FROM {base}\nRUN true\n"),
+ {"unchecked root stage"},
+ id="unnamed-stage-from-the-base-image-without-the-checks",
+ ),
+ pytest.param(
+ lambda text: f"{text}\nFROM ldr AS debug\nUSER root\n",
+ {"shipped stage"},
+ id="named-stage-after-ldr",
+ ),
+ pytest.param(
+ lambda text: f"{text}\nFROM ldr\nUSER root\n",
+ {"shipped stage"},
+ id="unnamed-stage-after-ldr",
+ ),
+ pytest.param(
+ lambda text: (
+ _with_stage_before_ldr(text, "FROM python AS python\nRUN true\n")
+ + "\nCOPY --from=python /usr/local/lib/python3.14/zipfile/"
+ " /usr/local/lib/python3.14/zipfile/\n"
+ ),
+ {"base images"},
+ id="stage-named-after-its-image-and-copied-into-ldr",
+ ),
+ pytest.param(
+ lambda text: _with_stage_before_ldr(
+ text,
+ "FROM later AS early\nRUN true\n"
+ "FROM builder-base AS later\nRUN true\n",
+ ),
+ {"base images"},
+ id="stage-from-a-stage-defined-after-it",
+ ),
+ pytest.param(
+ lambda text: _with_stage_before_ldr(
+ text, "FROM BUILDER-BASE AS extra\nRUN true\n"
+ ),
+ {"base images"},
+ id="stage-from-a-stage-name-in-capitals",
+ ),
+ pytest.param(
+ lambda text: _with_stage_before_ldr(
+ text,
+ "FROM builder-base AS Extra\nRUN true\nFROM extra AS more\nRUN true\n",
+ ),
+ set(),
+ id="stage-from-a-stage-named-in-capitals",
+ ),
+ pytest.param(
+ lambda text: (
+ f"{text}\nCOPY --from={PYTHON_3_14_7_IMAGE}"
+ " /usr/local/lib/python3.14/zipfile/"
+ " /usr/local/lib/python3.14/zipfile/\n"
+ ),
+ {"unreviewed image"},
+ id="copy-from-python-3.14.7-into-ldr",
+ ),
+ pytest.param(
+ lambda text: (
+ "ARG BUILDPLATFORM=linux/amd64\n"
+ + _with_stage_before_ldr(
+ text,
+ 'FROM --platform=$BUILDPLATFORM${{U:+" builder-base "}} '
+ f"{PYTHON_3_14_7_IMAGE} AS old\nRUN true\n",
+ )
+ + "\nCOPY --from=3 /usr/local/lib/python3.14/zipfile/"
+ " /usr/local/lib/python3.14/zipfile/\n"
+ ),
+ {"unchecked root stage"},
+ id="stage-from-python-3.14.7-behind-a-quoted-flag-copied-into-ldr",
+ ),
+]
+
+# A Dockerfile of three stages. Lines put into the middle one, `two`, have
+# the stage `one` before them and the stage `three` after them.
+SOURCE_STAGES = "FROM a AS one\nFROM one AS two\n{lines}\nFROM two AS three\n"
+
+# Lines put into the stage `two` of SOURCE_STAGES, the images that
+# unreviewed_source_images is given as reviewed, and what it reports
+# (test_a_source_counts_as_a_stage_only_before_it). One clause of the
+# function decides each case. A stage before `two`, named in any case or,
+# in COPY, by its index, is a stage. `two` itself, a later stage and an
+# image are not, and in a `from` field a number is an image. BuildKit reads
+# a `--mount` key in any case, and quotes and backslashes in a flag as
+# quoting and escapes: `--fr'om'=python` is `--from=python` to it. It reads
+# the two bytes of `Å` in UTF-8 one by one, and the second as whitespace,
+# so it reads the last line as two `--mount` flags, of which the second is
+# `--mount=from=python`.
+COPY_AND_RUN_SOURCES = [
+ pytest.param("COPY --from=one /a /b", frozenset(), [], id="stage-before"),
+ pytest.param(
+ "COPY --from=ONE /a /b", frozenset(), [], id="stage-before-in-capitals"
+ ),
+ pytest.param(
+ "COPY --from=0 /a /b", frozenset(), [], id="index-of-a-stage-before"
+ ),
+ pytest.param(
+ "COPY --from=1 /a /b",
+ frozenset(),
+ ["stage 'two': COPY --from=1"],
+ id="index-of-its-own-stage",
+ ),
+ pytest.param(
+ "COPY --from=two /a /b",
+ frozenset(),
+ ["stage 'two': COPY --from=two"],
+ id="its-own-stage",
+ ),
+ pytest.param(
+ "COPY --from=three /a /b",
+ frozenset(),
+ ["stage 'two': COPY --from=three"],
+ id="stage-after",
+ ),
+ pytest.param(
+ f"COPY --from={PYTHON_3_14_7_IMAGE} /a /b",
+ frozenset(),
+ [f"stage 'two': COPY --from={PYTHON_3_14_7_IMAGE}"],
+ id="image",
+ ),
+ pytest.param(
+ "COPY --from=python /a /b",
+ frozenset({"python"}),
+ [],
+ id="reviewed-image",
+ ),
+ pytest.param(
+ "COPY --chown=1000 --from=python /a /b",
+ frozenset(),
+ ["stage 'two': COPY --from=python"],
+ id="image-after-another-flag",
+ ),
+ pytest.param(
+ "RUN --mount=type=bind,from=one,target=/x true",
+ frozenset(),
+ [],
+ id="mount-from-a-stage-before",
+ ),
+ pytest.param(
+ "RUN --mount=type=bind,from=python,target=/x true",
+ frozenset(),
+ ["stage 'two': RUN --mount from=python"],
+ id="mount-from-an-image",
+ ),
+ pytest.param(
+ "RUN --mount=type=bind,from=0,target=/x true",
+ frozenset(),
+ ["stage 'two': RUN --mount from=0"],
+ id="mount-from-a-number",
+ ),
+ pytest.param(
+ "RUN --mount=type=bind,FROM=python,target=/x true",
+ frozenset(),
+ ["stage 'two': RUN --mount from=python"],
+ id="mount-key-in-capitals",
+ ),
+ pytest.param(
+ "ONBUILD COPY --from=python /a /b",
+ frozenset(),
+ ["stage 'two': ONBUILD COPY --from=python"],
+ id="onbuild-copy",
+ ),
+ pytest.param(
+ "RUN echo --mount=type=bind,from=python",
+ frozenset(),
+ [],
+ id="command-word-that-looks-like-a-flag",
+ ),
+ pytest.param(
+ "COPY --fr'om'=python /a /b",
+ frozenset(),
+ [
+ "stage 'two': COPY --fr'om'=python: a quote, a backslash or a "
+ "character outside ASCII in a flag"
+ ],
+ id="quote-in-a-flag",
+ ),
+ pytest.param(
+ 'COPY --fr"om"=python /a /b',
+ frozenset(),
+ [
+ "stage 'two': COPY --fr\"om\"=python: a quote, a backslash or a "
+ "character outside ASCII in a flag"
+ ],
+ id="double-quote-in-a-flag",
+ ),
+ pytest.param(
+ "COPY --fr\\om=python /a /b",
+ frozenset(),
+ [
+ "stage 'two': COPY --fr\\om=python: a quote, a backslash or a "
+ "character outside ASCII in a flag"
+ ],
+ id="backslash-in-a-flag",
+ ),
+ pytest.param(
+ "RUN --mount=type=cache,target=/xÅ--mount=from=python true",
+ frozenset(),
+ [
+ "stage 'two': RUN --mount=type=cache,target=/xÅ--mount="
+ "from=python: a quote, a backslash or a character outside ASCII "
+ "in a flag"
+ ],
+ id="character-outside-ascii-in-a-flag",
+ ),
+]
+
+
+def suppressed_vulnerabilities() -> set[str]:
+ """Return the IDs that ``.grype.yaml`` and ``.trivyignore`` suppress."""
+ scanner_policy = yaml.safe_load(
+ (REPO_ROOT / ".grype.yaml").read_text(encoding="utf-8")
+ )
+ ignored = {entry.get("vulnerability") for entry in scanner_policy["ignore"]}
+ trivyignore = (REPO_ROOT / ".trivyignore").read_text(encoding="utf-8")
+ ignored |= {
+ line.split()[0]
+ for line in trivyignore.splitlines()
+ if line.strip() and not line.lstrip().startswith("#")
+ }
+ return ignored
+
+
+class TestCPythonBaseImage:
+ """The CPython base image and the scanner entries that depend on it.
+
+ Both root stages start from one digest-pinned image whose tag names
+ PATCHED_PYTHON or newer. Docker pulls a ``name:tag@sha256:<digest>``
+ reference by its digest and ignores the tag, so the tag text alone does
+ not show which interpreter the digest contains; each root stage
+ therefore checks the interpreter version while the image builds. The
+ Dockerfile may not use syntax that misread_dockerfile_syntax reports
+ (test_root_stages_check_interpreter_version_at_build), such as a quote
+ in a flag, around which this module's parsers can read other stages,
+ flags or sources than Docker. In a Dockerfile without it, every image
+ that a FROM, COPY or RUN line names is accounted for: every stage, named
+ or not, is built from that image or from a stage defined before it,
+ COPY and RUN take files only from stages defined before them or from
+ images in REVIEWED_SOURCE_IMAGES, and the last stage, which a build
+ without ``--target`` ships, is ``ldr`` and holds no ONBUILD trigger.
+ These tests are static: they pin the reference, the stages, the exact
+ form of those build-time checks, the Dockerfile syntax around them and
+ the scanner entries. The checks run only when CI builds the image.
+ """
+
+ def test_all_root_stages_use_patched_python(self, dockerfile_instructions):
+ """The base is digest-pinned and its tag names PATCHED_PYTHON+."""
base, rooted = stages_from_base_image(dockerfile_instructions)
- assert base.startswith("python:"), (
- f"base image is no longer a python image ({base}); the "
- "CVE-2026-15310 backport and its .grype.yaml suppression need "
- "re-checking"
+ match = re.fullmatch(
+ r"python:(3\.\d+\.\d+)-slim@sha256:[0-9a-f]{64}", base
+ )
+ assert match is not None, (
+ f"base image is not digest-pinned CPython: {base}"
+ )
+ assert Version(match.group(1)) >= Version(PATCHED_PYTHON_TEXT), (
+ f"Python {match.group(1)} predates {PATCHED_PYTHON_TEXT}, the "
+ "first 3.14 release with the CVE-2026-15310 fix"
)
- # Floor: the derived list must not be silently empty.
assert {"builder-base", "ldr"} <= set(rooted), (
"expected builder-base and ldr to be built directly from "
f"{base}, got {sorted(rooted)}"
)
- stages = dockerfile_stages(dockerfile_instructions)
- for stage in rooted:
- body = stages[stage]
- copies = []
- for index, (keyword, argument) in enumerate(body):
- if keyword != "COPY":
- continue
- tokens = copy_argument_tokens(argument)
- if tokens is None:
- continue
- if len(tokens) >= 2 and ZIPFILE_PATCH_SCRIPT_REL in tokens[:-1]:
- copies.append((index, tokens[-1]))
- assert len(copies) == 1, (
- f"stage '{stage}' is built from {base} but does not COPY "
- f"{ZIPFILE_PATCH_SCRIPT_REL} exactly once (found "
- f"{len(copies)}); it would ship an unpatched zipfile while "
- ".grype.yaml still suppresses CVE-2026-15310"
- )
- index, destination = copies[0]
- executed = [
- argument
- for keyword, argument in body[index + 1 :]
- if keyword == "RUN"
- and re.search(
- r"\bpython[0-9.]*\s+" + re.escape(destination),
- argument,
- )
+ def test_a_build_without_a_target_ships_ldr(self, dockerfile_instructions):
+ """The last stage, which a build without ``--target`` builds, is ldr.
+
+ prerelease-docker.yml builds the release images, which
+ docker-publish.yml only retags, without a target, and so do
+ grype.yml, container-security.yml (Trivy), dockle.yml and
+ docker-multiarch-test.yml for the images they scan or test. Each of
+ them builds the Dockerfile's last stage. A stage after ldr, named or
+ not, would ship instead of ldr, and the tests that read the ldr
+ stage would no longer be about the image that ships.
+ """
+ shipped = stage_built_without_a_target(dockerfile_instructions)
+ assert shipped == "ldr", (
+ f"the last stage of the Dockerfile is '{shipped}', not 'ldr'; "
+ "prerelease-docker.yml builds the release images without "
+ "--target, so they would ship that stage"
+ )
+
+ def test_the_shipped_stage_holds_no_onbuild_trigger(
+ self, dockerfile_instructions, tmp_path
+ ):
+ """The stage that a build without ``--target`` builds has no ONBUILD.
+
+ That stage, ldr, is the image that ships. Docker keeps a trigger of
+ it in that image's list of ONBUILD triggers and runs it in each
+ image that a user builds from the published image
+ (_unwrap_onbuild). An ONBUILD USER there, for example, would start
+ each such container as that user instead of root, which the
+ entrypoint's root phase needs. The tests of ldr's own settings do
+ not read its triggers, since none runs in ldr itself, and the
+ readers that read triggers in every stage look only for a build
+ argument, a credential, a secret mount, a source or a misread flag.
+ As a control, the test finds ``ONBUILD USER nobody`` added to ldr in
+ a copy of the Dockerfile.
+ """
+
+ def triggers(instructions):
+ shipped = stage_built_without_a_target(instructions)
+ body = dockerfile_stages(instructions)[shipped]
+ return [
+ argument for keyword, argument in body if keyword == "ONBUILD"
]
- assert executed, (
- f"stage '{stage}' copies {ZIPFILE_PATCH_SCRIPT_REL} to "
- f"{destination} but never RUNs it, so the stdlib is left "
- "unpatched"
+
+ found = triggers(dockerfile_instructions)
+ assert found == [], (
+ "the stage that a build without --target builds holds ONBUILD "
+ "triggers, which run in each image built from the published "
+ f"image: {found}"
+ )
+ dockerfile = tmp_path / "Dockerfile"
+ text = DOCKERFILE.read_text(encoding="utf-8")
+ dockerfile.write_text(
+ f"{text}\nONBUILD USER nobody\n", encoding="utf-8"
+ )
+ assert triggers(parse_dockerfile(dockerfile)) == ["USER nobody"]
+
+ def test_root_stages_check_interpreter_version_at_build(
+ self, dockerfile_instructions
+ ):
+ """Each root stage fails its build below PATCHED_PYTHON."""
+ base, rooted = stages_from_base_image(dockerfile_instructions)
+ assert {"builder-base", "ldr"} <= set(rooted), (
+ "expected builder-base and ldr to be built directly from "
+ f"{base}, got {sorted(rooted)}"
+ )
+ misread = misread_dockerfile_syntax(DOCKERFILE)
+ assert not misread, (
+ "the Dockerfile uses syntax that this module's parsers read "
+ "differently from Docker, so they cannot show that each root "
+ f"stage runs its interpreter check: {misread}"
+ )
+ checked = stages_checking_the_interpreter(DOCKERFILE)
+ for stage in rooted:
+ assert stage in checked, (
+ f"stage '{stage}' is built from {base} but has no RUN line "
+ "that is exactly the interpreter check (INTERPRETER_CHECK_RUN) "
+ f"with a floor of {PATCHED_PYTHON_TEXT} or higher; the tag is "
+ "ignored when a digest is given, so an older interpreter "
+ "behind the digest would not fail the build"
)
+ @pytest.mark.parametrize("rewrite", REWRITTEN_INTERPRETER_CHECKS)
+ def test_a_rewritten_interpreter_check_does_not_count(
+ self, dockerfile_instructions, tmp_path, rewrite
+ ):
+ """Only the exact check line, in syntax Docker reads alike, counts.
+
+ Each case rewrites the Dockerfile's own check line, keeping its
+ text in all but the case that lowers the floor, and writes the
+ result to a scratch Dockerfile. That file is read with the helper
+ the real Dockerfile is read with, stages_checking_the_interpreter:
+ this module's parsers plus misread_dockerfile_syntax, not Docker's
+ own parser. The unchanged line is accepted first, so each rejection
+ comes from the rewrite.
+ """
+ stages = dockerfile_stages(dockerfile_instructions)
+ checks = [
+ argument
+ for keyword, argument in stages["builder-base"]
+ if keyword == "RUN" and interpreter_check_floor(argument)
+ ]
+ assert len(checks) == 1, (
+ f"expected one interpreter check in builder-base, got {checks}"
+ )
+ check = checks[0]
+
+ def checked_stages(text):
+ # A rewrite that writes its own FROM line is the whole file.
+ if not re.search(r"(?m)^FROM ", text):
+ text = f"{REWRITE_STAGE}\n{text}"
+ dockerfile = tmp_path / "Dockerfile"
+ dockerfile.write_text(f"{text}\n", encoding="utf-8")
+ return stages_checking_the_interpreter(dockerfile)
+
+ assert checked_stages(f"RUN {check}") == {"stage"}
+ rewritten = rewrite(check)
+ assert rewritten != f"RUN {check}"
+ assert checked_stages(rewritten) == set(), (
+ f"a rewritten check still counts: {rewritten!r}"
+ )
+
+ @pytest.mark.parametrize("char", INTERPRETER_MESSAGE_REFUSED)
+ def test_an_interpreter_message_character_is_refused_alone(
+ self, dockerfile_instructions, char
+ ):
+ """Each character of INTERPRETER_MESSAGE_REFUSED is refused alone.
+
+ Each case puts one of them at the start of the failure message of
+ the Dockerfile's own check line, which then does not count. Some of
+ them can make the check pass in a build: the message cases of
+ REWRITTEN_INTERPRETER_CHECKS end the message early with a `'`, a
+ `"`, `$(` or a backtick, and message-expression adds an expression
+ with `{` and `}`. A backslash, or one brace without the other,
+ gives a character of the message or a SyntaxError, which fails the
+ build at any version, but the shell and Python still read it there,
+ so it is refused as well.
+ """
+ stages = dockerfile_stages(dockerfile_instructions)
+ checks = [
+ argument
+ for keyword, argument in stages["builder-base"]
+ if keyword == "RUN" and interpreter_check_floor(argument)
+ ]
+ assert len(checks) == 1, (
+ f"expected one interpreter check in builder-base, got {checks}"
+ )
+ start = "else f'"
+ assert checks[0].count(start) == 1, checks[0]
+ rewritten = checks[0].replace(start, start + char)
+ assert interpreter_check_floor(rewritten) is None, rewritten
+
+ def test_a_line_that_is_not_an_instruction_is_misread(self, tmp_path):
+ """A logical line with an unknown first word is refused, not skipped.
+
+ docker build 29.1.3 fails on each of these lines with "unknown
+ instruction". parse_dockerfile leaves them out, so without this
+ refusal a line that this module's parsers joined or split in
+ another way than Docker would go unseen.
+ """
+ dockerfile = tmp_path / "Dockerfile"
+ dockerfile.write_text(
+ f'{REWRITE_STAGE}\nSHE LL ["/bin/true"]\nRUN["/bin/true"]\n',
+ encoding="utf-8",
+ )
+ assert misread_dockerfile_syntax(dockerfile) == [
+ "line 3: not an instruction: 'SHE LL [\"/bin/true\"]'",
+ "line 4: not an instruction: 'RUN[\"/bin/true\"]'",
+ ]
+
+ @pytest.mark.parametrize(("text", "misread"), MISREAD_ON_ITS_OWN)
+ def test_a_construct_is_misread_on_its_own(self, tmp_path, text, misread):
+ """Each clause of misread_dockerfile_syntax refuses a case by itself.
+
+ A rule refuses through one or more clauses: each side of the ``or``
+ of the character rule (is_misread_character) and of the heredoc
+ rule, each directive name, the case-insensitive matching of a
+ directive name and of ``AS``, the lowercasing of stage names, the
+ optional whitespace in a pattern, around a directive name or after
+ a backslash, each way in which a line is not an instruction
+ (_instruction), for the blank-line rule, comment lines, which leave
+ a continuation open, and, for the word rule, each kind of character
+ of is_misread_word and each kind of word it is applied to: the words
+ of a FROM line, its flags among them, the flags of another
+ instruction and those of an ONBUILD trigger. Each Dockerfile here
+ holds one construct that the function refuses and nothing else that
+ it refuses, so removing a clause that refuses it fails this test.
+ Each other clause is the only one that refuses one of
+ REWRITTEN_INTERPRETER_CHECKS, whose check counts without that
+ clause, a line in test_a_line_that_is_not_an_instruction_is_misread,
+ or the Dockerfiles in test_checks_in_a_split_heredoc_do_not_count.
+ """
+ dockerfile = tmp_path / "Dockerfile"
+ dockerfile.write_text(text, encoding="utf-8")
+ assert misread_dockerfile_syntax(dockerfile) == misread
+
+ @pytest.mark.parametrize(("rewrite", "refused"), REWRITTEN_STAGE_GRAPHS)
+ def test_an_added_stage_is_held_to_the_contract(
+ self, tmp_path, rewrite, refused
+ ):
+ """Every stage counts, every image is accounted for, and ldr ships.
+
+ Each case adds a stage or a line to the Dockerfile and writes the
+ result to a scratch Dockerfile, which is read with the helpers that
+ the other tests of this class read the Dockerfile with:
+ stages_from_base_image, both check contracts,
+ stage_built_without_a_target and unreviewed_source_images. The
+ unchanged Dockerfile is accepted first, so each refusal comes from
+ the added stage or line.
+ """
+ dockerfile = tmp_path / "Dockerfile"
+
+ def refusals(text):
+ dockerfile.write_text(text, encoding="utf-8")
+ instructions = parse_dockerfile(dockerfile)
+ found = set()
+ try:
+ _, rooted = stages_from_base_image(instructions)
+ except AssertionError:
+ found.add("base images")
+ else:
+ checked = stages_checking_the_interpreter(
+ dockerfile
+ ) & stages_checking_pyexpat(dockerfile)
+ if not set(rooted) <= checked:
+ found.add("unchecked root stage")
+ if stage_built_without_a_target(instructions) != "ldr":
+ found.add("shipped stage")
+ if unreviewed_source_images(instructions):
+ found.add("unreviewed image")
+ return found
+
+ text = DOCKERFILE.read_text(encoding="utf-8")
+ assert refusals(text) == set()
+ assert refusals(rewrite(text)) == refused
+
+ def test_copy_and_run_take_files_only_from_stages(
+ self, dockerfile_instructions
+ ):
+ """COPY and RUN take files only from earlier stages or reviewed images.
+
+ An image that a COPY ``--from`` or a RUN ``--mount`` names is pulled
+ when the image builds, and what COPY or RUN takes from it can reach
+ the image that ships without the checks that the root stages run,
+ so such an image has to be a reviewed entry of
+ REVIEWED_SOURCE_IMAGES.
+ """
+ unreviewed = unreviewed_source_images(dockerfile_instructions)
+ assert not unreviewed, (
+ "COPY or RUN takes files from a source that is neither a stage "
+ f"before it nor in REVIEWED_SOURCE_IMAGES: {unreviewed}"
+ )
+
+ @pytest.mark.parametrize(
+ ("lines", "reviewed", "unreviewed"), COPY_AND_RUN_SOURCES
+ )
+ def test_a_source_counts_as_a_stage_only_before_it(
+ self, tmp_path, lines, reviewed, unreviewed
+ ):
+ """Each clause of unreviewed_source_images decides at least one case.
+
+ Each case puts lines into the stage ``two`` of SOURCE_STAGES, writes
+ the result to a scratch Dockerfile and reads it with
+ parse_dockerfile and unreviewed_source_images, given the case's
+ reviewed images. Without any one clause of unreviewed_source_images,
+ or of _flag_words, _unwrap_onbuild and is_misread_word, which it
+ reads flags with, the function reports something else for at least
+ one case.
+ """
+ dockerfile = tmp_path / "Dockerfile"
+ dockerfile.write_text(
+ SOURCE_STAGES.format(lines=lines), encoding="utf-8"
+ )
+ instructions = parse_dockerfile(dockerfile)
+ assert unreviewed_source_images(instructions, reviewed) == unreviewed
+
+ def test_fixed_cves_are_not_suppressed(self):
+ """CVEs that no longer apply to the pinned Python stay unsuppressed.
+
+ Most are fixed in 3.14.8 or earlier. The CVE records of two others
+ list 3.14 as affected only below 3.14.8, although 3.14 keeps the
+ behaviour they describe.
+ """
+ ignored = suppressed_vulnerabilities()
+ fixed_in_base_image = {
+ "CVE-2025-13462", # tarfile, fixed in 3.14.4
+ "CVE-2025-15366", # imaplib, fixed in 3.14.7
+ "CVE-2026-4360", # tarfile, fixed in 3.14.7
+ "CVE-2026-6879", # xml.etree.ElementPath, fixed in 3.14.7
+ "CVE-2026-15308", # html.parser, fixed in 3.14.7
+ "CVE-2026-15310", # zipfile, fixed in 3.14.8
+ "CVE-2026-19672", # tarfile, fixed in 3.14.8
+ "CVE-2026-15806", # urllib, fixed in 3.14.8
+ "CVE-2026-17084", # stringprep, fixed in 3.14.8
+ }
+ outside_record_range = {
+ # base64 altchars: affected below 3.14.1 per the record; the
+ # 3.14 change added tests only, and 3.15 deprecates the behaviour
+ "CVE-2025-12781",
+ # pkgutil, disputed: affected below 3.14.4 per the record; the
+ # 3.14 change was documentation only
+ "CVE-2026-3479",
+ }
+ assert not ((fixed_in_base_image | outside_record_range) & ignored)
+
+ def test_cve_2026_87910_stays_visible_to_the_release_gate(self):
+ """CVE-2026-87910 is not suppressed, although the pinned Python has it.
+
+ Python 3.14.8 does not fix it: the 3.14 backport of its tarfile fix
+ (gh-157265) merged after the v3.14.8 tag. It is left unsuppressed on
+ purpose, so that the release gate keeps reporting it until an
+ official python image has the fix. Suppressing it changes that
+ policy, so it has to change this test too.
+ """
+ assert "CVE-2026-87910" not in suppressed_vulnerabilities()
+
# ---------------------------------------------------------------------------
# CI marker-lane wiring -- the nonroot battery's teeth hang on one flag
@@ -1679,3 +3562,679 @@ class TestCIMarkerLaneWiring:
"it must select both markers so a module carrying either one "
"still runs"
)
+
+
+# ---------------------------------------------------------------------------
+# 10. The minimum version of CPython's bundled expat (pyexpat)
+# ---------------------------------------------------------------------------
+
+# The minimum the Dockerfile's pyexpat checks enforce. It must be the expat
+# that the base image's CPython release bundles (BUNDLED_EXPAT). Change it
+# together with those RUN lines and the version in their failure message.
+PYEXPAT_MINIMUM = (2, 8, 5)
+
+# The expat version that each CPython release bundles, from
+# Modules/expat/expat.h (XML_MAJOR_VERSION, XML_MINOR_VERSION and
+# XML_MICRO_VERSION) at the release's tag. pyexpat.version_info in the
+# pinned python:3.14.7-slim and python:3.14.8-slim images matches.
+BUNDLED_EXPAT = {
+ (3, 14, 7): (2, 8, 2),
+ (3, 14, 8): (2, 8, 5),
+}
+
+# The build-time pyexpat check, matched against a root stage's whole RUN
+# argument, as INTERPRETER_CHECK_RUN is. Only the floor and the failure
+# message may differ from the Dockerfile's check. The message is a plain
+# string, which may hold any character but `'`, which would end it, and `"`,
+# a backslash, a backtick and `$`, which the shell reads inside double
+# quotes; a backslash also starts a Python escape (PYEXPAT_MESSAGE_REFUSED).
+# The check is matched instead of being accepted for how its code behaves
+# against a stub pyexpat, because code can behave as the check does at the
+# versions a stub reports and still fail open in a build
+# (REWRITTEN_PYEXPAT_CHECKS).
+PYEXPAT_CHECK_RUN = re.compile(
+ r'python3 -c "(?P<code>import pyexpat, sys; v = pyexpat\.version_info; '
+ r"print\(f'pyexpat uses expat \{v\}'\); "
+ r"sys\.exit\(0 if v >= \((?P<floor>\d+, \d+, \d+)\) "
+ r"else '[^'\"\\`$]*'\))\""
+)
+
+# The characters that PYEXPAT_CHECK_RUN refuses in the failure message, as
+# INTERPRETER_MESSAGE_REFUSED lists them for the interpreter check. The
+# message is a plain string, so `{` and `}` are not among them.
+PYEXPAT_MESSAGE_REFUSED = [
+ pytest.param("'", id="quote"),
+ pytest.param('"', id="double-quote"),
+ pytest.param("$", id="dollar-sign"),
+ pytest.param("`", id="backtick"),
+ pytest.param("\\", id="backslash"),
+]
+
+
+def pyexpat_checks(
+ body: list[tuple[str, str]],
+) -> list[tuple[tuple[int, ...], str]]:
+ """Return ``(floor, code)`` for each pyexpat check RUN line in ``body``.
+
+ A RUN line is a pyexpat check when its whole argument matches
+ PYEXPAT_CHECK_RUN. ``code`` is the program that the line passes to
+ ``python3 -c``. Any other RUN line, including a rewritten check, is left
+ out.
+ """
+ checks = []
+ for keyword, argument in body:
+ if keyword != "RUN":
+ continue
+ match = PYEXPAT_CHECK_RUN.fullmatch(argument)
+ if match is not None:
+ floor = match.group("floor").split(", ")
+ checks.append(
+ (tuple(int(part) for part in floor), match.group("code"))
+ )
+ return checks
+
+
+def stages_checking_pyexpat(path: Path) -> set[str]:
+ """Return the root stages of ``path`` whose build checks pyexpat's expat.
+
+ A root stage counts when one of its RUN lines is exactly the pyexpat
+ check (PYEXPAT_CHECK_RUN) with the floor PYEXPAT_MINIMUM. No stage
+ counts while ``path`` uses syntax that misread_dockerfile_syntax
+ reports, because a check line found there may never run.
+ """
+ if misread_dockerfile_syntax(path):
+ return set()
+ instructions = parse_dockerfile(path)
+ _, rooted = stages_from_base_image(instructions)
+ stages = dockerfile_stages(instructions)
+ return {
+ stage
+ for stage in rooted
+ if any(
+ floor == PYEXPAT_MINIMUM
+ for floor, _ in pyexpat_checks(stages[stage])
+ )
+ }
+
+
+def _with_condition(check: str, condition: str) -> str:
+ """Return ``RUN <check>`` with ``condition`` in place of its comparison.
+
+ ``{}`` in ``condition`` stands for the comparison that it replaces.
+ """
+ comparison = re.search(r"v >= \(\d+, \d+, \d+\)", check).group()
+ return "RUN " + check.replace(comparison, condition.format(comparison), 1)
+
+
+# Rewrites of the Dockerfile's pyexpat check line ``RUN <check>``. With the
+# floor at 2.8.5, the first three exit as the check does at expat 2.8.4 and
+# 2.8.5 at every level in OPTIMISATION_FLAGS, so a contract that ran each
+# check against a stub pyexpat at those two versions would accept them. In a
+# build, the first passes on expat 2.8.2, which CPython 3.14.7 bundles, the
+# second on 2.7.9, and the third, which tells a stub from the real module, on
+# any expat. The fourth is the exact check with a floor of 2.8.2, so it
+# passes on 2.8.2. Of the others, all but the assert form pass on any expat
+# in a build, and the assert form passes once -O or PYTHONOPTIMIZE removes
+# it. The last one keeps the check line unchanged after SHELL ["/bin/true"],
+# which misread_dockerfile_syntax refuses.
+REWRITTEN_PYEXPAT_CHECKS = [
+ pytest.param(
+ lambda check: _with_condition(check, "v != (2, 8, 4)"),
+ id="not-equal-to-the-version-below-the-floor",
+ ),
+ pytest.param(
+ lambda check: _with_condition(check, "v[2] >= 5"),
+ id="micro-version-only",
+ ),
+ pytest.param(
+ lambda check: _with_condition(
+ check, "isinstance(pyexpat, type(sys)) or {}"
+ ),
+ id="real-module-detected",
+ ),
+ pytest.param(
+ lambda check: _with_condition(check, "v >= (2, 8, 2)"),
+ id="floor-below-the-minimum",
+ ),
+ pytest.param(
+ lambda check: _with_condition(check, "{} or True"),
+ id="or-true-condition",
+ ),
+ pytest.param(
+ lambda check: _with_condition(check, "{} or __debug__"),
+ id="debug-gated-condition",
+ ),
+ pytest.param(
+ lambda check: "RUN " + _check_as_assert(check), id="assert-statement"
+ ),
+ pytest.param(
+ lambda check: "RUN " + check.replace("; sys.exit(", "; # sys.exit(", 1),
+ id="python-comment",
+ ),
+ pytest.param(
+ lambda check: (
+ "RUN " + check.replace("else '", "else f'{sys.exit(0)}", 1)
+ ),
+ id="message-expression",
+ ),
+ pytest.param(
+ lambda check: _message_ended_early(check, "'"),
+ id="message-ended-by-a-quote",
+ ),
+ pytest.param(
+ lambda check: _message_ended_early(check, '"'),
+ id="argument-ended-by-a-double-quote",
+ ),
+ pytest.param(
+ lambda check: _message_ended_early(check, "$("),
+ id="message-ended-by-a-command-substitution",
+ ),
+ pytest.param(
+ lambda check: _message_ended_early(check, "`"),
+ id="message-ended-by-a-backtick-substitution",
+ ),
+ pytest.param(
+ lambda check: f'SHELL ["/bin/true"]\nRUN {check}', id="shell-override"
+ ),
+]
+
+
+# The optimisation levels a build can run a check at: none, which is the
+# level of the Dockerfile's RUN lines, and those of PYTHONOPTIMIZE=1 and
+# PYTHONOPTIMIZE=2, which -O and -OO set. Both remove assert statements and
+# make __debug__ False, and -OO also removes docstrings, so a check can pass
+# at one level and fail at another.
+OPTIMISATION_FLAGS = ((), ("-O",), ("-OO",))
+
+# The expat versions that pyexpat_check_failures runs a check's code at.
+# With PYEXPAT_MINIMUM at 2.8.5, four of them are below it: 2.8.2, which
+# CPython 3.14.7 bundles, the patch release just below the minimum, and two
+# versions whose minor or micro version alone is not below the minimum's.
+# The other three are at or above it.
+EXPAT_PROBE_VERSIONS = (
+ (1, 99, 99),
+ (2, 7, 9),
+ (2, 8, 2),
+ (2, 8, 4),
+ (2, 8, 5),
+ (2, 9, 0),
+ (3, 0, 0),
+)
+
+
+def run_pyexpat_check(
+ code: str, version: tuple[int, int, int], flags: tuple[str, ...]
+) -> subprocess.CompletedProcess[str]:
+ """Run a check's code under ``python -I <flags>`` with a stub ``pyexpat``.
+
+ ``code`` is taken from the logical line that ``_logical_lines`` builds
+ exactly as BuildKit does, and the shell passes it to python3 unchanged
+ (PYEXPAT_CHECK_RUN), so this runs the code that the build runs.
+ """
+ stub = (
+ "import sys, types; sys.modules['pyexpat'] = "
+ f"types.SimpleNamespace(version_info={version!r})\n"
+ )
+ return subprocess.run(
+ [sys.executable, "-I", *flags, "-c", stub + code],
+ capture_output=True,
+ text=True,
+ check=False,
+ timeout=60,
+ )
+
+
+def pyexpat_check_failures(
+ code: str, minimum: tuple[int, int, int] = PYEXPAT_MINIMUM
+) -> list[str]:
+ """Return the ways in which a check's code fails to enforce ``minimum``.
+
+ At every level in OPTIMISATION_FLAGS, the code must exit 1 with a
+ message that names ``minimum`` when the stub reports a version in
+ EXPAT_PROBE_VERSIONS below ``minimum``, and exit 0 when it reports one
+ at or above ``minimum``. An empty list means that the code does both at
+ every level. This is a control on code that PYEXPAT_CHECK_RUN already
+ accepts: code can pass it and still fail open in a build, for example
+ by telling the stub from the real pyexpat, so passing it does not make
+ a RUN line a check.
+ """
+ minimum_text = ".".join(str(part) for part in minimum)
+ failures = []
+ for flags in OPTIMISATION_FLAGS:
+ level = " ".join(flags) or "no -O flag"
+ for version in EXPAT_PROBE_VERSIONS:
+ result = run_pyexpat_check(code, version, flags)
+ if version >= minimum:
+ if result.returncode != 0:
+ failures.append(
+ f"{level}: exit status {result.returncode} at expat "
+ f"{version}, not 0: {result.stderr.strip()}"
+ )
+ elif result.returncode != 1:
+ failures.append(
+ f"{level}: exit status {result.returncode} at expat "
+ f"{version}, not 1"
+ )
+ elif minimum_text not in result.stderr:
+ failures.append(
+ f"{level}: the message at expat {version} does not name "
+ f"{minimum_text}: {result.stderr.strip()}"
+ )
+ return failures
+
+
+class TestPyexpatMinimumCheck:
+ """Each stage built from the Python base image checks pyexpat's expat.
+
+ The official python images build CPython with its bundled expat instead
+ of Debian's libexpat1, and Grype sees only libexpat1 (``.grype.yaml``,
+ libexpat1). The RUN lines that fail the build on an older bundled copy
+ are the only check on the expat that pyexpat uses. ``ldr`` starts from
+ the base image again, so the interpreter the image ships needs its own
+ check; the one in ``builder-base`` also covers ``builder`` and
+ ``ldr-test``, which are built from it.
+
+ Stages are derived from the parsed Dockerfile, as in section 9, so a
+ future stage built from the base image is held to the same rule, and the
+ Dockerfile may not use syntax that misread_dockerfile_syntax reports,
+ since a check line found there may never run. A RUN line counts as a
+ check only when it is exactly PYEXPAT_CHECK_RUN, as an interpreter check
+ counts only when it is exactly INTERPRETER_CHECK_RUN, and its floor must
+ be PYEXPAT_MINIMUM. The real checks run only when CI builds the image.
+ As a control on that exact form, the Python code of each check, as
+ BuildKit joins its RUN line, runs against a stub ``pyexpat`` at every
+ version in EXPAT_PROBE_VERSIONS and every level in OPTIMISATION_FLAGS.
+ The minimum itself is tied to the CPython release in the base image's
+ tag through BUNDLED_EXPAT.
+ """
+
+ def test_every_root_stage_runs_the_check(self, dockerfile_instructions):
+ base, rooted = stages_from_base_image(dockerfile_instructions)
+ assert {"builder-base", "ldr"} <= set(rooted), (
+ "expected builder-base and ldr to be built directly from "
+ f"{base}, got {sorted(rooted)}"
+ )
+ misread = misread_dockerfile_syntax(DOCKERFILE)
+ assert not misread, (
+ "the Dockerfile uses syntax that this module's parsers read "
+ "differently from Docker, so they cannot show that each root "
+ f"stage runs its pyexpat check: {misread}"
+ )
+ checked = stages_checking_pyexpat(DOCKERFILE)
+ stages = dockerfile_stages(dockerfile_instructions)
+ for stage in rooted:
+ floors = [floor for floor, _ in pyexpat_checks(stages[stage])]
+ assert stage in checked, (
+ f"stage '{stage}' is built from {base}, and the floors of "
+ "its RUN lines that are exactly the pyexpat check "
+ f"(PYEXPAT_CHECK_RUN) are {floors}; expected one with the "
+ f"floor {PYEXPAT_MINIMUM}, or the bundled expat of this "
+ "stage's interpreter is not checked"
+ )
+
+ @pytest.mark.parametrize("rewrite", REWRITTEN_PYEXPAT_CHECKS)
+ def test_a_rewritten_pyexpat_check_does_not_count(
+ self, dockerfile_instructions, tmp_path, rewrite
+ ):
+ """Only the exact check line, in syntax Docker reads alike, counts.
+
+ Each case rewrites the Dockerfile's own pyexpat check line and
+ writes the result to a scratch Dockerfile. That file is read with
+ the helper the real Dockerfile is read with, stages_checking_pyexpat.
+ The unchanged line is accepted first, so each rejection comes from
+ the rewrite.
+ """
+ stages = dockerfile_stages(dockerfile_instructions)
+ checks = [
+ argument
+ for keyword, argument in stages["builder-base"]
+ if pyexpat_checks([(keyword, argument)])
+ ]
+ assert len(checks) == 1, (
+ f"expected one pyexpat check in builder-base, got {checks}"
+ )
+ check = checks[0]
+ dockerfile = tmp_path / "Dockerfile"
+
+ def checked_stages(text):
+ dockerfile.write_text(
+ f"{REWRITE_STAGE}\n{text}\n", encoding="utf-8"
+ )
+ return stages_checking_pyexpat(dockerfile)
+
+ assert checked_stages(f"RUN {check}") == {"stage"}
+ rewritten = rewrite(check)
+ assert rewritten != f"RUN {check}"
+ assert checked_stages(rewritten) == set(), (
+ f"a rewritten check still counts: {rewritten!r}"
+ )
+
+ @pytest.mark.parametrize("char", PYEXPAT_MESSAGE_REFUSED)
+ def test_a_pyexpat_message_character_is_refused_alone(
+ self, dockerfile_instructions, char
+ ):
+ """Each character of PYEXPAT_MESSAGE_REFUSED is refused alone.
+
+ Each case puts one of them at the start of the failure message of
+ the Dockerfile's own pyexpat check line, which then does not count.
+ The message cases of REWRITTEN_PYEXPAT_CHECKS end the message early
+ with each of them but a backslash. A backslash gives a character of
+ the message or a SyntaxError, which fails the build at any version,
+ but the shell and Python still read it there, so it is refused as
+ well.
+ """
+ stages = dockerfile_stages(dockerfile_instructions)
+ checks = [
+ argument
+ for keyword, argument in stages["builder-base"]
+ if pyexpat_checks([(keyword, argument)])
+ ]
+ assert len(checks) == 1, (
+ f"expected one pyexpat check in builder-base, got {checks}"
+ )
+ start = "else '"
+ assert checks[0].count(start) == 1, checks[0]
+ rewritten = checks[0].replace(start, start + char)
+ assert pyexpat_checks([("RUN", rewritten)]) == [], rewritten
+
+ @pytest.mark.parametrize(
+ "split",
+ [
+ pytest.param("<\\", id="before-a-backslash"),
+ pytest.param("<\\ \t", id="before-a-backslash-a-space-and-a-tab"),
+ ],
+ )
+ def test_checks_in_a_split_heredoc_do_not_count(
+ self, dockerfile_instructions, tmp_path, split
+ ):
+ """Neither check counts in a heredoc whose ``<<`` a continuation splits.
+
+ The first line of the heredoc ends in ``<`` and a continuation, and
+ the next line starts with ``<'RUN'``. BuildKit removes the
+ backslash, with any spaces and tabs after it, and appends the next
+ line, so the RUN line holds ``<<'RUN'``. Both check lines are then
+ heredoc content, up to the line of ``RUN`` that ends the heredoc,
+ and each logical line of the file is an instruction to this
+ module's parsers. docker build 29.1.3 (classic builder, --target stage)
+ completes both Dockerfiles on the python:3.14.7 image, on which
+ either check alone fails the build. Of all the clauses of
+ misread_dockerfile_syntax, only the heredoc rule's clause for a
+ ``<`` before a backslash that ends a line refuses them, and only
+ with the spaces and tabs it allows after the backslash for the
+ second.
+ """
+ stages = dockerfile_stages(dockerfile_instructions)
+ interpreter = [
+ argument
+ for keyword, argument in stages["builder-base"]
+ if keyword == "RUN" and interpreter_check_floor(argument)
+ ]
+ pyexpat = [
+ argument
+ for keyword, argument in stages["builder-base"]
+ if pyexpat_checks([(keyword, argument)])
+ ]
+ assert len(interpreter) == len(pyexpat) == 1, (interpreter, pyexpat)
+ checks = f"RUN {interpreter[0]}\nRUN {pyexpat[0]}"
+ dockerfile = tmp_path / "Dockerfile"
+
+ def checked_stages(text):
+ dockerfile.write_text(
+ f"{REWRITE_STAGE}\n{text}\n", encoding="utf-8"
+ )
+ return (
+ stages_checking_the_interpreter(dockerfile),
+ stages_checking_pyexpat(dockerfile),
+ )
+
+ assert checked_stages(checks) == ({"stage"}, {"stage"})
+ heredoc = f"RUN cat > /dev/null {split}\n<'RUN'\n{checks}\nRUN"
+ assert checked_stages(heredoc) == (set(), set()), heredoc
+ assert misread_dockerfile_syntax(dockerfile) == ["line 3: heredoc"]
+
+ def test_checks_in_onbuild_triggers_do_not_count(
+ self, dockerfile_instructions, tmp_path
+ ):
+ """Neither check counts as an ONBUILD trigger of a root stage.
+
+ Docker does not run a trigger in the stage that holds it, only in
+ each stage built from that stage and in each image built from its
+ image, and no trigger in the Dockerfile runs in a root stage
+ (_unwrap_onbuild). A root stage whose checks are ONBUILD RUN
+ triggers therefore never runs them.
+ misread_dockerfile_syntax accepts the file, so it is refused only
+ because both check readers read a stage's own RUN lines, not its
+ triggers.
+ """
+ stages = dockerfile_stages(dockerfile_instructions)
+ interpreter = [
+ argument
+ for keyword, argument in stages["builder-base"]
+ if keyword == "RUN" and interpreter_check_floor(argument)
+ ]
+ pyexpat = [
+ argument
+ for keyword, argument in stages["builder-base"]
+ if pyexpat_checks([(keyword, argument)])
+ ]
+ assert len(interpreter) == len(pyexpat) == 1, (interpreter, pyexpat)
+ dockerfile = tmp_path / "Dockerfile"
+
+ def checked_stages(prefix):
+ dockerfile.write_text(
+ f"{REWRITE_STAGE}\n{prefix}RUN {interpreter[0]}\n"
+ f"{prefix}RUN {pyexpat[0]}\n",
+ encoding="utf-8",
+ )
+ return (
+ stages_checking_the_interpreter(dockerfile),
+ stages_checking_pyexpat(dockerfile),
+ )
+
+ assert checked_stages("") == ({"stage"}, {"stage"})
+ assert checked_stages("ONBUILD ") == (set(), set())
+ assert misread_dockerfile_syntax(dockerfile) == []
+
+ def test_each_check_fails_below_the_minimum_at_every_optimisation_level(
+ self, dockerfile_instructions
+ ):
+ """Control: the code of each check enforces PYEXPAT_MINIMUM.
+
+ The checks counted above are exactly PYEXPAT_CHECK_RUN. This runs
+ their code against a stub pyexpat, so it fails if PYEXPAT_CHECK_RUN
+ accepts a Dockerfile check line whose code does not enforce its
+ floor. Passing it does not make a RUN line a check.
+ """
+ _, rooted = stages_from_base_image(dockerfile_instructions)
+ stages = dockerfile_stages(dockerfile_instructions)
+ checks = [
+ (stage, code)
+ for stage in rooted
+ for _, code in pyexpat_checks(stages[stage])
+ ]
+ assert len(checks) >= 2, f"expected a check per root stage: {checks}"
+ for code in dict.fromkeys(code for _, code in checks):
+ failures = pyexpat_check_failures(code)
+ assert not failures, (
+ f"the pyexpat check {code!r} must exit 1, naming "
+ f"{PYEXPAT_MINIMUM}, below that expat and exit 0 at and above "
+ "it, with and without -O and -OO. Call sys.exit(): -O removes "
+ f"an assert. {failures}"
+ )
+
+ def test_minimum_is_the_expat_bundled_with_the_pinned_python(
+ self, dockerfile_instructions
+ ):
+ """PYEXPAT_MINIMUM is the expat of the CPython release in the tag.
+
+ test_every_root_stage_runs_the_check requires a check with the floor
+ PYEXPAT_MINIMUM in each root stage; this one holds PYEXPAT_MINIMUM
+ and the floor of every check to the expat that the base image's
+ CPython release bundles. When the base image moves to another
+ CPython release, this test fails until the minimum and both checks
+ move to that release's bundled expat.
+ """
+ base, rooted = stages_from_base_image(dockerfile_instructions)
+ tag = re.match(r"python:(\d+)\.(\d+)\.(\d+)[-@]", base)
+ assert tag is not None, (
+ f"cannot read the CPython release from the base image tag: {base}"
+ )
+ python = tuple(int(part) for part in tag.groups())
+ assert python in BUNDLED_EXPAT, (
+ f"BUNDLED_EXPAT has no row for CPython {python}, the release in "
+ f"the base image tag ({base}). Add a row with the expat version "
+ "in Modules/expat/expat.h at that release's tag, then set "
+ "PYEXPAT_MINIMUM and the Dockerfile's pyexpat checks to it."
+ )
+ assert PYEXPAT_MINIMUM == BUNDLED_EXPAT[python], (
+ f"CPython {python}, the release in the base image tag, bundles "
+ f"expat {BUNDLED_EXPAT[python]}, but PYEXPAT_MINIMUM and the "
+ f"Dockerfile's pyexpat checks are at {PYEXPAT_MINIMUM}. Set both "
+ "to the bundled version."
+ )
+ stages = dockerfile_stages(dockerfile_instructions)
+ floors = {
+ stage: [floor for floor, _ in pyexpat_checks(stages[stage])]
+ for stage in rooted
+ }
+ assert {floor for found in floors.values() for floor in found} == {
+ BUNDLED_EXPAT[python]
+ }, (
+ f"CPython {python}, the release in the base image tag, bundles "
+ f"expat {BUNDLED_EXPAT[python]}, but the floors of the "
+ f"Dockerfile's pyexpat checks are {floors}"
+ )
+
+ def test_neutered_check_lines_do_not_count(self, tmp_path):
+ """Negative control: RUN lines that cannot fail the build."""
+ code = (
+ "import pyexpat, sys; v = pyexpat.version_info; "
+ "print(f'pyexpat uses expat {v}'); "
+ "sys.exit(0 if v >= (2, 8, 2) else 'old')"
+ )
+ bare = f'python3 -c "{code}"'
+ neutered = [
+ f"{bare} || true",
+ f"{bare}; true",
+ f"PYTHONOPTIMIZE=1 {bare}",
+ f"env PYTHONOPTIMIZE=1 {bare}",
+ f"echo {bare}",
+ f"true # {bare}",
+ ]
+ dockerfile = tmp_path / "Dockerfile"
+ dockerfile.write_text(
+ "\n".join(
+ ["FROM python:3-slim AS ldr", f"RUN {bare}", f"# RUN {bare}"]
+ + [f"RUN {line}" for line in neutered]
+ )
+ + "\n",
+ encoding="utf-8",
+ )
+ body = dockerfile_stages(parse_dockerfile(dockerfile))["ldr"]
+ # The commented-out RUN is not an instruction at all.
+ assert len(body) == 1 + len(neutered)
+ assert pyexpat_checks(body) == [((2, 8, 2), code)]
+
+ def test_an_assert_only_check_passes_when_optimised(self):
+ """Negative control: -O and -OO remove an assert, so it fails open."""
+ asserted = "import pyexpat; assert pyexpat.version_info >= (2, 8, 2)"
+ assert run_pyexpat_check(asserted, (2, 8, 1), ()).returncode == 1
+ for flags in (("-O",), ("-OO",)):
+ assert run_pyexpat_check(asserted, (2, 8, 1), flags).returncode == 0
+ assert pyexpat_check_failures(asserted, (2, 8, 2))
+ explicit = (
+ "import pyexpat, sys; sys.exit(0 if pyexpat.version_info >= "
+ "(2, 8, 2) else 'pyexpat embeds an expat older than 2.8.2')"
+ )
+ assert pyexpat_check_failures(explicit, (2, 8, 2)) == []
+
+ def test_a_debug_gated_check_passes_without_optimisation(self):
+ """Negative control: a build runs the check with __debug__ True.
+
+ -O and -OO make __debug__ False, so this check fails below the
+ minimum under them but passes on any expat without them, which is
+ how the Dockerfile's RUN lines run it.
+ """
+ gated = (
+ "import pyexpat, sys; sys.exit(0 if pyexpat.version_info >= "
+ "(2, 8, 2) or __debug__ else 'pyexpat embeds an expat older than "
+ "2.8.2')"
+ )
+ for flags in (("-O",), ("-OO",)):
+ assert run_pyexpat_check(gated, (2, 8, 1), flags).returncode == 1
+ assert run_pyexpat_check(gated, (2, 8, 1), ()).returncode == 0
+ assert pyexpat_check_failures(gated, (2, 8, 2))
+
+ def test_the_control_fails_code_that_misses_one_requirement(self):
+ """Negative control: pyexpat_check_failures checks each requirement.
+
+ Each code below enforces the minimum 2.8.2 but for one requirement
+ of pyexpat_check_failures. The first exits 1 at the minimum, the
+ second exits 0 below it, with a message that names it, and the third
+ exits 1 below it with a message that does not name it. Each fails
+ the control for that reason alone, at every level in
+ OPTIMISATION_FLAGS.
+ """
+ start = "import pyexpat, sys; v = pyexpat.version_info; "
+ codes = {
+ ", not 0": "sys.exit(0 if v >= (2, 8, 4) else 'older than 2.8.2')",
+ ", not 1": (
+ "v >= (2, 8, 2) or print('older than 2.8.2', file=sys.stderr)"
+ ),
+ "does not name 2.8.2": (
+ "sys.exit(0 if v >= (2, 8, 2) else 'expat is too old')"
+ ),
+ }
+ for reason, code in codes.items():
+ failures = pyexpat_check_failures(start + code, (2, 8, 2))
+ assert failures, code
+ assert all(reason in failure for failure in failures), failures
+ levels = {failure.split(":", 1)[0] for failure in failures}
+ assert levels == {"no -O flag", "-O", "-OO"}, failures
+
+ @pytest.mark.parametrize(
+ ("split", "joined"),
+ [
+ pytest.param(
+ "'a\\\nb' == 'ab'", "'ab' == 'ab'", id="nothing-put-between"
+ ),
+ pytest.param(
+ "'a\\\n b' == 'a b'",
+ "'a b' == 'a b'",
+ id="leading-whitespace-kept",
+ ),
+ pytest.param(
+ "'a\\ \t\nb' == 'ab'",
+ "'ab' == 'ab'",
+ id="spaces-and-tabs-after-the-backslash",
+ ),
+ ],
+ )
+ def test_a_check_split_by_a_continuation_is_read_as_docker_joins_it(
+ self, tmp_path, split, joined
+ ):
+ """A check's RUN line is read as BuildKit joins it.
+
+ BuildKit removes the backslash and the spaces and tabs after it,
+ keeps the next line's leading whitespace and puts nothing between
+ the two lines, so each condition is True in the build and the check
+ passes on any expat. docker build 29.1.3 (classic builder) completes
+ each of these RUN lines on the python:3.14.7 image, whose bundled
+ expat is 2.8.2. The joined line is not the pyexpat check, so it does
+ not count.
+ """
+ check = (
+ "import pyexpat, sys; v = pyexpat.version_info; "
+ "sys.exit(0 if v >= (2, 8, 5) or {} else 'pyexpat embeds an "
+ "expat older than 2.8.5')"
+ )
+ dockerfile = tmp_path / "Dockerfile"
+ dockerfile.write_text(
+ "FROM python:3-slim AS ldr\n"
+ f'RUN python3 -c "{check.format(split)}"\n',
+ encoding="utf-8",
+ )
+ assert misread_dockerfile_syntax(dockerfile) == []
+ body = dockerfile_stages(parse_dockerfile(dockerfile))["ldr"]
+ assert body == [("RUN", f'python3 -c "{check.format(joined)}"')]
+ assert pyexpat_checks(body) == []
diff --git a/tests/security/test_unstructured_url_partition_tripwire.py b/tests/security/test_unstructured_url_partition_tripwire.py
index 6927013d05..8f6851b23a 100644
--- a/tests/security/test_unstructured_url_partition_tripwire.py
+++ b/tests/security/test_unstructured_url_partition_tripwire.py
@@ -11,7 +11,7 @@ SSRF (loopback admin APIs, internal services, cloud metadata endpoints).
Every release carrying the fix (0.24.0+, up to and including 0.27.6) declares
``Requires-Python: <3.14``, and this project's ``requires-python`` is
-``>=3.12,<3.15`` with a Python 3.14.7 production image — so no fixed version
+``>=3.12,<3.15`` with a Python 3.14.8 production image — so no fixed version
can be locked until upstream ships 3.14 support (tracked in the
``.trivyignore`` / ``.grype.yaml`` suppression entries).