Skip to content

gh-155397: Raise InvalidFileException for malformed XML plists - #156177

Open
jaideeppyne wants to merge 1 commit into
python:mainfrom
jaideeppyne:gh-155397-plistlib-invalidfile
Open

gh-155397: Raise InvalidFileException for malformed XML plists#156177
jaideeppyne wants to merge 1 commit into
python:mainfrom
jaideeppyne:gh-155397-plistlib-invalidfile

Conversation

@jaideeppyne

@jaideeppyne jaideeppyne commented Aug 21, 2026

Copy link
Copy Markdown

plistlib.load()/loads() is documented to raise plistlib.InvalidFileException when a file cannot be parsed, but _PlistParser.parse() called expat's ParseFile() with no exception translation. Two classes of malformed XML plist leaked the underlying exception instead:

  • 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.

Neither is a subclass of ValueError (InvalidFileException's base), so callers following the documented contract did not catch them. This translates both into InvalidFileException and adds regression tests.

Fixes #155397.

plistlib.load()/loads() is documented to raise InvalidFileException when
a file cannot be parsed, but _PlistParser.parse() called expat's
ParseFile() with no exception translation. Two classes of malformed XML
plist leaked the underlying exception instead:

* 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.

Neither is a subclass of ValueError (InvalidFileException's base), so
callers following the documented contract did not catch them. Translate
both into InvalidFileException and add regression tests.
@python-cla-bot

python-cla-bot Bot commented Aug 21, 2026

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f8a455f9a4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Lib/plistlib.py
Comment on lines +192 to +195
except LookupError as e:
# An <?xml ... ?> declaration naming an encoding that Python's
# codec registry does not know raises LookupError from expat.
raise InvalidFileException(str(e)) from e

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Restrict translation to the codec lookup failure

When a caller supplies a custom dict_type whose constructor or __setitem__ raises KeyError or IndexError, the exception propagates through ParseFile() and is converted here because both exceptions subclass LookupError; a custom file object's read() can be affected similarly. Consequently, even valid XML now raises a misleading InvalidFileException and hides an error originating in caller-provided code, unlike the binary parser and the behavior before this change. Only the plain codec-registry LookupError produced for an unknown declared encoding should be translated.

Useful? React with 👍 / 👎.

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

1 participant