Skip to content

gh-151857: Fix IndexError in the email header parser on empty input#151858

Open
tonghuaroot wants to merge 2 commits into
python:mainfrom
tonghuaroot:fix-email-get_parameter-indexerror
Open

gh-151857: Fix IndexError in the email header parser on empty input#151858
tonghuaroot wants to merge 2 commits into
python:mainfrom
tonghuaroot:fix-email-get_parameter-indexerror

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

Fixes two instances of a single bug class in the email header parser: an empty token-list/string is indexed and escapes as a bare IndexError rather than being handled as a parse defect.

  1. get_parameter — a parameter name ending in the extended-parameter marker * with no value leaves value empty before value[0]. Guard: if not value or value[0] != '='.
  2. DisplayName.display_name — a display name consisting only of a comment empties res after res.pop(0), then res[-1] raised. Guard: early-return res.value ('') when res is empty after the pop, mirroring the pre-existing len(res) == 0 guard.

Both now degrade gracefully (a parse defect / an empty display name).

Surveyed both instances and closed the class: a focused fuzz of malformed address and parameter headers — 23.5k structured combinations plus 100k randomized message_from_string parses under email.policy.default — shows no other non-HeaderParseError exception escaping the parser. The empty-string IndexError still produced by directly calling the low-level get_* token parsers is their documented non-empty precondition and is unreachable from public header parsing (every caller checks non-empty first).

Tests: regression tests for both instances at the parser level (test__header_value_parser.py) and via the public header API (test_headerregistry.py). The previous name*; charset=utf-8 case did not actually exercise the regression — it raises HeaderParseError on both patched and unpatched code — and has been replaced with cases that IndexError unpatched (name*0*, x=1; name*).

…nput

Guard two empty-input index escapes in the modern email header parser
that raised a bare IndexError instead of a parse defect: a MIME
parameter name ending with '*', and an address display name that is
only a comment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant