From cc206975bb1ca16a0fbe2e37077f30aca2ea84d6 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Thu, 15 Oct 2026 00:08:12 +0200 Subject: [PATCH] Check the IndieAuth client_id hostname without the port (#186537) Co-authored-by: Claude Opus 5.5 --- homeassistant/components/auth/indieauth.py | 19 +++++++------------ tests/components/auth/test_indieauth.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/auth/indieauth.py b/homeassistant/components/auth/indieauth.py index 3ceba6481bf3..068ec6b3229c 100644 --- a/homeassistant/components/auth/indieauth.py +++ b/homeassistant/components/auth/indieauth.py @@ -1,5 +1,6 @@ """Helpers to resolve client ID/secret.""" +from contextlib import suppress from html.parser import HTMLParser from http import HTTPStatus from ipaddress import ip_address @@ -389,19 +390,13 @@ def _parse_client_id(client_id: str) -> ParseResult: # We are not goint to follow the spec here. We are going to allow # any internal network IP to be used inside a client id. + # hostname excludes the port and the [, ] around ipv6 addresses + if parts.hostname is None: + raise ValueError("Client ID must contain a hostname") + address = None - - try: - netloc = parts.netloc - - # Strip the [, ] from ipv6 addresses before parsing - if netloc[0] == "[" and netloc[-1] == "]": - netloc = netloc[1:-1] - - address = ip_address(netloc) - except ValueError: - # Not an ip address - pass + with suppress(ValueError): + address = ip_address(parts.hostname) if address is None or is_local(address): return parts diff --git a/tests/components/auth/test_indieauth.py b/tests/components/auth/test_indieauth.py index 104b8d126207..62c7cd3f4c92 100644 --- a/tests/components/auth/test_indieauth.py +++ b/tests/components/auth/test_indieauth.py @@ -77,6 +77,8 @@ def test_client_id_hostname() -> None: """Test we enforce valid hostname.""" assert indieauth._parse_client_id("http://www.home-assistant.io/") assert indieauth._parse_client_id("http://[::1]") + assert indieauth._parse_client_id("http://[::1]:8123/") + assert indieauth._parse_client_id("http://[fe80::1]:8123/") assert indieauth._parse_client_id("http://127.0.0.1") assert indieauth._parse_client_id("http://10.0.0.0") assert indieauth._parse_client_id("http://10.255.255.255") @@ -93,6 +95,18 @@ def test_client_id_hostname() -> None: assert indieauth._parse_client_id("http://172.32.0.0/") with pytest.raises(ValueError): assert indieauth._parse_client_id("http://192.167.0.0/") + with pytest.raises(ValueError): + assert indieauth._parse_client_id("http://8.8.8.8:8123/") + with pytest.raises(ValueError): + assert indieauth._parse_client_id("http://[2001:db8::1]/") + with pytest.raises(ValueError): + assert indieauth._parse_client_id("http://[2001:db8::1]:8123/") + with pytest.raises(ValueError): + assert indieauth._parse_client_id("http://") + with pytest.raises(ValueError): + assert indieauth._parse_client_id("https:///callback") + with pytest.raises(ValueError): + assert indieauth._parse_client_id("http://:8123/") def test_parse_url_lowercase_host() -> None: