With the increase in supply-chain attacks, it’s important to harden against attacks on dependencies. In this post, we’ve collected key hardening tips for Python projects, especially those using GitHub and GitHub Actions.

We’ll assume that you have already vetted (or trust) the dependency versions you use. This post will instead focus on protecting against two different cases, which have been popular attack targets recently:

  • The account of the maintainer or the CI setup of a dependency is compromised, and a release with malware is published
  • A server distributing a dependency is hacked, either through the platform itself or by stealing a credential

There are two levels of protection against such attacks. The first is pinning a specific version of each (transitive) dependency. This protects against a new version being published with malicious code. However, PyPI allows adding files to existing releases up to 14 days after release, and GitHub allows changing the value behind a tag used in GitHub Actions. The protection against this is hash-pinning. For each file that can be installed, a cryptographic hash is recorded, often a SHA-256 hash. After downloading the file and before executing anything, the installer verifies that the downloaded contents match the hash. This would even protect against a hack of PyPI or GitHub hosting itself, or a man-in-the-middle attack with forged TLS certificates, as is enabled by the recent eMudhra CA compromise.

uv’s lockfiles include hashes for all files1. This is different for GitHub Actions, which don’t have a central repository like PyPI.

Hash-pin GitHub Actions

For versioning, GitHub Actions uses Git tags and Git SHAs, both of which are resolved against the GitHub repository of the action.

steps:
  # Action referred to by Git tag
  - uses: actions/[email protected]
  # Action referred to by Git SHA
  - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

Git tags, such as v7.0.1, are mutable and can be changed by a Git force-push. Git SHAs, such as 3d3c42e5aac5ba805825da76410c181273ba90b1, are cryptographic records of the contents; a fetch of a Git SHA can only return the requested contents and any manipulation would be flagged2. This protects against a takeover of the action’s repository.

If you are currently using tags, you can convert them to hashes using pinact. To maintain and update the pins, popular tools are Dependabot and Renovate, which can update the hash when a new version (tag) of the GitHub Action is released. It’s possible to configure GitHub settings to allow only pinned Actions to run.

You should also check if any action downloads binaries and whether they are transitively hash-pinned or require manually passing hashes.

You can also start using the GitHub Actions lockfile, which is currently rolling out, but does not have full tooling support yet. This new lockfile also covers actions’ dependencies on other actions.

Hash-pin binary downloads

If you’re downloading binaries from GitHub releases or other URL sources, it's important to validate their integrity before running them. This hardens against a GitHub repository or other third-party resource like an S3 bucket being taken over, potentially without the genuine owner knowing.

When using Bash on Linux, you can check a file using sha256sum -c.

set -e

url="https://github.com/astral-sh/uv/releases/download/0.13.8/uv-x86_64-unknown-linux-gnu.tar.gz"
expected="5acfd9177cfecf0bf87578f059728215ccc884388988f55f7a65041b57387d91"
archive="uv.tar.gz"

wget -q "$url" -O "$archive"
printf '%s  %s\n' "$expected" "$archive" | sha256sum -c -

The same check can be implemented with hashlib for Python scripts on Python 3.11 or newer.

import hashlib
from pathlib import Path
from urllib.request import urlretrieve

url = "https://github.com/astral-sh/uv/releases/download/0.13.8/uv-x86_64-unknown-linux-gnu.tar.gz"
expected = "5acfd9177cfecf0bf87578f059728215ccc884388988f55f7a65041b57387d91"
archive = Path("uv.tar.gz")

urlretrieve(url, archive)
with archive.open("rb") as file:
    actual = hashlib.file_digest(file, "sha256").hexdigest()
if actual != expected:
    raise RuntimeError(f"uv.tar.gz: Expected SHA-256 {expected}, got {actual}")

When writing your own GitHub Actions, you can also implement the check in TypeScript.

import { downloadTool } from "@actions/tool-cache";
import { createHash } from "node:crypto";
import { readFile } from "node:fs/promises";

const url =
  "https://github.com/astral-sh/uv/releases/download/0.13.8/uv-x86_64-unknown-linux-gnu.tar.gz";
const expected = "5acfd9177cfecf0bf87578f059728215ccc884388988f55f7a65041b57387d91";
const archive = "uv.tar.gz";

await downloadTool(url, archive);
const actual = createHash("sha256")
  .update(await readFile(archive))
  .digest("hex");
if (actual !== expected) {
  throw new Error(`uv.tar.gz: Expected SHA-256 ${expected}, got ${actual}`);
}

A pattern for actions that wrap other tools is to embed a file with the hashes for known versions.

Use Trusted Publishing for PyPI

With traditional uploading, there’s a long-lived credential in your CI configuration that an attacker could steal and use. With Trusted Publishing, you tell PyPI which repository, workflow, and environment you intend to publish from. Then, during publishing, GitHub uses OIDC to prove to PyPI that the upload is coming from that publisher. This produces a short-lived and minimally scoped credential, making the window for attack much smaller.

Refer to the uv publishing guide and more broadly to the PyPI Trusted Publisher documentation for instructions.

jobs:
  publish:
    runs-on: ubuntu-latest
    # Use an environment
    environment:
      name: pypi
    # Allow this job to use OIDC credentials for publishing
    permissions:
      id-token: write
    steps:
      [...]
      - name: Publish
        # uv will pick up on the id-token and use Trusted Publishing
        run: uv publish

Consider locking down release workflows

In a project with multiple team members, you may want to lock down the release workflow using a GitHub Actions environment with requirements such as requiring an approval while banning self-approvals. This reduces the impact of a single account or credential being compromised.

Use zizmor

We recommend configuring zizmor, a linter for CI security best practices. It includes lints for some of the problems mentioned above, such as GitHub Actions with mutable tags and missing Trusted Publishing, but also many others, such as flagging the notoriously dangerous workflow triggers pull_request_target, workflow_run, and issue_comment.

Use dependency cooldowns

PyPI removes the vast majority of malware reports within 24 hours. A dependency cooldown gives security scanning parties time to detect malicious releases before you install them. In uv, set exclude-newer to exclude files published within the cooldown period. Use exclude-newer-package to override it for individual packages:

[tool.uv]
exclude-newer = "3d"
exclude-newer-package = {
    # Move the cooldown ahead (or back) to a specific date
    tqdm = "2026-11-20T00:00:00Z",
    # Ignore the cooldown for this package
    markupsafe = false,
}

Dependency cooldowns are also supported by Dependabot with the cooldown option and by Renovate using Minimum Release Age.

Dependabot: .github/dependabot.yml

version: 2
updates:
  - package-ecosystem: "uv"
    directory: "/"
    schedule:
      interval: "daily"
    cooldown:
      default-days: 3

Renovate: renovate.json

{
  "extends": ["security:minimumReleaseAgePypi"]
}

Hash-pin tools in CI

When using a tool in CI with uvx <tool>, any bad release of the tool or its dependencies could take over your CI runs and steal all your credentials. To prevent this, you can lock and hash-pin the tools with their dependencies.

Locally, first lock the tool as a dependency group. Dependency groups can be installed and used independently of your runtime dependencies. For example, if you are using Nox as a workflow manager, we can add it to a group named after the tool.

uv add --group nox nox

In the CI configuration, find unpinned invocations of Nox.

uvx nox -s mylintcommand

Replace them with calls using the lockfile.

uv run --only-group nox --isolated nox -s mylintcommand

More verbose, but also more secure.

A wrinkle here is that this only works when uv.lock is up to date with pyproject.toml; otherwise uv run still has to resolve. You can assert this with --locked in any individual command, or by setting the environment variable UV_LOCKED=1 for the entire workflow.

For GitHub Actions, this can be configured at the top level of any workflow file (.github/workflows/*.yml).

env:
  UV_LOCKED: 1

Use lockfiles for scripts

Python scripts can define inline dependencies (PEP 723).

#!/usr/bin/env -S uv run --script
#
# /// script
# requires-python = ">=3.15"
# dependencies = ["httpx2"]
# ///

import httpx2

print(httpx2.get("https://example.com"))

By default, a script resolves to the latest version of its (transitive) dependencies. As with workspaces, uv lock also supports scripts. Locking a script foo.py creates a script lockfile foo.py.lock with pins and hashes for all dependencies.

uv lock --script foo.py

Now, running the script will use the locked dependencies.

uv run foo.py

The considerations from the section above about --locked and UV_LOCKED=1 apply here too.

Disallow source distribution builds

Python packages have files in two formats: source distributions and wheels. If there is no matching wheel for the current platform, uv runs the build backend from the source distribution to build a wheel. This mechanism is important, for example, for wider platform support or to support newer Python versions for older releases. However, build backends and their dependencies currently need to be manually hash-pinned and those builds mean code execution at a critical location. If suitable for your dependencies and your deployments, we recommend disabling source distribution builds:

[tool.uv]
no-build = true

If there are individual packages that are supposed to get built from source, you can exempt them by forcing a build from source:

[tool.uv]
no-build = true
no-binary-package = ["foo"]

This is not feasible for all projects and should be regarded as defense in depth.

Footnotes

  1. Assuming the index consistently provides hashes, which is the case for PyPI and the major commercial registry products. ↩

  2. Git uses SHA-1, which has known weaknesses, but GitHub has rolled out protections against that. Git 3.0 will switch to SHA-256 for new repositories. ↩