From 18cf5b2105ca784b641cd308d5d5e91c886fadf1 Mon Sep 17 00:00:00 2001 From: alvaro Date: Sat, 1 Aug 2026 10:36:00 -0600 Subject: [PATCH 1/2] Add Echo body cache middleware --- go.mod | 5 ++ middleware/body_cache.go | 113 ++++++++++++++++++++++++++++++ middleware/body_cache_test.go | 128 ++++++++++++++++++++++++++++++++++ 3 files changed, 246 insertions(+) create mode 100644 go.mod create mode 100644 middleware/body_cache.go create mode 100644 middleware/body_cache_test.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..ee4679c --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/RoseMark45/echo + +go 1.22 + +require github.com/labstack/echo/v4 v4.13.4 diff --git a/middleware/body_cache.go b/middleware/body_cache.go new file mode 100644 index 0000000..05b6f41 --- /dev/null +++ b/middleware/body_cache.go @@ -0,0 +1,113 @@ +package middleware + +import ( + "bytes" + "errors" + "io" + "net/http" + + "github.com/labstack/echo/v4" +) + +const DefaultBodyCacheLimit int64 = 1 << 20 + +const bodyCacheKey = "body_cache.raw_body" + +var ErrBodyTooLarge = errors.New("request body exceeds configured cache limit") + +type BodyCacheConfig struct { + Limit int64 +} + +type CachedBinder struct { + Binder echo.Binder +} + +func BodyCache() echo.MiddlewareFunc { + return BodyCacheWithConfig(BodyCacheConfig{}) +} + +func BodyCacheWithConfig(config BodyCacheConfig) echo.MiddlewareFunc { + limit := config.Limit + if limit <= 0 { + limit = DefaultBodyCacheLimit + } + + return func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + body, err := cacheRequestBody(c.Request(), limit) + if err != nil { + if errors.Is(err, ErrBodyTooLarge) { + return echo.NewHTTPError(http.StatusRequestEntityTooLarge, ErrBodyTooLarge.Error()) + } + return err + } + + c.Set(bodyCacheKey, body) + return next(c) + } + } +} + +func (b CachedBinder) Bind(i interface{}, c echo.Context) error { + if err := binderOrDefault(b.Binder).Bind(i, c); err != nil { + return err + } + RestoreBody(c) + return nil +} + +func RawBody(c echo.Context) []byte { + body, _ := c.Get(bodyCacheKey).([]byte) + if body == nil { + return nil + } + + copied := make([]byte, len(body)) + copy(copied, body) + return copied +} + +func RestoreBody(c echo.Context) { + body, _ := c.Get(bodyCacheKey).([]byte) + if body == nil || c.Request() == nil { + return + } + + c.Request().Body = io.NopCloser(bytes.NewReader(body)) +} + +func cacheRequestBody(req *http.Request, limit int64) ([]byte, error) { + if req == nil || req.Body == nil { + return nil, nil + } + + limited := http.MaxBytesReader(nil, req.Body, limit) + body, err := io.ReadAll(limited) + if err != nil { + req.Body.Close() + if isTooLarge(err) { + return nil, ErrBodyTooLarge + } + return nil, err + } + + if err := req.Body.Close(); err != nil { + return nil, err + } + + req.Body = io.NopCloser(bytes.NewReader(body)) + return body, nil +} + +func binderOrDefault(b echo.Binder) echo.Binder { + if b != nil { + return b + } + return &echo.DefaultBinder{} +} + +func isTooLarge(err error) bool { + var maxBytesErr *http.MaxBytesError + return errors.As(err, &maxBytesErr) +} diff --git a/middleware/body_cache_test.go b/middleware/body_cache_test.go new file mode 100644 index 0000000..ca5bc4a --- /dev/null +++ b/middleware/body_cache_test.go @@ -0,0 +1,128 @@ +package middleware + +import ( + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/labstack/echo/v4" +) + +type bindPayload struct { + Foo string `json:"foo" xml:"foo" form:"foo"` +} + +func TestCachedBinderRestoresBodyAfterBind(t *testing.T) { + tests := []struct { + name string + contentType string + body string + }{ + {name: "json", contentType: echo.MIMEApplicationJSON, body: `{"foo":"bar"}`}, + {name: "xml", contentType: echo.MIMEApplicationXML, body: `bar`}, + {name: "form", contentType: echo.MIMEApplicationForm, body: `foo=bar`}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := echo.New() + e.Binder = CachedBinder{Binder: e.Binder} + e.Use(BodyCache()) + e.Use(func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + var payload bindPayload + if err := c.Bind(&payload); err != nil { + return err + } + if payload.Foo != "bar" { + t.Fatalf("Bind() decoded Foo=%q, want bar", payload.Foo) + } + return next(c) + } + }) + e.POST("/", func(c echo.Context) error { + body, err := io.ReadAll(c.Request().Body) + if err != nil { + return err + } + if string(body) != tt.body { + t.Fatalf("downstream body=%q, want %q", string(body), tt.body) + } + return c.NoContent(http.StatusNoContent) + }) + + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(tt.body)) + req.Header.Set(echo.HeaderContentType, tt.contentType) + rec := httptest.NewRecorder() + + e.ServeHTTP(rec, req) + if rec.Code != http.StatusNoContent { + t.Fatalf("status=%d, body=%s", rec.Code, rec.Body.String()) + } + }) + } +} + +func TestBodyCacheHandlesEmptyBody(t *testing.T) { + e := echo.New() + e.Use(BodyCache()) + e.POST("/", func(c echo.Context) error { + body, err := io.ReadAll(c.Request().Body) + if err != nil { + return err + } + if len(body) != 0 { + t.Fatalf("body=%q, want empty", string(body)) + } + return c.NoContent(http.StatusNoContent) + }) + + req := httptest.NewRequest(http.MethodPost, "/", http.NoBody) + rec := httptest.NewRecorder() + + e.ServeHTTP(rec, req) + if rec.Code != http.StatusNoContent { + t.Fatalf("status=%d, body=%s", rec.Code, rec.Body.String()) + } +} + +func TestBodyCacheRejectsLargeBody(t *testing.T) { + e := echo.New() + e.Use(BodyCacheWithConfig(BodyCacheConfig{Limit: 3})) + e.POST("/", func(c echo.Context) error { + t.Fatal("handler should not run for oversized body") + return nil + }) + + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("too large")) + rec := httptest.NewRecorder() + + e.ServeHTTP(rec, req) + if rec.Code != http.StatusRequestEntityTooLarge { + t.Fatalf("status=%d, want %d", rec.Code, http.StatusRequestEntityTooLarge) + } +} + +func TestRawBodyReturnsCopy(t *testing.T) { + e := echo.New() + e.Use(BodyCache()) + e.POST("/", func(c echo.Context) error { + raw := RawBody(c) + raw[0] = 'X' + again := RawBody(c) + if string(again) != "abc" { + t.Fatalf("RawBody returned mutable cache: %q", string(again)) + } + return c.NoContent(http.StatusNoContent) + }) + + req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader("abc")) + rec := httptest.NewRecorder() + + e.ServeHTTP(rec, req) + if rec.Code != http.StatusNoContent { + t.Fatalf("status=%d, body=%s", rec.Code, rec.Body.String()) + } +} From dd245cee286c27c1936cc6ba33d0c7b83a80be36 Mon Sep 17 00:00:00 2001 From: alvaro Date: Sat, 1 Aug 2026 10:42:41 -0600 Subject: [PATCH 2/2] Add module checksums --- go.mod | 14 +++++++++++++- go.sum | 29 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index ee4679c..f105ccf 100644 --- a/go.mod +++ b/go.mod @@ -1,5 +1,17 @@ module github.com/RoseMark45/echo -go 1.22 +go 1.23.0 require github.com/labstack/echo/v4 v4.13.4 + +require ( + github.com/labstack/gommon v0.4.2 // indirect + github.com/mattn/go-colorable v0.1.14 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/valyala/bytebufferpool v1.0.0 // indirect + github.com/valyala/fasttemplate v1.2.2 // indirect + golang.org/x/crypto v0.38.0 // indirect + golang.org/x/net v0.40.0 // indirect + golang.org/x/sys v0.33.0 // indirect + golang.org/x/text v0.25.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b92f49c --- /dev/null +++ b/go.sum @@ -0,0 +1,29 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/labstack/echo/v4 v4.13.4 h1:oTZZW+T3s9gAu5L8vmzihV7/lkXGZuITzTQkTEhcXEA= +github.com/labstack/echo/v4 v4.13.4/go.mod h1:g63b33BZ5vZzcIUF8AtRH40DrTlXnx4UMC8rBdndmjQ= +github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= +github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= +github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= +github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= +github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= +github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= +github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= +golang.org/x/crypto v0.38.0 h1:jt+WWG8IZlBnVbomuhg2Mdq0+BBQaHbtqHEFEigjUV8= +golang.org/x/crypto v0.38.0/go.mod h1:MvrbAqul58NNYPKnOra203SB9vpuZW0e+RRZV+Ggqjw= +golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY= +golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= +golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= +golang.org/x/text v0.25.0 h1:qVyWApTSYLk/drJRO5mDlNYskwQznZmkpV2c8q9zls4= +golang.org/x/text v0.25.0/go.mod h1:WEdwpYrmk1qmdHvhkSTNPm3app7v4rsT8F2UD6+VHIA= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=