Repository navigation
Add support for requiring hashes for build dependencies in uv pip - #21314
astral-automations-bot[bot] wants to merge 4 commits into
Conversation
uv pip
| pub refresh: RefreshArgs, | ||
|
|
||
| #[command(flatten)] | ||
| pub hash_checking: HashCheckingArgs, |
There was a problem hiding this comment.
Can't go in HashCheckingArgs?
There was a problem hiding this comment.
I think it's because uv build uses it too
Merging this PR will not alter performance1 warning
Comparing Footnotes
|
|
This will need to wait for the ability to create constraints files with the hashes |
78d9df0 to
4f8ab32
Compare
4f8ab32 to
d6c3566
Compare
|
This doesn't actually need to wait since technically you can construct these constraint files today |
180ee9c to
67a8e07
Compare
67a8e07 to
023fcc6
Compare
| let build_hash_checking = if require_build_hashes { | ||
| Some(HashCheckingMode::Require) |
There was a problem hiding this comment.
[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.
| let build_hash_checking = if require_build_hashes { | ||
| Some(HashCheckingMode::Require) |
There was a problem hiding this comment.
[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.
uv test inventory changesThis PR changes the tests when compared with the
|
023fcc6 to
c5f70e1
Compare
c5f70e1 to
190685f
Compare
190685f to
f45d445
Compare
5e0a611 to
3c5134b
Compare
3c5134b to
195a5a9
Compare
195a5a9 to
83efee6
Compare
83efee6 to
2e8d766
Compare
| 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` |
There was a problem hiding this comment.
[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.
| digests.retain(|digest| digest.algorithm() != HashAlgorithm::Md5); | ||
| merged.retain(|digest| digest.algorithm() != HashAlgorithm::Md5); |
There was a problem hiding this comment.
[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.
5204a8a to
362c662
Compare
| let trusted_requirements = if is_source_tree { | ||
| requirements.as_slice() | ||
| } else { | ||
| extra_build_dependencies |
There was a problem hiding this comment.
[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(_)), |
There was a problem hiding this comment.
[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.
| let hash_requirements = if self.require_build_hashes() { | ||
| trusted_requirements | ||
| } else { | ||
| requirements | ||
| } |
There was a problem hiding this comment.
[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.
| if mode.is_require() { | ||
| digests.retain(satisfies_require_hashes); | ||
| merged.retain(satisfies_require_hashes); | ||
| } |
There was a problem hiding this comment.
[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.
| 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. |
There was a problem hiding this comment.
[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.
362c662 to
3e682e2
Compare
| let hash_requirements = if self.require_build_hashes() { | ||
| trusted_requirements | ||
| } else { |
There was a problem hiding this comment.
[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.
| if mode.is_require() { | ||
| digests.retain(satisfies_require_hashes); | ||
| merged.retain(satisfies_require_hashes); |
There was a problem hiding this comment.
[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.
| requirements: &'a [Requirement], | ||
| trusted_requirements: &'a [Requirement], | ||
| hash_override: Option<&'a HashStrategy>, |
There was a problem hiding this comment.
[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.
| uv_snapshot!(context.pip_install() | ||
| .arg("requests==1.2") | ||
| .arg("--build-constraint") | ||
| .arg("build_constraints.txt") | ||
| .arg("--require-build-hashes") |
There was a problem hiding this comment.
[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.
| 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. |
There was a problem hiding this comment.
[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.
3e682e2 to
07e488e
Compare
Adds an opt-in
--require-build-hashesmode foruv pip installanduv pip sync, with a correspondingUV_PIP_REQUIRE_BUILD_HASHESenvironment 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.