Skip to content

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

Open

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

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