From e7a60bc3e59474147ff72b92826811ff96b1c78b Mon Sep 17 00:00:00 2001 From: Josef Zweck Date: Thu, 15 Oct 2026 08:16:11 +0200 Subject: [PATCH] Guard owner and existing credentials in auth provider create command (#186398) --- .../config/auth_provider_homeassistant.py | 36 +++++- .../test_auth_provider_homeassistant.py | 118 +++++++++++++++++- 2 files changed, 147 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/config/auth_provider_homeassistant.py b/homeassistant/components/config/auth_provider_homeassistant.py index e8b5396c73c6..05a97ed51647 100644 --- a/homeassistant/components/config/auth_provider_homeassistant.py +++ b/homeassistant/components/config/auth_provider_homeassistant.py @@ -1,5 +1,6 @@ """Offer API to configure the Home Assistant auth provider.""" +import asyncio from typing import Any import probatio @@ -8,11 +9,17 @@ from homeassistant.auth.providers import homeassistant as auth_ha from homeassistant.components import websocket_api from homeassistant.core import HomeAssistant, callback from homeassistant.exceptions import Unauthorized +from homeassistant.util.hass_dict import HassKey + +CREATE_LOCK: HassKey[asyncio.Lock] = HassKey( + "config_auth_provider_homeassistant_create_lock" +) @callback def async_setup(hass: HomeAssistant) -> bool: """Enable the Home Assistant views.""" + hass.data[CREATE_LOCK] = asyncio.Lock() websocket_api.async_register_command(hass, websocket_create) websocket_api.async_register_command(hass, websocket_delete) websocket_api.async_register_command(hass, websocket_change_password) @@ -51,12 +58,31 @@ async def websocket_create( ) return - await provider.async_add_auth(msg["username"], msg["password"]) + if user.is_owner and not connection.user.is_owner: + raise Unauthorized(context=connection.context(msg)) - credentials = await provider.async_get_or_create_credentials( - {"username": msg["username"]} - ) - await hass.auth.async_link_user(user, credentials) + # Serialize the check with the link so concurrent requests can't both + # pass it and attach two passwords. + async with hass.data[CREATE_LOCK]: + # A second password would let an admin log in as the user without + # going through the owner-only admin_change_password command. + if any( + credential.auth_provider_type == provider.type + for credential in user.credentials + ): + connection.send_error( + msg["id"], + "credentials_exist", + "User already has credentials for this provider", + ) + return + + await provider.async_add_auth(msg["username"], msg["password"]) + + credentials = await provider.async_get_or_create_credentials( + {"username": msg["username"]} + ) + await hass.auth.async_link_user(user, credentials) connection.send_result(msg["id"]) diff --git a/tests/components/config/test_auth_provider_homeassistant.py b/tests/components/config/test_auth_provider_homeassistant.py index 56fa85859d01..2e0a8e04c0a5 100644 --- a/tests/components/config/test_auth_provider_homeassistant.py +++ b/tests/components/config/test_auth_provider_homeassistant.py @@ -74,9 +74,123 @@ async def test_create_auth_system_generated_user( assert result["error"]["code"] == "system_generated" -async def test_create_auth_user_already_credentials() -> None: +async def test_create_auth_user_already_credentials( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + auth_provider: prov_ha.HassAuthProvider, +) -> None: """Test we can't create auth for user with pre-existing credentials.""" - # assert False + client = await hass_ws_client(hass) + user = MockUser().add_to_hass(hass) + + await client.send_json( + { + "id": 5, + "type": "config/auth_provider/homeassistant/create", + "user_id": user.id, + "username": "test-user2", + "password": "test-pass", + } + ) + result = await client.receive_json() + assert result["success"], result + + await client.send_json( + { + "id": 6, + "type": "config/auth_provider/homeassistant/create", + "user_id": user.id, + "username": "test-user3", + "password": "test-pass", + } + ) + result = await client.receive_json() + assert not result["success"], result + assert result["error"]["code"] == "credentials_exist" + assert len(user.credentials) == 1 + + with pytest.raises(prov_ha.InvalidAuth): + await auth_provider.async_validate_login("test-user3", "test-pass") + + +async def test_create_auth_concurrent( + hass: HomeAssistant, hass_ws_client: WebSocketGenerator +) -> None: + """Test concurrent creates for the same user attach only one credential.""" + client = await hass_ws_client(hass) + user = MockUser().add_to_hass(hass) + + for msg_id, username in ((5, "test-user2"), (6, "test-user3")): + await client.send_json( + { + "id": msg_id, + "type": "config/auth_provider/homeassistant/create", + "user_id": user.id, + "username": username, + "password": "test-pass", + } + ) + + results = { + result["id"]: result + for result in (await client.receive_json(), await client.receive_json()) + } + assert results[5]["success"], results[5] + assert not results[6]["success"], results[6] + assert results[6]["error"]["code"] == "credentials_exist" + assert len(user.credentials) == 1 + + +async def test_create_auth_owner_not_owner( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + hass_owner_user: MockUser, + auth_provider: prov_ha.HassAuthProvider, +) -> None: + """Test an admin that is not the owner can't add credentials to the owner.""" + client = await hass_ws_client(hass) + + await client.send_json( + { + "id": 5, + "type": "config/auth_provider/homeassistant/create", + "user_id": hass_owner_user.id, + "username": "test-user2", + "password": "test-pass", + } + ) + + result = await client.receive_json() + assert not result["success"], result + assert result["error"]["code"] == "unauthorized" + assert len(hass_owner_user.credentials) == 0 + + with pytest.raises(prov_ha.InvalidAuth): + await auth_provider.async_validate_login("test-user2", "test-pass") + + +async def test_create_auth_owner( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + hass_owner_user: MockUser, + owner_access_token: str, +) -> None: + """Test the owner can add credentials to itself.""" + client = await hass_ws_client(hass, owner_access_token) + + await client.send_json( + { + "id": 5, + "type": "config/auth_provider/homeassistant/create", + "user_id": hass_owner_user.id, + "username": "test-user2", + "password": "test-pass", + } + ) + + result = await client.receive_json() + assert result["success"], result + assert len(hass_owner_user.credentials) == 1 async def test_create_auth_unknown_user(