Repository navigation
Reject long internationalized domains before calling idna.alabel - #194
Merged
Merged
Conversation
validate_email_domain_name converts internationalized domains with idna.alabel per label and checks the length only after encoding, which skips the up-front length check idna.encode does. a long "xn--" label is then punycode-decoded in full, quadratic in its length, and with idna 3.14 and earlier so is a long run of arabic-indic digits. reject domains over the dns limit first; alabel never shortens a label, so these already failed the length check after encoding.
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.
validate_email_domain_name sends internationalized domains through idna.alabel one label at a time, to keep its own error messages, and checks the domain length only after encoding, so it skips the up-front length check idna.encode does to avoid quadratic work on long labels. with the pinned idna 3.15, idna.ulabel punycode-decodes an xn-- label before looking at its length, so validate_email("me@é.xn--" + "9" * 200000 + ".com") spends 5 s in cpython's punycode decoder before rejecting it (1.3 s at 100 KB, 20 s at 400 KB, Apple M5, CPython 3.13), while an all-ascii domain of the same size is rejected in 90 ms because it reaches the length check first. with idna 3.14 and earlier, which idna>=2.0.0 still allows, check_label has no length guard, and a label of 40,000 arabic-indic digits ("٠") takes 33 s in valid_contexto. checking len(domain) against DOMAIN_MAX_LENGTH before the alabel loop brings both under 0.1 s, growing linearly, on either idna version. alabel never returns a label shorter than its input, so these domains already failed the length check after encoding; across 100,000 random internationalized domains around the 253-character limit nothing changed between valid and invalid, and the only difference is the message for domains over 253 characters, which now matches the ascii one. the check has to live here even if idna changes ulabel, since this code calls alabel directly and runs against whichever idna is installed; it is separate from #189, which fixed split_email's own normalization. test_email_long_idna_domain_is_not_quadratic fails on main after 5 s and passes in 0.11 s; flake8, mypy and the suite pass on 3.10, 3.11, 3.13 and 3.14.