python / python/cpython

plistlib leaks ExpatError/LookupError instead of InvalidFileException for malformed XML

Aberta
#155,397 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

stdlib type-bug
Linguagem predominante
Python
Estrelas
77.2k
Forks
35.9k
Métricas de merge de PRs
Métricas de PR pendentes

Descrição

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

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Direção de pesquisa

Comece em plistlib._PlistParser.parse, que é chamado por plistlib.load() e loads() e carrega os dois exemplos de XML malformado da issue. Confirme que XML malformado e codificações desconhecidas são convertidos em plistlib.InvalidFileException e, em seguida, verifique se o comportamento existente para entradas malformadas permanece intacto.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
python
Domínio
backend
Tipo de issue
Bug
Dificuldade
2/5
Tempo estimado
1-3 horas
Status de atividade
Estagnada
Clareza
Claramente especificada
Facilidade para iniciantes
25/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.