Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion rpc_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,16 @@ func recvAndDecompress(p *parser, s recvCompressor, dc Decompressor, maxReceiveM
func decompress(compressor encoding.Compressor, d mem.BufferSlice, dc Decompressor, maxReceiveMessageSize int, pool mem.BufferPool) (mem.BufferSlice, error) {
if dc != nil {
r := d.Reader()
uncompressed, err := dc.Do(r)
// Limit decompression to maxReceiveMessageSize+1 bytes so that the
// size check below fires before io.ReadAll buffers the full payload.
// Without this limit a client can send a tiny compressed message that
// expands to many gigabytes, exhausting server memory before the size
// check is reached.
reader := io.Reader(r)
if limit := int64(maxReceiveMessageSize); limit < math.MaxInt64 {
reader = io.LimitReader(r, limit+1)
}
uncompressed, err := dc.Do(reader)
if err != nil {
r.Close() // ensure buffers are reused
return nil, status.Errorf(codes.Internal, "grpc: failed to decompress the received message: %v", err)
Expand Down
Loading