Skip to content

Check the whole username in url() - #608

Open
Yasser-Ameur wants to merge 1 commit into
python-validators:masterfrom
Yasser-Ameur:fix/validators-url-userinfo
Open

Yasser-Ameur wants to merge 1 commit into
python-validators:masterfrom
Yasser-Ameur:fix/validators-url-userinfo

Conversation

@Yasser-Ameur

Copy link
Copy Markdown

This change only tightens url(). Nothing that was invalid becomes valid. I checked it with a fuzz of 300000 random usernames, the old pattern with re.match against the new one with re.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.

import validators as v

v.url("http://a<>\"{}|^`@example.com/")   # False on both
v.url("http://ā<>\"{}|^`@example.com/")   # master: True    now: False
v.url("http://ā\\@example.com/")          # master: True    now: False

The first alternative of the username pattern in src/validators/url.py matches a single extended-Latin letter, and _validate_auth_segment applied the pattern with re.match in three places, so nothing after that letter was looked at. The email validator uses the same pattern, and email was switched to re.fullmatch in 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 as aā.

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: valid userid, user.name, user%40name:p%40ss, ā, ā.b, Łókaść, Łókaść:password and ā%41; invalid ASCII twins a"b and a\b, a<>"{}|^`, and ā"b, ā\b, ā<>"{}|^`, the same with a password, ā%22b:x:y and Łó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, and ruff check, ruff format --check, pyright and bandit are clean.

This also covers the case in #603: http://a:b:c%0A@example.com is valid on master and invalid here, since fullmatch does not stop before a trailing newline. #604 is open for that issue and edits the same pattern. The two merge without conflicts.

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
Yasser-Ameur marked this pull request as ready for review December 5, 2026 23:34

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