diff --git a/slackerbot/internal/tools/server.go b/slackerbot/internal/tools/server.go index a9f64a7..2aabe23 100644 --- a/slackerbot/internal/tools/server.go +++ b/slackerbot/internal/tools/server.go @@ -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) diff --git a/slackerbot/internal/tools/server_test.go b/slackerbot/internal/tools/server_test.go index e35e518..c81d619 100644 --- a/slackerbot/internal/tools/server_test.go +++ b/slackerbot/internal/tools/server_test.go @@ -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) + } +}