Files
ariandClaude Opus 5.5 8c83a3316a 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
2026-11-01 20:30:39 -05:00

68 lines
2.2 KiB
Go

package tools
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
)
func TestSendFileTool(t *testing.T) {
home := t.TempDir()
os.WriteFile(filepath.Join(home, "chart.png"), []byte("png"), 0o644)
os.Mkdir(filepath.Join(home, "dir"), 0o755)
os.Symlink("/etc/passwd", filepath.Join(home, "escape"))
s := &Server{Home: home, MaxMB: 1}
call := func(path string) string {
return `{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"send_file","arguments":{"path":"` + path + `"}}}`
}
in := strings.Join([]string{
`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-06-18"}}`,
`{"jsonrpc":"2.0","method":"notifications/initialized"}`,
call("chart.png"), call(home + "/dir"), call(home + "/escape"), call("missing"),
}, "\n")
var out bytes.Buffer
if err := s.Serve(strings.NewReader(in), &out); err != nil {
t.Fatal(err)
}
lines := strings.Split(strings.TrimSpace(out.String()), "\n")
if len(lines) != 5 || !strings.Contains(lines[0], `"protocolVersion":"2025-06-18"`) || !strings.Contains(lines[0], "devShells.default") {
t.Fatalf("replies: %v", lines)
}
want := []struct {
text string
isErr bool
}{{"queued", false}, {"regular file", true}, {"isn't under", true}, {"can't find", true}}
for i, w := range want {
var r struct {
Result struct {
Content []struct{ Text string }
IsError bool
}
}
_ = json.Unmarshal([]byte(lines[i+1]), &r)
if r.Result.IsError != w.isErr || !strings.Contains(r.Result.Content[0].Text, w.text) {
t.Fatalf("call %d: %s", i, lines[i+1])
}
}
}
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)
}
}