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.
This commit is contained in:
Coff0xc
2026-11-21 19:53:50 +00:00
parent 5bed53b6ae
commit f4c7184dcc
4 changed files with 59 additions and 3 deletions
+1
View File
@@ -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
+2 -2
View File
@@ -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
}
}
+33
View File
@@ -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)
}
}
+23 -1
View File
@@ -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, "\"", "")