Python

Deploy a git checkout and create the virtual environment from a lockfile that pins file hashes. The attesterThe program on each server. It collects facts and reports them as evidence; it never decides whether the server passes. hashes every file in the checkout, every file each installed distribution's RECORD lists, the scripts in the environment's bin/, and its start-up hook, and inspects each Python process. The verifierThe program in CI that sends a nonce, fetches references itself, compares, and writes the report. downloads the wheel whose SHA-256 the lockfile pins, checks it, and compares its contents with what is installed, following the wheel install rules.

What gets verified#

WhatReferenceCheck
Tracked filesThe deployed commit, on the audited branchsource
Each installed distribution in site-packagesThe wheel the lockfile pins by SHA-256, matched to the installed WHEEL tags, compared file by file through RECORDpackages:pypi
Entry-point scripts in bin/The script the installer generates for the wheel's entry pointspackages:pypi
Other files in bin/Must belong to an installed distribution; activation scripts and interpreter links are listedpackages:pypi
Files in site-packages no distribution claimsNone: Python runs .pth files and sitecustomize at start-up, so each one failspackages:pypi
The _virtualenv start-up hook (uv, virtualenv)The hook in the source of the uv or virtualenv version pyvenv.cfg namespackages:pypi
pip, setuptools, wheel seeded by the environment toolThe registry release, when the lockfile does not pin them; on Debian and Ubuntu, also the distribution's patched wheel (python3-pip-whl, python3-setuptools-whl) of that version in its signed archive, which python3 -m venv installspackages:pypi
Each processPYTHONPATH, PYTHONHOME, PYTHONSTARTUP, PYTHONBREAKPOINT, PYTHONWARNINGS imports, -c, -X pycache_prefix=, debuggers (-m debugpy, -m pdb), memory mapsprocess

Requirements#

  • Commit a lockfile with hashes. The first one found at the repository root is read, in this order: uv.lock, pylock.toml, poetry.lock, Pipfile.lock, requirements.lock, requirements.txt. A requirements file must carry --hash=sha256: for each package (pip-compile --generate-hashes, uv export).
  • Install from the lockfile, nothing else:
    • uv sync --frozen --no-install-project
    • poetry install --sync
    • pipenv install --deploy
    • pip install --require-hashes -r requirements.txt
  • Install wheels, not source distributions. A package the server built from a source distribution has no reference (policy.unverifiablePackages, default fail).
  • Do not install the project itself into the environment. Its entry in the lockfile pins no hash, so it cannot be verified. Run it from the checkout instead (--no-install-project with uv).
  • Do not install in editable or egg mode (setup.py develop): no file hashes are recorded.
  • Keep bytecode out: install without compiling (uv's default, pip install --no-compile) and run with PYTHONDONTWRITEBYTECODE=1 or python -B. Bytecode written while the app runs also adds __pycache__ to the directories of its code after the process started, which fails (policy.metadataChangedAfterStart).
  • Ignore the environment and __pycache__/ in .gitignore.

Attester configuration#

YAML
version: 2
services:
  - name: app
    root: /srv/app/current
    user: app
    ecosystems: [pypi]

Environments named .venv, venv, env, .virtualenv or virtualenv in the service root are found (each needs a pyvenv.cfg). For an environment elsewhere, give its site-packages directory:

YAML
    installs:
      - ecosystem: pypi
        dir: /opt/venvs/app/lib/python3.12/site-packages

The environment's bin/ and pyvenv.cfg are read from three levels above that directory.

Verifier configuration#

YAML
version: 2
services:
  - name: app
    repository:
      url: https://github.com/example/app.git
      branch: main
    # Where the lockfile is, when not at the repository root:
    # lockfiles:
    #   pypi: backend/uv.lock
servers:
  - name: app1
    host: app1.example.com

Registry and source locations can be changed under references.registries (pypi, uvSource). See configuration.

Build and deploy#

The tested setup:

Shell
uv lock                                   # in the repository, then commit uv.lock
git clone https://github.com/example/app.git /srv/app/current
cd /srv/app/current
UV_PYTHON_DOWNLOADS=never PYTHONDONTWRITEBYTECODE=1 uv sync --frozen --no-install-project
.venv/bin/python -B main.py
  • With pip on Debian or Ubuntu: python3 -m venv .venv, then PYTHONDONTWRITEBYTECODE=1 .venv/bin/pip install --no-compile --require-hashes -r requirements.txt. python3 -m venv compiles the pip it installs; remove that bytecode once: find .venv -name __pycache__ -prune -exec rm -r {} +. The pip the environment starts with is the distribution's, compared with its wheel in the signed archive. The interpreter is compared with the archive too: a Python from another repository (a PPA) is not in it, and is reported as unexplained code unless listed in executables.
  • gunicorn, uvicorn and Celery run from .venv/bin/; their scripts are compared with the installer's template for their entry points.
  • Restart the workers after each deploy. A tracked file changed after a process started fails (policy.modifiedAfterStart).

Common findings#

FindingCause and fix
No lockfile at the deployed commit pins these packagesNo supported lockfile at the commit. Commit one, or set lockfiles.pypi.
installed package is not pinned by the lockfileThe environment has a package the lockfile does not list. Recreate it from the lockfile.
the lockfile pins no hashes for this packageA requirements file without --hash, or the project itself installed into the environment.
built on the server from a source distributionNo wheel for the server's platform and Python version is pinned. Pick versions that publish wheels for it, and lock again.
files differ from the pinned wheelA file in site-packages was changed after install. Recreate the environment.
Python bytecode (.pyc) cannot be verified__pycache__ in site-packages. Install without compiling and run with PYTHONDONTWRITEBYTECODE=1 (policy.bytecode, default warn). Pip writes bytecode for its own modules when it runs, so set the variable for the install too.
Files in bytecode cache directories that are not bytecodeSomething other than .pyc is in a __pycache__ directory. Remove it.
Files in site-packages that belong to no installed packageA stray .pth, sitecustomize.py or leftover. Remove it.
Files in the environment's bin directory that no installed package createdA script added to bin/ by hand. Remove it.
The environment's start-up hook ... differs from its creator's release_virtualenv.py or _virtualenv.pth was changed. Recreate the environment.
The environment's start-up hook (_virtualenv) matches its creator's releaseInformational: the hook matches the uv or virtualenv release that created the environment.
Interpreter links and activation scripts in the environment (not run by the application)Informational: files in bin/ that belong to the environment, not to a package.
Packages the environment tool installed (not in the lockfile) match the distribution's patched wheels in its signed archiveInformational: python3 -m venv on Debian or Ubuntu installed the distribution's pip (or setuptools), which differs from the registry's; it matches the wheel the distribution's package of that version ships.
PYTHONPATH (process)The service sets PYTHONPATH. Remove it, or run with -I.

Limits#

  • Bytecode caches cannot be compared with anything; they are only listed.
  • Source distributions built on the server cannot be verified.
  • The interpreter itself is not part of this check. It is explained as code when a distribution package owns it (compared with the signed Debian or Ubuntu archive), or when it is pinned under executables; see the verifier guide.
  • The project's own .py files are compared through the git checkout, not the environment.

See how it works for the overall flow.

Edit this page on GitHub