diff --git a/pkgs/mcpx/internal/searchpath/searchpath.go b/pkgs/mcpx/internal/searchpath/searchpath.go index ab7be3c193..f5db5af4cc 100644 --- a/pkgs/mcpx/internal/searchpath/searchpath.go +++ b/pkgs/mcpx/internal/searchpath/searchpath.go @@ -114,9 +114,17 @@ func splice(list []string, marker string, builtin []string) []string { return out } +// absolute resolves one search-path entry against the base dir. +// Only a bare ~ and ~/... name the caller's home. A ~name entry is +// NOT expanded (#1702): the tilde survives as a literal first +// segment of an ordinary relative entry, resolved against dir like +// any other. Resolving ~name under the caller's home silently +// searched a lookalike directory -- and invented one for users that +// do not exist -- while a passwd lookup would answer differently +// for the CLI user and the daemon user, so neither is done. func absolute(path, dir string) string { path = strings.TrimSpace(path) - if strings.HasPrefix(path, "~") { + if path == "~" || strings.HasPrefix(path, "~/") { if home, err := os.UserHomeDir(); err == nil { path = filepath.Join(home, strings.TrimPrefix(path, "~")) } diff --git a/pkgs/mcpx/internal/searchpath/searchpath_test.go b/pkgs/mcpx/internal/searchpath/searchpath_test.go index 6990fb1adb..cea02ed4b3 100644 --- a/pkgs/mcpx/internal/searchpath/searchpath_test.go +++ b/pkgs/mcpx/internal/searchpath/searchpath_test.go @@ -232,3 +232,44 @@ func TestMissingEntriesAreKeptUnlessAskedOtherwise(t *testing.T) { t.Errorf("dropped when asked: %+v", pruned.Entries) } } + +func TestOnlyBareTildeAndTildeSlashExpandHome(t *testing.T) { + // #1702, rule pinned by triage task 1921: only a bare ~ and ~/... + // name the caller's home. A ~name entry is not expanded -- the + // tilde survives as a literal first segment of an ordinary + // relative entry -- because resolving it under the caller's home + // silently searches a lookalike directory (and invents one for + // users that do not exist), while a passwd lookup would answer + // differently for the CLI user and the daemon user. + t.Setenv("HOME", "/home/testhome") + got := searchpath.Resolve([]string{ + "~", "~/scripts", "~alice/scripts", "~nosuchuser/scripts", "~scratch", + }, null, searchpath.Options{Dir: "/w"}) + want := []string{ + "/home/testhome", + "/home/testhome/scripts", + "/w/~alice/scripts", + "/w/~nosuchuser/scripts", + "/w/~scratch", + } + if len(got.Entries) != len(want) { + t.Fatalf("entries = %+v", got.Entries) + } + for i, w := range want { + if got.Entries[i].Path != w { + t.Errorf("entry %d = %q, want %q", i, got.Entries[i].Path, w) + } + } + // Describe renders what Find/List actually search, so a reader + // can see the literal ~name directory instead of a rewritten + // path under the caller's home. + d := got.Describe() + for _, w := range want { + if !strings.Contains(d, w) { + t.Errorf("Describe() does not mention %q:\n%s", w, d) + } + } + if strings.Contains(d, "/home/testhome/alice") || strings.Contains(d, "/home/testhome/scratch") { + t.Errorf("Describe() still rewrites a ~name entry under the caller's home:\n%s", d) + } +}