plistlib leaks ExpatError/LookupError instead of InvalidFileException for malformed XML
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug description:
plistlib.load()/loads() is documented to raise plistlib.InvalidFileException when a file cannot be parsed:
The parser raises
InvalidFileExceptionwhen 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:
- XML that is not well-formed raises a raw
xml.parsers.expat.ExpatError. - An
<?xml ... ?>declaration naming an encoding Python's codec registry doesn't know raises a rawLookupError. 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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at plistlib._PlistParser.parse, which is called by plistlib.load() and loads(), and reproduce both malformed XML examples from the issue. Confirm that malformed XML and unknown encodings are translated to plistlib.InvalidFileException, then verify the existing malformed-input behavior remains intact.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100