From c9a4a20141e53b24043505fcb56f3e62782db680 Mon Sep 17 00:00:00 2001 From: Rishita Shaw Date: Fri, 22 Nov 2024 21:31:50 +0530 Subject: [PATCH 1/3] fix: handle "request body too large" error using type assertion --- modules/caddyhttp/requestbody/requestbody.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/caddyhttp/requestbody/requestbody.go b/modules/caddyhttp/requestbody/requestbody.go index 1c804aa134a..a2c577091e4 100644 --- a/modules/caddyhttp/requestbody/requestbody.go +++ b/modules/caddyhttp/requestbody/requestbody.go @@ -94,8 +94,10 @@ type errorWrapper struct { 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) + if err != nil { + if _, ok := err.(*http.MaxBytesError); ok { + err = caddyhttp.Error(http.StatusRequestEntityTooLarge, err) + } } return } From 42a3c0c4c9373eecf5c5b85ccd86ca608dbd4c43 Mon Sep 17 00:00:00 2001 From: Rishita Shaw Date: Fri, 22 Nov 2024 22:39:22 +0530 Subject: [PATCH 2/3] fix: address overlooked nil check for MaxBytesError --- modules/caddyhttp/requestbody/requestbody.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/modules/caddyhttp/requestbody/requestbody.go b/modules/caddyhttp/requestbody/requestbody.go index a2c577091e4..fa51200120c 100644 --- a/modules/caddyhttp/requestbody/requestbody.go +++ b/modules/caddyhttp/requestbody/requestbody.go @@ -94,10 +94,8 @@ type errorWrapper struct { func (ew errorWrapper) Read(p []byte) (n int, err error) { n, err = ew.ReadCloser.Read(p) - if err != nil { - if _, ok := err.(*http.MaxBytesError); ok { - err = caddyhttp.Error(http.StatusRequestEntityTooLarge, err) - } + if _, ok := err.(*http.MaxBytesError); ok { + err = caddyhttp.Error(http.StatusRequestEntityTooLarge, err) } return } From 6194f5a24413f5695d9c7312d9a9da0e34f3f37e Mon Sep 17 00:00:00 2001 From: Rishita Shaw Date: Fri, 22 Nov 2024 23:38:50 +0530 Subject: [PATCH 3/3] fix: replace type assertion with errors.As() for MaxBytesError --- modules/caddyhttp/requestbody/requestbody.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/caddyhttp/requestbody/requestbody.go b/modules/caddyhttp/requestbody/requestbody.go index fa51200120c..830050416e9 100644 --- a/modules/caddyhttp/requestbody/requestbody.go +++ b/modules/caddyhttp/requestbody/requestbody.go @@ -15,6 +15,7 @@ package requestbody import ( + "errors" "io" "net/http" "time" @@ -94,7 +95,8 @@ type errorWrapper struct { func (ew errorWrapper) Read(p []byte) (n int, err error) { n, err = ew.ReadCloser.Read(p) - if _, ok := err.(*http.MaxBytesError); ok { + var mbe *http.MaxBytesError + if errors.As(err, &mbe) { err = caddyhttp.Error(http.StatusRequestEntityTooLarge, err) } return