Repository navigation
Check the whole username in url() - #608
Open
Yasser-Ameur wants to merge 1 commit into
Open
Yasser-Ameur wants to merge 1 commit into
Yasser-Ameur wants to merge 1 commit into
Conversation
url() accepted any username that starts with an extended-Latin letter: `http://ā"b@example.com` was valid, while `http://a"b@example.com` is not. The same holds for `\` and for other characters the username pattern refuses after an ASCII first letter. The first alternative of the username pattern matches a single extended-Latin letter and is applied with `re.match`, so the rest of the username was never looked at. The email validator uses the same pattern; email was switched to `re.fullmatch` in e01166b. Use `re.fullmatch` for the username in the three places it is checked, and let the extended-Latin alternative continue with the characters the non-quoted-string alternative accepts, so names such as `Łókaść` stay valid. Nothing is newly accepted: every username that passes now passed before.
Yasser-Ameur
marked this pull request as ready for review
December 5, 2026 23:34
This branch has not been deployed
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.
This change only tightens
url(). Nothing that was invalid becomes valid. I checked it with a fuzz of 300000 random usernames, the old pattern withre.matchagainst the new one withre.fullmatch: the new code accepted 151261, rejected 8295 that the old one accepted, and accepted 0 that the old one rejected. A second fuzz of 200000 usernames and 50000 whole URLs also found nothing newly accepted.url()accepts any username that starts with an extended-Latin letter, whatever follows. The same junk after an ASCII first letter is rejected.The first alternative of the username pattern in
src/validators/url.pymatches a single extended-Latin letter, and_validate_auth_segmentapplied the pattern withre.matchin three places, so nothing after that letter was looked at. The email validator uses the same pattern, and email was switched tore.fullmatchin e01166b.The three
_username_regex().match(calls are now.fullmatch(. That alone would rejectŁókaść, because the URL's non-quoted-string class has no extended-Latin letters while the email one does. So the extended-Latin alternative now continues with[ -ÿĀ-ſƀ-ɏ]plus the character class of the non-quoted-string alternative. I did not copy the email class into the URL one, since that would newly accept names such asaā.Names that stop being valid contain a double quote, an unescaped backslash, a space or tab, or a letter outside Latin-1 and Latin Extended-A and B, such as the Vietnamese name written with U+1EB7. Their ASCII-led twins are already rejected on master, and the percent-encoded form still passes.
<and>stay valid, because they are in the same class for ASCII-led names:http://a<script>@example.com/is valid on master too. Passwords and the characters the username class admits are unchanged.Tests in
tests/test_url.py: validuserid,user.name,user%40name:p%40ss,ā,ā.b,Łókaść,Łókaść:passwordandā%41; invalid ASCII twinsa"banda\b,a<>"{}|^`, andā"b,ā\b,ā<>"{}|^`, the same with a password,ā%22b:x:yandŁókaść". The six invalid unicode-led cases fail on master. With the locked dev dependencies,pytest .gives 1591 passed on Python 3.11, 3.12, 3.13 and 3.14 on Windows, andruff check,ruff format --check,pyrightandbanditare clean.This also covers the case in #603:
http://a:b:c%0A@example.comis valid on master and invalid here, sincefullmatchdoes not stop before a trailing newline. #604 is open for that issue and edits the same pattern. The two merge without conflicts.