@@ -157,27 +157,6 @@ class MultipartState(IntEnum): |
157 | 157 | """ |
158 | 158 |
|
159 | 159 |
|
160 | | -def _split_mime_parameters(value: str) -> list[str]: |
161 | | - """Split a MIME parameter string on semicolons that are outside quoted strings.""" |
162 | | - parts: list[str] = [] |
163 | | - start = 0 |
164 | | - in_quotes = False |
165 | | - i = 0 |
166 | | - while i < len(value): |
167 | | - c = value[i] |
168 | | - if in_quotes and c == "\\": |
169 | | - i += 2 # skip the escaped character |
170 | | - continue |
171 | | - if c == '"': |
172 | | - in_quotes = not in_quotes |
173 | | - elif c == ";" and not in_quotes: |
174 | | - parts.append(value[start:i]) |
175 | | - start = i + 1 |
176 | | - i += 1 |
177 | | - parts.append(value[start:]) |
178 | | - return parts |
179 | | - |
180 | | - |
181 | 160 | def parse_options_header(value: str | bytes | None) -> tuple[bytes, dict[bytes, bytes]]: |
182 | 161 | """Parses a Content-Type header into a value in the following format: (content_type, {parameters}).""" |
183 | 162 | # Uses email.message.Message to parse the header as described in PEP 594. |
@@ -196,27 +175,19 @@ def parse_options_header(value: str | bytes | None) -> tuple[bytes, dict[bytes, |
196 | 175 | if ";" not in value: |
197 | 176 | return (value.lower().strip().encode("latin-1"), {}) |
198 | 177 |
|
199 | | - ctype_part, params_part = value.split(";", 1) |
200 | | - |
201 | | - # Pre-check for mixed RFC2231 parameter continuations (e.g., `filename*` and `filename*0*`). |
202 | | - # email.message.Message.get_params() handles these maliciously formed headers |
203 | | - # differently in Python 3.12 vs 3.13. We validate them here to ensure consistent behavior. |
204 | | - # _split_mime_parameters is used to avoid false positives from semicolons inside quoted values. |
205 | | - param_names = [p.split("=", 1)[0].strip().lower() for p in _split_mime_parameters(params_part) if "=" in p] |
206 | | - for name in param_names: |
207 | | - if "*" in name: |
208 | | - base, _, rest = name.partition("*") |
209 | | - if rest.rstrip("*").isdigit() and f"{base}*" in param_names: |
210 | | - return (ctype_part.lower().strip().encode("latin-1"), {}) |
211 | | - |
212 | 178 | # Split at the first semicolon, to get our value and then options. |
213 | 179 | # ctype, rest = value.split(b';', 1) |
214 | 180 | message = Message() |
215 | 181 | message["content-type"] = value |
| 182 | + # `get_params()` can raise on malformed RFC 2231 headers found via fuzzing: |
| 183 | + # - ValueError on oversized continuation indices (all supported versions). |
| 184 | + # - TypeError on mixed `filename*` + `filename*0*` continuations (Python 3.12 only; |
| 185 | + # 3.13+ silently picks a value). |
| 186 | + # TODO: drop `TypeError` once Python 3.12 reaches EOL (October 2028). |
216 | 187 | try: |
217 | 188 | params = message.get_params() |
218 | | - except (TypeError, ValueError): |
219 | | - return (ctype_part.lower().strip().encode("latin-1"), {}) |
| 189 | + except (TypeError, ValueError): # pragma: no cover |
| 190 | + return (value.split(";", 1)[0].lower().strip().encode("latin-1"), {}) |
220 | 191 | # If there were no parameters, this would have already returned above |
221 | 192 | assert params, "At least the content type value should be present" |
222 | 193 | ctype = params.pop(0)[0].encode("latin-1") |
|