Skip to content

armgit / ARTEX

Public Imported from GitHub
0

ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)

fix: support 500 MiB chat attachments

Chubboy Ren202de31b5b7c
diff --git a/server/chatupload.go b/server/chatupload.go
index 58844bd..593eab9 100644
--- a/server/chatupload.go
+++ b/server/chatupload.go
@@ -2,6 +2,7 @@ package server
import (
"encoding/json"
+ "errors"
"fmt"
"net/http"
"os"
@@ -12,8 +13,8 @@ import (
"github.com/Autumn-27/artex/db"
)
-// maxChatUpload caps a single chat-attachment upload request (memory + spill).
-const maxChatUpload = 128 << 20 // 128 MiB
+// maxChatUpload caps the combined file data in one attachment upload.
+const maxChatUpload = 500 << 20 // 500 MiB, plus 1 MiB for multipart metadata
// safeChatID guards the {id} path segment against traversal — task ids are numeric,
// session ids are alnum/_/- ; anything with "/" or ".." is rejected.
@@ -79,16 +80,30 @@ func (s *Server) chatUpload(w http.ResponseWriter, r *http.Request) {
writeErr(w, 500, "failed to create directory: "+err.Error())
return
}
- r.Body = http.MaxBytesReader(w, r.Body, maxChatUpload)
+ r.Body = http.MaxBytesReader(w, r.Body, maxChatUpload+(1<<20))
if err := r.ParseMultipartForm(32 << 20); err != nil {
+ var sizeErr *http.MaxBytesError
+ if errors.As(err, &sizeErr) {
+ writeErr(w, http.StatusRequestEntityTooLarge, "files exceed the 500 MiB upload limit")
+ return
+ }
writeErr(w, 400, "failed to parse the upload or size limit exceeded: "+err.Error())
return
}
+ defer r.MultipartForm.RemoveAll()
files := r.MultipartForm.File["file"]
if len(files) == 0 {
writeErr(w, 400, "missing upload file (form field file)")
return
}
+ var totalSize int64
+ for _, hdr := range files {
+ totalSize += hdr.Size
+ }
+ if totalSize > maxChatUpload {
+ writeErr(w, http.StatusRequestEntityTooLarge, "files exceed the 500 MiB upload limit")
+ return
+ }
out := make([]chatAttachment, 0, len(files))
for _, hdr := range files {
name := filepath.Base(hdr.Filename) // strip any path component
diff --git a/server/chatupload_limit_test.go b/server/chatupload_limit_test.go
new file mode 100644
index 0000000..78667d7
--- /dev/null
+++ b/server/chatupload_limit_test.go
@@ -0,0 +1,100 @@
+package server
+
+import (
+ "encoding/json"
+ "io"
+ "mime/multipart"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+type uploadLimitZeroReader struct{}
+
+func (uploadLimitZeroReader) Read(p []byte) (int, error) {
+ clear(p)
+ return len(p), nil
+}
+
+func TestChatUpload500MiBBoundary(t *testing.T) {
+ if maxChatUpload != 500<<20 {
+ t.Fatal("attachment limit must be 500 MiB")
+ }
+ cases := []struct {
+ name string
+ sizes []int64
+ status int
+ }{
+ {"above previous limit", []int64{129 << 20}, http.StatusOK},
+ {"exactly 500 MiB", []int64{500 << 20}, http.StatusOK},
+ {"combined files over limit", []int64{250 << 20, (250 << 20) + 1}, http.StatusRequestEntityTooLarge},
+ {"request body over limit", []int64{(501 << 20) + 1}, http.StatusRequestEntityTooLarge},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ workDir := t.TempDir()
+ spillDir := t.TempDir()
+ t.Setenv("TMPDIR", spillDir)
+ body, writer := io.Pipe()
+ form := multipart.NewWriter(writer)
+ done := make(chan error, 1)
+ go func() {
+ for _, size := range tc.sizes {
+ part, err := form.CreateFormFile("file", "boundary.bin")
+ if err == nil {
+ _, err = io.CopyN(part, uploadLimitZeroReader{}, size)
+ }
+ if err != nil {
+ writer.CloseWithError(err)
+ done <- err
+ return
+ }
+ }
+ err := form.Close()
+ writer.CloseWithError(err)
+ done <- err
+ }()
+ req := httptest.NewRequest(http.MethodPost, "/api/chat/upload?scope=staging&id=boundary-test", body)
+ req.Header.Set("Content-Type", form.FormDataContentType())
+ rec := httptest.NewRecorder()
+ srv := &Server{m: &Manager{dir: workDir}}
+ srv.chatUpload(rec, req)
+ body.Close()
+ writeErr := <-done
+ if rec.Code != tc.status {
+ t.Fatalf("status %d, want %d: %s", rec.Code, tc.status, rec.Body.String())
+ }
+ if tc.status == http.StatusOK {
+ if writeErr != nil {
+ t.Fatal(writeErr)
+ }
+ var reply struct {
+ Attachments []chatAttachment `json:"attachments"`
+ }
+ if err := json.Unmarshal(rec.Body.Bytes(), &reply); err != nil {
+ t.Fatal(err)
+ }
+ if len(reply.Attachments) != len(tc.sizes) {
+ t.Fatalf("got %d attachments", len(reply.Attachments))
+ }
+ for i, attachment := range reply.Attachments {
+ info, err := os.Stat(attachment.Abs)
+ if err != nil || attachment.Size != tc.sizes[i] || info.Size() != tc.sizes[i] {
+ t.Fatalf("attachment size mismatch: %+v, %v", attachment, err)
+ }
+ }
+ } else {
+ files, err := os.ReadDir(filepath.Join(workDir, "drafts", "boundary-test", "uploads"))
+ if err != nil || len(files) != 0 {
+ t.Fatalf("oversized upload saved files: %v, %v", files, err)
+ }
+ }
+ spillFiles, err := os.ReadDir(spillDir)
+ if err != nil || len(spillFiles) != 0 {
+ t.Fatalf("multipart files were not cleaned up: %v, %v", spillFiles, err)
+ }
+ })
+ }
+}