Skip to content

fix(email): accept exactly one pair of brackets around address literals - #586

Closed
yuchengshen931122-creator wants to merge 1 commit into
python-validators:masterfrom
yuchengshen931122-creator:g33/fix-email-address-literal-brackets
Closed

yuchengshen931122-creator wants to merge 1 commit into
python-validators:masterfrom
yuchengshen931122-creator:g33/fix-email-address-literal-brackets

Conversation

@yuchengshen931122-creator

Copy link
Copy Markdown

Fixes #583.

What

email unwrapped address literals with domain_part.lstrip("[").rstrip("]"), which removes any number of brackets, so user@[[192.168.0.1]] and the unbalanced user@[[192.168.0.1] were valid with ipv4_address=True. RFC 5321 §4.1.3 allows exactly one pair. The literal is now unwrapped with domain_part[1:-1]; anything left over makes the IP check fail.

Tests

  • New: test_rejects_extra_brackets_around_address_literal (4 cases, IPv4 and IPv6) – fails on master, passes with this change; it also checks that a single pair is still accepted.
  • Checked locally with the CI commands: uv run pytest . (1,578 passed), ruff format ., ruff check ., pyright . (0 errors).

Found while writing specification-based tests for a university software-testing project (NYCU). The change and this description were prepared with the help of an AI assistant (Claude Code).

🤖 Generated with Claude Code

`lstrip("[")`/`rstrip("]")` removed any number of brackets, so
`user@[[192.168.0.1]]` and even `user@[[192.168.0.1]` were valid with
`ipv4_address=True`. RFC 5321 4.1.3 allows exactly one pair.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yuchengshen931122-creator

Copy link
Copy Markdown
Author

Closing this in favour of #535, which is older, covers hostname() as well, and handles a case this pull request gets wrong.

With domain_part[1:-1], a zone identifier that consists of a bracket slips through:

>>> email('user@[2001:db8::1%]]', ipv6_address=True)   # master and #535: ValidationError; this branch: True

The inner text becomes 2001:db8::1%], which the host check accepts. So the sentence in #583 that the inner value "then fails the IP check because it still contains a bracket" is not true for this input.

Sorry for the duplicate. (Checked by running master, #535 and this branch; this text was prepared with the help of an AI assistant.)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

email: address literals with extra brackets are accepted (user@[[192.168.0.1]])

1 participant