Skip to content

Commit dba1c4b

Browse files
authored
Enforce max_fields and max_part_size in FormParser (#3329)
1 parent 45e51dc commit dba1c4b

3 files changed

Lines changed: 154 additions & 3 deletions

File tree

‎starlette/formparsers.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,19 @@ def __init__(self, message: str) -> None:
5555

5656

5757
class FormParser:
58-
def __init__(self, headers: Headers, stream: AsyncGenerator[bytes, None]) -> None:
58+
def __init__(
59+
self,
60+
headers: Headers,
61+
stream: AsyncGenerator[bytes, None],
62+
*,
63+
max_fields: int | float = 1000,
64+
max_part_size: int = 1024 * 1024, # 1MB
65+
) -> None:
5966
assert multipart is not None, "The `python-multipart` library must be installed to use form parsing."
6067
self.headers = headers
6168
self.stream = stream
69+
self.max_fields = max_fields
70+
self.max_part_size = max_part_size
6271
self.messages: list[tuple[FormMessage, bytes]] = []
6372

6473
def on_field_start(self) -> None:
@@ -97,6 +106,7 @@ async def parse(self) -> FormData:
97106
field_value = bytearray()
98107

99108
items: list[tuple[str, str | UploadFile]] = []
109+
field_count = 0
100110

101111
# Feed the parser with data from the request.
102112
async for chunk in self.stream:
@@ -111,10 +121,17 @@ async def parse(self) -> FormData:
111121
field_name = bytearray()
112122
field_value = bytearray()
113123
elif message_type == FormMessage.FIELD_NAME:
124+
if len(field_name) + len(field_value) + len(message_bytes) > self.max_part_size:
125+
raise MultiPartException(f"Field exceeded maximum size of {int(self.max_part_size / 1024)}KB.")
114126
field_name.extend(message_bytes)
115127
elif message_type == FormMessage.FIELD_DATA:
128+
if len(field_name) + len(field_value) + len(message_bytes) > self.max_part_size:
129+
raise MultiPartException(f"Field exceeded maximum size of {int(self.max_part_size / 1024)}KB.")
116130
field_value.extend(message_bytes)
117131
elif message_type == FormMessage.FIELD_END:
132+
field_count += 1
133+
if field_count > self.max_fields:
134+
raise MultiPartException(f"Too many fields. Maximum number of fields is {self.max_fields}.")
118135
name = unquote_plus(field_name.decode("latin-1"))
119136
value = unquote_plus(field_value.decode("latin-1"))
120137
items.append((name, value))

‎starlette/requests.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,18 @@ async def _get_form(
294294
raise HTTPException(status_code=400, detail=exc.message)
295295
raise exc
296296
elif content_type == b"application/x-www-form-urlencoded":
297-
form_parser = FormParser(self.headers, self.stream())
298-
self._form = await form_parser.parse()
297+
try:
298+
form_parser = FormParser(
299+
self.headers,
300+
self.stream(),
301+
max_fields=max_fields,
302+
max_part_size=max_part_size,
303+
)
304+
self._form = await form_parser.parse()
305+
except MultiPartException as exc:
306+
if "app" in self.scope:
307+
raise HTTPException(status_code=400, detail=exc.message)
308+
raise exc
299309
else:
300310
self._form = FormData()
301311
return self._form

‎tests/test_formparsers.py‎

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,130 @@ def test_multipart_multi_field_app_reads_body(tmpdir: Path, test_client_factory:
465465
assert response.json() == {"some": "data", "second": "key pair"}
466466

467467

468+
@pytest.mark.parametrize(
469+
"app,expectation",
470+
[
471+
(app, pytest.raises(MultiPartException)),
472+
(Starlette(routes=[Mount("/", app=app)]), does_not_raise()),
473+
],
474+
)
475+
def test_urlencoded_too_many_fields_raise(
476+
app: ASGIApp,
477+
expectation: AbstractContextManager[Exception],
478+
test_client_factory: TestClientFactory,
479+
) -> None:
480+
client = test_client_factory(app)
481+
data = "&".join(f"N{i}=" for i in range(1001))
482+
with expectation:
483+
res = client.post(
484+
"/",
485+
content=data,
486+
headers={"Content-Type": "application/x-www-form-urlencoded"},
487+
)
488+
assert res.status_code == 400
489+
assert res.text == "Too many fields. Maximum number of fields is 1000."
490+
491+
492+
@pytest.mark.parametrize(
493+
"app,expectation",
494+
[
495+
(app, pytest.raises(MultiPartException)),
496+
(Starlette(routes=[Mount("/", app=app)]), does_not_raise()),
497+
],
498+
)
499+
def test_urlencoded_field_exceeds_max_part_size_raise(
500+
app: ASGIApp,
501+
expectation: AbstractContextManager[Exception],
502+
test_client_factory: TestClientFactory,
503+
) -> None:
504+
client = test_client_factory(app)
505+
data = "field=" + "x" * (1024 * 1024 + 1)
506+
with expectation:
507+
res = client.post(
508+
"/",
509+
content=data,
510+
headers={"Content-Type": "application/x-www-form-urlencoded"},
511+
)
512+
assert res.status_code == 400
513+
assert res.text == "Field exceeded maximum size of 1024KB."
514+
515+
516+
@pytest.mark.parametrize(
517+
"app,expectation",
518+
[
519+
(app, pytest.raises(MultiPartException)),
520+
(Starlette(routes=[Mount("/", app=app)]), does_not_raise()),
521+
],
522+
)
523+
def test_urlencoded_field_name_exceeds_max_part_size_raise(
524+
app: ASGIApp,
525+
expectation: AbstractContextManager[Exception],
526+
test_client_factory: TestClientFactory,
527+
) -> None:
528+
client = test_client_factory(app)
529+
data = "x" * (1024 * 1024 + 1) + "=value"
530+
with expectation:
531+
res = client.post(
532+
"/",
533+
content=data,
534+
headers={"Content-Type": "application/x-www-form-urlencoded"},
535+
)
536+
assert res.status_code == 400
537+
assert res.text == "Field exceeded maximum size of 1024KB."
538+
539+
540+
@pytest.mark.parametrize(
541+
"app,expectation",
542+
[
543+
(make_app_max_parts(max_fields=1), pytest.raises(MultiPartException)),
544+
(
545+
Starlette(routes=[Mount("/", app=make_app_max_parts(max_fields=1))]),
546+
does_not_raise(),
547+
),
548+
],
549+
)
550+
def test_urlencoded_max_fields_is_customizable(
551+
app: ASGIApp,
552+
expectation: AbstractContextManager[Exception],
553+
test_client_factory: TestClientFactory,
554+
) -> None:
555+
client = test_client_factory(app)
556+
with expectation:
557+
res = client.post(
558+
"/",
559+
content="a=1&b=2",
560+
headers={"Content-Type": "application/x-www-form-urlencoded"},
561+
)
562+
assert res.status_code == 400
563+
assert res.text == "Too many fields. Maximum number of fields is 1."
564+
565+
566+
@pytest.mark.parametrize(
567+
"app,expectation",
568+
[
569+
(make_app_max_parts(max_part_size=1024 * 10), pytest.raises(MultiPartException)),
570+
(
571+
Starlette(routes=[Mount("/", app=make_app_max_parts(max_part_size=1024 * 10))]),
572+
does_not_raise(),
573+
),
574+
],
575+
)
576+
def test_urlencoded_max_part_size_is_customizable(
577+
app: ASGIApp,
578+
expectation: AbstractContextManager[Exception],
579+
test_client_factory: TestClientFactory,
580+
) -> None:
581+
client = test_client_factory(app)
582+
with expectation:
583+
res = client.post(
584+
"/",
585+
content="field=" + "x" * (1024 * 10 + 1),
586+
headers={"Content-Type": "application/x-www-form-urlencoded"},
587+
)
588+
assert res.status_code == 400
589+
assert res.text == "Field exceeded maximum size of 10KB."
590+
591+
468592
def test_user_safe_decode_helper() -> None:
469593
result = _user_safe_decode(b"\xc4\x99\xc5\xbc\xc4\x87", "utf-8")
470594
assert result == "ężć"

0 commit comments

Comments
 (0)