From 36c82f4fdd9024a9ff671585e4d15280f99ecf4a Mon Sep 17 00:00:00 2001 From: Shashank Yadav <65058495+shashank11yadav@users.noreply.github.com> Date: Sun, 29 Nov 2026 02:05:51 -0700 Subject: [PATCH] Reject trailing newline in percent-decoded URL userinfo A userinfo with two or more colons is percent-decoded before it is matched against the username regex. A decoded newline in the middle was rejected, but one at the very end was accepted, because `$` also matches just before a trailing newline. Anchor the dot-atom and non-quoted-string branches with `\Z` so both positions agree. --- src/validators/url.py | 4 ++-- tests/test_url.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/validators/url.py b/src/validators/url.py index ad42140e..31b0f6a6 100644 --- a/src/validators/url.py +++ b/src/validators/url.py @@ -17,9 +17,9 @@ def _username_regex(): # extended latin r"(^[\u0100-\u017F\u0180-\u024F]" # dot-atom - + r"|[-!#$%&'*+/=?^_`{}|~0-9a-z]+(\.[-!#$%&'*+/=?^_`{}|~0-9a-z]+)*$" + + r"|[-!#$%&'*+/=?^_`{}|~0-9a-z]+(\.[-!#$%&'*+/=?^_`{}|~0-9a-z]+)*\Z" # non-quoted-string - + r"|^([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\011.])*$)", + + r"|^([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\011.])*\Z)", re.IGNORECASE, ) diff --git a/tests/test_url.py b/tests/test_url.py index d48a03ec..b4a663bd 100644 --- a/tests/test_url.py +++ b/tests/test_url.py @@ -259,6 +259,22 @@ def test_url_accepts_percent_encoded_controls(encoded: str, template: str): assert url(template.format(encoded=encoded)) is True +@pytest.mark.parametrize( + "value", + [ + "http://a:b:c%0A@example.com", + "http://a:b:c%0a@example.com", + "https://user:pass:word%0A@example.com:8080/", + "http://a:b:c%0Ad@example.com", + "http://%0A:b:c@example.com", + "http://a:b:c%0A%0A@example.com", + ], +) +def test_returns_failed_validation_on_newline_in_decoded_userinfo(value: str): + """Test returns failed validation on a percent-encoded newline in a multi-colon userinfo.""" + assert isinstance(url(value), ValidationError) + + @pytest.mark.parametrize( "value", ["https://example.\uff43\uff4f\uff4d/", "https://exam\u00adple.com/", "http://\u017ferver/"],