Skip to content

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

Not planned

Description

Summary

With ipv4_address=True or ipv6_address=True, email unwraps the domain part with domain_part.lstrip("[").rstrip("]"). lstrip/rstrip remove every leading [ and trailing ], so any number of brackets is accepted on either side, even unbalanced ones.

Reproduction (master at 22786cc, Python 3.13)

>>> from validators import email
>>> email("user@[[192.168.0.1]]", ipv4_address=True)
True
>>> email("user@[[192.168.0.1]", ipv4_address=True)       # unbalanced
True
>>> email("user@[[[2001:db8::1]]]", ipv6_address=True)
True

Expected

RFC 5321 §4.1.3: address-literal = "[" ( IPv4-address-literal / IPv6-address-literal / General-address-literal ) "]" – exactly one pair of brackets. All three values above are invalid.

Proposed fix

Remove exactly one bracket on each side (domain_part[1:-1]); the inner value then fails the IP check because it still contains a bracket. A PR follows.


Found while writing specification-based tests for a university software-testing project (NYCU). The investigation and this write-up were done with the help of an AI assistant (Claude Code); the reproduction was run on the current master.

Activity

  1. yuchengshen931122-creator commented on Nov 24, 2026

    @yuchengshen931122-creator
    Author

    Closing: #535 (opened on 22 October) already fixes this, for email() and for hostname().

    One correction to the text above. The suggested fix says that after removing one bracket on each side "the inner value then fails the IP check because it still contains a bracket". That is not true for user@[2001:db8::1%]]: the inner value is 2001:db8::1%], which is accepted as an address with the zone identifier ]. #535 checks for this explicitly; our pull request #586 did not and is closed.

    Sorry for the duplicate. (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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions