Skip to content

UnboundLocalError in email._header_value_parser.parse_message_id #134152

Closed
@gabe-sherman

Description

@gabe-sherman

During fuzzing of Python standard libraries, the following code snippet causes an UnboundLocalError with the following message: UnboundLocalError: cannot access local variable 'pos' where it is not associated with a value'. This occurs in the _get_ptext_to_endchars function at line 1035 in email/_header_value_parser.py.

import email._header_value_parser
email._header_value_parser.parse_message_id("<T@[")

Exception Trace

Traceback (most recent call last):
  File "/usr/lib/python3.12/email/_header_value_parser.py", line 2118, in get_msg_id
    token, value = get_dot_atom_text(value)
                   ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/email/_header_value_parser.py", line 1344, in get_dot_atom_text
    raise errors.HeaderParseError("expected atom at a start of "
email.errors.HeaderParseError: expected atom at a start of dot-atom-text but found '['

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "rep.py", line 2, in <module>
    email._header_value_parser.parse_message_id("<T@[")
  File "/usr/lib/python3.12/email/_header_value_parser.py", line 2149, in parse_message_id
    token, value = get_msg_id(value)
                   ^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/email/_header_value_parser.py", line 2121, in get_msg_id
    token, value = get_no_fold_literal(value)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/email/_header_value_parser.py", line 2066, in get_no_fold_literal
    token, value = get_dtext(value)
                   ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/email/_header_value_parser.py", line 1557, in get_dtext
    ptext, value, had_qp = _get_ptext_to_endchars(value, '[]')
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/email/_header_value_parser.py", line 1033, in _get_ptext_to_endchars
    pos = pos + 1
          ^^^
UnboundLocalError: cannot access local variable 'pos' where it is not associated with a value

CPython versions tested on:

3.12, 3.11, 3.10, 3.9

Operating systems tested on:

Linux

Linked PRs

Activity

changed the title [-]'UnboundLocalError' in `parse_message_id`[/-] [+]'UnboundLocalError' in `email._header_value_parser.parse_message_id`[/+] on May 17, 2025
ZeroIntensity

ZeroIntensity commented on May 18, 2025

@ZeroIntensity
Member

Do we want to fix private APIs?

picnixz

picnixz commented on May 18, 2025

@picnixz
Member

In general no, but UnboundLocalError is pretty much a real issue, even in a private API, so I think we should fix this one at least. We don't need to fix the HeaderParseError, but we need to fix the UnboundLocalError.

sergey-miryanov

sergey-miryanov commented on May 18, 2025

@sergey-miryanov
Contributor

I have a patch, want to send it in a couple of hours.

sergey-miryanov

sergey-miryanov commented on May 18, 2025

@sergey-miryanov
Contributor

@picnixz Please take a look.

changed the title [-]'UnboundLocalError' in `email._header_value_parser.parse_message_id`[/-] [+]`UnboundLocalError` in `email._header_value_parser.parse_message_id`[/+] on May 18, 2025
added a commit that references this issue on May 19, 2025

Merge branch 'pythongh-134152-fix-unbound-local-error-in-email' of gi…

added a commit that references this issue on May 25, 2025

gh-134152: Fix UnboundLocalError in email._header_value_parser _get_p…

a32ea45
added a commit that references this issue on May 25, 2025

pythongh-134152: Fix UnboundLocalError in email._header_value_parser …

12 remaining items

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython 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

      `UnboundLocalError` in `email._header_value_parser.parse_message_id` · Issue #134152 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant