Skip to content

Reject leading and trailing hyphens in last domain label in validate.Email (#3072) - #3109

Closed
ANO3457 wants to merge 1 commit into
marshmallow-code:devfrom
ANO3457:fix/validate-email-domain-hyphens-3072
Closed

ANO3457 wants to merge 1 commit into
marshmallow-code:devfrom
ANO3457:fix/validate-email-domain-hyphens-3072

Conversation

@ANO3457

@ANO3457 ANO3457 commented Nov 9, 2026

Copy link
Copy Markdown

Fixes #3072

Summary

RFC 5321 §4.1.2 disallows leading and trailing hyphens in domain labels. While validate.Email.DOMAIN_REGEX enforces this for preceding domain labels, the final alternative for the last domain label ([A-Z0-9" + _UNICODE_LETTERS + r"-]{2,}) permitted leading or trailing hyphens (such as user@example.-com, user@example.com-, or IDN variants like user@例え.-テスト).

Changes

  • Enforce that the last domain label alternative cannot start or end with a hyphen using negative lookahead and lookbehind: (?!-)[A-Z0-9" + _UNICODE_LETTERS + r"-]{2,}(?<!-).
  • Add regression test cases for both standard and IDN invalid email formats with leading/trailing hyphens in the last domain label.

Test Plan

  • Run pytest tests/test_validate.py -k "test_email" (42 passed)
  • Run full test suite: pytest tests/ (1196 passed)
  • Run linter: ruff check src/marshmallow/validate.py tests/test_validate.py (all passed)
  • Run format check: ruff format --check src/marshmallow/validate.py tests/test_validate.py (clean)

@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

2 participants