From f4c7184dcc2060c23c67b142e117b9a4f4652cb2 Mon Sep 17 00:00:00 2001 From: Coff0xc <184964491+Coff0xc@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:53:50 +0000 Subject: [PATCH] fix(server): refuse to upload through a symlink os.Create follows a symlink, and Stat treats a dangling link as missing, so a name planted in the uploads directory could redirect the server's write. Destinations are now Lstat'd, a symlink is rejected, and the chat name search treats a link as taken. --- CHANGELOG.md | 1 + server/chatupload.go | 4 ++-- server/upload_symlink_test.go | 33 +++++++++++++++++++++++++++++++++ server/workspace.go | 24 +++++++++++++++++++++++- 4 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 server/upload_symlink_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 5af6fb6..ed0db96 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ All notable changes to this project are documented in this file. The format is b ### Fixed +- **Uploads do not follow a symlink**: chat and workspace uploads `Lstat` the destination and refuse to write when it is already a symlink. A dangling link is no longer treated as a free name. - **Docker Compose database passwords with URL-special characters**: `docker-compose.yml` no longer pastes `POSTGRES_PASSWORD` into `ARTEX_PG_DSN`. The server builds the URL from `ARTEX_PG_HOST`, `ARTEX_PG_PORT`, `ARTEX_PG_USER`, `ARTEX_PG_PASSWORD`, `ARTEX_PG_DBNAME` and `ARTEX_PG_SSLMODE`, percent-encoding the user and password, so `/`, `?`, `%`, `@`, `:` and spaces connect. An explicit `ARTEX_PG_DSN` still wins and is not rewritten. `ARTEX_PG_PASSWORD` is removed from the environment with the other startup secrets (#450). ## [0.7.0] - 2026-11-21 diff --git a/server/chatupload.go b/server/chatupload.go index bd7cc03..de9edea 100644 --- a/server/chatupload.go +++ b/server/chatupload.go @@ -138,14 +138,14 @@ func (s *Server) chatUpload(w http.ResponseWriter, r *http.Request) { // so re-uploading the same filename never clobbers a prior attachment. func uniqueUploadPath(dir, name string) string { dest := filepath.Join(dir, name) - if _, err := os.Stat(dest); os.IsNotExist(err) { + if _, err := os.Lstat(dest); os.IsNotExist(err) { return dest } ext := filepath.Ext(name) stem := strings.TrimSuffix(name, ext) for i := 1; ; i++ { cand := filepath.Join(dir, fmt.Sprintf("%s-%d%s", stem, i, ext)) - if _, err := os.Stat(cand); os.IsNotExist(err) { + if _, err := os.Lstat(cand); os.IsNotExist(err) { return cand } } diff --git a/server/upload_symlink_test.go b/server/upload_symlink_test.go new file mode 100644 index 0000000..8371fbd --- /dev/null +++ b/server/upload_symlink_test.go @@ -0,0 +1,33 @@ +package server + +import ( + "os" + "path/filepath" + "testing" +) + +func TestRefuseUploadSymlink(t *testing.T) { + dir := t.TempDir() + target := filepath.Join(dir, "secret") + if err := os.WriteFile(target, []byte("keep"), 0o644); err != nil { + t.Fatal(err) + } + link := filepath.Join(dir, "upload") + if err := os.Symlink(target, link); err != nil { + t.Skipf("symlink: %v", err) + } + if err := refuseUploadSymlink(link); err == nil { + t.Fatal("symlink destination was accepted") + } + body, err := os.ReadFile(target) + if err != nil { + t.Fatal(err) + } + if string(body) != "keep" { + t.Fatalf("target changed to %q", body) + } + missing := filepath.Join(dir, "fresh") + if err := refuseUploadSymlink(missing); err != nil { + t.Fatal(err) + } +} diff --git a/server/workspace.go b/server/workspace.go index 19044e5..40237e7 100644 --- a/server/workspace.go +++ b/server/workspace.go @@ -2,6 +2,7 @@ package server import ( "bytes" + "fmt" "io" "mime/multipart" "net/http" @@ -340,12 +341,15 @@ func (s *Server) wsUpload(w http.ResponseWriter, r *http.Request) { } func saveUpload(hdr *multipart.FileHeader, dest string) error { + if err := refuseUploadSymlink(dest); err != nil { + return err + } src, err := hdr.Open() if err != nil { return err } defer src.Close() - out, err := os.Create(dest) + out, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) if err != nil { return err } @@ -354,6 +358,24 @@ func saveUpload(hdr *multipart.FileHeader, dest string) error { return err } +// refuseUploadSymlink rejects a destination that is already a symlink. os.Create +// follows one, including a dangling link that Stat reports as missing, so an +// agent that can write the uploads directory could aim the server's write at +// another path. Lstat sees the link itself. +func refuseUploadSymlink(dest string) error { + fi, err := os.Lstat(dest) + if err != nil { + if os.IsNotExist(err) { + return nil + } + return err + } + if fi.Mode()&os.ModeSymlink != 0 { + return fmt.Errorf("refusing to write through a symlink") + } + return nil +} + // sanitizeFilename strips characters unsafe for a Content-Disposition filename token. func sanitizeFilename(name string) string { name = strings.ReplaceAll(name, "\"", "")