Skip to content

Commit 36c82f4

Browse files
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.
1 parent 22786cc commit 36c82f4

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎src/validators/url.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def _username_regex():
1717
# extended latin
1818
r"(^[\u0100-\u017F\u0180-\u024F]"
1919
# dot-atom
20-
+ r"|[-!#$%&'*+/=?^_`{}|~0-9a-z]+(\.[-!#$%&'*+/=?^_`{}|~0-9a-z]+)*$"
20+
+ r"|[-!#$%&'*+/=?^_`{}|~0-9a-z]+(\.[-!#$%&'*+/=?^_`{}|~0-9a-z]+)*\Z"
2121
# non-quoted-string
22-
+ r"|^([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\011.])*$)",
22+
+ r"|^([\001-\010\013\014\016-\037!#-\[\]-\177]|\\[\011.])*\Z)",
2323
re.IGNORECASE,
2424
)
2525

‎tests/test_url.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,22 @@ def test_url_accepts_percent_encoded_controls(encoded: str, template: str):
259259
assert url(template.format(encoded=encoded)) is True
260260

261261

262+
@pytest.mark.parametrize(
263+
"value",
264+
[
265+
"http://a:b:c%0A@example.com",
266+
"http://a:b:c%0a@example.com",
267+
"https://user:pass:word%0A@example.com:8080/",
268+
"http://a:b:c%0Ad@example.com",
269+
"http://%0A:b:c@example.com",
270+
"http://a:b:c%0A%0A@example.com",
271+
],
272+
)
273+
def test_returns_failed_validation_on_newline_in_decoded_userinfo(value: str):
274+
"""Test returns failed validation on a percent-encoded newline in a multi-colon userinfo."""
275+
assert isinstance(url(value), ValidationError)
276+
277+
262278
@pytest.mark.parametrize(
263279
"value",
264280
["https://example.\uff43\uff4f\uff4d/", "https://exam\u00adple.com/", "http://\u017ferver/"],

0 commit comments

Comments
 (0)