Skip to content

email.headerregistry.Address fails to quote usernames with leading dots #158706

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: Address.init docstring: "The addr_spec and the string value of the object are both quoted according to RFC5322 rules, but without any Content Transfer Encoding."

Expected: Address(username='.a', domain='example.com').addr_spec returns '".a"@example.com', which parses with username='.a' and domain='example.com'.

Actual: addr_spec returns '.a@example.com'; parsing it raises InvalidHeaderDefect.

import re
from email.headerregistry import Address

u, d = ".a", "example.com"
try:
    if not re.fullmatch(r"[A-Za-z.]+", u) or d != "example.com":
        raise ValueError("input is outside the documented test case")
    # username is supplied unquoted, not as an addr_spec; dots are legal
    # quoted-string content. Only dot-atoms may remain unquoted here.
    local = u if re.fullmatch(r"[A-Za-z]+(?:\.[A-Za-z]+)*", u) else f'"{u}"'
    expected = (local + "@" + d, u, d)
    a = Address(username=u, domain=d)
    if (a.username, a.domain) != (u, d):
        raise ValueError("constructor did not accept the input unchanged")
    s = a.addr_spec
    try:
        b = Address(addr_spec=s)
        actual = (s, b.username, b.domain)
    except Exception as e:
        actual = (s, "parse error", type(e).__name__, str(e))
    if actual != expected:
        print("REFUTATION CONFIRMED:", (u, d), "actual:", actual, "expected:", expected)
    else:
        print("REFUTATION REJECTED: output is correctly quoted and round-trips")
except Exception as e:
    print("REFUTATION REJECTED:", type(e).__name__, str(e))

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library email.headerregistry:

REFUTATION CONFIRMED: ('.a', 'example.com') actual: ('.a@example.com', 'parse error', 'InvalidHeaderDefect', 'local-part is not dot-atom, quoted-string, or obs-local-part') expected: ('".a"@example.com', '.a', 'example.com')

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/email.headerregistry-20261003-021509-c1

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions