mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-27 18:30:54 +08:00
fix(server): cap request bodies that claim a multipart type
limitRequestBody skipped every request with a multipart Content-Type, so any handler decoding JSON straight from r.Body (about 30 of them) read an unbounded body when the client simply labelled the request multipart. Multipart bodies now get an upload-sized cap (the largest limit an upload handler sets); upload handlers keep their own tighter limits and messages.
This commit is contained in:
@@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
@@ -52,6 +53,42 @@ func TestRawJSONBodyOverTheCapIs413(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultipartLabelDoesNotUncapTheBody(t *testing.T) {
|
||||
// A JSON handler reading r.Body directly, reached with a multipart
|
||||
// Content-Type, must still hit a cap rather than read without limit.
|
||||
var readErr error
|
||||
h := limitRequestBody(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, readErr = io.Copy(io.Discard, r.Body)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}))
|
||||
r := httptest.NewRequest(http.MethodPost, "/", io.LimitReader(zeroReader{}, maxMultipartBodyBytes+1))
|
||||
r.Header.Set("Content-Type", "multipart/form-data; boundary=abc")
|
||||
h.ServeHTTP(httptest.NewRecorder(), r)
|
||||
var tooLarge *http.MaxBytesError
|
||||
if !errors.As(readErr, &tooLarge) || tooLarge.Limit != maxMultipartBodyBytes {
|
||||
t.Fatalf("read err=%v, want MaxBytesError at %d", readErr, maxMultipartBodyBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultipartCapCoversEveryUploadHandler(t *testing.T) {
|
||||
for name, limit := range map[string]int64{
|
||||
"chat upload": maxChatUpload + chatUploadMultipartOverhead,
|
||||
"workspace upload": maxWorkspaceUpload,
|
||||
"skill zip": maxSkillZipBytes,
|
||||
} {
|
||||
if limit > maxMultipartBodyBytes {
|
||||
t.Fatalf("%s limit %d exceeds maxMultipartBodyBytes %d: real uploads would be refused", name, limit, maxMultipartBodyBytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type zeroReader struct{}
|
||||
|
||||
func (zeroReader) Read(p []byte) (int, error) {
|
||||
clear(p)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func TestMultipartBodyIsNotGivenTheJSONCap(t *testing.T) {
|
||||
h := limitRequestBody(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if _, ok := r.Body.(*limitedBody); ok {
|
||||
|
||||
+20
-5
@@ -86,11 +86,19 @@ func pathInt(r *http.Request, name string) (int64, bool) {
|
||||
return n, err == nil
|
||||
}
|
||||
|
||||
// decodeMaxBytes is the default JSON body cap. Multipart uploads are not capped
|
||||
// here; those handlers set their own limit. A smaller MaxBytesReader installed
|
||||
// by a handler still wins, because it sits inside this one.
|
||||
// decodeMaxBytes is the default JSON body cap. Multipart uploads get the larger
|
||||
// maxMultipartBodyBytes here instead; the upload handlers set their own,
|
||||
// tighter limit. A smaller MaxBytesReader installed by a handler still wins,
|
||||
// because it sits inside this one.
|
||||
const decodeMaxBytes = 32 << 20
|
||||
|
||||
// maxMultipartBodyBytes caps any request that declares a multipart Content-Type.
|
||||
// It is the largest limit an upload handler sets (chat attachments; workspace
|
||||
// uploads and skill zips are smaller), so no real upload is refused, while a
|
||||
// handler that decodes JSON straight from r.Body can no longer be handed an
|
||||
// unbounded body just by labelling the request multipart.
|
||||
const maxMultipartBodyBytes = maxChatUpload + chatUploadMultipartOverhead
|
||||
|
||||
// limitedBody records a MaxBytesError so a later 400 can be answered as 413.
|
||||
type limitedBody struct {
|
||||
io.ReadCloser
|
||||
@@ -128,10 +136,17 @@ func (b *bodyLimitWriter) Unwrap() http.ResponseWriter { return b.ResponseWriter
|
||||
|
||||
// limitRequestBody caps a non-multipart body at decodeMaxBytes and passes the
|
||||
// real ResponseWriter into MaxBytesReader. A handler that then answers 400 for
|
||||
// that read is rewritten to 413.
|
||||
// that read is rewritten to 413. A multipart body gets the plain upload-sized
|
||||
// cap instead: it is not a limitedBody, so decode still applies the JSON cap
|
||||
// if a JSON handler receives it, and upload handlers keep their own messages.
|
||||
func limitRequestBody(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Body == nil || r.Body == http.NoBody || isMultipart(r) {
|
||||
if r.Body == nil || r.Body == http.NoBody {
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
if isMultipart(r) {
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxMultipartBodyBytes)
|
||||
next.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user