send_file: resolve home's symlinks before the "under home" check
Check resolved symlinks in the file path but compared it to s.Home as given, so a home reached through a symlink rejected its own files. Seen when tools_test ran on NativeLink, whose work dir is under the /var/lib/nativelink -> private/nativelink symlink. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0119iKoN3fSvRGUAYHupPW4U
This commit is contained in:
1 parent
af249326e8
commit
8c83a3316a
2 files changed
+21
-1
No files matched your search
@@ -247,7 +247,11 @@ func (s *Server) Check(path string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't find %s", path)
|
||||
}
|
||||
if !strings.HasPrefix(real, filepath.Clean(s.Home)+"/") {
|
||||
home := filepath.Clean(s.Home)
|
||||
if h, err := filepath.EvalSymlinks(home); err == nil {
|
||||
home = h
|
||||
}
|
||||
if !strings.HasPrefix(real, home+"/") {
|
||||
return fmt.Errorf("%s isn't under %s; copy it there first", path, s.Home)
|
||||
}
|
||||
st, err := os.Stat(real)
|
||||
|
||||
@@ -49,3 +49,19 @@ func TestSendFileTool(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckSymlinkedHome(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
os.Mkdir(filepath.Join(dir, "real"), 0o755)
|
||||
os.WriteFile(filepath.Join(dir, "real", "chart.png"), []byte("png"), 0o644)
|
||||
os.Symlink(filepath.Join(dir, "real"), filepath.Join(dir, "home"))
|
||||
os.Symlink("/etc/passwd", filepath.Join(dir, "real", "escape"))
|
||||
s := &Server{Home: filepath.Join(dir, "home"), MaxMB: 1}
|
||||
|
||||
if err := s.Check("chart.png"); err != nil {
|
||||
t.Fatalf("file under a symlinked home: %v", err)
|
||||
}
|
||||
if err := s.Check("escape"); err == nil || !strings.Contains(err.Error(), "isn't under") {
|
||||
t.Fatalf("escape: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user