Skip to content

Commit b70ec6a

Browse files
committed
add checks for invalid casts
1 parent cdcf16d commit b70ec6a

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

middleware/body_limit.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package middleware
66
import (
77
"fmt"
88
"io"
9+
"net/http"
910
"sync"
1011

1112
"github.com/labstack/echo/v4"
@@ -77,7 +78,10 @@ func BodyLimitWithConfig(config BodyLimitConfig) echo.MiddlewareFunc {
7778
}
7879

7980
// Based on content read
80-
r := pool.Get().(*limitedReader)
81+
r, ok := pool.Get().(*limitedReader)
82+
if !ok {
83+
return echo.NewHTTPError(http.StatusInternalServerError, "invalid pool object")
84+
}
8185
r.Reset(req.Body)
8286
defer pool.Put(r)
8387
req.Body = r

middleware/compress.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func GzipWithConfig(config GzipConfig) echo.MiddlewareFunc {
9696
i := pool.Get()
9797
w, ok := i.(*gzip.Writer)
9898
if !ok {
99-
return echo.NewHTTPError(http.StatusInternalServerError, i.(error).Error())
99+
return echo.NewHTTPError(http.StatusInternalServerError, "invalid pool object")
100100
}
101101
rw := res.Writer
102102
w.Reset(rw)
@@ -189,7 +189,9 @@ func (w *gzipResponseWriter) Flush() {
189189
w.Writer.Write(w.buffer.Bytes())
190190
}
191191

192-
w.Writer.(*gzip.Writer).Flush()
192+
if gw, ok := w.Writer.(*gzip.Writer); ok {
193+
gw.Flush()
194+
}
193195
_ = http.NewResponseController(w.ResponseWriter).Flush()
194196
}
195197

middleware/compress_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ func TestGzipErrorReturnedInvalidConfig(t *testing.T) {
284284
rec := httptest.NewRecorder()
285285
e.ServeHTTP(rec, req)
286286
assert.Equal(t, http.StatusInternalServerError, rec.Code)
287-
assert.Contains(t, rec.Body.String(), "gzip")
287+
assert.Contains(t, rec.Body.String(), `{"message":"invalid pool object"}`)
288288
}
289289

290290
// Issue #806

0 commit comments

Comments
 (0)