Skip to content

Add support for requiring hashes for build dependencies in uv pip - #21314

Open
astral-automations-bot[bot] wants to merge 4 commits into
mainfrom
zb/require-build-hashes
Open

astral-automations-bot[bot] wants to merge 4 commits into
mainfrom
zb/require-build-hashes

Conversation

@astral-automations-bot

Copy link
Copy Markdown
Contributor

Adds an opt-in --require-build-hashes mode for uv pip install and uv pip sync, with a corresponding UV_PIP_REQUIRE_BUILD_HASHES environment variable. The build hash policy remains separate from the runtime requirement policy, so hashes are required for dependencies installed into isolated build environments without changing the requirements installed into the target environment.

@zanieb zanieb changed the title Require hashes for build dependencies Add support for requiring hashes for build dependencies in uv pip Aug 27, 2026
@zanieb zanieb added the enhancement New feature or improvement to existing functionality label Aug 27, 2026
Comment thread crates/uv-cli/src/lib.rs
pub refresh: RefreshArgs,

#[command(flatten)]
pub hash_checking: HashCheckingArgs,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't go in HashCheckingArgs?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it can, I'll look.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's because uv build uses it too

Comment thread crates/uv-cli/src/lib.rs Resolved
@codspeed

codspeed Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 7 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 89 untouched benchmarks
⏩ 84 skipped benchmarks1


Comparing zb/require-build-hashes (07e488e) with main (b4de0ec)

Open in CodSpeed

Footnotes

  1. 84 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@zanieb

zanieb commented Aug 27, 2026

Copy link
Copy Markdown
Member

This will need to wait for the ability to create constraints files with the hashes

@zanieb

zanieb commented Aug 29, 2026

Copy link
Copy Markdown
Member

This doesn't actually need to wait since technically you can construct these constraint files today

Comment thread crates/uv-pip-commands/src/install.rs Outdated
Comment on lines +483 to +484
let build_hash_checking = if require_build_hashes {
Some(HashCheckingMode::Require)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Reject MD5 added after pip install enables required build hashes

When this branch creates a Required build strategy, BuildDispatch::resolve later calls augment_with_requirements for build-system.requires. That path accepts URL-fragment digests without applying Required mode's MD5 filter, so a direct build dependency with only #md5=... is allowed even though ordinary required-hash inputs reject MD5 alone. A separately controlled dependency publisher that prepared colliding artifacts can then replace build-backend bytes and have them downloaded and executed despite the explicit --require-build-hashes guarantee. Apply required-mode algorithm validation during augmentation and add an install regression test for an MD5-only direct build URL.

Comment thread crates/uv-pip-commands/src/sync.rs Outdated
Comment on lines +355 to +356
let build_hash_checking = if require_build_hashes {
Some(HashCheckingMode::Require)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Reject MD5 added after pip sync enables required build hashes

When this branch creates a Required build strategy, BuildDispatch::resolve later calls augment_with_requirements for build-system.requires. That path accepts URL-fragment digests without applying Required mode's MD5 filter, so a direct build dependency with only #md5=... is allowed even though ordinary required-hash inputs reject MD5 alone. A separately controlled dependency publisher that prepared colliding artifacts can then replace build-backend bytes and have them downloaded and executed despite the explicit --require-build-hashes guarantee. Apply required-mode algorithm validation during augmentation and add a sync regression test for an MD5-only direct build URL.

@astral-sh-bot

astral-sh-bot Bot commented Sep 4, 2026

Copy link
Copy Markdown

uv test inventory changes

This PR changes the tests when compared with the main base revision.

  • Added tests: 2
  • Removed tests: 0
  • Changed suites: 2
uv::pip: +1 / -0

Added:

  • uv::pip::pip_sync::require_build_hashes_from_build_constraint

Removed: none

uv::pip_install: +1 / -0

Added:

  • uv::pip_install::pip_install::require_build_hashes_from_build_constraint

Removed: none

error: Failed to build `hash-test @ file://[TEMP_DIR]/`
cause: Failed to resolve requirements from `build-system.requires`
cause: No solution found when resolving: `ok @ http://[LOCALHOST]/ok-1.0.0-py3-none-any.whl#md5=88d6d524262f256596aa7f663c88038b`
cause: In `--require-hashes` mode, all requirements must be pinned upfront with `==`, but found: `ok`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Update both MD5 snapshots to the current diagnostic

Both new require_build_hashes_rejects_md5_url snapshots expect a diagnostic that no longer exists. After augmentation removes the MD5-only entry, Resolver::request_package returns ResolveError::UnhashedPackage, whose message is Hash-checking is required, but no trusted hash was provided for: followed by the package name. These snapshot assertions cannot match the current implementation. Update the install and sync snapshots while retaining the backend-execution assertions.

Comment thread crates/uv-types/src/hash.rs Outdated
Comment on lines +482 to +483
digests.retain(|digest| digest.algorithm() != HashAlgorithm::Md5);
merged.retain(|digest| digest.algorithm() != HashAlgorithm::Md5);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Reuse the shared required-hash algorithm predicate

satisfies_require_hashes is already imported and used by parse_declared_hashes and with_constraint_hashes to define accepted algorithms. Repeating the MD5 exclusion here creates another policy definition that must stay synchronized when algorithm support changes. Use .retain(satisfies_require_hashes) for both vectors so augmented requirements follow the same policy as initial requirements and constraints.

Comment on lines +672 to +675
let trusted_requirements = if is_source_tree {
requirements.as_slice()
} else {
extra_build_dependencies

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Do not trust URL hashes inherited through match-runtime

Not every hash in extra_build_dependencies comes from local configuration. After runtime resolution, ExtraBuildRequires::match_runtime replaces a name-only requirement with the selected source, including its URL fragment. For a direct-URL sdist with static metadata, that URL can come from the sdist’s own runtime dependencies. As on the base, its digest can therefore satisfy --require-build-hashes without an operator-provided hash. Keep the matched source for resolution, but exclude its inferred fragment when collecting trusted hashes.

None,
version_id,
dist,
matches!(source.source, Source::Directory(_)),

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Determine hash trust from the input to the wheel stage

In the default uv build plan, SdistToWheel passes the extracted sdist as source_tree but keeps the original directory as source. This expression is therefore true even when the wheel stage reads a pyproject.toml generated by the backend. A URL hash added only to that sdist still authorizes a build dependency, as it did on the base. Pass the actual stage provenance and carry forward hashes from the original local declarations rather than trusting new ones from the archive.

Comment on lines +379 to +383
let hash_requirements = if self.require_build_hashes() {
trusted_requirements
} else {
requirements
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve hash conflicts for already-authorized build URLs

If an sdist and a build constraint specify the same wheel URL with different SHA-256 digests, the base rejects the conflict in merge_digests. Required mode now skips the sdist fragment entirely, so a wheel matching the constraint can be installed even though it contradicts build-system.requires; verify mode still rejects it. Keep the trusted URL set closed, but check declared fragments for URLs that already have a trusted hash. A distribution can restrict an authorized artifact without authorizing a new one.

Comment on lines +491 to +494
if mode.is_require() {
digests.retain(satisfies_require_hashes);
merged.retain(satisfies_require_hashes);
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Enforce the required-hash invariant when creating the strategy

Filtering merged repairs an existing MD5-only entry only if a later requirement supplies a fragment for that same identity. from_resolution(..., Require), used for pylock.toml, accepts any nonempty digest list, and allows_url checks only map membership. An entry never revisited here remains usable, as on the base. Enforce the nonempty, accepted-algorithm invariant when constructing a Required map; augmentation then only needs to filter incoming digests, not selectively repair existing entries.

Comment on lines +85 to +87
hash. URL fragments in a local project's `build-system.requires` can also supply hashes. For source
distributions, provide hashes in build constraints or other local configuration. Hashes supplied by
a distribution or returned by its build backend cannot authorize a build dependency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Document the local-project restriction in help and settings

The concept guide now distinguishes local projects from sdists, but the --require-build-hashes help in uv-cli and the require-build-hashes setting description in uv-settings still say that fragments in build-system.requires can supply hashes without this qualification. That advice worked for sdists on the base but no longer does. Update both source descriptions and regenerate the schema so users see the same rule where they configure the option.

Comment on lines +379 to +381
let hash_requirements = if self.require_build_hashes() {
trusted_requirements
} else {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Evaluate build-constraint hashes against the build interpreter

On Python 3.12 with --python-version 3.13, pip install and pip sync discard a build constraint marked python_version == '3.12' when constructing build_hasher, even though the isolated build runs on 3.12. If the constraint and the sdist declare the same hashed wheel URL, the base succeeds because it also reads the sdist fragment; head now reports no trusted hash despite the operator having supplied one. Keep the sdist untrusted and construct both pip commands’ build hash strategies with the interpreter’s markers, as pip compile already does.

Comment on lines +480 to +482
if mode.is_require() {
digests.retain(satisfies_require_hashes);
merged.retain(satisfies_require_hashes);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Select an accepted hash before discarding a mixed URL fragment

For a local build-system.requires URL with #md5=…&sha256=…, Hashes::parse_url_fragment returns only the first recognized digest. This filter removes that MD5 and reports no trusted hash; reversing the two parameters succeeds. The base accepted the URL using MD5 alone, so the fix should switch verification to the supplied SHA-256 rather than reject the build. In required mode, select the first accepted algorithm while scanning the fragment, not after selecting a single digest.

Comment on lines 158 to 160
requirements: &'a [Requirement],
trusted_requirements: &'a [Requirement],
hash_override: Option<&'a HashStrategy>,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Make initial and backend hash inputs mutually exclusive

The backend-hook caller now has to remember to pass both &[] and Some(previous_hasher). On the base, supplying the previous strategy was enough to prevent backend output from adding trusted hashes. With these independent arguments, passing the hook requirements alongside that same strategy silently authorizes their fragments in required mode. Represent the two cases as one argument, such as Initial { trusted_requirements } or Backend { previous_hasher }, so the backend path cannot supply new trusted hashes.

Comment on lines +12108 to +12112
uv_snapshot!(context.pip_install()
.arg("requests==1.2")
.arg("--build-constraint")
.arg("build_constraints.txt")
.arg("--require-build-hashes")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Exercise the trusted-input branch in a successful sdist build

Both new success tests build requests==1.2 through the default setup.py backend with registry constraints. They would still pass if the non-local branch always passed &[] and ignored explicitly configured extra-build-dependencies. Retarget one of the two success cases to an sdist requiring a wheel URL whose SHA-256 is supplied only by a local extra-build-dependencies entry. That tests the allowed path alongside the existing sdist rejection test without adding another scenario.

Comment on lines +85 to +87
hash. URL fragments in a local project's `build-system.requires` can also supply hashes. For source
distributions, provide hashes in build constraints or other local configuration. Hashes supplied by
a distribution or returned by its build backend cannot authorize a build dependency.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Point pip sync users to a build-constraint file

The preceding example puts hashes in [tool.uv].build-constraint-dependencies, but PipSyncSettings reads build-constraint files from the CLI or environment, not that workspace setting. On the base, a matching fragment in the sdist masked this distinction; on head, pip install can accept the workspace hash while pip sync rejects the same build. Qualify the advice for uv pip sync and tell readers to pass the hash in a --build-constraint file.

A distribution could authorize its own build dependency by supplying a URL hash in `build-system.requires`. When build hashes are required, only local project requirements and operator-provided configuration can contribute hashes to the build resolution.

This branch was previously deployed

1 inactive (outdated) deployment
automations — 5204a8a5 Deployed Oct 28, 2026 by zanieb via review / security review #52884
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or improvement to existing functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants