Repository navigation
Reject IDNA-mapped lookalikes in domains and simple hostnames - #527
Merged
Merged
Conversation
Python's idna codec implements IDNA 2003, whose nameprep step drops invisible characters (soft hyphen, zero width space, joiners, variation selectors), folds compatibility characters such as fullwidth letters, the Kelvin sign or the long s to ASCII, and splits labels on U+3002 and similar dots. domain() validated the mapped result, so "example.com" or "example.com" passed as example.com through domain(), hostname(), url() and email(). Reject non-ASCII values that are not NFKC-stable or that contain characters nameprep maps to nothing or treats as a dot. Internationalized names that need no such mapping, such as "пример.рф" or "straße.de", are still accepted. The simple hostname regex used [a-z] with re.IGNORECASE, which also matches U+0130, U+0131, U+017F and U+212A; add re.ASCII.
nandgator
force-pushed
the
fix-idna-lookalike-domains
branch
from
October 22, 2026 02:00
13a5cdb to
3c2413d
Compare
This was referenced Oct 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Python's
idnacodec implements IDNA 2003 (RFC 3490). Its nameprep step (RFC 3491) silently drops invisible characters (RFC 3454 table B.1), folds compatibility characters to ASCII, and RFC 3490 section 3.1 makes U+3002, U+FF0E and U+FF61 label separators.domain()validated the mapped result, so names that only look like another name passeddomain(),hostname(),url()andemail():With
consider_tld=True, the raw-TLD lookup happened to reject some of these, but only when the lookalike was in the TLD. #523 moves the lookup to the encoded TLD, which would remove that accidental protection, so this should land first.Change
domain()rejects non-ASCII values that are not NFKC-stable, or that contain a character nameprep maps to nothing or treats as a dot. IDNA 2008 (RFC 5894 section 7.3) drops these mappings as well.re.ASCII. Without it,[a-z]withre.IGNORECASEalso matches U+0130, U+0131, U+017F and U+212A (Pythonredocs). This is the same issue Reject non-ASCII case equivalents in IBANs #512 fixes for IBANs.Internationalized names that need no such mapping keep validating, e.g.
пример.рф,ПРИМЕР.РФ,例子.中国,kräuter.comandstraße.de. All 151 IDN TLDs in the current IANA list pass, both alone and with #523 applied underconsider_tld=True.Tests
New cases in
test_domain.py,test_hostname.py,test_url.pyandtest_email.pyfail on master and pass here. The full suite passes (1518 tests), as doruff check,ruff formatandpyright.