Skip to content

email: IndexError when folding address headers containing empty comments #159621

Description

@KohakuBlueleaf

Bug report

Minimal reproduce:

from email.parser import Parser
from email import policy

value = "a" * 50 + "@example.com,()b@example.com"
m = Parser(policy=policy.default).parsestr(f"To: {value}\r\n\r\nx")

assert m["To"].defects == ()
m.as_string()  # IndexError

As shown above, when there is an email valu that is over the policy's folding limit it will trigger split and a check about leading folding whitespace by recursively inspect the childs.

def startswith_fws(self):
    return self[0].startswith_fws()

When the comment is empty the token list is empty, so self[0] become index error.

Note there is a historical issue: #71445 that hit similar issue about startswith_fws() reject to working.

This issue have been reproduced in python 3.12, 3.13 and main branch.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

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

    stdlibStandard Library Python modules in the Lib/ directorytopic-emailtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions