python / python/cpython

plistlib leaks ExpatError/LookupError instead of InvalidFileException for malformed XML

Abierto
#155,397 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib type-bug
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Bug description:

plistlib.load()/loads() is documented to raise plistlib.InvalidFileException when a file cannot be parsed:

The parser raises InvalidFileException when the file cannot be parsed.

For the XML format, _PlistParser.parse() calls expat's ParserCreate().ParseFile() with no exception translation at all. Two classes of malformed input escape as the underlying exception instead, and neither is a ValueError (InvalidFileException's base class), so code that follows the documented contract and catches InvalidFileException (or even just ValueError) does not catch these:

  1. XML that is not well-formed raises a raw xml.parsers.expat.ExpatError.
  2. An <?xml ... ?> declaration naming an encoding Python's codec registry doesn't know raises a raw LookupError. This is what CIFuzz found in gh-152211 (LookupError: unknown encoding: Latin-5_________________________); the well-formedness case above is a second, broader instance of the same missing-translation bug.
>>> import plistlib
>>> plistlib.loads(b"<plist><foo></bar></plist>")
Traceback (most recent call last):
  ...
xml.parsers.expat.ExpatError: mismatched tag: line 1, column 14
>>> plistlib.loads(b'<?xml version="1.0" encoding="BogusEncoding"?><plist></plist>')
Traceback (most recent call last):
  ...
LookupError: unknown encoding: BogusEncoding

Expected in both cases: plistlib.InvalidFileException, as already happens for other malformed inputs (e.g. plistlib.loads(b"not a plist") correctly raises InvalidFileException).

Related: gh-152211 (the CIFuzz report for the LookupError case specifically; discussion there is about the fuzz-target/CI setup, not a fix to plistlib itself).

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-155398
  • gh-156177

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza en plistlib._PlistParser.parse, que es llamado por plistlib.load() y loads() y carga los dos ejemplos de XML malformado del issue. Confirma que el XML malformado y las codificaciones desconocidas se traducen a plistlib.InvalidFileException y, a continuación, verifica que el comportamiento existente ante entradas malformadas se mantiene intacto.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
backend
Tipo de issue
Error
Dificultad
2/5
Tiempo estimado
1-3 horas
Estado de actividad
Estancado
Claridad
Bien especificado
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.