Skip to content

UnboundLocalError in email._header_value_parser.parse_message_id #134152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
gabe-sherman opened this issue May 17, 2025 · 6 comments
Closed

UnboundLocalError in email._header_value_parser.parse_message_id #134152

gabe-sherman opened this issue May 17, 2025 · 6 comments
Labels
stdlib Python modules in the Lib dir topic-email type-bug An unexpected behavior, bug, or error

Comments

@gabe-sherman
Copy link

gabe-sherman commented May 17, 2025

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

@gabe-sherman gabe-sherman added the type-bug An unexpected behavior, bug, or error label May 17, 2025
@picnixz picnixz added stdlib Python modules in the Lib dir topic-email labels May 17, 2025
@picnixz picnixz changed the title 'UnboundLocalError' in parse_message_id 'UnboundLocalError' in email._header_value_parser.parse_message_id May 17, 2025
@ZeroIntensity
Copy link
Member

Do we want to fix private APIs?

@picnixz
Copy link
Member

picnixz commented May 18, 2025

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
Copy link
Contributor

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

@sergey-miryanov
Copy link
Contributor

@picnixz Please take a look.

@picnixz picnixz changed the title 'UnboundLocalError' in email._header_value_parser.parse_message_id UnboundLocalError in email._header_value_parser.parse_message_id May 18, 2025
sergey-miryanov added a commit to sergey-miryanov/cpython that referenced this issue May 19, 2025
…thub.com:sergey-miryanov/cpython into pythongh-134152-fix-unbound-local-error-in-email
bitdancer added a commit that referenced this issue May 25, 2025
…text_to_endchars (#134233)

Fix an UnboundLocalError that can occur when parsing certain delimited constructs in headers (domain literals, quoted strings, comments). After the fix the _get_ptext_to_endchars returns an empty string if there is no content after the opening delimiter. The calling code is responsible for handling the lack of the trailing delimiter, which it already does; this edge case was the header ending immediately after the opening delimiter.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 25, 2025
…_get_ptext_to_endchars (pythonGH-134233)

Fix an UnboundLocalError that can occur when parsing certain delimited constructs in headers (domain literals, quoted strings, comments). After the fix the _get_ptext_to_endchars returns an empty string if there is no content after the opening delimiter. The calling code is responsible for handling the lack of the trailing delimiter, which it already does; this edge case was the header ending immediately after the opening delimiter.
(cherry picked from commit a32ea45)

Co-authored-by: R. David Murray <rdmurray@bitdance.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 25, 2025
…_get_ptext_to_endchars (pythonGH-134233)

Fix an UnboundLocalError that can occur when parsing certain delimited constructs in headers (domain literals, quoted strings, comments). After the fix the _get_ptext_to_endchars returns an empty string if there is no content after the opening delimiter. The calling code is responsible for handling the lack of the trailing delimiter, which it already does; this edge case was the header ending immediately after the opening delimiter.
(cherry picked from commit a32ea45)

Co-authored-by: R. David Murray <rdmurray@bitdance.com>
hugovk pushed a commit that referenced this issue May 26, 2025
… _get_ptext_to_endchars (GH-134233) (#134678)

Co-authored-by: R. David Murray <rdmurray@bitdance.com>
hugovk pushed a commit that referenced this issue May 26, 2025
… _get_ptext_to_endchars (GH-134233) (#134677)

Co-authored-by: R. David Murray <rdmurray@bitdance.com>
hugovk added a commit to sergey-miryanov/cpython that referenced this issue May 26, 2025
@picnixz picnixz closed this as completed May 26, 2025
@picnixz picnixz reopened this May 26, 2025
@picnixz
Copy link
Member

picnixz commented May 26, 2025

Is it actually entirely fixed by #134233?

@bitdancer
Copy link
Member

The issue reported here is. In the course of testing sergey-miryanov also noticed a related but separate bug, which was already separately reported in #134155, which is fixed by his PR that will land soonish.

sergey-miryanov added a commit to sergey-miryanov/cpython that referenced this issue May 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-email type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

5 participants