Skip to content

Reject IDN labels that start or end with a hyphen - #593

Open
yuchengshen931122-creator wants to merge 1 commit into
python-validators:masterfrom
yuchengshen931122-creator:reject-idn-edge-hyphens
Open

yuchengshen931122-creator wants to merge 1 commit into
python-validators:masterfrom
yuchengshen931122-creator:reject-idn-edge-hyphens

Conversation

@yuchengshen931122-creator

Copy link
Copy Markdown

domain() rejects -bucher.de and bucher-.de, but accepts the same names once the label holds a non-ASCII character: domain("-bücher.de") and domain("bücher-.de") are both True. The hyphen rule is applied to the encoded name, where such a label has become xn---bcher-4ya and starts with x.

RFC 5891, section 4.2.3.1 does not allow a label to start or end with a hyphen. The labels are now checked before they are encoded. hostname(), email() and url() follow through domain().

Verdicts change only for labels that contain a non-ASCII character and have a hyphen at either end: a comparison with master on 60,000 generated names (ASCII and non-ASCII, all flag combinations) shows no other difference. Hyphens inside a label and A-labels such as xn--bcher-kva.de stay valid.

The added tests (19 cases) include 13 that fail on master. The full suite including doctests passes on Python 3.13 (1,593 passed); Ruff lint and format, Pyright and Bandit report nothing. Other Python versions are left to CI. The branch merges cleanly with every open pull request that touches domain.py.

Found while writing specification-based tests for a university software-testing course project (NYCU). Prepared with AI assistance; reproduced and tested locally.

🤖 Generated with Claude Code

domain() rejects "-bucher.de" and "bucher-.de", but accepted the same names
once the label holds a non-ASCII character:

    domain("-bücher.de")   # True
    domain("bücher-.de")   # True

The hyphen rule is applied to the encoded name, where such a label has become
"xn---bcher-4ya" and starts with "x". RFC 5891 (4.2.3.1) does not allow a
label to start or end with a hyphen, so the labels are now checked before
they are encoded.

Verdicts change only for labels that contain a non-ASCII character and have a
hyphen at either end (checked against master on 60,000 generated names);
hostname(), email() and url() follow through domain().

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

1 participant