55import shutil
66import sys
77import tempfile
8- from email.message import Message
98from enum import IntEnum
109from io import BufferedRandom, BytesIO
1110from 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+
159187def 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
210223class Field:
0 commit comments