Skip to content

gh-155397: Raise InvalidFileException for malformed XML plists in plistlib - #155398

Open
simitana wants to merge 1 commit into
python:mainfrom
simitana:fix-plistlib-invalidfileexception
Open

gh-155397: Raise InvalidFileException for malformed XML plists in plistlib#155398
simitana wants to merge 1 commit into
python:mainfrom
simitana:fix-plistlib-invalidfileexception

Conversation

@simitana

@simitana simitana commented Aug 8, 2026

Copy link
Copy Markdown

_PlistParser.parse() called expat's ParseFile() with no exception translation, so two classes of malformed XML plist escaped as the underlying exception instead of the documented InvalidFileException:

Neither exception type is a ValueError, so code written against the documented contract (catching InvalidFileException, or even just ValueError) did not catch them.

The fix wraps ParseFile() and translates both into InvalidFileException, the same way _BinaryPlistParser.parse() already does for its own format.

Fixes gh-155397.

…in plistlib

_PlistParser.parse() called expat's ParseFile() with no exception
translation, so two classes of malformed XML plist escaped as the
underlying exception instead of the documented InvalidFileException:

* XML that is not well-formed raised xml.parsers.expat.ExpatError.
* An <?xml ...?> declaration naming an encoding unknown to Python's
  codec registry raised LookupError (this is what CIFuzz found in
  pythongh-152211).

Neither exception type is a ValueError, so code written against the
documented contract (catching InvalidFileException, or even just
ValueError) did not catch them.
@python-cla-bot

python-cla-bot Bot commented Aug 8, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

Comment thread Lib/plistlib.py
Comment on lines +188 to +196
try:
self.parser.ParseFile(fileobj)
except (ExpatError, LookupError):
# gh-155397: ExpatError is raised for XML that is not
# well-formed, and LookupError for a <?xml ... ?> declaration
# naming an unknown encoding; neither is a ValueError, so it
# would otherwise escape uncaught instead of the documented
# InvalidFileException.
raise InvalidFileException()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't propagate the error messages so it won't be helpful. There are other places in plistlib that raises unexpected exceptions and we still don't know what to do with it. I would suggest we first discuss that.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now:

  • remove the long comment, it doesn't indicate anything
  • appropriately create an exception message for that
  • test it

And also I would wait for the decision on the ci-fuzz issue first, though I think we should indeed raise InvalidFileException here as documented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

plistlib leaks ExpatError/LookupError instead of InvalidFileException for malformed XML

2 participants