python / python/cpython

plistlib leaks ExpatError/LookupError instead of InvalidFileException for malformed XML

Aperta
#155,397 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia da plistlib._PlistParser.parse, che viene chiamato da plistlib.load() e loads() e carica entrambi gli esempi di XML malformato dell’issue. Verifica che l’XML malformato e le codifiche sconosciute vengano convertiti in plistlib.InvalidFileException, quindi verifica che il comportamento esistente con input malformati rimanga intatto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
backend
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.