From 36515d1527fffa857cf59a1b0b782ba822721d3b Mon Sep 17 00:00:00 2001 From: repo-worker-code-k Date: Tue, 10 Nov 2026 20:03:49 -0600 Subject: [PATCH] fix(mcpx): skip looping symlinks in directory sources --- pkgs/mcpx/internal/source/source.go | 4 +-- pkgs/mcpx/internal/source/source_test.go | 30 +++++++++++++++++++ .../internal/source/symlink_loop_other.go | 5 ++++ .../mcpx/internal/source/symlink_loop_unix.go | 12 ++++++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 pkgs/mcpx/internal/source/symlink_loop_other.go create mode 100644 pkgs/mcpx/internal/source/symlink_loop_unix.go diff --git a/pkgs/mcpx/internal/source/source.go b/pkgs/mcpx/internal/source/source.go index 5dee565b5e..c8fb61d8ff 100644 --- a/pkgs/mcpx/internal/source/source.go +++ b/pkgs/mcpx/internal/source/source.go @@ -173,8 +173,8 @@ func readDir(dir string, opt Options, explicit bool) (Resolved, error) { if entryInfo.Mode()&fs.ModeSymlink != 0 { entryInfo, err = os.Stat(entryPath) if err != nil { - if errors.Is(err, fs.ErrNotExist) { - // A dangling link has no regular target to read. + if errors.Is(err, fs.ErrNotExist) || isSymlinkLoop(err) { + // Dangling and looping links have no regular target to read. continue } return fmt.Errorf("reading %s: %w", entryPath, err) diff --git a/pkgs/mcpx/internal/source/source_test.go b/pkgs/mcpx/internal/source/source_test.go index 4cc34c9f97..98bab2f7bb 100644 --- a/pkgs/mcpx/internal/source/source_test.go +++ b/pkgs/mcpx/internal/source/source_test.go @@ -279,6 +279,36 @@ func TestDirectorySkipsDanglingLinkAndFollowsLinkOutsideRoot(t *testing.T) { } } +func TestDirectorySkipsSymlinkLoops(t *testing.T) { + for _, tc := range []struct { + name string + links map[string]string + }{ + {"self", map[string]string{"loop.ts": "loop.ts"}}, + {"pair", map[string]string{"a.ts": "b.ts", "b.ts": "a.ts"}}, + } { + t.Run(tc.name, func(t *testing.T) { + root := t.TempDir() + regular := filepath.Join(root, "regular.ts") + if err := os.WriteFile(regular, []byte("regular"), 0o644); err != nil { + t.Fatal(err) + } + for name, target := range tc.links { + if err := os.Symlink(target, filepath.Join(root, name)); err != nil { + t.Skipf("symlinks are unavailable: %v", err) + } + } + got, err := source.Resolve("@dir:"+root, opts(root)) + if err != nil { + t.Fatal(err) + } + if len(got.Files) != 1 || got.Files[0] != regular { + t.Fatalf("got files %v, want [%s]", got.Files, regular) + } + }) + } +} + func TestResolveAllJoinsInOrder(t *testing.T) { dir := t.TempDir() _ = os.WriteFile(filepath.Join(dir, "second.ts"), []byte("SECOND"), 0o644) diff --git a/pkgs/mcpx/internal/source/symlink_loop_other.go b/pkgs/mcpx/internal/source/symlink_loop_other.go new file mode 100644 index 0000000000..85449523ad --- /dev/null +++ b/pkgs/mcpx/internal/source/symlink_loop_other.go @@ -0,0 +1,5 @@ +//go:build !unix + +package source + +func isSymlinkLoop(error) bool { return false } diff --git a/pkgs/mcpx/internal/source/symlink_loop_unix.go b/pkgs/mcpx/internal/source/symlink_loop_unix.go new file mode 100644 index 0000000000..e0eedb25b7 --- /dev/null +++ b/pkgs/mcpx/internal/source/symlink_loop_unix.go @@ -0,0 +1,12 @@ +//go:build unix + +package source + +import ( + "errors" + "syscall" +) + +func isSymlinkLoop(err error) bool { + return errors.Is(err, syscall.ELOOP) +}