Open platform to collect and prioritize feedback
fix(web): cap request body size
Matt Roberts32f9c3c3a690
diff --git a/app/pkg/env/env.go b/app/pkg/env/env.goindex 2c968a983..9a6768176 100644--- a/app/pkg/env/env.go+++ b/app/pkg/env/env.go@@ -46,6 +46,9 @@ type config struct {ReadTimeout time.Duration `env:"HTTP_READ_TIMEOUT,default=5s,strict"`WriteTimeout time.Duration `env:"HTTP_WRITE_TIMEOUT,default=10s,strict"`IdleTimeout time.Duration `env:"HTTP_IDLE_TIMEOUT,default=120s,strict"`+ // Largest accepted request body in bytes. The biggest legitimate request is a post edit+ // carrying 3 attachments of 5 MiB each, which is ~20 MiB once base64-encoded in JSON.+ MaxBodySize int64 `env:"HTTP_MAX_BODY_SIZE,default=26214400,strict"`}Port string `env:"PORT,default=3000"`Host string `env:"HOST,default="`diff --git a/app/pkg/web/engine.go b/app/pkg/web/engine.goindex 622949fef..b5763ddff 100644--- a/app/pkg/web/engine.go+++ b/app/pkg/web/engine.go@@ -2,6 +2,7 @@ package webimport ("context"+ "encoding/json""fmt""io"stdLog "log"@@ -51,6 +52,9 @@ type notFoundHandler struct {}func (h *notFoundHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {+ if rejectOversizedBody(res, req) {+ return+ }ctx := NewContext(h.engine, req, res, nil)_ = h.handler(ctx)}@@ -265,6 +269,9 @@ func (e *Engine) handle(middlewares []MiddlewareFunc, handler HandlerFunc) httprnext = middlewares[i](next)}var h = func(res http.ResponseWriter, req *http.Request, ps httprouter.Params) {+ if rejectOversizedBody(res, req) {+ return+ }params := make(StringMap)for _, p := range ps {params[p.Key] = p.Value@@ -275,6 +282,27 @@ func (e *Engine) handle(middlewares []MiddlewareFunc, handler HandlerFunc) httprreturn h}+// rejectOversizedBody responds with 413 Request Entity Too Large and returns true when the+// request body is bigger than HTTP_MAX_BODY_SIZE. It must run before NewContext, because+// WrapRequest reads the whole body into memory.+func rejectOversizedBody(res http.ResponseWriter, req *http.Request) bool {+ maxBytes := env.Config.HTTP.MaxBodySize+ if req.ContentLength > maxBytes {+ res.Header().Set("Content-Type", UTF8JSONContentType)+ res.Header().Set("Connection", "close")+ res.WriteHeader(http.StatusRequestEntityTooLarge)+ _ = json.NewEncoder(res).Encode(Map{+ "errors": []Map{{"message": "The request is too large."}},+ })+ return true+ }++ // Bodies of unknown length (chunked) are not read by WrapRequest, but cap them anyway in+ // case anything reads the raw body later on.+ req.Body = http.MaxBytesReader(res, req.Body, maxBytes)+ return false+}+// Group is our router group wrappertype Group struct {engine *Enginediff --git a/app/pkg/web/engine_body_test.go b/app/pkg/web/engine_body_test.gonew file mode 100644index 000000000..fab55b6e7--- /dev/null+++ b/app/pkg/web/engine_body_test.go@@ -0,0 +1,83 @@+package web++import (+ "io"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"++ "github.com/getfider/fider/app/pkg/env"++ . "github.com/getfider/fider/app/pkg/assert"+)++func newBodyEchoEngine() *Engine {+ e := New()+ e.Post("/echo", func(c *Context) error {+ return c.String(http.StatusOK, c.Request.Body)+ })+ e.NotFound(func(c *Context) error {+ return c.String(http.StatusNotFound, c.Request.Body)+ })+ return e+}++func withMaxBodySize(t *testing.T, size int64) {+ original := env.Config.HTTP.MaxBodySize+ env.Config.HTTP.MaxBodySize = size+ t.Cleanup(func() { env.Config.HTTP.MaxBodySize = original })+}++func TestEngine_MaxBodySize_DefaultFitsLargestUpload(t *testing.T) {+ RegisterT(t)++ // 3 attachments of 5120 KB each, base64-encoded+ largestUpload := int64(3 * 5120 * 1024 * 4 / 3)+ Expect(env.Config.HTTP.MaxBodySize > largestUpload).IsTrue()+}++func TestEngine_BodyWithinLimit(t *testing.T) {+ RegisterT(t)+ withMaxBodySize(t, 10)++ e := newBodyEchoEngine()+ res := httptest.NewRecorder()+ e.mux.ServeHTTP(res, httptest.NewRequest("POST", "/echo", strings.NewReader("0123456789")))++ Expect(res.Code).Equals(http.StatusOK)+ Expect(res.Body.String()).Equals("0123456789")+}++func TestEngine_BodyTooLarge(t *testing.T) {+ RegisterT(t)+ withMaxBodySize(t, 10)++ e := newBodyEchoEngine()+ for _, path := range []string{"/echo", "/unknown-route"} {+ res := httptest.NewRecorder()+ e.mux.ServeHTTP(res, httptest.NewRequest("POST", path, strings.NewReader("01234567890")))++ Expect(res.Code).Equals(http.StatusRequestEntityTooLarge)+ Expect(res.Header().Get("Content-Type")).Equals(UTF8JSONContentType)+ Expect(res.Body.String()).ContainsSubstring(`"errors"`)+ }+}++func TestEngine_ChunkedBodyIsCapped(t *testing.T) {+ RegisterT(t)+ withMaxBodySize(t, 10)++ var readErr error+ e := New()+ e.Post("/raw", func(c *Context) error {+ _, readErr = io.ReadAll(c.Request.instance.Body)+ return c.NoContent(http.StatusOK)+ })++ req := httptest.NewRequest("POST", "/raw", strings.NewReader("01234567890"))+ req.ContentLength = -1+ e.mux.ServeHTTP(httptest.NewRecorder(), req)++ Expect(readErr).IsNotNil()+}