From 2f8628eed3d6fb196dd9d90092a31025b8878d1c Mon Sep 17 00:00:00 2001 From: Ozan Caglayan Date: Wed, 26 Apr 2023 00:05:22 +0100 Subject: [PATCH] sanic/app: When streaming, check REQUEST_MAX_SIZE if length is known --- sanic/app.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sanic/app.py b/sanic/app.py index 8eceec9a9e..fbe785d9fb 100644 --- a/sanic/app.py +++ b/sanic/app.py @@ -57,6 +57,7 @@ from sanic.config import SANIC_PREFIX, Config from sanic.exceptions import ( BadRequest, + PayloadTooLarge, SanicException, ServerError, URLBuildError, @@ -933,9 +934,11 @@ async def handle_request(self, request: Request): # no cov and request.stream.request_body and not route.extra.ignore_body ): + if hasattr(handler, "is_stream"): - # Streaming handler: lift the size limit - request.stream.request_max_size = float("inf") + rq_len = request.stream.request_bytes + if rq_len and rq_len > request.stream.request_max_size: + raise PayloadTooLarge("Request body exceeds the size limit") else: # Non-streaming handler: preload body await request.receive_body()