File._get_disk_file: filenames with NUL / non-UTF-8 extension bytes raise ValueError/UnicodeDecodeError instead of FileError
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 85/100
Línea de trabajo
Empieza en python_multipart/multipart.py en torno a los tres sitios en las líneas 550, 560 y 574: lee File._get_disk_file junto con el contrato de excepciones en THREAT_MODEL.md. Ejecuta el reproducer del issue con UPLOAD_KEEP_EXTENSIONS=True para confirmar el ValueError que escapa, luego convierte ValueError/UnicodeDecodeError en esos sitios en FileError y añade tests para un nombre de archivo con NUL y una extensión no UTF-8. Hecho significa que los nombres malformados lanzan FileError mientras que los nombres de archivo benignos siguen parseándose.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Hi! Filing this as a public bug per THREAT_MODEL.md — "What does not count" → "Report these as public issues if you think they are bugs." So: exception-contract bug on malformed input, not a vulnerability claim.
What happens
File._get_disk_file guards its sinks with except OSError (multipart.py:551, :575), and computes the tempfile suffix with a decode that sits outside any try (multipart.py:560). But the underlying calls can raise non-OSError exceptions on attacker-controlled filename= bytes:
| Site | Call | Attacker input that raises |
|---|---|---|
multipart.py:550 |
open(path, "w+b") |
ValueError: embedded null byte (filename contains NUL) |
multipart.py:560 |
self._ext.decode(sys.getfilesystemencoding()) |
UnicodeDecodeError (extension bytes not valid UTF-8) |
multipart.py:574 |
tempfile.NamedTemporaryFile(suffix=suffix, ...) |
ValueError: open: embedded null character in path (NUL in extension) |
ValueError/UnicodeDecodeError are not OSError, so the documented contract — "Malformed input raises FormParserError or one of its subclasses" (Security goals in THREAT_MODEL.md) — is bypassed and the raw exception escapes FormParser/parse_form into the calling application.
Reachability: UPLOAD_KEEP_FILENAME=True (with UPLOAD_DIR) or UPLOAD_KEEP_EXTENSIONS=True, plus a part larger than MAX_MEMORY_FILE_SIZE (default 1 MiB) so flush_to_disk runs. The filename bytes survive parse_options_header (latin-1 roundtrip) and os.path.basename/os.path.splitext untouched, including NUL and \xff.
Reproducer
"""python-multipart 0.0.34 reproducer: NUL in filename= escapes the FileError contract."""
import sys
from python_multipart.exceptions import FileError
from python_multipart.multipart import FormParser
body = (
b"--bnd\r\n"
b'Content-Disposition: form-data; name="f"; filename="a.tx\x00t"\r\n'
b"Content-Type: application/octet-stream\r\n\r\n"
+ b"A" * (1024 * 1024 + 16) # > MAX_MEMORY_FILE_SIZE (1 MiB default) forces flush_to_disk
+ b"\r\n--bnd--\r\n"
)
parser = FormParser("multipart/form-data", lambda f: None, lambda f: None,
boundary="bnd", config={"UPLOAD_KEEP_EXTENSIONS": True})
try:
parser.write(body)
parser.finalize()
except FileError as exc:
print("contract kept ->", type(exc).__name__, ":", exc); sys.exit(1)
except Exception as exc:
print(f"BUG: {type(exc).__name__} escaped the FileError contract: {exc}"); sys.exit(0)
print("clean parse (no disk write?)"); sys.exit(1)
$ python3 -m venv v && ./v/bin/pip install python-multipart==0.0.34 && ./v/bin/python repro.py
BUG: ValueError escaped the FileError contract: open: embedded null character in path
Verified against the 0.0.34 PyPI wheel and the 0.0.34 tag source, on Python 3.14 (host) and 3.12 (python:3.12-slim container). The same three escape variants fire with UPLOAD_KEEP_FILENAME=True + UPLOAD_DIR (open() path).
Control: the same NUL filename with MAX_MEMORY_FILE_SIZE=float("inf") (no flush → no disk write) parses cleanly — so it is specifically the disk-write path that converts the NUL byte into an escaping exception.
Suggested fix
Catch the concrete non-OSError failure modes at all three sites and convert them to FileError, e.g.:
tmp_file = open(path, "w+b")
- except OSError:
+ except (OSError, ValueError):
and in the else branch, move the suffix decode inside the existing try:
- suffix = self._ext.decode(sys.getfilesystemencoding()) if keep_extensions else None
...
try:
+ suffix = self._ext.decode(sys.getfilesystemencoding()) if keep_extensions else None
tmp_file = cast(BufferedRandom, tempfile.NamedTemporaryFile(suffix=suffix, delete=delete_tmp, dir=dir))
- except OSError:
+ except (OSError, ValueError, UnicodeDecodeError):
With that change the reproducer flips to contract kept -> FileError : Error creating named temporary file, and a benign filename keeps parsing cleanly (verified on a patched build). Happy to open a PR if you'd like — the patch is 6 insertions / 7 deletions against 0.0.34.
- Lenguaje dominante
- Python
- Estrellas
- 539
- Forks
- 109
- Merge medio
- 6 h 35 min
- PR fusionados (30 d)
- 20
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de Kludex/python-multipart
-
Clarify or handle non-Latin-1 str input in parse_options_headerQuizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 42/100
Kludex/python-multipart#319 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Skip header area?Posiblemente ocupada @Kludex la tomó hace 225 días. Abierto
Dificultad 3/5 1-2 días Aptitud para principiantes 48/100
Kludex/python-multipart#59 · 3 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de Kludex/python-multipart
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
Los mantenedores suelen responder en 3 días
-
enhancement
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
Los mantenedores suelen responder en 11 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 80/100
FuRongJun-1999/dsh-memory#1000 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
gcivil-nyu-org/team5-mon-fall26#154 ·
Los mantenedores suelen responder en 1 día
-
docs: reorder interface definition before fetch-libs and link PostgreSQL/library docs in tutorialAbierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 90/100
Los mantenedores suelen responder en 3 días