Repository navigation
Description
Overview Description
Catalog._set_mime_headers() parses Content-Type and Plural-Forms with parse_separated_header(), which returns dict(email.message.Message().get_params()). For an RFC 2231 extended parameter such as charset*=utf-8''utf-8, get_params() returns the value as a (charset, language, value) tuple instead of a string:
>>> from babel.messages.catalog import parse_separated_header
>>> parse_separated_header("text/plain; charset*=utf-8''utf-8")
{'text/plain': '', 'charset': ('utf-8', '', 'utf-8')}The callers expect strings, so read_po() either crashes or stores a corrupted value.
Steps to Reproduce
import io
from babel.messages.pofile import read_po
po = b'''msgid ""
msgstr ""
"Content-Type: text/plain; charset*=utf-8''utf-8\\n"
'''
read_po(io.BytesIO(po))Actual Results
Traceback (most recent call last):
File "babel_rfc2231.py", line 9, in <module>
read_po(io.BytesIO(po))
File ".../babel/messages/pofile.py", line 438, in read_po
parser.parse(fileobj)
File ".../babel/messages/pofile.py", line 365, in parse
self._finish_current_message()
File ".../babel/messages/pofile.py", line 238, in _finish_current_message
self._add_message()
File ".../babel/messages/pofile.py", line 225, in _add_message
self.catalog[msgid] = message
File ".../babel/messages/catalog.py", line 798, in __setitem__
self.mime_headers = message_from_string(message.string).items()
File ".../babel/messages/catalog.py", line 675, in mime_headers
self._set_mime_headers(value)
File ".../babel/messages/catalog.py", line 602, in _set_mime_headers
charset = params.get('charset', '').lower()
AttributeError: 'tuple' object has no attribute 'lower'The same tuple reaches the Plural-Forms branch:
Plural-Forms: nplurals*=2; plural=(n != 1);givesTypeError: int() argument must be a string, a bytes-like object or a real number, not 'tuple'(catalog.py line 608).Plural-Forms: nplurals=2; plural*=''%28n%20%21%3D%201%29;(that is,plural=(n != 1)) does not raise, but the tuple is stored as the plural expression:catalog.plural_formsis"nplurals=2; plural=('', '', '(n != 1)');", andwrite_po()writes that back out.
Expected Results
Either decode the extended parameter (email.utils.collapse_rfc2231_value(), so charset*=utf-8''utf-8 gives "utf-8"), or treat the header as invalid through the parser's normal path (a warning, or PoFileError with abort_invalid=True), as for other malformed input. It should not raise AttributeError/TypeError or store a tuple repr.
Reproducibility
Always. Reproduced on babel 2.19.0 and on master (3a7d239), Python 3.13.7, Linux.
Additional Information
- Possible fix: applying
email.utils.collapse_rfc2231_value()to each value inparse_separated_header()fixes all of these cases. - Disclosure: found by fuzzing
read_powith Atheris harnesses from an automatic harness generator.
Activity