Skip to content

Commit 23dd236

Browse files
mikolalysenkoclaude
andcommitted
Bound user-patch reads to the repository in workspace runs only
Owner clarification: the repository-root bound is for workspaces; a global run should be able to read anywhere on the system. The shared user-patch walk now takes a ReadScope { Workspace, Global }, chosen once from the CLI mode (GlobalArgs::read_scope, from --global / -g / --global-prefix), and only a Workspace walk bounds its reads: a workspace member or --patch-dir beyond the git repository root protects every npm package instead of being read. A Global walk reads both wherever they are and protects exactly what they patch. The ancestor walk itself (stop at .git, never reach the home directory) is the same in both scopes. UserPatchIndex::discover stays the workspace walk; UserPatchIndex::discover_scoped takes the scope, which the CLI's inspect() now threads through. A global run still inspects no project (a local checkout does not govern the global tree, #1646), so the Global arm is reached through the core API today. vex discovery and the in-memory hosted engine only run on workspaces and keep the bound. ReadScope has the shape of #2025's utils::repo_root::ReadScope (same variants and of_global), so it is swapped for that type when the two branches meet. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3f076f6 commit 23dd236

4 files changed

Lines changed: 151 additions & 16 deletions

File tree

‎crates/socket-patch-cli/CLI_CONTRACT.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,11 @@ ancestor walk stops there, and a workspace member or literal `--patch-dir`
211211
beyond it protects every npm package with a `workspace outside the git
212212
repository` / `patch-package directory outside the git repository` diagnostic
213213
instead of being read. Without a `.git`, the ancestor walk stops below the home
214-
directory. The hosted engine and `vex` run this same walk (npm-family rows only,
214+
directory. This repository bound is a workspace-run rule: the walk's global
215+
read scope has none (members and patch directories are read wherever they
216+
are, and protect only what they patch), though a global run (`--global`,
217+
`--global-prefix`) inspects no project today, since a local checkout does not
218+
govern the global tree. The hosted engine and `vex` run this same walk (npm-family rows only,
215219
without native npm / Bun); in memory the project is its own repository root.
216220

217221
| Mechanism | Evidence |

‎crates/socket-patch-cli/src/args.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,14 @@ impl GlobalArgs {
447447
self.global || self.global_prefix.is_some()
448448
}
449449

450+
/// The read scope this run's mode fixes: a global run (`--global` /
451+
/// `--global-prefix`) reads anywhere on the system; a project run never
452+
/// reads a project file outside the git repository root. #2025's
453+
/// `GlobalArgs::read_scope` has the same contract.
454+
pub(crate) fn read_scope(&self) -> socket_patch_core::patch::user_patches::ReadScope {
455+
socket_patch_core::patch::user_patches::ReadScope::of_global(self.is_global())
456+
}
457+
450458
/// Whether `--ecosystems` selects `eco` (every ecosystem when unset or
451459
/// empty). The names are validated at parse time, so this is an exact
452460
/// match.

‎crates/socket-patch-cli/src/commands/user_patches.rs‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! The CLI consent gate. --yes, --force, UUID selection and socket.yml do
22
//! not grant permission to combine a Socket patch with a project's own patch.
33
4-
use socket_patch_core::patch::user_patches::UserPatchIndex;
4+
use socket_patch_core::patch::user_patches::{ReadScope, UserPatchIndex};
55

66
use crate::args::GlobalArgs;
77
use crate::json_envelope::{Command, Envelope, EnvelopeError, PatchAction, PatchEvent};
@@ -25,11 +25,12 @@ fn any_detectable<'a>(common: &GlobalArgs, mut purls: impl Iterator<Item = &'a s
2525
}
2626

2727
pub(crate) async fn inspect(common: &GlobalArgs) -> UserPatchIndex {
28-
if common.is_global() {
29-
// A local checkout does not govern a separately selected global tree.
30-
UserPatchIndex::default()
31-
} else {
32-
UserPatchIndex::discover(&common.cwd).await
28+
match common.read_scope() {
29+
// A local checkout does not govern a separately selected global
30+
// tree, so a global run inspects no project (it would read one
31+
// with no repository bound, `ReadScope::Global`).
32+
ReadScope::Global => UserPatchIndex::default(),
33+
scope @ ReadScope::Workspace => UserPatchIndex::discover_scoped(&common.cwd, scope).await,
3334
}
3435
}
3536

‎crates/socket-patch-core/src/patch/user_patches.rs‎

Lines changed: 131 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,22 @@ impl UserPatchIndex {
3636
/// Inspect the project, declared workspace members and ancestor workspace
3737
/// settings. Errors in a potentially governing file protect its ecosystem;
3838
/// unreadable metadata must not silently disable the consent check.
39+
///
40+
/// A workspace run: nothing outside the git repository root is read
41+
/// (see [`Self::discover_scoped`]).
3942
pub async fn discover(root: &Path) -> Self {
43+
Self::discover_scoped(root, ReadScope::Workspace).await
44+
}
45+
46+
/// [`Self::discover`] in the read scope the run's mode fixes. A
47+
/// [`ReadScope::Workspace`] run never reads a project file outside the
48+
/// git repository root: a workspace member or `--patch-dir` beyond it
49+
/// protects every npm package instead. A [`ReadScope::Global`] run reads
50+
/// the evidence wherever it lives, with no repository bound and no
51+
/// such fallback.
52+
pub async fn discover_scoped(root: &Path, reads: ReadScope) -> Self {
4053
let root = std::path::absolute(root).unwrap_or_else(|_| root.to_path_buf());
41-
Self::walk(&ProjectView::Disk(&root), Some(&root), Scope::Gate).await
54+
Self::walk(&ProjectView::Disk(&root), Some(&root), Scope::Gate, reads).await
4255
}
4356

4457
/// The patches a project's pnpm, yarn or patch-package setup applies
@@ -61,7 +74,8 @@ impl UserPatchIndex {
6174
/// a vlt project patches a dependency only through a postinstall tool
6275
/// such as patch-package. Sources are reported root-relative.
6376
pub(crate) async fn npm_family_in(view: &ProjectView<'_>, locks: &[(&str, &str)]) -> Self {
64-
let mut index = Self::walk(view, None, Scope::NpmFamily).await;
77+
// Discovery and the hosted engine only ever run on a workspace.
78+
let mut index = Self::walk(view, None, Scope::NpmFamily, ReadScope::Workspace).await;
6579
for (rel, text) in locks {
6680
let path = Path::new(rel);
6781
match rel.rsplit('/').next() {
@@ -86,13 +100,20 @@ impl UserPatchIndex {
86100
/// anchored: the absolute project root for the gate, `None` for
87101
/// root-relative sources.
88102
///
89-
/// Nothing outside the git repository root is read: the ancestor walk
90-
/// stops at the directory holding `.git`, and a workspace member or
91-
/// patch-package directory beyond it protects every npm package
92-
/// instead of being read. Without a `.git` the gate's container rule
93-
/// holds (the walk never reaches the home directory or above it). An
94-
/// in-memory project is its own repository root.
95-
async fn walk(view: &ProjectView<'_>, shown_root: Option<&Path>, scope: Scope) -> Self {
103+
/// The ancestor walk stops at the directory holding `.git`, and without
104+
/// a `.git` the gate's container rule holds (the walk never reaches the
105+
/// home directory or above it). In a [`ReadScope::Workspace`] run
106+
/// nothing outside that git repository root is read: a workspace member
107+
/// or patch-package directory beyond it protects every npm package
108+
/// instead of being read. An in-memory project is its own repository
109+
/// root. A [`ReadScope::Global`] run reads members and patch-package
110+
/// directories wherever they are.
111+
async fn walk(
112+
view: &ProjectView<'_>,
113+
shown_root: Option<&Path>,
114+
scope: Scope,
115+
reads: ReadScope,
116+
) -> Self {
96117
let mut index = Self::default();
97118
let shown_base = shown_root.unwrap_or(Path::new(""));
98119
let at = |rel: &Path| Dir {
@@ -157,6 +178,12 @@ impl UserPatchIndex {
157178
} else {
158179
repo_depth = Some(0);
159180
}
181+
// The read bound: the repository root in a workspace run, none in a
182+
// global one.
183+
let repo_depth = match reads {
184+
ReadScope::Workspace => repo_depth,
185+
ReadScope::Global => None,
186+
};
160187
let mut seen = BTreeSet::new();
161188
while let Some(dir) = pending.pop_front() {
162189
if !seen.insert(dir.rel.clone()) {
@@ -950,6 +977,31 @@ impl UserPatchIndex {
950977
}
951978
}
952979

980+
/// Which project files a run may read, chosen once from the CLI mode
981+
/// (never inferred from the filesystem). The same shape as #2025's
982+
/// `utils::repo_root::ReadScope`, which replaces it when the two meet.
983+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
984+
pub enum ReadScope {
985+
/// A project run (the default `--cwd` project): project files are read
986+
/// only inside the git repository root.
987+
Workspace,
988+
/// A global run (`--global` / `--global-prefix`): files are read
989+
/// wherever they live on the system.
990+
Global,
991+
}
992+
993+
impl ReadScope {
994+
/// The scope of a run whose global flag (`--global` or
995+
/// `--global-prefix`) is `global`.
996+
pub fn of_global(global: bool) -> Self {
997+
if global {
998+
ReadScope::Global
999+
} else {
1000+
ReadScope::Workspace
1001+
}
1002+
}
1003+
}
1004+
9531005
/// What a [`UserPatchIndex::walk`] reads.
9541006
#[derive(Clone, Copy, PartialEq, Eq)]
9551007
enum Scope {
@@ -1596,6 +1648,76 @@ mod tests {
15961648
}
15971649
}
15981650

1651+
/// The repository-root bound is a workspace-run rule only. A workspace
1652+
/// run reads no workspace member or `--patch-dir` outside the git
1653+
/// repository: each protects every npm package instead. A global run
1654+
/// reads both wherever they are and protects exactly the packages they
1655+
/// patch.
1656+
#[tokio::test]
1657+
async fn the_repository_bound_holds_for_workspace_runs_only() {
1658+
let outer = tempfile::tempdir().unwrap();
1659+
let repo = outer.path().join("repo");
1660+
write(&repo, ".git/HEAD", "ref: refs/heads/main\n");
1661+
write(
1662+
&repo,
1663+
"package.json",
1664+
r#"{"workspaces":["../shared/*"],"scripts":{"postinstall":"patch-package --patch-dir ../fixes"}}"#,
1665+
);
1666+
write(outer.path(), "fixes/left-pad+1.3.0.patch", "diff\n");
1667+
write(outer.path(), "shared/a/package.json", "{}");
1668+
write(
1669+
outer.path(),
1670+
"shared/a/patches/is-number+7.0.0.patch",
1671+
"diff\n",
1672+
);
1673+
1674+
let workspace = UserPatchIndex::discover_scoped(&repo, ReadScope::Workspace).await;
1675+
let sources = workspace.conflicts("pkg:npm/other@1.0.0");
1676+
for what in [
1677+
"workspace outside the git repository",
1678+
"patch-package directory outside",
1679+
] {
1680+
assert!(
1681+
sources.iter().any(|s| s.contains(what)),
1682+
"{what}: {sources:?}"
1683+
);
1684+
}
1685+
assert!(
1686+
!sources.iter().any(|s| s.ends_with(".patch")),
1687+
"{sources:?}"
1688+
);
1689+
assert_eq!(
1690+
UserPatchIndex::discover(&repo)
1691+
.await
1692+
.conflicts("pkg:npm/other@1.0.0"),
1693+
sources,
1694+
"discover() is the workspace scope"
1695+
);
1696+
1697+
let global = UserPatchIndex::discover_scoped(&repo, ReadScope::Global).await;
1698+
assert!(
1699+
global.conflicts("pkg:npm/other@1.0.0").is_empty(),
1700+
"{global:?}"
1701+
);
1702+
let left_pad = global.conflicts("pkg:npm/left-pad@1.3.0");
1703+
assert!(
1704+
left_pad
1705+
.iter()
1706+
.any(|s| s.ends_with("fixes/left-pad+1.3.0.patch")),
1707+
"{left_pad:?}"
1708+
);
1709+
let is_number = global.conflicts("pkg:npm/is-number@7.0.0");
1710+
assert!(
1711+
is_number
1712+
.iter()
1713+
.any(|s| s.ends_with("shared/a/patches/is-number+7.0.0.patch")),
1714+
"{is_number:?}"
1715+
);
1716+
assert!(global.conflicts("pkg:npm/left-pad@1.2.0").is_empty());
1717+
assert_eq!(ReadScope::of_global(true), ReadScope::Global);
1718+
assert_eq!(ReadScope::of_global(false), ReadScope::Workspace);
1719+
}
1720+
15991721
/// The view-based index the hosted engine and `vex` discovery read
16001722
/// ([`UserPatchIndex::npm_family_in`]) protects exactly what the CLI's
16011723
/// consent gate ([`UserPatchIndex::discover`]) protects for a root

0 commit comments

Comments
 (0)