Skip to content

Commit b21ea59

Browse files
authored
Add streaming field limits to FormParser (#333)
1 parent 48f9e21 commit b21ea59

3 files changed

Lines changed: 57 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Unreleased
44

5+
* Add opt-in field-count and field-size limits for URL-encoded forms in `FormParser` [#332](https://github.com/Kludex/python-multipart/issues/332).
56
* Speed up querystring callback dispatch [#316](https://github.com/Kludex/python-multipart/pull/316).
67

78
## 0.0.32 (2026-06-04)

‎python_multipart/multipart.py‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class FileConfig(TypedDict, total=False):
5353
class FormParserConfig(FileConfig):
5454
UPLOAD_ERROR_ON_BAD_CTE: bool
5555
MAX_BODY_SIZE: float
56+
MAX_FIELDS: int | float
57+
MAX_FIELD_SIZE: int | float
5658
MAX_HEADER_COUNT: int
5759
MAX_HEADER_SIZE: int
5860

@@ -1612,6 +1614,8 @@ class FormParser:
16121614
#: Note: all file sizes should be in bytes.
16131615
DEFAULT_CONFIG: FormParserConfig = {
16141616
"MAX_BODY_SIZE": float("inf"),
1617+
"MAX_FIELDS": float("inf"),
1618+
"MAX_FIELD_SIZE": float("inf"),
16151619
"MAX_HEADER_COUNT": DEFAULT_MAX_HEADER_COUNT,
16161620
"MAX_HEADER_SIZE": DEFAULT_MAX_HEADER_SIZE,
16171621
"MAX_MEMORY_FILE_SIZE": 1 * 1024 * 1024,
@@ -1686,18 +1690,36 @@ def _on_end() -> None:
16861690
)
16871691

16881692
elif content_type == "application/x-www-form-urlencoded" or content_type == "application/x-url-encoded":
1689-
name_buffer: list[bytes] = []
1693+
max_fields = self.config["MAX_FIELDS"]
1694+
max_field_size = self.config["MAX_FIELD_SIZE"]
1695+
for key, limit in (("MAX_FIELDS", max_fields), ("MAX_FIELD_SIZE", max_field_size)):
1696+
if not isinstance(limit, (int, float)) or not limit >= 0:
1697+
raise ValueError(f"{key} must be a non-negative number, not {limit!r}")
16901698

1699+
name_buffer: list[bytes] = []
1700+
field_count = 0
1701+
field_size = 0
16911702
f: Field | None = None
16921703

16931704
def on_field_start() -> None:
1694-
pass
1705+
nonlocal field_count, field_size
1706+
field_count += 1
1707+
if field_count > max_fields:
1708+
raise FormParserError(f"Too many fields. Maximum number of fields is {max_fields}.")
1709+
field_size = 0
16951710

16961711
def on_field_name(data: bytes, start: int, end: int) -> None:
1712+
nonlocal field_size
1713+
field_size += end - start
1714+
if field_size > max_field_size:
1715+
raise FormParserError(f"Field exceeded maximum size of {max_field_size} bytes.")
16971716
name_buffer.append(data[start:end])
16981717

16991718
def on_field_data(data: bytes, start: int, end: int) -> None:
1700-
nonlocal f
1719+
nonlocal f, field_size
1720+
field_size += end - start
1721+
if field_size > max_field_size:
1722+
raise FormParserError(f"Field exceeded maximum size of {max_field_size} bytes.")
17011723
if f is None:
17021724
f = Field(b"".join(name_buffer))
17031725
del name_buffer[:]

‎tests/test_multipart.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,37 @@ def test_multipart_opening_boundary_max_size() -> None:
895895
assert events == ["begin", "end"]
896896

897897

898+
@pytest.mark.parametrize(
899+
"config,first,second,message",
900+
[
901+
({"MAX_FIELDS": 1}, b"a=1&", b"b=2", "Too many fields"),
902+
({"MAX_FIELD_SIZE": 3}, b"lo", b"ng=1", "Field exceeded maximum size of 3 bytes"),
903+
({"MAX_FIELD_SIZE": 3}, b"a=l", b"ong", "Field exceeded maximum size of 3 bytes"),
904+
],
905+
)
906+
def test_urlencoded_field_limits(config: dict[str, int], first: bytes, second: bytes, message: str) -> None:
907+
parser = FormParser("application/x-www-form-urlencoded", None, None, config=config)
908+
parser.write(first)
909+
with pytest.raises(FormParserError, match=message):
910+
parser.write(second)
911+
912+
913+
def test_urlencoded_field_limits_accept_exact_boundaries() -> None:
914+
fields: list[Field] = []
915+
parser = FormParser(
916+
"application/x-www-form-urlencoded", fields.append, None, config={"MAX_FIELDS": 2, "MAX_FIELD_SIZE": 3}
917+
)
918+
parser.write(b"a=12&b")
919+
parser.write(b"c=4")
920+
parser.finalize()
921+
assert [(field.field_name, field.value) for field in fields] == [(b"a", b"12"), (b"bc", b"4")]
922+
923+
924+
def test_invalid_urlencoded_field_limit() -> None:
925+
with pytest.raises(ValueError, match="MAX_FIELDS must be a non-negative number"):
926+
FormParser("application/x-www-form-urlencoded", None, None, config={"MAX_FIELDS": -1})
927+
928+
898929
@parametrize_class
899930
class TestFormParser(unittest.TestCase):
900931
def make(self, boundary: str | bytes, config: dict[str, Any] = {}) -> None:

0 commit comments

Comments
 (0)