From 0368ed87f28eb07e7c5cca2dbff5585a019b4fa1 Mon Sep 17 00:00:00 2001 From: Masahiro Furudate <178inaba.git@gmail.com> Date: Wed, 11 Dec 2024 19:04:21 +0900 Subject: [PATCH 1/2] Add Conditions to Ensure Bind Succeeds with `Transfer-Encoding: chunked` (#2717) * Add conditions to ensure Bind succeeds with `Transfer-Encoding: chunked`. * Revert the ContentLength conditions for BindBody --- bind.go | 2 +- bind_test.go | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/bind.go b/bind.go index acc346506..ed7ca3249 100644 --- a/bind.go +++ b/bind.go @@ -67,7 +67,7 @@ func (b *DefaultBinder) BindQueryParams(c Context, i interface{}) error { // See MIMEMultipartForm: https://golang.org/pkg/net/http/#Request.ParseMultipartForm func (b *DefaultBinder) BindBody(c Context, i interface{}) (err error) { req := c.Request() - if req.ContentLength <= 0 { + if req.ContentLength == 0 { return } diff --git a/bind_test.go b/bind_test.go index a7e8dbb3a..303c8854a 100644 --- a/bind_test.go +++ b/bind_test.go @@ -13,6 +13,7 @@ import ( "mime/multipart" "net/http" "net/http/httptest" + "net/http/httputil" "net/url" "reflect" "strconv" @@ -941,6 +942,7 @@ func TestDefaultBinder_BindBody(t *testing.T) { givenMethod string givenContentType string whenNoPathParams bool + whenChunkedBody bool whenBindTarget interface{} expect interface{} expectError string @@ -1061,12 +1063,30 @@ func TestDefaultBinder_BindBody(t *testing.T) { expectError: "code=415, message=Unsupported Media Type", }, { - name: "ok, JSON POST bind to struct with: path + query + http.NoBody", + name: "nok, JSON POST with http.NoBody", givenURL: "/api/real_node/endpoint?node=xxx", givenMethod: http.MethodPost, givenContentType: MIMEApplicationJSON, givenContent: http.NoBody, expect: &Node{ID: 0, Node: ""}, + expectError: "code=400, message=EOF, internal=EOF", + }, + { + name: "ok, JSON POST with empty body", + givenURL: "/api/real_node/endpoint?node=xxx", + givenMethod: http.MethodPost, + givenContentType: MIMEApplicationJSON, + givenContent: strings.NewReader(""), + expect: &Node{ID: 0, Node: ""}, + }, + { + name: "ok, JSON POST bind to struct with: path + query + chunked body", + givenURL: "/api/real_node/endpoint?node=xxx", + givenMethod: http.MethodPost, + givenContentType: MIMEApplicationJSON, + givenContent: httputil.NewChunkedReader(strings.NewReader("18\r\n" + `{"id": 1, "node": "zzz"}` + "\r\n0\r\n")), + whenChunkedBody: true, + expect: &Node{ID: 1, Node: "zzz"}, }, } @@ -1083,6 +1103,10 @@ func TestDefaultBinder_BindBody(t *testing.T) { case MIMEApplicationJSON: req.Header.Set(HeaderContentType, MIMEApplicationJSON) } + if tc.whenChunkedBody { + req.ContentLength = -1 + req.TransferEncoding = append(req.TransferEncoding, "chunked") + } rec := httptest.NewRecorder() c := e.NewContext(req, rec) From fd3f07447eddb53e096b0df3be38bbf79123d605 Mon Sep 17 00:00:00 2001 From: "Martti T." Date: Wed, 11 Dec 2024 12:18:50 +0200 Subject: [PATCH 2/2] Changelog for v4.13.1 (#2719) --- CHANGELOG.md | 8 ++++++++ echo.go | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c31a4ccb5..55f24418f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## v4.13.1 - 2024-12-11 + +**Fixes** + +* Fix BindBody ignoring `Transfer-Encoding: chunked` requests by @178inaba in https://github.com/labstack/echo/pull/2717 + + + ## v4.13.0 - 2024-12-04 **BREAKING CHANGE** JWT Middleware Removed from Core use [labstack/echo-jwt](https://github.com/labstack/echo-jwt) instead diff --git a/echo.go b/echo.go index aa69b84dc..c86ce6994 100644 --- a/echo.go +++ b/echo.go @@ -259,7 +259,7 @@ const ( const ( // Version of Echo - Version = "4.13.0" + Version = "4.13.1" website = "https://echo.labstack.com" // http://patorjk.com/software/taag/#p=display&f=Small%20Slant&t=Echo banner = `