Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkgs/mcpx/internal/searchpath/searchpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "~"))
}
Expand Down
41 changes: 41 additions & 0 deletions pkgs/mcpx/internal/searchpath/searchpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Loading