Repository navigation
fix(context): honor FormParseMaxMemory in FormValue and FormFile #3264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 "" | ||
|
Comment on lines
+381
to
+382
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Preserve valid values when parsing reports an error. For
|
||
| } | ||
| 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. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() | ||
|
Comment on lines
+29
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Register cleanup for multipart temporary files. Both tests create a 4 MiB temporary upload file.
|
||
| } | ||
|
|
||
| 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) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recognize multipart media types without case sensitivity.
For
Content-Type: Multipart/Form-Data; boundary=..., both multipart guards fail. Go accepts this capitalization and normalizes it during media-type parsing. (raw.githubusercontent.com)FormValuenow calls onlyParseForm, so it returns""for valid multipart fields that previously worked.FormFilestill parses the upload throughRequest.FormFile, but uses the 32 MiB default instead of the configured limit. Parse and normalize the media type in bothFormValuesandFormFile. Add mixed-case coverage alongside the lowercase tests. (pkg.go.dev)Also applies to: 413-413