fix: enforce 512 MiB uploads across client and proxy

This commit is contained in:
Chubboy Ren
2026-11-17 02:03:57 +00:00
parent d41a5232ab
commit 99a0d27f88
7 changed files with 120 additions and 13 deletions
+1
View File
@@ -105,6 +105,7 @@ jobs:
- run: npx tsc --noEmit
- run: npm run test:polling
- run: npm run test:replay
- run: npm run test:upload
- run: npm run build:static
python:
+4
View File
@@ -476,6 +476,10 @@ server {
# ssl_certificate / ssl_certificate_key ...
location / {
# 512 MiB combined file limit plus 1 MiB of multipart metadata.
client_max_body_size 513m;
proxy_request_buffering off;
proxy_pass http://127.0.0.1:8787;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
+11 -6
View File
@@ -13,8 +13,13 @@ import (
"github.com/Autumn-27/artex/db"
)
// maxChatUpload caps the combined file data in one attachment upload.
const maxChatUpload = 500 << 20 // 500 MiB, plus 1 MiB for multipart metadata
const (
// maxChatUpload caps the combined file data in one attachment upload.
maxChatUpload = 512 << 20 // 512 MiB
// Allow multipart headers and boundaries in addition to the file data.
chatUploadMultipartOverhead = 1 << 20 // 1 MiB
chatUploadLimitMessage = "files exceed the 512 MiB upload limit"
)
// safeChatID guards the {id} path segment against traversal — task ids are numeric,
// session ids are alnum/_/- ; anything with "/" or ".." is rejected.
@@ -80,14 +85,14 @@ 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+(1<<20))
r.Body = http.MaxBytesReader(w, r.Body, maxChatUpload+chatUploadMultipartOverhead)
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")
writeErr(w, http.StatusRequestEntityTooLarge, chatUploadLimitMessage)
return
}
writeErr(w, 400, "failed to parse the upload or size limit exceeded: "+err.Error())
writeErr(w, 400, "failed to parse the upload: "+err.Error())
return
}
defer r.MultipartForm.RemoveAll()
@@ -101,7 +106,7 @@ func (s *Server) chatUpload(w http.ResponseWriter, r *http.Request) {
totalSize += hdr.Size
}
if totalSize > maxChatUpload {
writeErr(w, http.StatusRequestEntityTooLarge, "files exceed the 500 MiB upload limit")
writeErr(w, http.StatusRequestEntityTooLarge, chatUploadLimitMessage)
return
}
out := make([]chatAttachment, 0, len(files))
+17 -6
View File
@@ -18,9 +18,9 @@ func (uploadLimitZeroReader) Read(p []byte) (int, error) {
return len(p), nil
}
func TestChatUpload500MiBBoundary(t *testing.T) {
if maxChatUpload != 500<<20 {
t.Fatal("attachment limit must be 500 MiB")
func TestChatUpload512MiBBoundary(t *testing.T) {
if maxChatUpload != 512<<20 {
t.Fatal("attachment limit must be 512 MiB")
}
cases := []struct {
name string
@@ -28,9 +28,11 @@ func TestChatUpload500MiBBoundary(t *testing.T) {
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},
{"exactly 512 MiB", []int64{512 << 20}, http.StatusOK},
{"single file over limit", []int64{(512 << 20) + 1}, http.StatusRequestEntityTooLarge},
{"combined files exactly at limit", []int64{256 << 20, 256 << 20}, http.StatusOK},
{"combined files over limit", []int64{256 << 20, (256 << 20) + 1}, http.StatusRequestEntityTooLarge},
{"request body over limit", []int64{(513 << 20) + 1}, http.StatusRequestEntityTooLarge},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
@@ -86,6 +88,15 @@ func TestChatUpload500MiBBoundary(t *testing.T) {
}
}
} else {
var reply struct {
Error string `json:"error"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &reply); err != nil {
t.Fatal(err)
}
if reply.Error != "files exceed the 512 MiB upload limit" {
t.Fatalf("unexpected size-limit error: %q", reply.Error)
}
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)
+2 -1
View File
@@ -14,7 +14,8 @@
"prepare": "husky",
"generate:presets": "ts-node -P tsconfig.scripts.json src/scripts/generate-theme-presets.ts",
"test:polling": "node --test scripts/polling.test.mjs",
"test:replay": "node --test scripts/replay.test.mjs"
"test:replay": "node --test scripts/replay.test.mjs",
"test:upload": "node --test scripts/upload.test.mjs"
},
"lint-staged": {
"*.{js,ts,jsx,tsx}": [
+78
View File
@@ -0,0 +1,78 @@
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import { test } from "node:test";
import { fileURLToPath } from "node:url";
import vm from "node:vm";
import ts from "typescript";
const web = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const output = ts.transpileModule(fs.readFileSync(path.join(web, "src/lib/api.ts"), "utf8"), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
}).outputText;
const MiB = 1024 * 1024;
function setup(mock = false) {
const requests = [];
const forms = [];
const exports = {};
vm.runInNewContext(output, {
exports,
require(name) {
if (name === "@/lib/mock/enabled") return { MOCK: mock };
if (name === "@/lib/mock/handler") return { mockHandle: () => assert.fail("unexpected mock request") };
throw new Error("Unexpected import: " + name);
},
FormData: class {
fields = [];
constructor() {
forms.push(this);
}
append(name, value) {
this.fields.push([name, value]);
}
},
fetch: async (url, init) => {
requests.push({ url, init });
return { ok: true, json: async () => ({ attachments: [] }) };
},
});
return { api: exports.api, requests, forms };
}
for (const scope of ["task", "session", "staging"]) {
test(scope + ": rejects oversized single and combined files before FormData or fetch", async () => {
const s = setup();
for (const sizes of [[512 * MiB + 1], [256 * MiB, 256 * MiB + 1]]) {
const files = sizes.map((size, i) => ({ name: i + ".apk", size }));
await assert.rejects(s.api.chatUpload(scope, "42", files), /512 MiB.*combined size/);
}
assert.equal(s.forms.length, 0);
assert.equal(s.requests.length, 0);
});
test(scope + ": accepts ordinary files and exactly 512 MiB, including combined files", async () => {
for (const sizes of [[129 * MiB], [512 * MiB], [256 * MiB, 256 * MiB]]) {
const s = setup();
const files = sizes.map((size, i) => ({ name: i + ".apk", size }));
const result = await s.api.chatUpload(scope, "42", files);
assert.equal(result.attachments.length, 0);
assert.equal(s.requests.length, 1);
assert.equal(s.requests[0].url, "/api/chat/upload?scope=" + scope + "&id=42");
assert.equal(s.requests[0].init.method, "POST");
assert.equal(s.requests[0].init.body, s.forms[0]);
assert.deepEqual(s.forms[0].fields, files.map((file) => ["file", file]));
}
});
}
test("mock uploads enforce the same combined limit", async () => {
const s = setup(true);
await assert.rejects(
s.api.chatUpload("staging", "42", [{ name: "app.apk", size: 512 * MiB + 1 }]),
/512 MiB/,
);
const result = await s.api.chatUpload("staging", "42", [{ name: "app.apk", size: 512 * MiB }]);
assert.equal(result.attachments[0].size, 512 * MiB);
assert.equal(s.requests.length, 0);
});
+7
View File
@@ -110,6 +110,9 @@ import type {
WorkspaceListing,
} from "@/lib/types";
// Combined file data per attachment upload; matches maxChatUpload in server/chatupload.go.
const MAX_CHAT_UPLOAD_BYTES = 512 * 1024 * 1024;
function getToken(): string | null {
if (typeof window === "undefined") return null;
return localStorage.getItem("artex_token");
@@ -935,6 +938,10 @@ export const api = {
chatStatus: (taskId: string) => get<{ running: boolean }>(`/tasks/${taskId}/chat/status`),
// Option 1, file upload: lands in the session/task working directory uploads/, returning a relative path the agent can Read.
chatUpload: async (scope: "task" | "session" | "staging", id: string, files: File[]) => {
const totalSize = files.reduce((total, file) => total + file.size, 0);
if (totalSize > MAX_CHAT_UPLOAD_BYTES) {
throw new Error("Files exceed the 512 MiB upload limit (combined size).");
}
if (MOCK)
return {
attachments: files.map((f) => ({