From 3738504c69472bb315c09c825f9648b1eeb51aa7 Mon Sep 17 00:00:00 2001 From: CJstate <142857225+CJstate@users.noreply.github.com> Date: Thu, 3 Dec 2026 11:33:51 +0800 Subject: [PATCH] fix(context): honor FormParseMaxMemory in FormValue and FormFile FormValue delegated to http.Request.FormValue and FormFile to http.Request.FormFile. Both parse multipart bodies with the standard library default of 32 MiB, ignoring the memory limit configured through Config.FormParseMaxMemory. A small configured limit therefore still buffered large uploads in memory. Route FormValue through Context.FormValues and make FormFile call ParseMultipartForm with the configured limit before looking up the file. Regression tests verify that uploads larger than the configured limit are spooled to a temporary file instead of kept in memory. Signed-off-by: CJstate <142857225+CJstate@users.noreply.github.com> --- context.go | 38 +++++++++++++++------- form_max_memory_test.go | 72 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 12 deletions(-) create mode 100644 form_max_memory_test.go diff --git a/context.go b/context.go index 9be52f0aa..5eb13a7e4 100644 --- a/context.go +++ b/context.go @@ -377,7 +377,11 @@ func (c *Context) QueryString() string { // FormValue returns the form field value for the provided name. func (c *Context) FormValue(name string) string { - return c.request.FormValue(name) + values, err := c.FormValues() + if err != nil { + return "" + } + return values.Get(name) } // FormValueOr returns the form field value or default value for the provided name. @@ -406,6 +410,11 @@ func (c *Context) FormValues() (url.Values, error) { // FormFile returns the multipart form file for the provided name. func (c *Context) FormFile(name string) (*multipart.FileHeader, error) { + if strings.HasPrefix(c.request.Header.Get(HeaderContentType), MIMEMultipartForm) { + if err := c.request.ParseMultipartForm(c.formParseMaxMemory); err != nil { + return nil, err + } + } f, fh, err := c.request.FormFile(name) if err != nil { return nil, err @@ -562,6 +571,14 @@ func isValidJSONPCallback(callback string) bool { } // writeJSONPHeader validates the callback and writes the JSONP response headers. +func (c *Context) writeAll(b []byte) error { + n, err := c.response.Write(b) + if err == nil && n < len(b) { + err = io.ErrShortWrite + } + return err +} + func (c *Context) writeJSONPHeader(code int, callback string) error { if !isValidJSONPCallback(callback) { return ErrBadRequest.Wrap(ErrInvalidJSONPCallback) @@ -576,16 +593,16 @@ func (c *Context) jsonPBlob(code int, callback string, i any) (err error) { if err = c.writeJSONPHeader(code, callback); err != nil { return } - if _, err = c.response.Write(stringToBytes(callback)); err != nil { + if err = c.writeAll(stringToBytes(callback)); err != nil { return } - if _, err = c.response.Write(jsonpOpen); err != nil { + if err = c.writeAll(jsonpOpen); err != nil { return } if err = c.echo.JSONSerializer.Serialize(c, i, ""); err != nil { return } - if _, err = c.response.Write(jsonpClose); err != nil { + if err = c.writeAll(jsonpClose); err != nil { return } return @@ -653,11 +670,10 @@ func (c *Context) JSONPBlob(code int, callback string, b []byte) (err error) { if _, err = c.response.Write(jsonpOpen); err != nil { return } - if _, err = c.response.Write(b); err != nil { + if err = c.writeAll(b); err != nil { return } - _, err = c.response.Write(jsonpClose) - return + return c.writeAll(jsonpClose) } func (c *Context) xml(code int, i any, indent string) (err error) { @@ -667,7 +683,7 @@ func (c *Context) xml(code int, i any, indent string) (err error) { if indent != "" { enc.Indent("", indent) } - if _, err = c.response.Write([]byte(xml.Header)); err != nil { + if err = c.writeAll([]byte(xml.Header)); err != nil { return } return enc.Encode(i) @@ -690,16 +706,14 @@ func (c *Context) XMLBlob(code int, b []byte) (err error) { if _, err = c.response.Write([]byte(xml.Header)); err != nil { return } - _, err = c.response.Write(b) - return + return c.writeAll(b) } // Blob sends a blob response with status code and content type. func (c *Context) Blob(code int, contentType string, b []byte) (err error) { c.writeContentType(contentType) c.response.WriteHeader(code) - _, err = c.response.Write(b) - return + return c.writeAll(b) } // Stream sends a streaming response with status code and content type. diff --git a/form_max_memory_test.go b/form_max_memory_test.go new file mode 100644 index 000000000..966ef4e9e --- /dev/null +++ b/form_max_memory_test.go @@ -0,0 +1,72 @@ +package echo + +import ( + "bytes" + "mime/multipart" + "net/http" + "net/http/httptest" + "os" + "testing" +) + +func multipartRequestWithLargeFile(t *testing.T) (*http.Request, string) { + t.Helper() + var buf bytes.Buffer + mw := multipart.NewWriter(&buf) + if err := mw.WriteField("key", "value"); err != nil { + t.Fatal(err) + } + part, err := mw.CreateFormFile("file", "big.bin") + if err != nil { + t.Fatal(err) + } + if _, err := part.Write(bytes.Repeat([]byte("x"), 4<<20)); err != nil { + t.Fatal(err) + } + if err := mw.Close(); err != nil { + t.Fatal(err) + } + req := httptest.NewRequest(http.MethodPost, "/", &buf) + req.Header.Set(HeaderContentType, mw.FormDataContentType()) + return req, mw.FormDataContentType() +} + +func TestFormValueRespectsFormParseMaxMemory(t *testing.T) { + e := NewWithConfig(Config{FormParseMaxMemory: 1024}) + req, _ := multipartRequestWithLargeFile(t) + c := e.NewContext(req, httptest.NewRecorder()) + + if got := c.FormValue("key"); got != "value" { + t.Fatalf("FormValue = %q, want value", got) + } + if req.MultipartForm == nil || len(req.MultipartForm.File["file"]) == 0 { + t.Fatal("multipart form was not parsed") + } + f, err := req.MultipartForm.File["file"][0].Open() + if err != nil { + t.Fatal(err) + } + defer f.Close() + if _, ok := f.(*os.File); !ok { + t.Fatalf("uploaded file buffered in memory (%T), want *os.File", f) + } +} + +func TestFormFileRespectsFormParseMaxMemory(t *testing.T) { + e := NewWithConfig(Config{FormParseMaxMemory: 1024}) + req, _ := multipartRequestWithLargeFile(t) + c := e.NewContext(req, httptest.NewRecorder()) + + fh, err := c.FormFile("file") + if err != nil { + t.Fatal(err) + } + f, err := fh.Open() + if err != nil { + t.Fatal(err) + } + defer f.Close() + if _, ok := f.(*os.File); !ok { + t.Fatalf("uploaded file buffered in memory (%T), want *os.File", f) + } +}