@@ -167,6 +167,24 @@ pub fn symlink_refusal(linked: &str) -> Refusal {
167167 }
168168}
169169
170+ /// The refusal for a candidate read through a link: the file itself
171+ /// ([`symlink_refusal`]), or, on disk, a directory on its path that links
172+ /// out of the repository root, so a rewrite would land outside it.
173+ fn linked_read_refusal ( view : & ProjectView < ' _ > , rel : & str ) -> Refusal {
174+ if matches ! ( view, ProjectView :: Memory ( _) ) || view. is_symlink ( rel) {
175+ return symlink_refusal ( rel) ;
176+ }
177+ Refusal {
178+ code : SYMLINK_REFUSAL . to_string ( ) ,
179+ message : format ! (
180+ "{rel} is reached through a symbolic link that leaves the repository root; \
181+ socket-patch does not read or rewrite project files outside it — replace the \
182+ link with a regular directory (or run socket-patch in the directory it points \
183+ to) and re-run; nothing was written"
184+ ) ,
185+ }
186+ }
187+
170188/// The refusal for a symlinked `bun.lockb` (checked before any takeover
171189/// changes wiring).
172190pub fn bun_lockb_symlink_refusal ( ) -> Refusal {
@@ -402,11 +420,19 @@ impl CandidateFiles {
402420 self . undecodable_reads . push ( rel. to_string ( ) ) ;
403421 None
404422 }
405- // A candidate linked out of the repository root: the bound
406- // refuses the read, but the package manager still honors
407- // the file, so it is a symlinked rewrite target the guard
408- // refuses, never an absent one.
409- Err ( e) if crate :: utils:: repo_root:: is_refused_read ( & e) && view. is_symlink ( rel) => {
423+ // A candidate whose real path leaves the repository root
424+ // (the file itself, or a directory on its path, is a link
425+ // out of it; a candidate name never climbs): the bound
426+ // refuses the read, but the package manager still follows
427+ // the link, so the guard refuses it, never takes it for
428+ // absent. Install state under `node_modules/` (a linked
429+ // `node_modules` is a common layout) is no rewrite target:
430+ // only a link of the file itself counts there.
431+ Err ( e)
432+ if crate :: utils:: repo_root:: is_refused_read ( & e)
433+ && ( view. is_symlink ( rel)
434+ || !rel. split ( '/' ) . any ( |segment| segment == "node_modules" ) ) =>
435+ {
410436 self . symlinked_reads . push ( rel. to_string ( ) ) ;
411437 None
412438 }
@@ -3654,14 +3680,14 @@ pub fn guard(
36543680 . iter ( )
36553681 . map ( |c| c. dep . ecosystem . as_str ( ) )
36563682 . collect ( ) ;
3657- // In memory, every linked candidate; on disk, those linked out of the
3658- // repository root, whose reads the bound refused.
3683+ // In memory, every linked candidate; on disk, those whose real path
3684+ // leaves the repository root, whose reads the bound refused.
36593685 if let Some ( linked) = done
36603686 . symlinked_reads
36613687 . iter ( )
36623688 . find ( |rel| file_ecosystem ( rel) . is_some_and ( |eco| candidate_ecosystems. contains ( eco) ) )
36633689 {
3664- return Some ( symlink_refusal ( linked) ) ;
3690+ return Some ( linked_read_refusal ( view , linked) ) ;
36653691 }
36663692 let ProjectView :: Memory ( project) = view else {
36673693 return None ;
@@ -3726,6 +3752,44 @@ mod tests {
37263752 ) ;
37273753 }
37283754
3755+ /// A disk candidate whose real path leaves the repository root, as
3756+ /// the file itself or through a directory link on its path, is
3757+ /// recorded as a linked read (refused by the guard), never as absent.
3758+ #[ cfg( unix) ]
3759+ #[ tokio:: test]
3760+ async fn disk_candidates_linked_out_of_the_repository_are_refused ( ) {
3761+ let tmp = tempfile:: tempdir ( ) . unwrap ( ) ;
3762+ let repo = tmp. path ( ) . join ( "repo" ) ;
3763+ let outside = tmp. path ( ) . join ( "outside" ) ;
3764+ std:: fs:: create_dir_all ( repo. join ( ".git" ) ) . unwrap ( ) ;
3765+ std:: fs:: create_dir_all ( outside. join ( "sub" ) ) . unwrap ( ) ;
3766+ std:: fs:: write ( outside. join ( "uv.lock" ) , "version = 1\n " ) . unwrap ( ) ;
3767+ std:: fs:: write ( outside. join ( "sub/package-lock.json" ) , "{}\n " ) . unwrap ( ) ;
3768+ std:: os:: unix:: fs:: symlink ( outside. join ( "uv.lock" ) , repo. join ( "uv.lock" ) ) . unwrap ( ) ;
3769+ std:: os:: unix:: fs:: symlink ( outside. join ( "sub" ) , repo. join ( "sub" ) ) . unwrap ( ) ;
3770+ let view = ProjectView :: Disk ( & repo) ;
3771+ let mut out = CandidateFiles :: default ( ) ;
3772+ for rel in [ "uv.lock" , "sub/package-lock.json" ] {
3773+ assert ! ( !out. read( & view, & BTreeSet :: new( ) , rel) . await , "{rel}" ) ;
3774+ }
3775+ assert_eq ! ( out. symlinked_reads, [ "uv.lock" , "sub/package-lock.json" ] ) ;
3776+ assert ! ( out. files. is_empty( ) ) ;
3777+ let file = linked_read_refusal ( & view, "uv.lock" ) ;
3778+ assert_eq ! ( file. code, SYMLINK_REFUSAL ) ;
3779+ assert ! (
3780+ file. message. contains( "uv.lock is a symbolic link" ) ,
3781+ "{}" ,
3782+ file. message
3783+ ) ;
3784+ let dir = linked_read_refusal ( & view, "sub/package-lock.json" ) ;
3785+ assert_eq ! ( dir. code, SYMLINK_REFUSAL ) ;
3786+ assert ! (
3787+ dir. message. contains( "leaves the repository root" ) ,
3788+ "{}" ,
3789+ dir. message
3790+ ) ;
3791+ }
3792+
37293793 fn no_cargo_outer ( ) -> CratesIoSource {
37303794 CratesIoSource :: Registry
37313795 }
0 commit comments