From a1af789c85216162fd5e20209fa46bbc46186252 Mon Sep 17 00:00:00 2001 From: Josef Zweck Date: Mon, 19 Oct 2026 17:32:43 +0200 Subject: [PATCH] Check async_can_start_login when a login flow is started (#187321) --- homeassistant/auth/__init__.py | 15 ++++- homeassistant/auth/providers/__init__.py | 5 +- tests/components/auth/test_login_flow.py | 71 ++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/homeassistant/auth/__init__.py b/homeassistant/auth/__init__.py index 47a937ee1074..377d24025d24 100644 --- a/homeassistant/auth/__init__.py +++ b/homeassistant/auth/__init__.py @@ -18,7 +18,12 @@ from homeassistant.core import ( HomeAssistant, callback, ) -from homeassistant.data_entry_flow import FlowHandler, FlowManager, FlowResultType +from homeassistant.data_entry_flow import ( + FlowHandler, + FlowManager, + FlowResultType, + UnknownHandler, +) from homeassistant.helpers.event import async_track_point_in_utc_time from homeassistant.util import dt as dt_util @@ -128,8 +133,12 @@ class AuthManagerFlowManager( ) -> LoginFlow[Any]: """Create a login flow.""" auth_provider = self.auth_manager.get_auth_provider(*handler_key) - if not auth_provider: - raise KeyError(f"Unknown auth provider {handler_key}") + # Not offered on the login screen is answered like not configured, so + # it doesn't reveal which providers are set up. + if not auth_provider or not auth_provider.async_can_start_login( + context or AuthFlowContext() + ): + raise UnknownHandler return await auth_provider.async_login_flow(context) @override diff --git a/homeassistant/auth/providers/__init__.py b/homeassistant/auth/providers/__init__.py index f7eaff4c66e7..c9cca3e78a66 100644 --- a/homeassistant/auth/providers/__init__.py +++ b/homeassistant/auth/providers/__init__.py @@ -107,7 +107,10 @@ class AuthProvider: @callback def async_can_start_login(self, context: AuthFlowContext) -> bool: - """Return if the login screen may offer this provider for the context.""" + """Return if a login flow may be started with this provider for the context. + + The login screen only offers the providers for which this returns True. + """ return True async def async_get_or_create_credentials( diff --git a/tests/components/auth/test_login_flow.py b/tests/components/auth/test_login_flow.py index 4fad39eb1cf6..cde1e7134e94 100644 --- a/tests/components/auth/test_login_flow.py +++ b/tests/components/auth/test_login_flow.py @@ -122,6 +122,77 @@ async def test_fetch_auth_providers_skips_providers_that_cannot_start_login( assert (await resp.json())["providers"] == [] +async def test_login_flow_refuses_providers_that_cannot_start_login( + hass: HomeAssistant, aiohttp_client: ClientSessionGenerator +) -> None: + """Test a provider left off the login screen can't be started directly.""" + client = await async_setup_auth(hass, aiohttp_client, BASE_CONFIG) + with patch( + "homeassistant.auth.providers.insecure_example.ExampleAuthProvider" + ".async_can_start_login", + return_value=False, + ): + resp = await client.post( + "/auth/login_flow", + json={ + "client_id": CLIENT_ID, + "handler": ["insecure_example", None], + "redirect_uri": CLIENT_REDIRECT_URI, + }, + ) + + assert resp.status == HTTPStatus.NOT_FOUND + assert await resp.json() == {"message": "Invalid handler specified"} + + +async def test_login_flow_trusted_networks_from_trusted_network( + hass: HomeAssistant, aiohttp_client: ClientSessionGenerator +) -> None: + """Test a trusted networks login can be started from a trusted network.""" + client = await async_setup_auth( + hass, aiohttp_client, [_TRUSTED_NETWORKS_CONFIG], custom_ip="192.168.0.1" + ) + resp = await client.post( + "/auth/login_flow", + json={ + "client_id": CLIENT_ID, + "handler": ["trusted_networks", None], + "redirect_uri": CLIENT_REDIRECT_URI, + }, + ) + + assert resp.status == HTTPStatus.OK + + +@pytest.mark.parametrize( + "provider_configs", + [ + pytest.param([_TRUSTED_NETWORKS_CONFIG], id="untrusted-network"), + pytest.param(BASE_CONFIG, id="not-configured"), + ], +) +async def test_login_flow_hides_unavailable_trusted_networks( + hass: HomeAssistant, + aiohttp_client: ClientSessionGenerator, + provider_configs: list[dict[str, Any]], +) -> None: + """Test trusted networks from outside is answered like it isn't configured.""" + client = await async_setup_auth( + hass, aiohttp_client, provider_configs, custom_ip="1.2.3.4" + ) + resp = await client.post( + "/auth/login_flow", + json={ + "client_id": CLIENT_ID, + "handler": ["trusted_networks", None], + "redirect_uri": CLIENT_REDIRECT_URI, + }, + ) + + assert resp.status == HTTPStatus.NOT_FOUND + assert await resp.json() == {"message": "Invalid handler specified"} + + async def test_fetch_auth_providers_onboarding( hass: HomeAssistant, aiohttp_client: ClientSessionGenerator ) -> None: