Securing the Python Software Supply Chain with uv.lock, Hashes, and SBOMs

Infrastructure & DevOps

Secure Python builds with uv.lock, SHA-256 hashes, SBOMs, and provenance. Add CI controls that make dependency updates reviewable and releases traceable.

TL;DR

  • A lockfile makes dependency resolution repeatable within its platform and project constraints. It does not prove that a package is safe, that its publisher is authentic, or that your final container contains only expected files.
  • Commit uv.lock, check it against project metadata in CI, and install from the committed lock with a locked command. Review intentional upgrades rather than silently resolving new versions during release.
  • Use a Software Bill of Materials (SBOM) to inventory what is in a release. Generate it from the built artifact as well as the source environment, and keep its subject tied to an artifact digest.
  • Use hash verification, provenance, action pinning, vulnerability and license policy, artifact signing, and isolated builds as separate controls. Each answers a different security question.

Where Python supply chain risk enters the build

A Python service can be compromised before its own code runs. A package name can be mistyped, an account or maintainer can be compromised, a release can contain unexpected code, or a build workflow can execute untrusted scripts with deployment credentials. A new dependency can also introduce a license or known vulnerability that your release process has no way to see. An old lockfile prevents some surprise upgrades, but it cannot make those other risks disappear.

Separate the guarantees you want. Dependency resolution answers which package versions and artifacts should be installed. Artifact integrity asks whether the bytes you downloaded match an expected digest. Package provenance asks which identity and build process produced a release. An SBOM records a machine-readable inventory. Vulnerability analysis compares that inventory with advisory sources. Build provenance relates an output to source and build inputs. Code review, least privilege, and isolation constrain what can happen when a package or workflow is malicious.

These controls are useful together because none is a substitute for the others. A SHA-256 digest can detect that an archive differs from the locked archive, but if the lock update was approved after a compromised package was published, the digest faithfully identifies the wrong bytes. A vulnerability scanner can flag a known advisory but may have no evidence for a new malicious release. An SBOM lists components, but it is not a security verdict.

Lock dependency resolution with uv

Use pyproject.toml to declare direct requirements and policy constraints. Use the committed uv.lock to capture resolved versions and artifact details for the project. uv documents its lockfile as cross-platform and intended to be checked into version control. The lock is generated by uv, so update it through uv commands instead of hand-editing the file. uv project structure and lockfiles

[project]
name = "service-api"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
  "fastapi>=0.115,<1",
  "uvicorn[standard]>=0.34,<1",
]

[dependency-groups]
dev = [
  "pytest>=8.3,<9",
  "ruff>=0.11,<1",
]

The example declares acceptable ranges, while the lock records the selected resolution. That separation lets a team accept compatible updates through a reviewed lockfile change without pretending that the broad ranges are exact. Keep runtime and development dependencies distinguishable so the release environment excludes tools it does not need.

Generate or update the lock deliberately with uv lock. Review the resulting package changes, source indexes, platform markers, and hashes along with the dependency manifest. To verify CI has a current lock without rewriting it, use uv lock --check or a command with --locked. uv documents that --frozen uses the lock without checking whether it matches project metadata. Prefer the check when the purpose is to fail on stale metadata. uv locking and syncing behavior

# Developer makes an intentional dependency change
uv add "httpx>=0.28,<1"
uv lock

# CI verifies committed metadata and installs the locked runtime set
uv lock --check
uv sync --locked --no-dev

Do not treat uv sync --locked as a complete hardened build. It is one step in the workflow. Use a fresh environment or container, avoid installing undeclared global packages into the release, and make sure the artifact actually deployed is the artifact you scanned and signed. Pin the Python version and base image digest in the build environment where your reproducibility requirements call for them.

Know what the lock hash does and does not prove

Artifact hashes let the installer compare downloaded bytes with the digest recorded during resolution. That helps detect a changed file when installing from the lock. It does not establish who built the artifact or whether its contents are benign. Source distributions add another boundary because a build backend and build dependencies can execute while producing a wheel. Evaluate whether your production path accepts source builds, which build tools run, and whether those tools are also constrained and isolated.

If your release toolchain uses pip and requirements files, pip offers hash-checking mode through --require-hashes. The mode requires pinned requirements and hashes for all transitive requirements, so it fails closed when an unhashed dependency appears. The pip documentation recommends SHA-256 and describes --only-binary :all: as an additional way to disallow source distributions in that install path. These flags can be incompatible with editable or VCS installs that lack a known archive hash, so separate development workflows from the hardened release installation. pip secure installs and hash-checking mode

Reduce dependency confusion and source ambiguity

A private package index does not automatically make names private. If a dependency is expected from an internal registry, configure the resolver so that the public index cannot silently supply a same-named package. Use explicit source configuration and keep credentials out of committed manifests. Restrict which package indexes the build can reach, review direct URL and Git dependencies, and resolve Git references to immutable commits rather than moving branches.

Review the dependency graph as product code. A newly introduced package needs an owner, a reason, an update path, and an acceptable maintenance and license status. Prefer established projects, inspect ownership and release history, and check whether the package is actually needed in the runtime image. A popular package can still be compromised, so review is a risk-reduction control rather than a guarantee.

Automated upgrade pull requests should show the resolution diff and run the same checks as normal code changes. Avoid scheduled updates that merge solely because they pass a build. A lockfile update can introduce a malicious release that is syntactically valid and fully test passing. Use review requirements proportional to the package's privileges and exposure. For higher-risk dependencies, review release attestations and upstream security advisories before approving.

Generate an SBOM for the artifact you ship

A Software Bill of Materials (SBOM) is a structured inventory of software components and, depending on its format and completeness, their relationships and metadata. CycloneDX and SPDX are widely used formats. An SBOM helps answer questions such as which releases contain an affected library, but it does not prove the inventory is complete or that components are safe. The inventory source and generation step matter.

uv can export a lockfile in CycloneDX JSON format, which is useful for a dependency-resolution view:

uv export --locked --format cyclonedx1.5 -o build/dependencies.cdx.json

Then generate an SBOM from the actual container or packaged output. A source lock may include development or optional packages that never ship, while a final image may contain operating-system packages, copied binaries, or dependencies installed outside the Python resolver.

# Generate an inventory from the exact image digest prepared for release
syft "registry.example.com/service-api@sha256:$IMAGE_DIGEST" \
  -o cyclonedx-json=build/image.cdx.json

# Preserve a separate SPDX view when a consumer requires it
syft "registry.example.com/service-api@sha256:$IMAGE_DIGEST" \
  -o spdx-json=build/image.spdx.json

Syft documents output options for CycloneDX and SPDX, including image and filesystem scanning. Pin the Syft version used by CI and record its invocation and output beside the release record. Validate the produced document against the supported schema or consumer requirements before publishing it. Do not convert between SBOM formats without checking whether conversion loses relationships or fields. Syft SBOM generation and output formats

Attach the inventory to an immutable artifact digest and keep the report after deployment. If the image is rebuilt after a scan, the old SBOM is no longer evidence about the new image. Avoid storing secrets, private repository URLs, or sensitive build parameters in public SBOM metadata. Keep a signed copy internally and publish only fields permitted by your disclosure policy.

Use CI to enforce policy with least privilege

A useful pipeline has distinct jobs for resolution checks, tests, policy scans, artifact build, SBOM generation, provenance, and publication. The deployment credential should not be available to pull request code that can be changed by an untrusted contributor. Give read-only permissions by default, grant publishing only in the release job, and use environment protection or a separate trusted workflow where required.

name: python-supply-chain
on:
  pull_request:
  push:
    branches: [main]

permissions:
  contents: read

jobs:
  verify:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@<reviewed-full-commit-sha>
      - uses: astral-sh/setup-uv@<reviewed-full-commit-sha>
        with:
          enable-cache: true
      - run: uv lock --check
      - run: uv sync --locked
      - run: uv run --locked pytest
      - run: uv export --locked --format cyclonedx1.5 -o dependencies.cdx.json

The action references are placeholders: replace them with full commit SHAs reviewed from the official repositories. GitHub states that a full-length commit SHA is the immutable way to reference an action release. Its security guidance also recommends least-privilege workflow permissions and warns that secrets can escape through transformed output or logs. GitHub Actions secure-use guidance

Build and scan the release image in a trusted job, then use the same digest for vulnerability analysis, SBOM generation, signing, and deployment. A policy gate should distinguish severity, reachability, fixed-version availability, and accepted exceptions. Avoid failing on every advisory without triage, but make exceptions named, time-bound, and reviewable. License policies need the actual license metadata and project-specific rules, not guesses based on package names.

Add provenance and verify the publishing path

Provenance records how an artifact was built, including its inputs and builder identity. SLSA defines an attestation model for describing source and build inputs, while PyPI documents attestations that bind published distributions to a cryptographic digest and an identity such as a publishing workflow. These records help consumers verify origin. They do not mean the source code was reviewed or that the build was safe; the consumer still needs a trust policy for the builder and its inputs. SLSA provenance specification · PyPI attestations

For publishing Python packages, prefer a short-lived trusted publishing identity supported by the package index instead of a long-lived API token stored in CI. Restrict the workflow that can publish, protect release branches and environments, and verify the artifact digest before it leaves the build. Where you consume packages, check available provenance against an explicit expected publisher and repository rather than accepting any valid signature.

Keep build inputs hermetic where feasible. An isolated builder, pinned action revisions, constrained network access, and declared toolchain reduce hidden inputs. Reproducibility can help investigate differences, but identical builds are not themselves proof against a compromised source, compiler, or builder. Record both the source revision and the produced digest so an incident responder can identify what ran.

What to check after an advisory or suspected compromise

  1. Use the deployed image digest and its SBOM to identify affected services and package versions.
  2. Compare the lockfile and source manifests with the last trusted release. Identify changed versions, indexes, URLs, and build backends.
  3. Inspect provenance and CI run records to confirm the source commit, builder, workflow revision, and artifact digest.
  4. Determine whether the package was imported or executed in reachable runtime paths. Do not rely on reachability alone when build-time code may have run.
  5. Rebuild from reviewed inputs after pinning or removing the affected dependency. Rotate credentials if untrusted build code could have accessed them.
  6. Preserve evidence and record the deployed artifact replacement. A clean dependency scan does not erase exposure to an earlier image.

Plan the inventory before an incident. Keep the current SBOM, prior release SBOMs, lockfile, source commit, image digest, and build attestation searchable by release identifier. Measure time to identify an affected artifact, time to rebuild, and time to confirm deployment. These operational measures describe your response capability without turning them into a universal security score.

Frequently asked questions

Does uv.lock make a Python project secure?

No. A lockfile makes dependency resolution repeatable and records resolved package details, but a locked release can still contain vulnerable or malicious code. Add review, source controls, isolated builds, artifact scanning, an SBOM, and trusted provenance. Keep the lock checked in and validate it during CI so release installs use the reviewed resolution.

Is an SBOM the same as a vulnerability scan?

No. An SBOM is an inventory of components and sometimes their relationships. A vulnerability scanner compares an inventory with an advisory source and policy. The scan may miss vulnerabilities, and an SBOM may be incomplete if it was generated from the wrong directory or before the final image was assembled. Generate it from the release artifact.

Should a CI pipeline use pip hashes or uv.lock?

Choose a dependency workflow and enforce it consistently. uv projects can use a committed uv.lock with uv lock --check and locked sync commands. A pip requirements workflow can use --require-hashes, which requires every transitive requirement to be pinned and hashed. Avoid a second generated dependency list that can silently drift from the lock.

Do full commit SHAs make GitHub Actions safe?

A full-length commit SHA pins an action to immutable source revision, which reduces risk from a mutable tag. It does not make the pinned action trustworthy by itself. Verify that the SHA belongs to the official repository, review the action's behavior and permissions, and keep its update and vulnerability review process current.

Turn dependency data into a release control

Start with a committed lockfile and a CI check that refuses stale dependency metadata. Add artifact-level inventory, advisory scanning, source and publisher review, and provenance as separate controls. Make each release traceable to the exact image digest, source revision, and workflow that produced it. That gives engineering teams a practical basis for triage when a dependency or build tool is later questioned.

If you need to harden a Python build and release pipeline, talk with our engineering team about lockfile policy, SBOM coverage, provenance verification, and least-privilege CI design. For resilient background processing patterns that can survive retries safely, read our guide to worker quotas and queue isolation.

Dumb Potato Editorial

Putting this into practice?

Share the problem you’re solving and the tradeoffs you’re weighing with our engineering team.

See how we help with cloud and devops

Discuss your project