|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +name: Set up hardened Python |
| 6 | +description: >- |
| 7 | + Install CPython with a pinned uv, create a venv from it, and put the venv |
| 8 | + first on PATH. setup-uv verifies the uv binary and uv verifies the |
| 9 | + python-build-standalone archive against embedded checksums, so the |
| 10 | + interpreter's integrity does not depend on TLS. The job then fails unless |
| 11 | + the interpreter's ssl module rejects the compromised eMudhra/emSign CA keys |
| 12 | + (CVE-2026-115557; first fixed releases: 3.11.18, 3.12.16, 3.13.18, 3.14.10, |
| 13 | + 3.15.2), and pip is updated through that ssl module. Unlike setup-python, |
| 14 | + this provides patched Windows builds of maintenance series, for which |
| 15 | + python.org publishes no installers. See |
| 16 | + https://github.com/NVIDIA/cuda-python/issues/3195 and |
| 17 | + https://discuss.python.org/t/109678. |
| 18 | +
|
| 19 | +inputs: |
| 20 | + python-version: |
| 21 | + description: >- |
| 22 | + uv Python request, e.g. "3.12" or "3.14t" (free-threaded). The newest |
| 23 | + patch known to the pinned uv is installed. Only series with a fixed |
| 24 | + release listed in the interpreter check (3.11-3.15) are accepted. |
| 25 | + required: true |
| 26 | + venv: |
| 27 | + description: Directory for the venv. Defaults to a new directory under RUNNER_TEMP. |
| 28 | + default: "" |
| 29 | + |
| 30 | +outputs: |
| 31 | + python-path: |
| 32 | + description: Absolute path of the venv's interpreter. |
| 33 | + value: ${{ steps.venv.outputs.python-path }} |
| 34 | + python-version: |
| 35 | + description: Full version of the interpreter, e.g. "3.14.10". |
| 36 | + value: ${{ steps.venv.outputs.python-version }} |
| 37 | + |
| 38 | +runs: |
| 39 | + using: composite |
| 40 | + steps: |
| 41 | + - name: Install uv |
| 42 | + uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 |
| 43 | + with: |
| 44 | + # The newest uv whose checksum ships inside the pinned setup-uv, so |
| 45 | + # the binary is verified against the SHA-pinned action itself. The uv |
| 46 | + # release determines the newest patch of each CPython series, so |
| 47 | + # bumping the setup-uv SHA also moves the CPython patch versions. |
| 48 | + version: "latest-known" |
| 49 | + enable-cache: false |
| 50 | + # Some callers (e.g. release verification) have no checkout. |
| 51 | + ignore-empty-workdir: true |
| 52 | + |
| 53 | + - name: Install Python and create venv |
| 54 | + id: venv |
| 55 | + shell: bash --noprofile --norc -euo pipefail {0} |
| 56 | + env: |
| 57 | + PY_REQUEST: ${{ inputs.python-version }} |
| 58 | + VENV_DIR: ${{ inputs.venv }} |
| 59 | + # A per-job install directory ensures the newest patch known to the |
| 60 | + # pinned uv is used, never an older interpreter left on a persistent |
| 61 | + # self-hosted runner. |
| 62 | + UV_PYTHON_INSTALL_DIR: ${{ runner.temp }}/setup-hardened-python/interpreters |
| 63 | + UV_NO_CONFIG: "1" |
| 64 | + run: | |
| 65 | + uv python install --no-bin --no-registry "${PY_REQUEST}" |
| 66 | + base="$(uv python find --managed-python --no-project "${PY_REQUEST}")" |
| 67 | + venv="${VENV_DIR}" |
| 68 | + if [[ -z "${venv}" ]]; then |
| 69 | + # A fresh directory per call, so a second call in the same job |
| 70 | + # (e.g. build-wheel.yml) leaves the first venv intact. |
| 71 | + venv="$("${base}" -c 'import sys, tempfile; print(tempfile.mkdtemp(prefix="venv-", dir=sys.argv[1]))' "${RUNNER_TEMP}")" |
| 72 | + fi |
| 73 | + # The stdlib venv seeds pip from the interpreter's bundled wheel, so |
| 74 | + # nothing is downloaded before the ssl check below. |
| 75 | + "${base}" -m venv --clear "${venv}" |
| 76 | + if [[ "${RUNNER_OS}" == Windows ]]; then |
| 77 | + py="${venv}/Scripts/python.exe" |
| 78 | + else |
| 79 | + py="${venv}/bin/python" |
| 80 | + fi |
| 81 | + # Let the interpreter report native absolute paths (Windows included). |
| 82 | + python_path="$("${py}" -c 'import sys; print(sys.executable)')" |
| 83 | + "${py}" -c 'import os, sys; print(os.path.dirname(sys.executable))' >> "${GITHUB_PATH}" |
| 84 | + { |
| 85 | + echo "python-path=${python_path}" |
| 86 | + echo "python-version=$("${py}" -c 'import platform; print(platform.python_version())')" |
| 87 | + } >> "${GITHUB_OUTPUT}" |
| 88 | +
|
| 89 | + - name: Check Python interpreter is patched (CVE-2026-115557) |
| 90 | + # Runs the venv's python through the PATH entry added above. |
| 91 | + shell: python {0} |
| 92 | + env: |
| 93 | + PYTHON: ${{ steps.venv.outputs.python-path }} |
| 94 | + run: | |
| 95 | + import os |
| 96 | + import platform |
| 97 | + import sys |
| 98 | +
|
| 99 | + if os.path.normcase(sys.executable) != os.path.normcase(os.environ["PYTHON"]): |
| 100 | + print(f"::error::PATH resolves python to {sys.executable}, not the venv interpreter {os.environ['PYTHON']}.") |
| 101 | + sys.exit(1) |
| 102 | +
|
| 103 | + FIXED = {(3, 11): 18, (3, 12): 16, (3, 13): 18, (3, 14): 10, (3, 15): 2} |
| 104 | + v = sys.version_info |
| 105 | + series = (v.major, v.minor) |
| 106 | + name = f"{v.major}.{v.minor}" |
| 107 | + full = platform.python_version() |
| 108 | + print("sys.version:", sys.version) |
| 109 | + if series in FIXED: |
| 110 | + floor = f"{name}.{FIXED[series]}" |
| 111 | + if v.micro < FIXED[series]: |
| 112 | + print(f"::error::Python {full} is older than {floor}, the first {name} release whose ssl module rejects the compromised eMudhra CA keys (CVE-2026-115557).") |
| 113 | + sys.exit(1) |
| 114 | + print(f"Python {full}: ssl module carries the fix (first fixed: {floor}).") |
| 115 | + else: |
| 116 | + # Fail closed: a series is only accepted once its fixed release is listed. |
| 117 | + print(f"::error::Python {full}: no {name} release is known to reject the compromised eMudhra CA keys (CVE-2026-115557).") |
| 118 | + sys.exit(1) |
| 119 | +
|
| 120 | + - name: Update pip |
| 121 | + shell: bash --noprofile --norc -xeuo pipefail {0} |
| 122 | + env: |
| 123 | + PYTHON: ${{ steps.venv.outputs.python-path }} |
| 124 | + run: | |
| 125 | + "${PYTHON}" -m pip install -U pip |
0 commit comments