Skip to content

Commit

Permalink
requestbody: Return HTTP 413 (fix #4558)
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Mar 11, 2022
1 parent b82e22b commit 3d616e8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
3 changes: 3 additions & 0 deletions modules/caddyhttp/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func (e HandlerError) Error() string {
return strings.TrimSpace(s)
}

// Unwrap returns the underlying error value. See the `errors` package for info.
func (e HandlerError) Unwrap() error { return e.Err }

// randString returns a string of n random characters.
// It is not even remotely secure OR a proper distribution.
// But it's good enough for some things. It excludes certain
Expand Down
8 changes: 2 additions & 6 deletions modules/caddyhttp/replacer.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,8 @@ func addHTTPVarsToReplacer(repl *caddy.Replacer, req *http.Request, w http.Respo
// read the request body into a buffer (can't pool because we
// don't know its lifetime and would have to make a copy anyway)
buf := new(bytes.Buffer)
_, err := io.Copy(buf, req.Body)
if err != nil {
return "", true
}
// replace real body with buffered data
req.Body = io.NopCloser(buf)
_, _ = io.Copy(buf, req.Body) // can't handle error, so just ignore it
req.Body = io.NopCloser(buf) // replace real body with buffered data
return buf.String(), true

// original request, before any internal changes
Expand Down
18 changes: 17 additions & 1 deletion modules/caddyhttp/requestbody/requestbody.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package requestbody

import (
"io"
"net/http"

"github.com/caddyserver/caddy/v2"
Expand All @@ -28,6 +29,7 @@ func init() {
// RequestBody is a middleware for manipulating the request body.
type RequestBody struct {
// The maximum number of bytes to allow reading from the body by a later handler.
// If more bytes are read, an error with HTTP status 413 is returned.
MaxSize int64 `json:"max_size,omitempty"`
}

Expand All @@ -44,10 +46,24 @@ func (rb RequestBody) ServeHTTP(w http.ResponseWriter, r *http.Request, next cad
return next.ServeHTTP(w, r)
}
if rb.MaxSize > 0 {
r.Body = http.MaxBytesReader(w, r.Body, rb.MaxSize)
r.Body = errorWrapper{http.MaxBytesReader(w, r.Body, rb.MaxSize)}
}
return next.ServeHTTP(w, r)
}

// errorWrapper wraps errors that are returned from Read()
// so that they can be associated with a proper status code.
type errorWrapper struct {
io.ReadCloser
}

func (ew errorWrapper) Read(p []byte) (n int, err error) {
n, err = ew.ReadCloser.Read(p)
if err != nil && err.Error() == "http: request body too large" {
err = caddyhttp.Error(http.StatusRequestEntityTooLarge, err)
}
return
}

// Interface guard
var _ caddyhttp.MiddlewareHandler = (*RequestBody)(nil)
4 changes: 3 additions & 1 deletion modules/caddyhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package caddyhttp
import (
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
Expand Down Expand Up @@ -600,7 +601,8 @@ func PrepareRequest(r *http.Request, repl *caddy.Replacer, w http.ResponseWriter
// If err is a HandlerError, the returned values will
// have richer information.
func errLogValues(err error) (status int, msg string, fields []zapcore.Field) {
if handlerErr, ok := err.(HandlerError); ok {
var handlerErr HandlerError
if errors.As(err, &handlerErr) {
status = handlerErr.StatusCode
if handlerErr.Err == nil {
msg = err.Error()
Expand Down

0 comments on commit 3d616e8

Please sign in to comment.