Skip to content

Email Address Validation Fails Prominent Edge-Case Example #111223

Open
@jpl-jengelke

Description

@jpl-jengelke

Bug report

Bug description:

The Address object from email.headerregistry seems to fail validation on a prominent edge-case email address. (Example presented in Wikipedia and at Stack Overflow.) If it fails this prominent edge case it perhaps fails others that are untested.

Here is the fail case address:

"unusual" <"very.():;,<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com>

Here is the example of how it fails:

<some_machine_id>:~ workstation_user$ python3 --version
Python 3.11.4
<some_machine_id>:~ workstation_user$ python3
Python 3.11.4 (v3.11.4:d2340ef257, Jun  6 2023, 19:15:51) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from email.headerregistry import Address
>>> from email.utils import getaddresses
>>> a='"very.(),:;<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com;"unusual" <"very.():;,<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com>'
>>> getaddresses([a])
[('', '"very.(),:;<>[]".VERY."very@ "very".unusual"@strange.example.com'), ('', ''), ('unusual', '"very.():;,<>[]".VERY."very@ "very".unusual"@strange.example.com')]
>>> Address(display_name='', addr_spec='"very.(),:;<>[]".VERY."very@ "very".unusual"@strange.example.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/email/headerregistry.py", line 49, in __init__
    raise a_s.all_defects[0]
email.errors.InvalidHeaderDefect: local-part is not dot-atom, quoted-string, or obs-local-part
>>> Address(display_name='unusual',addr_spec='"very.():;,<>[]".VERY."very@ "very".unusual"@strange.example.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/email/headerregistry.py", line 49, in __init__
    raise a_s.all_defects[0]
email.errors.InvalidHeaderDefect: local-part is not dot-atom, quoted-string, or obs-local-part
>>> Address(display_name='unusual', addr_spec='very.():;,<>[]\".VERY.\"very@\\ \"very\".unusual"@strange.example.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/email/headerregistry.py", line 45, in __init__
    raise ValueError("Invalid addr_spec; only '{}' "
ValueError: Invalid addr_spec; only 'very.()' could be parsed from 'very.():;,<>[]".VERY."very@\ "very".unusual"@strange.example.com'
>>> 

There are some things to note here:

  1. Method getaddresses() seems to work to decompose the display name from the address spec.
    1. For some reason, however, the result of this call displays an empty tuple when it shouldn't. (This is easy to ignore in code, but that probably shouldn't be a concern of a developer using a built-in Python API.)
  2. Matching/decomposition fails on the edge-case address.
    1. It fails on both the unescaped output from getaddresses() and the original escaped address.
    2. It has trouble figuring out the local-part of the address, and fails on that.
    3. Different slight escaping variations have been tried but it still seems to fail on the local-part.

This occurs in Python 11, but I suspect it occurs in earlier versions of Python, also. I suppose this could be an issue with escaping the actual unusual address, but it seems not.

Unclear how much this will come up in practice, but I suspect if it fails on this edge-case address that it may fail on some less edge-case addresses, also. (NOTE: Please let me know if I have missed something glaring or somehow misconstrued the example and/or call to Address(). Thanks.)

CPython versions tested on:

3.11

Operating systems tested on:

macOS

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