Skip to content

Commit ff808b3

Browse files
committed
http1connection: Enforce max_body_size in _GzipMessageDelegate
This ensures we limit the post-decompression size of the body, and not only the compressed size (which is enforced via the Content-Length header at header-processing time).
1 parent ede4e37 commit ff808b3

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎tornado/http1connection.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ def read_response(self, delegate: httputil.HTTPMessageDelegate) -> Awaitable[boo
182182
been read. The result is true if the stream is still open.
183183
"""
184184
if self.params.decompress:
185-
delegate = _GzipMessageDelegate(delegate, self.params.chunk_size)
185+
delegate = _GzipMessageDelegate(
186+
delegate, self.params.chunk_size, self._max_body_size
187+
)
186188
return self._read_message(delegate)
187189

188190
async def _read_message(self, delegate: httputil.HTTPMessageDelegate) -> bool:
@@ -705,9 +707,16 @@ async def _read_body_until_close(
705707
class _GzipMessageDelegate(httputil.HTTPMessageDelegate):
706708
"""Wraps an `HTTPMessageDelegate` to decode ``Content-Encoding: gzip``."""
707709

708-
def __init__(self, delegate: httputil.HTTPMessageDelegate, chunk_size: int) -> None:
710+
def __init__(
711+
self,
712+
delegate: httputil.HTTPMessageDelegate,
713+
chunk_size: int,
714+
max_body_size: int,
715+
) -> None:
709716
self._delegate = delegate
710717
self._chunk_size = chunk_size
718+
self._max_body_size = max_body_size
719+
self._decompressed_body_size = 0
711720
self._decompressor = None # type: Optional[GzipDecompressor]
712721

713722
def headers_received(
@@ -732,6 +741,9 @@ async def data_received(self, chunk: bytes) -> None:
732741
compressed_data, self._chunk_size
733742
)
734743
if decompressed:
744+
self._decompressed_body_size += len(decompressed)
745+
if self._decompressed_body_size > self._max_body_size:
746+
raise httputil.HTTPInputError("decompressed body too large")
735747
ret = self._delegate.data_received(decompressed)
736748
if ret is not None:
737749
await ret

‎tornado/test/httpserver_test.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ def test_uncompressed(self):
11331133

11341134
class GzipTest(GzipBaseTest, AsyncHTTPTestCase):
11351135
def get_httpserver_options(self):
1136-
return dict(decompress_request=True)
1136+
return dict(decompress_request=True, max_body_size=100)
11371137

11381138
def test_gzip(self):
11391139
response = self.post_gzip("foo=bar")
@@ -1154,6 +1154,10 @@ def test_gzip_case_insensitive(self):
11541154
)
11551155
self.assertEqual(json_decode(response.body), {"foo": ["bar"]})
11561156

1157+
def test_size_limit(self):
1158+
with ExpectLog(gen_log, ".*decompressed body too large", level=logging.INFO):
1159+
self.post_gzip("x" * 101)
1160+
11571161

11581162
class GzipUnsupportedTest(GzipBaseTest, AsyncHTTPTestCase):
11591163
def test_gzip_unsupported(self):

0 commit comments

Comments
 (0)