Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/socket-patch-cli/CLI_CONTRACT.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ Recognition rules that hold for every ecosystem:

* **Patch hosts.** A hosted reference counts only on `https://patch.socket.dev` or the `--patch-server-url` / `SOCKET_PATCH_SERVER_URL` origin, with no userinfo. The uuid is the URL's LAST canonical-uuid path segment, because grant tokens may themselves be uuid-shaped. The Go module prefix is fixed. `socket-patch-<uuid>` registry / repository / source names count only through a pin. For a URL on any other host, see **Patch hosts** above.
* **Pins, not definitions.** A registry, index or source *definition* alone (cargo `[registries]`, nuget `<add>`, pom `<repository>`, uv index tables, `.npmrc`) never makes a reference, because it survives a reverted pin. Sections the package manager ignores are not read: npm's v2 `dependencies` mirror, a `.cargo/config.toml` shadowed by `.cargo/config`. A Socket pin inside a maven `<profile>` is diagnosed, never a reference.
* **Contested locks.** When one lock wires a package to a patch and another lock resolves the same `name@version` from a non-Socket source, the build's bytes depend on which package manager runs. The reference is then dropped with a `patched_ref_unattributable` diagnostic naming both files. This applies across npm / pnpm / yarn / bun and across uv / pylock / poetry / pdm / Pipfile.lock / requirements. PEP 723 script locks neither contest nor are contested. The root `requirements.txt` and its in-root `-r` includes count as one lock here: pip reads them as one requirement set, where a direct reference wins over a compatible `==` pin of the same version in another file of the set (#1086). A **bundled** npm copy (`inBundle: true`, or v1 `bundled: true`) of the same `name@version` contests the reference too, in the same lock, in the other npm lock of a shrinkwrap/package-lock pair, or in any other lock. npm unpacks it from the parent package's tarball, so no rewire reaches it and it stays unpatched. Bun and vlt unpack bundled copies the same way (#469, #471). pnpm unpacks bundled copies too, but its lock cannot tie one to a reference (see **Unattested references** below). For Bun, that is a `bun.lock` entry whose meta is `{ "bundled": true }`, or a `bun.lockb` record that a dependency edge with the `bundled` behavior bit reaches, including one Bun shares with a regular install. Such an entry is never a reference, and it contests the reference the same way. For vlt, the lock records no node for a bundled copy, so the copy is found in the installed store: a real package directory inside a store package's own `node_modules`. Hosted and vendored scans skip these copies with `redirect_bun_bundled_instance_skipped` / `redirect_vlt_bundled_instance_skipped` / `vendor_bundled_instance_skipped`. When a bundled copy is the only instance, vendoring refuses with `vendor_lock_entry_not_rewritable`. A reference withheld only because such an unreachable copy installs beside it (a bundled npm / Bun / vlt copy, an npm copy beneath a `hasShrinkwrap` package (#753) or one npm installs from a git / url / `file:` spec, or a yarn classic git or `file:` directory block of the same `name@version`) is still the rewriter's own wiring of exactly one package version: it is never attested, but `rollback`, `remove`, `list` and the hosted → vendored takeover treat it as an ordinary hosted pin and restore its upstream registry entry (#828), rather than refusing it with `hosted_wiring_contested`. Another entry of the **same** npm, Bun or yarn lock that resolves the wired `name@version` from a non-Socket source (for example a workspace member added after the rewire, then `npm install` / `bun install` / `yarn install`; for yarn berry, a registry locator such as `left-pad@npm:1.3.0` beside the hosted `…::__archiveUrl=` one) contests the reference too (#588). The package manager installs both entries, and that copy stays unpatched. Re-running `scan` / `vendor` rewires every copy.
* **Contested locks.** When one lock wires a package to a patch and another lock resolves the same `name@version` from a non-Socket source, the build's bytes depend on which package manager runs. The reference is then dropped with a `patched_ref_unattributable` diagnostic naming both files. This applies across npm / pnpm / yarn / bun and across uv / pylock / poetry / pdm / Pipfile.lock / requirements. PEP 723 script locks neither contest nor are contested. The root `requirements.txt` and its in-root `-r` includes count as one lock here: pip reads them as one requirement set, where a direct reference wins over a compatible `==` pin of the same version in another file of the set (#1086). A PyPI version matches under PEP 440 equality, so a requirements line pinned in any spelling that can only install the wired release (`==1.2.3.0`, `==v1.2.3`, `===1.2.3`) contests it like `==1.2.3` does; a range or wildcard (`~=1.2.3`, `==1.2.*`) does not (#1602). A **bundled** npm copy (`inBundle: true`, or v1 `bundled: true`) of the same `name@version` contests the reference too, in the same lock, in the other npm lock of a shrinkwrap/package-lock pair, or in any other lock. npm unpacks it from the parent package's tarball, so no rewire reaches it and it stays unpatched. Bun and vlt unpack bundled copies the same way (#469, #471). pnpm unpacks bundled copies too, but its lock cannot tie one to a reference (see **Unattested references** below). For Bun, that is a `bun.lock` entry whose meta is `{ "bundled": true }`, or a `bun.lockb` record that a dependency edge with the `bundled` behavior bit reaches, including one Bun shares with a regular install. Such an entry is never a reference, and it contests the reference the same way. For vlt, the lock records no node for a bundled copy, so the copy is found in the installed store: a real package directory inside a store package's own `node_modules`. Hosted and vendored scans skip these copies with `redirect_bun_bundled_instance_skipped` / `redirect_vlt_bundled_instance_skipped` / `vendor_bundled_instance_skipped`. When a bundled copy is the only instance, vendoring refuses with `vendor_lock_entry_not_rewritable`. A reference withheld only because such an unreachable copy installs beside it (a bundled npm / Bun / vlt copy, an npm copy beneath a `hasShrinkwrap` package (#753) or one npm installs from a git / url / `file:` spec, or a yarn classic git or `file:` directory block of the same `name@version`) is still the rewriter's own wiring of exactly one package version: it is never attested, but `rollback`, `remove`, `list` and the hosted → vendored takeover treat it as an ordinary hosted pin and restore its upstream registry entry (#828), rather than refusing it with `hosted_wiring_contested`. Another entry of the **same** npm, Bun or yarn lock that resolves the wired `name@version` from a non-Socket source (for example a workspace member added after the rewire, then `npm install` / `bun install` / `yarn install`; for yarn berry, a registry locator such as `left-pad@npm:1.3.0` beside the hosted `…::__archiveUrl=` one) contests the reference too (#588). The package manager installs both entries, and that copy stays unpatched. Re-running `scan` / `vendor` rewires every copy.
* **Unattested references.** Some evidence shows a build may run a copy no wiring reaches, but cannot be tied to the reference's exact `name@version` or cannot say the build runs it. The reference then stays a reference: `list`, `rollback` and `remove` find it, and the ledgers' liveness gates (`vendor --check`, `scan`) keep treating the wiring as live, because re-running `scan` / `vendor` could never clear the evidence. Only `vex` omits it, as a run warning and as the `failed[].reason`. Four cases besides Gradle's (`vex_gradle_lock_above_base`): **pnpm bundled copies** (`vex_pnpm_bundled_copy`): a `packages:` entry's `bundledDependencies:` names the copies pnpm unpacks from that package's own tarball, but pnpm never locks them, so the bundled version is not in the lock. A pnpm reference whose package name a `bundledDependencies` list in the same lock names is omitted whatever its version (a missed attestation when the bundled copy is another version, never a false one), and `bundledDependencies: true` (or a value that cannot be read) omits every reference of that lock. It reaches no other lock. **deno.lock** (`vex_deno_lock_copy`): `deno install` installs a `package.json` project's npm dependencies from `deno.lock` and never reads `package-lock.json` / `pnpm-lock.yaml` / `yarn.lock`, so an npm-family reference whose `name@version` the `deno.lock` npm section also locks is omitted (#406). Whether Deno or another package manager populates `node_modules` (`nodeModulesDir: "manual"` allows either) is not in the files, so this holds whatever `nodeModulesDir` says. **Maven reactor roots** (`vex_maven_reactor_root`): a hosted pin in a root `pom.xml` that declares `<modules>` / `<subprojects>` is omitted, because a module's own literal `<version>` overrides a root pin and module poms are not read (#261); hosted scan refuses such roots (`redirect_maven_multimodule_unsupported`), so this only covers pins an older release wrote. **Maven classifier copies** (`vex_maven_classifier_unpatched`): a `<classifier>` dependency of a hosted-pinned GA is its own coordinate; the classifier-less pin does not retarget it and the grant serves only the main jar, so Maven fetches it from the next repository. When a live, non-profile classifier copy other than `sources` / `javadoc` (`tests`, a native or JDK-specific build) resolves any version other than the suffixed pin — its own literal `<version>`, a `<dependencyManagement>` entry with the same classifier, or no version in the root pom — the pin is omitted. `sources` / `javadoc` copies never run on a classpath and do not withhold the statement; hosted scan only warns about them (`redirect_maven_classifier_unsupported`) and skips the dep when an executable copy names the patched release.
* **Shrinkwrap without a twin (#899).** npm >= 12 never reads `npm-shrinkwrap.json`: on a project whose only npm lock is the shrinkwrap it resolves the tree from the registry and writes a fresh `package-lock.json`, so a Socket reference there reaches npm <= 11 only. The reference is still discovered, so `list`, `rollback` and `remove` manage it, but `vex` omits the patch (`vex_npm_shrinkwrap_only`, as a run warning and as the `failed[].reason`). A `package-lock.json` beside it (wired the same way) makes it attestable again. Hosted and vendored runs still rewire the shrinkwrap and warn `redirect_npm_shrinkwrap_only` / `vendor_npm_shrinkwrap_only`.
* **Lockless pins.** With no lock to name a version, a `Cargo.toml` pin (every declaration on `socket-patch-<uuid>`, that registry defined on the patch host for the same uuid) or an exclusive nuget exact-id mapping is never a reference on its own, so v5.0 does not attest it (nor does `list` show it, or `rollback` / `remove` restore it — restore those files from version control). Only a pre-v5 redirect-ledger record naming a version the pin admits keeps it live. The same holds for a gem wired only in the `Gemfile` (the pre-bundler-2.6 mixed state, lock not converged).
Expand Down
19 changes: 19 additions & 0 deletions crates/socket-patch-cli/tests/e2e_vex_lockfile/pipenv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,22 @@ fn requirements_direct_reference_contests_the_lock_wiring() {
],
);
}

/// #1602: pip installs the same upstream release for every spelling of an
/// exact pin, so each contests the Pipfile.lock wiring like `==1.2.3`.
#[test]
fn requirements_pin_spellings_contest_the_lock_wiring() {
contested_by_a_non_socket_direct_reference(
&flavors()[..1],
"requirements.txt",
&[
("arbitrary equality", format!("{NAME}==={VERSION}\n")),
("trailing zero", format!("{NAME}=={VERSION}.0\n")),
("v prefix", format!("{NAME}==v{VERSION}\n")),
(
"padded, with a marker",
format!("{NAME}=={VERSION}.0.0 ; python_version >= '3'\n"),
),
],
);
}
4 changes: 3 additions & 1 deletion crates/socket-patch-core/src/vendor/lock_inventory/pypi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,9 @@ async fn inventory_requirements_txt(view: &ProjectView<'_>) -> Option<Vec<Lockfi
continue;
}
// `name==version` (extras, env markers, hash options stripped) —
// the shared exact-pin rule discovery reads requirements with.
// the shared exact-pin rule. Discovery's cross-lock contest reads
// more spellings as evidence (`==v1.0`, `===1.0`, #1602): scan
// can't rewire those, so they only withhold an attestation.
let mut integrity = LockIntegrity::None;
let (name, version) = match crate::utils::requirements::exact_pin(t) {
Some((raw_name, version)) => {
Expand Down
43 changes: 40 additions & 3 deletions crates/socket-patch-core/src/vex/discover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,13 +460,22 @@ pub struct ResolvedElsewhere {
pub purl: String,
/// Root-relative lock file.
pub file: PathBuf,
/// The version matches only as spelled: a requirements `===X` pin,
/// which pip compares as a string
/// ([`Discovery::resolved_elsewhere_as_spelled`]).
pub as_spelled: bool,
}

impl ResolvedElsewhere {
/// Whether this entry resolves the canonical base `purl`: the same
/// `name@version`, or any version of `name` for a versionless entry.
/// `name@version` (a PyPI version under PEP 440 equality unless the
/// entry matches only [as spelled](Self::as_spelled), so a hand-written
/// `==1.2.3.0` or `==v1.2.3` resolves `1.2.3`, #1602), or any version of
/// `name` for a versionless entry.
fn covers(&self, purl: &str) -> bool {
self.purl == purl || purl_without_version(purl) == Some(self.purl.as_str())
self.purl == purl
|| purl_without_version(purl) == Some(self.purl.as_str())
|| (!self.as_spelled && same_pypi_release(&self.purl, purl))
}

/// Whether the entry names no version, so may resolve any.
Expand All @@ -475,6 +484,16 @@ impl ResolvedElsewhere {
}
}

/// Whether canonical bases `a` and `b` name one PyPI release under PEP 440
/// equality (`@1.16`, `@1.16.0.0` and `@v1.16.0` are `@1.16.0`). Checks
/// the names first: the contest asks this for every pair of a ref and a
/// lock entry.
fn same_pypi_release(a: &str, b: &str) -> bool {
Ecosystem::from_purl(a) == Some(Ecosystem::Pypi)
&& purl_without_version(a).is_some_and(|name| purl_without_version(b) == Some(name))
&& crate::utils::purl_key::pypi_same_release(a, b)
}

/// `pkg:<type>/<name>` of a versioned `pkg:<type>/<name>@<version>`;
/// `None` for a purl with no version (an npm scope's `@` is part of the
/// name).
Expand Down Expand Up @@ -926,6 +945,19 @@ impl Discovery {
self.elsewhere.push(ResolvedElsewhere {
purl: canonical_base_purl(&purl),
file: PathBuf::from(file),
as_spelled: false,
});
}

/// [`Discovery::resolved_elsewhere`] for a version that matches only as
/// spelled: a requirements `===X` pin installs the release whose version
/// string is `X`, never a PEP 440-equal spelling of it (#1602).
pub(crate) fn resolved_elsewhere_as_spelled(&mut self, file: &str, purl: Option<String>) {
let Some(purl) = purl else { return };
self.elsewhere.push(ResolvedElsewhere {
purl: canonical_base_purl(&purl),
file: PathBuf::from(file),
as_spelled: true,
});
}

Expand All @@ -944,6 +976,7 @@ impl Discovery {
self.elsewhere.push(ResolvedElsewhere {
purl,
file: PathBuf::from(file),
as_spelled: false,
});
}

Expand Down Expand Up @@ -1105,9 +1138,13 @@ impl Discovery {
let tree = self.tree_of(&r.source_file);
self.elsewhere.iter().find(|e| {
let other = self.tree_of(&e.file);
// A tree that wires the release itself, in any
// spelling, is no contester.
e.covers(&r.purl)
&& other != tree
&& !wiring.contains(&(r.purl.clone(), other.to_path_buf()))
&& !wiring.iter().any(|(purl, t)| {
t == other && (*purl == r.purl || same_pypi_release(purl, &r.purl))
})
})
})
.flatten();
Expand Down
Loading
Loading