Skip to content
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 @@ -894,7 +894,7 @@ Recognition rules that hold for every ecosystem:
| Hosted: a discovered patch-host reference (or a live pre-v5 redirect-ledger record) | The installed copies the build **consumes** through the hosted wiring are hash-verified when any exist: the Go replacement module, never the pristine `M@v` in the module cache; the Socket-registry cargo source dir; maven's suffixed version. Installed evidence wins: `hash_mismatch` / `not_applied` are omitted. With **nothing installed**, a discovered reference whose lock pins the artifact (or whose format's rewriter never writes a pin) attests from that pin, which is the same evidence as in-run `scan --mode hosted --vex`. A pre-v5 ledger-only record, or a reference whose required pin is missing, stays `package_not_found`. So do purls that `--ecosystems` kept out of the crawl, because "not installed" has to mean the crawler looked. The same goes for npm purls when an installed pnpm tree records its virtual store outside the project (`enableGlobalVirtualStore`, or a `virtualStoreDir` that climbs out): transitive deps there are invisible to the crawler. A pnpm `modulesDir` inside the project is crawled. A Composer purl is omitted as `composer_install_unread` instead when `composer.json`'s `config.vendor-dir` puts the install outside the project (an absolute path, `~/…`, `$VAR/…`, or one that climbs out) and an install is there (its `composer/installed.json` exists, or the directory cannot be resolved): the crawler reads no tree outside the project that only a project file names, so that install was not checked and may predate the redirect (#686). While nothing is installed there the pin still attests, and with `COMPOSER_VENDOR_DIR` set to the directory the crawler reads it and the installed bytes decide. A yarn Plug'n'Play project (`.pnp.cjs` / `.pnp.js`) has no tree to crawl: an npm purl there attests from the lock's pin only when the PnP loader itself resolves it through the patch (berry names the hosted url, yarn 1 the resolved url's `#<sha1>` in its cache folder). A loader written before the lock was rewired runs the registry copy, so the purl stays omitted until `yarn install` rewrites it (#519). | `(redirected)` |
| Agent: a manifest record with no live hosted/vendored wiring | The installed tree, unchanged. **Every** installed copy the crawler finds for the purl (npm nests duplicates of one `name@version`; pnpm, vlt, Bun and Deno stores add peer-variant copies and copies bundled inside other packages) must hash to the patched bytes, as `apply` patches every copy (Maven: every copy a build consumes, Gradle hash dirs included — see [Gradle builds](#gradle-builds-v50)). One unpatched copy omits the purl with that copy's tag (`not_applied` / `hash_mismatch`). | none |

**Unlinked store entries.** In every row an installed copy is one the install can load. Bun never prunes `node_modules/.bun`, and pnpm keeps a `node_modules/.pnpm` entry it no longer uses (pnpm 7–11 for up to 7 days, pnpm 12 until `pnpm prune`). After the in-place install that a hosted or vendored scan asks for, the project links a new entry (on pnpm, `<name>@socket-<id>+<version>` for a release routed through the registry of its patch and `<name>@file+…` for a vendored tarball, `file+…` on pnpm 8 and earlier), and the upstream `<name>@<version>` entry stays in the store, pristine. An entry that no link leads into is not an installed copy: `vex` and `apply --check` do not hash it, so it neither omits a patch as `not_applied` nor warns `vendored_tree_out_of_sync`. For pnpm this applies to a package version only when its copies sit in two or more entries of one store and do not hold the same bytes in each; a version in one entry is hashed as before, linked or not, and so are copies that hold the same bytes (the peer variants of a package), since leaving one out would change no verdict; copies of more than 64 MB or 50,000 files are not compared and count as holding other bytes. For a version in two entries, every symbolic link is read below the scanned directory (or below the project that holds the store, when the scan started inside it), in every directory whatever its name; nothing above that directory is read. A link that leads nowhere (no target, a loop, a path through a file) is left out. An entry is hashed when a link outside the store's entries resolves into it, or a link inside a hashed entry does; the entries left out are those that only other left-out entries link, and only beside a hashed entry of the same version: when none of a version's entries is linked, all are hashed as before (a hosted scan leaves such a pair when the patched package is a peer dependency: the dependent gets a new entry, and its old entry keeps its link to the old one). A copy in the store but in none of its entries is always hashed. Only links are read, not a path to an entry that a file spells out or `NODE_PATH` names. Every store is judged once per run, by the same links for the record check and the hosted one. Where a link may have gone unread, every entry is hashed: on Windows; when the scanned directory and the store's project are unrelated (`--global`, a `node_modules` linked from another checkout), or the store's project lies above the git checkout the run started in or, above the scanned directory, is the home directory or above it; under `node-linker=hoisted`, `shamefully-hoist` or a moved virtual store; when the install record or the lockfile in the store is missing or unreadable, `pnpm-lock.yaml` is unreadable, or either lists its importers in a way pnpm does not write; when a lockfile names an importer that is not a directory inside the workspace's root (under a `modulesDir` of more than one level, the directory whose `modulesDir` setting names the store's `node_modules`); when a directory or a link cannot be read, or the directory holds more than 500,000 directories and links; when a link resolves to a directory outside the directory that was read, to a file outside it that has a `node_modules` in a directory above it (the home directory and above are not looked at), to the store itself, or into the store by a path that names none of its entries (another spelling of the store's path or of an entry's name); and when no link resolves into any entry (`docs/ecosystems.md`, "npm: which `node_modules` trees are crawled", has the list). `rollback` and `remove` restore unlinked entries too.
**Unlinked store entries.** In every row an installed copy is one the install can load. Bun never prunes `node_modules/.bun`, and pnpm keeps a `node_modules/.pnpm` entry it no longer uses (pnpm 7–11 for up to 7 days, pnpm 12 until `pnpm prune`). After the in-place install that a hosted or vendored scan asks for, the project links a new entry (on pnpm, `<name>@socket-<id>+<version>` for a release routed through the registry of its patch and `<name>@file+…` for a vendored tarball, `file+…` on pnpm 8 and earlier), and the upstream `<name>@<version>` entry stays in the store, pristine. An entry that no link leads into is not an installed copy: `vex` and `apply --check` do not hash it, so it neither omits a patch as `not_applied` nor warns `vendored_tree_out_of_sync`. For pnpm this applies to a package version only when its copies sit in two or more entries of one store and do not hold the same bytes in each; a version in one entry is hashed as before, linked or not, and so are copies that hold the same bytes (the peer variants of a package), since leaving one out would change no verdict; copies of more than 64 MB or 50,000 files are not compared and count as holding other bytes. For a version in two entries, every symbolic link is read below the scanned directory (or below the project that holds the store, when the scan started inside it), in every directory whatever its name; nothing above that directory is read for its own sake. A link to a directory outside it is followed: that directory, and each `node_modules` in a directory above it (up to the home directory, which is not read), are read the same way and once each, so a `link:` dependency on another checkout and Bazel's `bazel-*` links are read; a directory above the scanned one is read only this way. A link that leads nowhere (no target, a loop, a path through a file) is left out. An entry is hashed when a link outside the store's entries resolves into it, or a link inside a hashed entry does; the entries left out are those that only other left-out entries link, and only beside a hashed entry of the same version: when none of a version's entries is linked, all are hashed as before (a hosted scan leaves such a pair when the patched package is a peer dependency: the dependent gets a new entry, and its old entry keeps its link to the old one). A copy in the store but in none of its entries is always hashed. Only links are read, not a path to an entry that a file spells out or `NODE_PATH` names. Every store is judged once per run, by the same links for the record check and the hosted one. Where a link may have gone unread, every entry is hashed: on Windows; when the scanned directory and the store's project are unrelated (`--global`, a `node_modules` linked from another checkout), or the store's project lies above the git checkout the run started in or, above the scanned directory, is the home directory or above it; under `node-linker=hoisted`, `shamefully-hoist` or a moved virtual store; when the install record or the lockfile in the store is missing or unreadable, `pnpm-lock.yaml` is unreadable, or either lists its importers in a way pnpm does not write; when a lockfile names an importer that is not a directory inside the workspace's root (under a `modulesDir` of more than one level, the directory whose `modulesDir` setting names the store's `node_modules`); when a directory or a link cannot be read, or all that is read comes to more than 500,000 directories and links; when a link resolves to the home directory or one above it, to a file outside what was read that has a `node_modules` in a directory above it (the home directory and above are not looked at), to the store itself, or into the store by a path that names none of its entries (another spelling of the store's path or of an entry's name); and when no link resolves into any entry (`docs/ecosystems.md`, "npm: which `node_modules` trees are crawled", has the list). `rollback` and `remove` restore unlinked entries too.

**Liveness gates.** These gates run before hashing, and `--no-verify` / `--vex-no-verify` skips only the hashing, never the gates:

Expand Down
44 changes: 40 additions & 4 deletions crates/socket-patch-cli/tests/common/pnpm_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,49 @@ fn links_below(tree: &Path) -> Vec<(PathBuf, PathBuf)> {
}

/// The entries of the virtual store `store` that a path which starts
/// below `tree` and outside the store's entries leads into: each entry
/// that a link outside the entries resolves into, each entry that a link
/// inside one of those resolves into, and so on.
/// below `tree`, or below what a link leads to outside it, and outside
/// the store's entries leads into: each entry that a link outside the
/// entries resolves into, each entry that a link inside one of those
/// resolves into, and so on.
pub fn reachable_store_entries(tree: &Path, store: &Path) -> BTreeSet<String> {
let tree = std::fs::canonicalize(tree).unwrap();
let store = std::fs::canonicalize(store).unwrap();
let links = links_below(&tree);
// Node loads what a link leads to from where it really is: every
// directory a link leads to outside the trees read so far is read as
// well, and so is the `node_modules` of each directory above it. A
// `node_modules` that is a link is one more link, and what it leads
// to is read the same way. Nothing at the temp dir or above it is
// read: the tests build below it.
let stop = std::fs::canonicalize(std::env::temp_dir()).unwrap();
let mut trees = vec![tree];
let mut links = Vec::new();
let mut next = 0;
while next < trees.len() {
let found = links_below(&trees[next]);
next += 1;
let mut targets: Vec<PathBuf> = found.iter().map(|(_, to)| to.clone()).collect();
links.extend(found);
while let Some(target) = targets.pop() {
let read = trees.iter().any(|tree| target.starts_with(tree));
if read || target.starts_with(&store) || !target.is_dir() || stop.starts_with(&target) {
continue;
}
for above in target.ancestors().skip(1) {
if stop.starts_with(above) {
break;
}
let modules = above.join("node_modules");
let Ok(real) = std::fs::canonicalize(&modules) else {
continue;
};
if real != modules {
links.push((modules, real.clone()));
}
targets.push(real);
}
trees.push(target);
}
}
let mut reachable = BTreeSet::new();
loop {
let before = reachable.len();
Expand Down
84 changes: 83 additions & 1 deletion crates/socket-patch-cli/tests/e2e_redirect_pnpm_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3655,7 +3655,16 @@ fn write_route_workspace(proj: &Path, registry: &MockServer, modules_dir: Option
/// it attests with their status, and its stderr.
#[cfg(unix)]
fn route_vex(dir: &Path, server: &MockServer) -> (i32, Vec<(String, String)>, String) {
let (code, stdout, stderr) = run_socket(
route_vex_env(dir, server, &[])
}

/// [`route_vex`] with extra env.
fn route_vex_env(
dir: &Path,
server: &MockServer,
env: &[(String, String)],
) -> (i32, Vec<(String, String)>, String) {
let (code, stdout, stderr) = run_socket_env(
dir,
&[
"vex",
Expand All @@ -3670,6 +3679,7 @@ fn route_vex(dir: &Path, server: &MockServer) -> (i32, Vec<(String, String)>, St
"--patch-server-url",
&server.uri(),
],
env,
);
let statements = serde_json::from_str::<serde_json::Value>(&stdout)
.ok()
Expand Down Expand Up @@ -3911,6 +3921,78 @@ async fn registry_route_in_place_install_is_judged_by_every_link(pm: &str, tag:
attested.contains(&not_affected(leaf)),
"{tag}: without the links leaf is attested again: {attested:?}\n{stderr}"
);

// A link that leads out of the project is followed. A directory
// beside the project that links nothing of the store changes nothing;
// one whose `node_modules` links the upstream leaf, as a member that
// left the workspace keeps doing, refuses the patch. The test's own
// directory stands in for the home directory, so no `node_modules`
// that the machine keeps above it is read.
let home = [("HOME".to_string(), tmp.path().to_str().unwrap().to_string())];
let outside = tmp.path().join("shared");
std::fs::create_dir_all(outside.join("lib")).unwrap();
std::fs::create_dir_all(proj.join("tools")).unwrap();
std::os::unix::fs::symlink(outside.join("lib"), proj.join("tools/lib")).unwrap();
let (code, attested, stderr) = route_vex_env(&proj, &server, &home);
assert_eq!(
code, 0,
"{tag}: vex with a link out of the project:\n{stderr}"
);
assert!(
attested.contains(&not_affected(leaf)) && attested.contains(&not_affected(peer_user)),
"{tag}: a link to a directory that links nothing must change nothing: \
{attested:?}\n{stderr}"
);
std::fs::create_dir_all(outside.join("node_modules")).unwrap();
std::os::unix::fs::symlink(&old_leaf, outside.join("node_modules/leaf")).unwrap();
let reachable = pnpm_links::reachable_store_entries(&proj, &store);
assert!(
reachable.contains("leaf@1.0.0"),
"{tag}: the independent reading must follow the link out of the project: {reachable:?}"
);
let (code, attested, stderr) = route_vex_env(&proj, &server, &home);
assert_eq!(code, 0, "{tag}: vex with a stale member outside:\n{stderr}");
assert!(
!attested.contains(&not_affected(leaf))
&& attested.contains(&not_affected(peer_user))
&& stderr.contains("not_applied"),
"{tag}: a directory outside that links the upstream leaf must refuse its patch: \
{attested:?}\n{stderr}"
);
std::fs::remove_file(outside.join("node_modules/leaf")).unwrap();
let (code, attested, stderr) = route_vex_env(&proj, &server, &home);
assert_eq!(code, 0, "{tag}: vex once the stale link is gone:\n{stderr}");
assert!(
attested.contains(&not_affected(leaf)),
"{tag}: without the stale link leaf is attested again: {attested:?}\n{stderr}"
);

// A `node_modules` there that is a link itself is one more link: into
// the upstream entry's `node_modules`, it loads the upstream leaf.
std::fs::remove_dir(outside.join("node_modules")).unwrap();
std::os::unix::fs::symlink(old_leaf.parent().unwrap(), outside.join("node_modules")).unwrap();
let reachable = pnpm_links::reachable_store_entries(&proj, &store);
assert!(
reachable.contains("leaf@1.0.0"),
"{tag}: the independent reading must read a linked node_modules: {reachable:?}"
);
let (code, attested, stderr) = route_vex_env(&proj, &server, &home);
assert_eq!(
code, 0,
"{tag}: vex with a linked node_modules outside:\n{stderr}"
);
assert!(
!attested.contains(&not_affected(leaf)) && stderr.contains("not_applied"),
"{tag}: a node_modules outside that is a link into the upstream entry must refuse \
the patch: {attested:?}\n{stderr}"
);
std::fs::remove_file(outside.join("node_modules")).unwrap();
let (code, attested, stderr) = route_vex_env(&proj, &server, &home);
assert_eq!(code, 0, "{tag}: vex once that link is gone:\n{stderr}");
assert!(
attested.contains(&not_affected(leaf)),
"{tag}: without that link leaf is attested again: {attested:?}\n{stderr}"
);
}

#[cfg(unix)]
Expand Down
Loading
Loading