Skip to content

Defects when parsing email headers aren't raised or propagated to the email object #106144

Open
@me-and

Description

@me-and

Bug report

When parsing an email using email.parser.Parser(policy=email.policy.strict), where the email has invalid headers, I expected exceptions to be raised when the email was parsed. Instead, the header objects within the generated EmailMessage record defects, but no exception is raised and the overall EmailMessage has no defects listed.

import email
import email.policy

email_str = '''\
Date: xxx
From: "Bad from header" <bad, value>
'''

parsed_email = email.message_from_string(email_str, policy=email.policy.strict)

# Should never get to this point as the above should already have raised an
# exception.

print(parsed_email['Date'].defects)  # Prints "(InvalidDateDefect('Invalid date value or format'),)", expected
print(parsed_email['From'].defects)  # Prints "(InvalidHeaderDefect('invalid address in address-list'), ..." , expected
print(parsed_email.defects)  # Prints "[]", not expected

Your environment

  • Debian 12
  • Raspberry Pi 4 (arm64)
  • Python 3.11.2 (Debian package 3.11.2-1+b1)

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