Skip to content

Fix Cargo lock source look-alikes and two VEX remedies (#1933, #2045) - #2131

Merged
Mikola Lysenko (mikolalysenko) merged 1 commit into
mainfrom
fix/cargo-lock-sources
Nov 18, 2026
Merged

Mikola Lysenko (mikolalysenko) merged 1 commit into
mainfrom
fix/cargo-lock-sources

Conversation

@mikolalysenko

Copy link
Copy Markdown
Collaborator

Closes #1933. Closes #2045 (item 1 was fixed by #2052; this fixes items 2 and 3).

Problems

A look-alike host was read as crates.io (#1933). CargoLock::entries decided "is this lock entry from crates.io" by substring, so source = "sparse+https://index.crates.io.evil.example/" got SourceKind::CratesIo and a crates.io checksum verifier. Three other places answered the same question by other rules.

The remedy vex printed for a bypassed vendored copy could not be run (#2045 item 2). It suggested cargo update -p <crate> and cargo tree -i <crate>@<version>. The lock also holds the vendored copy at <version>+socket.<uuid>, and cargo leaves build metadata out when it matches a spec, so both commands fail:

$ cargo tree -i cfg-if@1.0.4
error: specification `cfg-if@1.0.4` is ambiguous

A stale entry from another registry got the wrong advice (#2045 item 3). With the copy wired under [patch."https://my-corp.example/index"] and a stale lock entry from that registry, vex did not see the entry as stale and sent the user to cargo tree -i for a declaring dependency that may not exist.

Fix

  • One rule per question, both in formats::cargo.
    • is_crates_io_source answers for a lock source: the git index behind registry+, or the sparse index behind sparse+, compared as whole URLs the way cargo's CanonicalUrl does. CargoLock::entries, the vendored source refusal, the hosted override check and VEX discovery all call it.
    • is_crates_io_patch_table answers for a [patch.<table>] key: crates-io or the git-index URL. It was a second function named is_crates_io_source in vendor::cargo_manifest; that module now re-exports it under the new name.
  • Remedies name the lock entry by its source: cargo update -p "https://github.com/rust-lang/crates.io-index#cfg-if@1.0.4". The same spec is used in the hosted twin message, and the vendored revert warning now says cargo update -p <crate>@<version>.
  • A registry entry is recognised as stale when a [patch] table for that registry points at the copy, by the index URL or by the name the project's cargo config defines for it.

Checked against real cargo (1.98.1)

  • cargo update -p cfg-if, cargo update -p cfg-if@1.0.4 and cargo tree -i cfg-if@1.0.4 are refused as ambiguous beside a 1.0.4+socket.<uuid> copy. The source-qualified spec works for a crates.io entry and for a git entry.
  • A [patch] table patches crates.io under crates-io and under the git-index URL only. Under "sparse+https://index.crates.io/", "https://index.crates.io/" or "registry+https://github.com/rust-lang/crates.io-index" cargo reports the patch as unused.
  • Not checked: the spec for an entry from a sparse alternative registry. It is printed as the lock spells it, sparse+ included.

Behaviour changes

  • A lock entry whose source only contains crates.io's host or path is no longer crates.io: no crates.io verifier, and vendor refuses it with vendor_source_unsupported.
  • vendor no longer refuses crates.io under a spelling cargo treats as the same source (a trailing /, .git, host case). It used to accept two exact strings.
  • registry+https://index.crates.io and sparse+https://github.com/rust-lang/crates.io-index (each index behind the other kind) are not crates.io.
  • A [patch] URL table with two trailing slashes is no longer a crates.io table (cargo drops one).

Review

/code-review high reported nine findings. Fixed:

  • The shared rule still accepted look-alikes: it dropped .git from the end of the whole URL, so the host index.crates.io.git passed, and it ignored the source kind. It now drops .git from a repository URL only and checks the kind.
  • The remedy put the lock's raw source into a command a user pastes. A source such as git+https://…$(…) would run in the shell. The spec is printed only when the source, name and version consist of characters no shell reads inside double quotes. Otherwise the command takes the bare crate name, which cargo answers with the specs it accepts.
  • Two functions with one name and different rules, with the normalisation written twice. See "Fix".
  • Registry names were resolved only when the root manifest parsed. They are read from the project config directly.
  • The sibling remedy in the vendored revert warning, an allocation per package in entries, and a doc comment that claimed more than the git spec delivers.

Not fixed:

  • route still treats a registry name and its index URL as different sources. With the copy under [patch.my-corp], a member that declares registry-index = "<my-corp's URL>" is still reported as bypassing it. That withholds an attestation; it never grants one. Making the two equal changes what vex attests, and I have not checked it against cargo with a real alternative registry.
  • A registry defined in the user's own cargo config or in the environment is not resolved. Only the project config is read, as everywhere else in discovery. The contract now says so.
  • A git spec does not carry the ?reference. Two references of one repository at one version still share a spec. Cargo before 1.77 does not read the git+…?branch= form, and the kind-less form works on every cargo.
  • The hosted override check keeps one wider reading on purpose. [patch."sparse+https://index.crates.io/"] is still refused as a crates.io override although cargo ignores it. The comment at the call says why: with the lock block sourceless, a pin beside the user's attempted override is refused, not guessed at.

Testing

  • a_lock_source_is_crates_io_only_as_a_whole_url and a_patch_table_is_crates_io_by_name_or_by_the_git_index_url: every spelling cargo reads as crates.io, and the look-alikes (a host or path that contains crates.io's, .git on a sparse host, either index behind the other kind, two trailing slashes).
  • only_a_crates_io_entry_carries_the_crates_io_verifier: the inventory gives the look-alike sources no crates.io provenance.
  • an_entry_of_another_registry_its_patch_table_redirects_is_stale: by index URL, by a name the project config defines, by a name it does not define, and a sparse index against its own table and against the git index's.
  • a_lock_entry_spec_carries_the_source_cargo_compares: the spec for crates.io, another registry, a sparse index and git entries, and the sources, names and versions that are not printed.
  • the_bypass_remedy_follows_its_evidence now asserts the full commands.
  • cargo test -p socket-patch-core --lib: 7,836 passed.
  • CLI suites e2e_vex_vendor, e2e_vex_redirect, e2e_vex_lockfile, vendor, in_process_vendor, in_process_redirect, mode_migration_cargo, hosted_memory_engine, scan: 1,195 passed.
  • Real cargo: e2e_redirect_cargo_shapes (15), e2e_redirect_cargo_build (3), e2e_cargo (5) and in_process_cargo_apply (3) pass. In e2e_vendor_cargo_build 20 pass and 2 fail on my machine before they reach socket-patch: they run rustup's 1.41.1-x86_64-apple-darwin toolchain, which this arm64 Mac no longer executes (Bad CPU type in executable).
  • cargo fmt --check and cargo clippy --locked --workspace --all-features -- -D warnings are clean.

🤖 Generated with Claude Code

Whether a Cargo.lock `source` is crates.io was decided in four places
by three rules. `CargoLock::entries` used a substring test, so a source
such as `sparse+https://index.crates.io.evil.example/` was recorded as
crates.io and given a crates.io checksum verifier (#1933).

`formats::cargo` now holds both rules. `is_crates_io_source` answers
for a lock `source`: the git index behind `registry+` or the sparse
index behind `sparse+`, compared as whole URLs the way cargo's
`CanonicalUrl` does. `is_crates_io_patch_table` answers for a
`[patch.<table>]` key: `crates-io` or the git-index URL, the only
spellings cargo applies to crates.io (checked with cargo 1.98). It was
a second `is_crates_io_source` in `vendor::cargo_manifest`.

Two remedies `vex` prints for a vendored copy the build bypasses were
wrong (#2045 items 2 and 3; item 1 was fixed by #2052):

- `cargo update -p <crate>` and `cargo tree -i <crate>@<version>` are
  refused by cargo as ambiguous, because the lock also holds the copy
  at `<version>+socket.<uuid>` and cargo leaves build metadata out when
  it matches a spec. The commands now name the entry by its source,
  `"<index or repository URL>#<crate>@<version>"`. A source, name or
  version with a character a shell reads inside double quotes is not
  printed.
- A stale entry locked from another registry was not recognised as
  stale when a `[patch]` table for that registry points at the copy, by
  its index URL or by the name the project config defines for it.

The hosted override check keeps reading `[patch."sparse+https://
index.crates.io/"]` as a crates.io override, which is wider than
cargo's reading and now says so.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cursor

cursor Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes attestation and vendor refusal rules for Cargo lock sources and VEX/user-facing shell commands; behavior is intentionally stricter for look-alike registry URLs but affects discovery across scan, vendor, and vex paths.

Overview
Centralizes crates.io detection in formats::cargo: lock source values use is_crates_io_source (protocol-aware, full-URL match like Cargo’s CanonicalUrl) instead of substring checks, so look-alike hosts no longer get crates.io checksum verification or vendor acceptance. [patch] table keys use a separate is_crates_io_patch_table (crates-io or git-index URL only), replacing the duplicate logic in vendor::cargo_manifest and redirect discovery.

VEX vex_cargo_patch_bypassed remedies now suggest source-qualified cargo update -p / cargo tree -i specs (e.g. "https://github.com/rust-lang/crates.io-index#cfg-if@1.0.4") so commands are not ambiguous beside a +socket.<uuid> lock entry; unsafe shell characters fall back to the bare crate name. Stale alternate-registry lock entries are detected when a [patch] table at the copy redirects that registry (by index URL or name from the project’s .cargo/config.toml). Vendor revert warnings use cargo update -p <crate>@<version>.

CLI contract documents the expanded [patch] / re-lock guidance for vendored Cargo bypass cases.

Reviewed by Cursor Bugbot for commit 7acd65c. Configure here.

@mikolalysenko

Copy link
Copy Markdown
Collaborator Author

[agent] PR shepherd: review brief for 7acd65c

What it does: It adds one rule for "is this Cargo.lock source crates.io". The rule compares the whole URL and the kind, instead of matching a substring, and every lock-source caller now uses it. A look-alike such as sparse+https://index.crates.io.evil.example/ no longer gets the crates.io verifier, and vendor refuses it. The VEX bypass and hosted-twin remedies now print a source-qualified cargo update -p "url#name@version" / cargo tree -i spec, and a lock entry from another registry counts as stale when a [patch] table for that registry points at the copy.

Risk: Low. The changes make the checks stricter: look-alikes now fail closed with vendor_source_unsupported. vendor also accepts a few more canonical crates.io spellings (a trailing /, .git, host case). The remedy text quotes a source only if every character is in a safe set, so it is safe to paste into a shell.

Look here

  • formats/cargo/mod.rs:105-150: the new crates.io predicates
  • formats/cargo/mod.rs:544: where the verifier is attached
  • [vex/discover/cargo.rs:552-567](https://github.com/SocketDev/socket-patch/blob/7acd65c6641691d4e4c6f437414dff25b40c828c/crates/socket-patch-core/src/vex/discover/cargo.rs#L552-L567): of_lock_source
  • [vex/discover/cargo.rs:742-764](https://github.com/SocketDev/socket-patch/blob/7acd65c6641691d4e4c6f437414dff25b40c828c/crates/socket-patch-core/src/vex/discover/cargo.rs#L742-L764): how the remedy spec is quoted
  • patch/redirect/mod.rs:2632: the hosted [patch] override check

Verified

Changes I made: none.

Open questions

  • The hosted override check still refuses a [patch."sparse+https://index.crates.io/"] table, but it no longer refuses the kind-less [patch."https://index.crates.io/"], which the old rule did. Cargo ignores both tables, so nothing breaks. Should the kind-less form be refused too, for consistency?
  • Nobody has run the remedy spec for an entry from a sparse alternative registry against real cargo. It is only advice text.

Generated by Claude Code

@mikolalysenko Mikola Lysenko (mikolalysenko) added the Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review label Nov 18, 2026
Merged via the queue into main with commit e8006f0 Nov 18, 2026
50 checks passed
@mikolalysenko
Mikola Lysenko (mikolalysenko) deleted the fix/cargo-lock-sources branch November 18, 2026 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Ready for review Agent-verified: mergeable, CI green, Bugbot clean — awaiting human review

Projects

None yet

2 participants