Skip to content

Commit cbb774f

Browse files
[PR #12216/9cc4b917 backport][3.13] Check multipart max_size during iteration (#12229)
**This is a backport of PR #12216 as merged into master (9cc4b91).** --------- Co-authored-by: Sam Bull <git@sambull.org>
1 parent 8a74257 commit cbb774f

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

aiohttp/web_request.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -769,17 +769,25 @@ async def post(self) -> "MultiDictProxy[Union[str, bytes, FileField]]":
769769
out.add(field.name, ff)
770770
else:
771771
# deal with ordinary data
772-
value = await field.read(decode=True)
772+
raw_data = bytearray()
773+
while chunk := await field.read_chunk():
774+
size += len(chunk)
775+
if 0 < max_size < size:
776+
raise HTTPRequestEntityTooLarge(
777+
max_size=max_size, actual_size=size
778+
)
779+
raw_data.extend(chunk)
780+
781+
value = bytearray()
782+
# form-data doesn't support compression, so don't need to check size again.
783+
async for d in field.decode_iter(raw_data):
784+
value.extend(d)
785+
773786
if field_ct is None or field_ct.startswith("text/"):
774787
charset = field.get_charset(default="utf-8")
775788
out.add(field.name, value.decode(charset))
776789
else:
777790
out.add(field.name, value)
778-
size += len(value)
779-
if 0 < max_size < size:
780-
raise HTTPRequestEntityTooLarge(
781-
max_size=max_size, actual_size=size
782-
)
783791
else:
784792
raise ValueError(
785793
"To decode nested multipart you need to use custom reader",

0 commit comments

Comments
 (0)