Skip to content

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

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

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

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

Fixes three cases where the email header parser lets a bare IndexError escape on malformed input instead of recording a defect:

  1. get_parameter: a parameter name ending in * with no value leaves value empty before value[0].
  2. DisplayName.display_name: a display name that is only a comment empties res after res.pop(0), then res[-1] raises.
  3. Value.stripped_value: an empty quoted RFC 2231 extended value (name*="'", name*="us-ascii''") builds an empty Value, then self[0] raises.

Each now degrades to the existing recovery path. Calling the low-level get_* token parsers directly with an empty string still raises, but every caller checks for that first.

Tests cover each case at the parser level (test__header_value_parser.py) and through the header API (test_headerregistry.py).

…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.
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Sep 23, 2026
…arser

Value.stripped_value indexed an empty Value built by get_parameter's
quoted extended-value recovery, so name*="'" raised IndexError.
Return early for an empty value.
@tonghuaroot

Copy link
Copy Markdown
Contributor Author

the description said the fuzz found no other exception escaping the parser. that was wrong, it missed a third case of the same bug. an empty quoted rfc 2231 extended value, for example name*="'" or name*="us-ascii''", builds an empty Value in get_parameter, and Value.stripped_value then raises IndexError from message_from_string().

the new commit makes stripped_value return early for an empty value, so the parameter gets an empty value plus the existing InvalidHeaderDefect. it adds tests for both layers, and i updated the news entry, the description and the issue to cover the three cases.

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

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant