Skip to content

Install tools from packaged locks - #21962

Closed
konstin wants to merge 19 commits into
konsti/build-packaged-lock-pep517from
konsti/locked-tools
Closed

konstin wants to merge 19 commits into
konsti/build-packaged-lock-pep517from
konsti/locked-tools

Conversation

@konstin

@konstin konstin commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Adds --locked to uv tool install, uv tool run/uvx, and uv tool upgrade behind the locked-tools preview feature. Runtime dependencies come from the selected wheel's .dist-info/pylock.toml, with artifact hashes required and verified. Stacked on #22595, which extends lock export to PEP 517 builds.

Retained locked dependencies must use wheel or source distributions listed by a configured package index for the locked name and version, with filenames matching the index. Their URLs and filenames are checked before dependency files are accessed, including files for other platforms or extras. Selected artifacts require a non-MD5 hash; uv also checks non-MD5 hashes supplied by the index and checks Python compatibility against the index and wheel metadata. Local paths, VCS sources, and direct archives are rejected for retained locked dependencies. Offline use requires cached index metadata. Build requirements for source distributions are resolved separately and are not pinned by the packaged lock. Lock, index, and explicitly supplied constraint and override hashes are checked before running a source build backend. Hashes discovered later in dependency metadata are checked when encountered; a backend may already have run.

Tool selection skips releases without a wheel compatible with the requested Python or platform before reading the lock. Git sources and source-only distributions are not supported for the tool itself. Once a wheel is selected, a missing, invalid, or incompatible lock fails the operation instead of searching older releases for a lock. The selected wheel is bound to its computed hash, including when an index supplies no hash. Installed tools remember locked mode for later upgrades; --unlocked returns to normal resolution. Locked runs use an environment built from the packaged lock. Existing packages are reinstalled during locked installation and upgrade because their archive hashes cannot be verified from the installed files.

Overrides can replace locked packages and resolve their new dependencies. Exclusions omit named packages. Since a standard lock does not provide an installation dependency graph, uv retains every other selected locked package even if it becomes unnecessary. Constraints must be compatible with retained locked packages. Selecting extras for the tool, --with, scoped exclusions, and overrides or constraints with extra markers are unsupported. Scoped overrides apply to the tool and retained locked packages, but not packages that are themselves replaced; scoped overrides introduced by a replacement cannot replace another locked package. When an override or constraint uses hashes, overrides must include package names. UV_TOOL_LOCKED enables locked tool operations by default.

&requirement.extras,
&groups,
&settings.build_options,
Some(HashCheckingMode::Verify),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Require hashes in packaged tool locks

HashCheckingMode::Verify only checks digests that exist, while the PEP 751 parser still accepts hashes = {} with a warning. Consequently an opt-in --locked install, run, or upgrade can consume a packaged lock with no digest for the selected dependency, allowing a dependency publisher or mutable origin to replace that artifact after the tool wheel was published and have the new code installed with developer or CI privileges. Require hashes here so missing digests fail; this one-line change plus an empty-hash regression test passed all five focused packaged-lock tests.

Suggested change
Some(HashCheckingMode::Verify),
Some(HashCheckingMode::Require),

@konstin
konstin force-pushed the konsti/build-export-lock branch from 05a6998 to 374fd4e Compare September 24, 2026 10:40
@konstin
konstin force-pushed the konsti/locked-tools branch from 3aa08e8 to 4a52c5a Compare September 24, 2026 10:40
@konstin
konstin force-pushed the konsti/build-export-lock branch from 374fd4e to fe0d582 Compare September 24, 2026 10:42
@konstin
konstin force-pushed the konsti/locked-tools branch from 4a52c5a to 91b97f9 Compare September 24, 2026 10:42
@konstin
konstin force-pushed the konsti/locked-tools branch from 91b97f9 to eefe4ad Compare September 24, 2026 10:59
@konstin
konstin force-pushed the konsti/build-export-lock branch from 33b84c0 to d1b396c Compare September 24, 2026 12:15
@konstin
konstin force-pushed the konsti/locked-tools branch from eefe4ad to dbab749 Compare September 24, 2026 12:15
@konstin
konstin force-pushed the konsti/build-export-lock branch from d1b396c to 8687c59 Compare September 24, 2026 12:22
@konstin
konstin force-pushed the konsti/locked-tools branch from dbab749 to 793390f Compare September 24, 2026 12:22
Comment thread crates/uv/src/commands/tool/locked.rs Outdated
let build_hasher = HashStrategy::from_constraints(
build_constraints,
Some(&interpreter.to_resolver_marker_environment()),
HashCheckingMode::Verify,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Require hashes for locked tool build dependencies

HashCheckingMode::Verify only validates build artifacts present in optional build constraints, while BuildDispatch resolves an sdist's build-system.requires from current indexes. An independent build-dependency publisher can therefore upload a compatible malicious release after lock creation and have locked tool install, run, or upgrade install and execute it with the caller's privileges. Fail closed for unknown build artifacts; this change compiled with cargo check --locked -p uv, and the focused hash-policy tests passed.

Suggested change
HashCheckingMode::Verify,
HashCheckingMode::Require,

@zanieb

zanieb commented Sep 24, 2026

Copy link
Copy Markdown
Member

Why do we reject --with? Those go into a layered environment anyway, right?

@zanieb

zanieb commented Sep 24, 2026

Copy link
Copy Markdown
Member

I think I'd also expect exclusions to work?

@zanieb

zanieb commented Sep 24, 2026

Copy link
Copy Markdown
Member

Tool selection skips releases incompatible with the requested Python or platform before reading the lock.

Does this mean you've introduced a new tiny pre-solver?

@zanieb

zanieb commented Sep 24, 2026

Copy link
Copy Markdown
Member

Have you also considered the source tree case? e.g., astral-sh#405

Comment on lines +262 to +263
let (dependencies, _) = resolve_pylock_toml(
lock,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P3] Do not cache PyPI artifacts under publisher-supplied indexes

After checking each dependency URL against PyPI, this call passes the publisher-controlled lock through unchanged, so package.index becomes RegistryBuiltWheel.index and the public wheel is cached under WheelCache::Index(index). A malicious tool can name a victim's configured private index while using a valid PyPI URL and hash; a later unhashed install of the same package, version, and filename from that index can reuse the public wheel without comparing its original URL, substituting attacker bytes for the private package before the selected tool executes. This requires preview opt-in, a known index URL, an exact identity collision, the same cache, and no trusted hash. Normalize packaged-lock indexes to canonical PyPI or bind cache entries to the validated artifact origin before lowering the lock.

Comment on lines +473 to +475
.map(|entry| {
serde::Serialize::serialize(entry, toml_edit::ser::ValueSerializer::new())
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Preserve URL hashes in scoped override receipts

For a locked installation with a scoped override such as dep @ https://supplier.example/dep-1.0-py3-none-any.whl#sha256=..., this serializes the nested Requirement directly, whose wire format discards the URL hash. A later uv tool upgrade restores the unhashed override from the receipt, refreshes its URL, and excludes that replacement from the packaged lock. If the publisher changes the archive and no separate hash constraint protects it, different bytes can therefore be built or installed despite the original hash pin. Serialize and restore scoped dependencies through a hash-preserving receipt wrapper, as already done for global overrides.

Expose the active lock entries and their registry artifact filenames, along with the wheel metadata and resolution details needed to validate a packaged lock.
Allow resolution and source builds to require each applicable set of hashes, including hashes supplied by indexes and artifact URLs. Check cached archives against the same policy before using their metadata.
Allow tool receipts to retain archive URL hashes and additional constraint and override information so later operations can enforce the inputs supplied at installation.
Cover hash strength, index metadata, and Python compatibility before installing a locked artifact.
Check that override wheels and source distributions are verified before their metadata or build backends are used.
Cover independently supplied hashes and ensure invalid sources are rejected before their build backends run.

This branch was previously deployed

1 inactive deployment
automations — 06400a52 Deployed Nov 17, 2026 by konstin via check | crates / cargo-vet in . #54039
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants