Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions huma.go
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,8 @@ func processInputType(inputType reflect.Type, op *Operation, registry Registry)
rawBodyIndex = f.Index
initRequestBody(op, setRequestBodyRequired)
rbt = setRequestBodyFromRawBody(op, registry, f)
ensureBodyReadTimeout(op)
ensureMaxBodyBytes(op)
Comment on lines 1469 to +1472

if rbt == rbtMultipartDecoded {
dataField, ok := f.Type.FieldByName("data")
Expand Down
38 changes: 38 additions & 0 deletions huma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4502,6 +4502,44 @@ func TestCustomSchemaErrors(t *testing.T) {
assert.Contains(t, resp.Body.String(), `expected number >= 10`)
}

func TestRawBodyHasDefaultSizeLimit(t *testing.T) {
mux, api := humatest.New(t, huma.DefaultConfig("Test API", "1.0.0"))

var handlerCalled bool
var receivedLen int

huma.Register(api, huma.Operation{
Method: http.MethodPost,
Path: "/raw",
// MaxBodyBytes intentionally omitted
// Default is 1 MB set by "ensureMaxBodyBytes"
}, func(ctx context.Context, input *struct {
Comment on lines +4505 to +4516
RawBody []byte `contentType:"application/octet-stream"`
}) (*struct{}, error) {
handlerCalled = true
receivedLen = len(input.RawBody)
return &struct{}{}, nil
})

req := httptest.NewRequest(
http.MethodPost,
"/raw",
bytes.NewReader(bytes.Repeat([]byte("a"), 2*1024*1024)),
)
req.Header.Set("Content-Type", "application/octet-stream")

res := httptest.NewRecorder()
mux.ServeHTTP(res, req)

if handlerCalled {
t.Fatalf("handler received %d bytes, expected default body limit (1 MB) to reject request", receivedLen)
}

if got, want := res.Code, http.StatusRequestEntityTooLarge; got != want {
t.Fatalf("invalid status code: got %d, want %d", got, want)
}
}

func TestBodyRace(t *testing.T) {
// Run with the following:
// go test -run=TestBodyRace -race -parallel=100
Expand Down
Loading