From 6ee71ba9e0cd9975c6e4e77db855a4372e45135d Mon Sep 17 00:00:00 2001 From: Raman Gupta <7243222+raman325@users.noreply.github.com> Date: Wed, 21 Oct 2026 16:49:43 -0400 Subject: [PATCH] Allow spaces in the userinfo of validated URLs (#187840) Co-authored-by: Claude Opus 5.5 --- homeassistant/helpers/config_validation.py | 6 ++++-- tests/helpers/test_config_validation.py | 8 ++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index f571b1224d8b..c89fb7e312c2 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -833,8 +833,10 @@ def url( raise probatio.Invalid("invalid url") parsed = urlparse(url_in) - # HTTP clients encode spaces in the path and query, but not in the host - if parsed.scheme not in _schema_list or _URL_WHITESPACE.search(parsed.netloc): + # HTTP clients encode spaces in the userinfo, path and query, but not the host + if parsed.scheme not in _schema_list or _URL_WHITESPACE.search( + parsed.hostname or "" + ): raise probatio.Invalid("invalid url") try: diff --git a/tests/helpers/test_config_validation.py b/tests/helpers/test_config_validation.py index 1587057232e8..1fd981b05683 100644 --- a/tests/helpers/test_config_validation.py +++ b/tests/helpers/test_config_validation.py @@ -152,6 +152,7 @@ def test_url() -> None: "value", [ pytest.param("http://exa mple.com", id="space_in_host"), + pytest.param("http://user:pass@exa mple.com", id="space_in_host_with_userinfo"), pytest.param("http://example.com\x00", id="nul"), pytest.param("http://exam\tple.com", id="tab_in_host"), pytest.param("http://exam\nple.com", id="newline_in_host"), @@ -189,10 +190,13 @@ def test_url_strips_surrounding_whitespace(value: str) -> None: pytest.param( "http://example.com/?file=snapshot image.jpg", id="space_in_query" ), + pytest.param("http://example.com/#snapshot image", id="space_in_fragment"), + pytest.param("http://user:pa ss@example.com/", id="space_in_password"), + pytest.param("http://us er@example.com/", id="space_in_username"), ], ) -def test_url_allows_spaces_after_host(value: str) -> None: - """Test spaces in the path and query are kept, since HTTP clients encode them.""" +def test_url_allows_spaces_outside_host(value: str) -> None: + """Test spaces outside the host are kept, since HTTP clients encode them.""" assert probatio.Schema(cv.url)(value) == value