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 (
const (
requestBodyReadInitCap = 512
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
......@@ -69,21 +72,21 @@ func decompressRequestBody(encoding string, raw []byte) ([]byte, error) {
return nil, err
}
defer dec.Close()
return io.ReadAll(dec)
return io.ReadAll(io.LimitReader(dec, maxDecompressedBodySize))
case "gzip", "x-gzip":
gr, err := gzip.NewReader(bytes.NewReader(raw))
if err != nil {
return nil, err
}
defer gr.Close()
return io.ReadAll(gr)
defer func() { _ = gr.Close() }()
return io.ReadAll(io.LimitReader(gr, maxDecompressedBodySize))
case "deflate":
zr, err := zlib.NewReader(bytes.NewReader(raw))
if err != nil {
return nil, err
}
defer zr.Close()
return io.ReadAll(zr)
defer func() { _ = zr.Close() }()
return io.ReadAll(io.LimitReader(zr, maxDecompressedBodySize))
default:
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