Commit 40feb86b authored by shaw's avatar shaw
Browse files

fix(httputil): add decompression bomb guard and fix errcheck lint

parent f972a2fa
...@@ -16,6 +16,9 @@ import ( ...@@ -16,6 +16,9 @@ import (
const ( const (
requestBodyReadInitCap = 512 requestBodyReadInitCap = 512
requestBodyReadMaxInitCap = 1 << 20 requestBodyReadMaxInitCap = 1 << 20
// maxDecompressedBodySize limits the decompressed request body to 64 MB
// to prevent decompression bomb attacks.
maxDecompressedBodySize = 64 << 20
) )
// ReadRequestBodyWithPrealloc reads request body with preallocated buffer based // ReadRequestBodyWithPrealloc reads request body with preallocated buffer based
...@@ -69,21 +72,21 @@ func decompressRequestBody(encoding string, raw []byte) ([]byte, error) { ...@@ -69,21 +72,21 @@ func decompressRequestBody(encoding string, raw []byte) ([]byte, error) {
return nil, err return nil, err
} }
defer dec.Close() defer dec.Close()
return io.ReadAll(dec) return io.ReadAll(io.LimitReader(dec, maxDecompressedBodySize))
case "gzip", "x-gzip": case "gzip", "x-gzip":
gr, err := gzip.NewReader(bytes.NewReader(raw)) gr, err := gzip.NewReader(bytes.NewReader(raw))
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer gr.Close() defer func() { _ = gr.Close() }()
return io.ReadAll(gr) return io.ReadAll(io.LimitReader(gr, maxDecompressedBodySize))
case "deflate": case "deflate":
zr, err := zlib.NewReader(bytes.NewReader(raw)) zr, err := zlib.NewReader(bytes.NewReader(raw))
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer zr.Close() defer func() { _ = zr.Close() }()
return io.ReadAll(zr) return io.ReadAll(io.LimitReader(zr, maxDecompressedBodySize))
default: default:
return nil, errors.New("unsupported Content-Encoding") return nil, errors.New("unsupported Content-Encoding")
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment