Skip to content

Commit 9d58822

Browse files
[PR aio-libs#12216/9cc4b917 backport][3.14] Check multipart max_size during iteration (aio-libs#12230)
**This is a backport of PR aio-libs#12216 as merged into master (9cc4b91).** --------- Co-authored-by: Sam Bull <git@sambull.org>
1 parent 3007c37 commit 9d58822

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

aiohttp/web_request.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -778,17 +778,25 @@ async def post(self) -> "MultiDictProxy[str | bytes | FileField]":
778778
out.add(field.name, ff)
779779
else:
780780
# deal with ordinary data
781-
value = await field.read(decode=True)
781+
raw_data = bytearray()
782+
while chunk := await field.read_chunk():
783+
size += len(chunk)
784+
if 0 < max_size < size:
785+
raise HTTPRequestEntityTooLarge(
786+
max_size=max_size, actual_size=size
787+
)
788+
raw_data.extend(chunk)
789+
790+
value = bytearray()
791+
# form-data doesn't support compression, so don't need to check size again.
792+
async for d in field.decode_iter(raw_data):
793+
value.extend(d)
794+
782795
if field_ct is None or field_ct.startswith("text/"):
783796
charset = field.get_charset(default="utf-8")
784797
out.add(field.name, value.decode(charset))
785798
else:
786799
out.add(field.name, value)
787-
size += len(value)
788-
if 0 < max_size < size:
789-
raise HTTPRequestEntityTooLarge(
790-
max_size=max_size, actual_size=size
791-
)
792800
else:
793801
raise ValueError(
794802
"To decode nested multipart you need to use custom reader",

0 commit comments

Comments
 (0)