Skip to content

ValueError: embedded null character in email.message_from_file #134156

Closed as not planned
@gabe-sherman

Description

@gabe-sherman

Bug report

Bug description:

During fuzzing of Python standard libraries, the following code snippet causes a ValueError with the following message: ValueError: embedded null character. This occurs in the collapse_rfc2231_value function at line 455 in email/utils.py.

import email
import sys
import io
d = io.StringIO(open(sys.argv[1], "r").read())
message_from_fileval1 = email.message_from_file(d)

POC File

https://github.com/FuturesLab/POC/blob/main/py-email/poc-03

Exception Trace

Traceback (most recent call last):
  File "rep.py", line 5, in <module>
    message_from_fileval1 = email.message_from_file(d)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/email/__init__.py", line 53, in message_from_file
    return Parser(*args, **kws).parse(fp)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/email/parser.py", line 54, in parse
    return feedparser.close()
           ^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/email/feedparser.py", line 185, in close
    self._call_parse()
  File "/usr/lib/python3.12/email/feedparser.py", line 178, in _call_parse
    self._parse()
  File "/usr/lib/python3.12/email/feedparser.py", line 304, in _parsegen
    boundary = self._cur.get_boundary()
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/email/message.py", line 865, in get_boundary
    return utils.collapse_rfc2231_value(boundary).rstrip()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/email/utils.py", line 455, in collapse_rfc2231_value
    return str(rawbytes, charset, errors)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: embedded null character

CPython versions tested on:

3.12, 3.11, 3.10, 3.9

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedstdlibPython modules in the Lib dirtopic-emailtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions