Skip to content

Commit d69df35

Browse files
authored
Treat only & as the urlencoded field separator (#290)
1 parent 1e6ff97 commit d69df35

3 files changed

Lines changed: 14 additions & 16 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Parse `application/x-www-form-urlencoded` bodies per the WHATWG URL standard, treating only `&` as a field separator.
6+
37
## 0.0.29 (2026-05-17)
48

59
* Handle malformed RFC 2231 continuations in `parse_options_header` [#270](https://github.com/Kludex/python-multipart/pull/270).

‎python_multipart/multipart.py‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ class MultipartState(IntEnum):
126126
SPACE = b" "[0]
127127
HYPHEN = b"-"[0]
128128
AMPERSAND = b"&"[0]
129-
SEMICOLON = b";"[0]
130129
LOWER_A = b"a"[0]
131130
LOWER_Z = b"z"[0]
132131
NULL = b"\x00"[0]
@@ -840,13 +839,13 @@ def _internal_write(self, data: bytes, length: int) -> int:
840839
# yet reached a separator, and thus, if we do, we need to skip
841840
# it as it will be the boundary between fields that's supposed
842841
# to be there.
843-
if ch == AMPERSAND or ch == SEMICOLON:
842+
if ch == AMPERSAND:
844843
if found_sep:
845844
# If we're parsing strictly, we disallow blank chunks.
846845
if strict_parsing:
847-
raise QuerystringParseError("Skipping duplicate ampersand/semicolon at %d" % i, offset=i)
846+
raise QuerystringParseError("Skipping duplicate ampersand at %d" % i, offset=i)
848847
else:
849-
self.logger.debug("Skipping duplicate ampersand/semicolon at %d", i)
848+
self.logger.debug("Skipping duplicate ampersand at %d", i)
850849
else:
851850
# This case is when we're skipping the (first)
852851
# separator between fields, so we just set our flag
@@ -864,17 +863,15 @@ def _internal_write(self, data: bytes, length: int) -> int:
864863
elif state == QuerystringState.FIELD_NAME:
865864
# Try and find a separator - we ensure that, if we do, we only
866865
# look for the equal sign before it.
867-
sep_pos = data.find(b"&", i)
868-
if sep_pos == -1:
869-
sep_pos = data.find(b";", i)
866+
sep_pos = data.find(b"&", i, length)
870867

871868
# See if we can find an equals sign in the remaining data. If
872869
# so, we can immediately emit the field name and jump to the
873870
# data state.
874871
if sep_pos != -1:
875872
equals_pos = data.find(b"=", i, sep_pos)
876873
else:
877-
equals_pos = data.find(b"=", i)
874+
equals_pos = data.find(b"=", i, length)
878875

879876
if equals_pos != -1:
880877
# Emit this name.
@@ -921,11 +918,8 @@ def _internal_write(self, data: bytes, length: int) -> int:
921918
i = length
922919

923920
elif state == QuerystringState.FIELD_DATA:
924-
# Try finding either an ampersand or a semicolon after this
925-
# position.
926-
sep_pos = data.find(b"&", i)
927-
if sep_pos == -1:
928-
sep_pos = data.find(b";", i)
921+
# Try finding an ampersand after this position.
922+
sep_pos = data.find(b"&", i, length)
929923

930924
# If we found it, callback this bit as data and then go back
931925
# to expecting to find a field.

‎tests/test_multipart.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,10 @@ def test_streaming_break(self) -> None:
428428
self.p.write(b"f=baz")
429429
self.assert_fields((b"asdf", b"baz"))
430430

431-
def test_semicolon_separator(self) -> None:
432-
self.p.write(b"foo=bar;asdf=baz")
431+
def test_semicolon_is_data_not_a_field_separator(self) -> None:
432+
self.p.write(b"role=user&x=;role=admin")
433433

434-
self.assert_fields((b"foo", b"bar"), (b"asdf", b"baz"))
434+
self.assert_fields((b"role", b"user"), (b"x", b";role=admin"))
435435

436436
def test_too_large_field(self) -> None:
437437
self.p.max_size = 15

0 commit comments

Comments
 (0)