Skip to content

Disallow leading and trailing hyphens in last domain label for Email validator (fixes #3072) - #3108

Closed
X6M-Work wants to merge 2 commits into
marshmallow-code:devfrom
X6M-Work:fix/email-domain-hyphen-3072
Closed

X6M-Work wants to merge 2 commits into
marshmallow-code:devfrom
X6M-Work:fix/email-domain-hyphen-3072

Conversation

@X6M-Work

@X6M-Work X6M-Work commented Nov 7, 2026

Copy link
Copy Markdown

Summary

Closes #3072.

validate.Email previously allowed domain names with leading or trailing hyphens in the final domain label / TLD alternative (e.g. user@example.-com, user@example.com-, user@??.-???). Per RFC 5321 �4.1.2, domain labels must not start or end with a hyphen.

This PR:

  • Wraps the final domain label alternative in Email.DOMAIN_REGEX with (?!-) at the start and (?<!-) at the end, ensuring that the label can neither start nor end with a hyphen.
  • Adds test cases for leading/trailing hyphens on standard domains and IDNs to test_email_invalid and test_email_idn_invalid in tests/test_validate.py.

Verification

  • Ran full test suite: pytest (1,198 passed in 1.75s).

@1AbdullahButt 1AbdullahButt left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked the new pattern against the old one on a batch of domains (copied the two DOMAIN_REGEX variants into a small script):

domain main this PR
example.-com, example.com- accepted rejected
例え.-テスト, 例え.テスト- accepted rejected
example.--, example.1- accepted rejected
example.xn--p1ai, example.co-m accepted accepted
example.com, sub.example.museum accepted accepted

So the lookarounds only touch the last label, and punycode TLDs still pass.

A few small things:

  • user@ex-.com and user@-ex.com (tests/test_validate.py) already fail on main, since the earlier labels are anchored with [A-Z0-9…] at both ends. They're fine to keep as coverage, but they aren't regressions for #3072.
  • Possibly out of scope: an all-digit last label like user@example.123 is still accepted through the second alternative. It might be worth a follow-up issue if you want the TLD rules tightened further.
  • Recent fixes add a line to CHANGELOG.rst under the unreleased section ("Thanks :user:... for the PR"). That could be added here too.

Reviewed with help from Claude Code.

@X6M-Work

X6M-Work commented Nov 8, 2026

Copy link
Copy Markdown
Author

Thanks for checking this against main. I added the changelog entry, under a new 4.4.1 (unreleased) section, and the AUTHORS.rst line in 2761740.

  • You're right that user@ex-.com and user@-ex.com already fail on main. I'm keeping them only as extra coverage.
  • I'm leaving the all-digit last label (user@example.123) out of this PR, since it would change what the second alternative accepts. It's a good candidate for a separate issue.

@deckar01

Copy link
Copy Markdown
Member

dupe, bot

@deckar01 deckar01 closed this Nov 17, 2026
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.

validate.Email accepts a leading/trailing hyphen in the last domain label

3 participants