Skip to content

Commit 3506c15

Browse files
authored
Ignore RFC 2231 extended parameters in parse_options_header (#291)
1 parent d69df35 commit 3506c15

3 files changed

Lines changed: 87 additions & 38 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
* Parse `application/x-www-form-urlencoded` bodies per the WHATWG URL standard, treating only `&` as a field separator.
6+
* Ignore RFC 2231/5987 extended parameters (`name*`, `filename*`) in `parse_options_header`, keeping the plain parameter authoritative per [RFC 7578 §4.2](https://datatracker.ietf.org/doc/html/rfc7578#section-4.2).
67

78
## 0.0.29 (2026-05-17)
89

‎python_multipart/multipart.py‎

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import shutil
66
import sys
77
import tempfile
8-
from email.message import Message
98
from enum import IntEnum
109
from io import BufferedRandom, BytesIO
1110
from numbers import Number
@@ -156,10 +155,37 @@ class MultipartState(IntEnum):
156155
"""
157156

158157

158+
def _parseparam(s: str) -> list[str]:
159+
# Vendored from the standard library's
160+
# [`email.message._parseparam`](https://github.com/python/cpython/blob/v3.14.2/Lib/email/message.py#L73-L96)
161+
# to split a header into its `;`-separated parts without treating a `;` inside a double-quoted string as a
162+
# separator - and without the RFC 2231 decoding that `email.message.Message.get_params` would apply on top.
163+
s = ";" + s
164+
plist: list[str] = []
165+
start = 0
166+
while s.find(";", start) == start:
167+
start += 1
168+
end = s.find(";", start)
169+
ind, diff = start, 0
170+
while end > 0:
171+
diff += s.count('"', ind, end) - s.count('\\"', ind, end)
172+
if diff % 2 == 0:
173+
break
174+
end, ind = ind, s.find(";", end + 1)
175+
if end < 0:
176+
end = len(s)
177+
i = s.find("=", start, end)
178+
if i == -1:
179+
f = s[start:end]
180+
else:
181+
f = s[start:i].rstrip().lower() + "=" + s[i + 1 : end].lstrip()
182+
plist.append(f.strip())
183+
start = end
184+
return plist
185+
186+
159187
def parse_options_header(value: str | bytes | None) -> tuple[bytes, dict[bytes, bytes]]:
160188
"""Parses a Content-Type header into a value in the following format: (content_type, {parameters})."""
161-
# Uses email.message.Message to parse the header as described in PEP 594.
162-
# Ref: https://peps.python.org/pep-0594/#cgi
163189
if not value:
164190
return (b"", {})
165191

@@ -174,37 +200,24 @@ def parse_options_header(value: str | bytes | None) -> tuple[bytes, dict[bytes,
174200
if ";" not in value:
175201
return (value.lower().strip().encode("latin-1"), {})
176202

177-
# Split at the first semicolon, to get our value and then options.
178-
# ctype, rest = value.split(b';', 1)
179-
message = Message()
180-
message["content-type"] = value
181-
# `get_params()` can raise on malformed RFC 2231 headers found via fuzzing:
182-
# - ValueError on oversized continuation indices (all supported versions).
183-
# - TypeError on mixed `filename*` + `filename*0*` continuations (Python 3.12 only;
184-
# 3.13+ silently picks a value).
185-
# TODO: drop `TypeError` once Python 3.12 reaches EOL (October 2028).
186-
try:
187-
params = message.get_params()
188-
except (TypeError, ValueError): # pragma: no cover
189-
return (value.split(";", 1)[0].lower().strip().encode("latin-1"), {})
190-
# If there were no parameters, this would have already returned above
191-
assert params, "At least the content type value should be present"
192-
ctype = params.pop(0)[0].encode("latin-1")
203+
ctype, *segments = _parseparam(value)
193204
options: dict[bytes, bytes] = {}
194-
for param in params:
195-
key, value = param
196-
# If the value returned from get_params() is a 3-tuple, the last
197-
# element corresponds to the value.
198-
# See: https://docs.python.org/3/library/email.compat32-message.html
199-
if isinstance(value, tuple):
200-
value = value[-1]
201-
# If the value is a filename, we need to fix a bug on IE6 that sends
202-
# the full file path instead of the filename.
203-
if key == "filename":
204-
if value[1:3] == ":\\" or value[:2] == "\\\\":
205-
value = value.split("\\")[-1]
206-
options[key.encode("latin-1")] = value.encode("latin-1")
207-
return ctype, options
205+
for segment in segments:
206+
key, _, val = segment.partition("=")
207+
# [RFC 7578 §4.2](https://datatracker.ietf.org/doc/html/rfc7578#section-4.2)
208+
# forbids the RFC 5987/2231 extended syntax (`key*=`, `key*0`, ...) in
209+
# multipart/form-data, so we ignore those parameters and keep the plain
210+
# `key` authoritative.
211+
if "*" in key:
212+
continue
213+
if len(val) >= 2 and val[0] == '"' and val[-1] == '"':
214+
val = val[1:-1].replace("\\\\", "\\").replace('\\"', '"')
215+
# Work around an IE6 bug where the full file path is sent instead of
216+
# just the filename.
217+
if key == "filename" and (val[1:3] == ":\\" or val[:2] == "\\\\"):
218+
val = val.split("\\")[-1]
219+
options[key.encode("latin-1")] = val.encode("latin-1")
220+
return ctype.encode("latin-1"), options
208221

209222

210223
class Field:

‎tests/test_multipart.py‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,24 +299,59 @@ def test_redos_attack_header(self) -> None:
299299
# If vulnerable, this test wouldn't finish, the line above would hang
300300
self.assertIn(b'"\\', p[b"!"])
301301

302-
def test_handles_rfc_2231(self) -> None:
302+
def test_ignores_rfc_2231_extended_param(self) -> None:
303+
# RFC 7578 §4.2 forbids the RFC 5987/2231 extended syntax, so the
304+
# decoded `param*` value is not exposed under `param`.
303305
t, p = parse_options_header(b"text/plain; param*=us-ascii'en-us'encoded%20message")
304306

305-
self.assertEqual(p[b"param"], b"encoded message")
307+
self.assertEqual(t, b"text/plain")
308+
self.assertEqual(p, {})
309+
310+
def test_plain_param_authoritative_over_extended(self) -> None:
311+
# When both plain and extended forms are present, the plain one wins
312+
# and the extended one is ignored.
313+
_, p = parse_options_header(b"form-data; name=\"comment\"; name*=utf-8''other")
314+
315+
self.assertEqual(p, {b"name": b"comment"})
306316

307-
def test_rejects_oversized_rfc_2231_index(self) -> None:
317+
def test_ignores_rfc_2231_continuation_filename(self) -> None:
318+
_, p = parse_options_header(b'form-data; name="f"; filename*0="a"; filename*1="b.txt"')
319+
320+
self.assertEqual(p, {b"name": b"f"})
321+
322+
def test_ignores_oversized_rfc_2231_index(self) -> None:
308323
t, p = parse_options_header("text/plain; filename*" + ("1" * 4301) + "*=utf-8''x")
309324

310325
self.assertEqual(t, b"text/plain")
311326
self.assertEqual(p, {})
312327

313-
@pytest.mark.skipif(sys.version_info >= (3, 13), reason="email parser only raises TypeError on Python 3.12")
314-
def test_rejects_mixed_rfc_2231_continuations(self) -> None:
328+
def test_ignores_mixed_rfc_2231_continuations(self) -> None:
315329
t, p = parse_options_header("text/plain; filename*=utf-8''a; filename*0*=utf-8''b")
316330

317331
self.assertEqual(t, b"text/plain")
318332
self.assertEqual(p, {})
319333

334+
def test_ignores_extended_param_case_insensitively(self) -> None:
335+
_, p = parse_options_header(b"text/plain; UPPER*=utf-8''X")
336+
337+
self.assertEqual(p, {})
338+
339+
def test_preserves_quoted_semicolons_and_escapes(self) -> None:
340+
_, p = parse_options_header(b'text/plain; a="x;y"; b="esc \\" quote"')
341+
342+
self.assertEqual(p, {b"a": b"x;y", b"b": b'esc " quote'})
343+
344+
def test_preserves_content_type_case(self) -> None:
345+
t, p = parse_options_header(b"Text/Plain; a=b")
346+
347+
self.assertEqual(t, b"Text/Plain")
348+
self.assertEqual(p, {b"a": b"b"})
349+
350+
def test_preserves_backslash_unquoting_order(self) -> None:
351+
_, p = parse_options_header(b'text/plain; q="a\\\\b"')
352+
353+
self.assertEqual(p, {b"q": b"a\\b"})
354+
320355

321356
class TestBaseParser(unittest.TestCase):
322357
def setUp(self) -> None:

0 commit comments

Comments
 (0)