fix(server): stop the chat upload name search when the path cannot be created

uniqueUploadPath treated every Stat error except "not exist" as "try the next
suffix". A name the filesystem rejects never becomes "not exist", so the
handler looped. The search now returns that error, and it also stops after
10000 existing suffixes.
This commit is contained in:
Coff0xc
2026-11-21 19:33:22 +00:00
parent 5bed53b6ae
commit 0eb66f4c44
3 changed files with 64 additions and 8 deletions
+1
View File
@@ -6,6 +6,7 @@ All notable changes to this project are documented in this file. The format is b
### Fixed
- **Chat upload name search stops**: choosing `name`, `name-1`, `name-2`, … now returns an error when the filesystem rejects the path or after 10000 collisions, instead of looping while `Stat` keeps failing.
- **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
+30 -8
View File
@@ -21,6 +21,10 @@ const (
// Allow multipart headers and boundaries in addition to the file data.
chatUploadMultipartOverhead = 1 << 20 // 1 MiB
chatUploadLimitMessage = "files exceed the 512 MiB upload limit"
// maxUploadNameTries bounds the name-1, name-2, … search. A Stat error that
// is not "not exist" (a component the filesystem rejects) must stop the
// search instead of spinning.
maxUploadNameTries = 10000
)
// safeChatID guards the {id} path segment against traversal — task ids are numeric,
@@ -123,7 +127,11 @@ func (s *Server) chatUpload(w http.ResponseWriter, r *http.Request) {
if name == "" || name == "." || name == ".." || strings.ContainsAny(name, `/\`) {
continue
}
dest := uniqueUploadPath(dir, name)
dest, err := uniqueUploadPath(dir, name)
if err != nil {
writeErr(w, 500, "Save failed: "+err.Error())
return
}
if err := saveUpload(hdr, dest); err != nil {
writeErr(w, 500, "Save failed: "+err.Error())
return
@@ -136,19 +144,33 @@ func (s *Server) chatUpload(w http.ResponseWriter, r *http.Request) {
// uniqueUploadPath returns dir/name, or dir/name-1, dir/name-2… when it already exists,
// 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) {
return dest
func uniqueUploadPath(dir, name string) (string, error) {
if dest, ok, err := uploadPathFree(filepath.Join(dir, name)); err != nil || ok {
return dest, err
}
ext := filepath.Ext(name)
stem := strings.TrimSuffix(name, ext)
for i := 1; ; i++ {
for i := 1; i <= maxUploadNameTries; i++ {
cand := filepath.Join(dir, fmt.Sprintf("%s-%d%s", stem, i, ext))
if _, err := os.Stat(cand); os.IsNotExist(err) {
return cand
dest, ok, err := uploadPathFree(cand)
if err != nil || ok {
return dest, err
}
}
return "", fmt.Errorf("no free name for upload %q", name)
}
// uploadPathFree reports whether path can be created. ok is false when a file
// is already there. Any other Stat error is returned so the caller stops.
func uploadPathFree(path string) (dest string, ok bool, err error) {
_, err = os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
return path, true, nil
}
if err != nil {
return "", false, err
}
return "", false, nil
}
// composeAgentMessage appends an attachment manifest to the user's message so the agent
+33
View File
@@ -0,0 +1,33 @@
package server
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestUniqueUploadPathSuffixesCollisions(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "note.txt"), []byte("a"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "note-1.txt"), []byte("b"), 0o644); err != nil {
t.Fatal(err)
}
got, err := uniqueUploadPath(dir, "note.txt")
if err != nil {
t.Fatal(err)
}
if filepath.Base(got) != "note-2.txt" {
t.Fatalf("got %s", got)
}
}
func TestUniqueUploadPathStopsOnUnstorableName(t *testing.T) {
dir := t.TempDir()
name := strings.Repeat("a", 300) + ".txt"
if _, err := uniqueUploadPath(dir, name); err == nil {
t.Fatal("an overlong file name must not be searched forever")
}
}