diff --git a/homeassistant/components/assist_pipeline/default_pipeline.py b/homeassistant/components/assist_pipeline/default_pipeline.py index 0d2628cd221b..55f2b17bae0c 100644 --- a/homeassistant/components/assist_pipeline/default_pipeline.py +++ b/homeassistant/components/assist_pipeline/default_pipeline.py @@ -14,7 +14,7 @@ import hass_nabucasa from homeassistant.components import conversation, media_player, stt, tts, wake_word from homeassistant.const import MATCH_ALL, EntityStateAttribute from homeassistant.core import Context, HomeAssistant, callback -from homeassistant.exceptions import HomeAssistantError +from homeassistant.exceptions import HomeAssistantError, Unauthorized from homeassistant.helpers import ( chat_session, device_registry as dr, @@ -847,6 +847,11 @@ class _DefaultPipelineProcessor: ) ) + except Unauthorized as src_error: + raise IntentRecognitionError( + code="intent-unauthorized", + message="Not allowed to continue this conversation", + ) from src_error except Exception as src_error: _LOGGER.exception("Unexpected error during intent recognition") raise IntentRecognitionError( diff --git a/homeassistant/components/wyoming/conversation.py b/homeassistant/components/wyoming/conversation.py index 0b9f3c46c500..abb59be944dd 100644 --- a/homeassistant/components/wyoming/conversation.py +++ b/homeassistant/components/wyoming/conversation.py @@ -121,24 +121,26 @@ class WyomingConversationEntity( context["satellite_id"] = user_input.satellite_id try: - async with AsyncTcpClient(self.service.host, self.service.port) as client: - await client.write_event( - Transcript( - user_input.text, - context=context, - language=user_input.language, - ).event() - ) - with ( - chat_session.async_get_chat_session( - self.hass, - user_input.conversation_id, - context=user_input.context, - ) as session, - conversation.async_get_chat_log( - self.hass, session, user_input - ) as chat_log, - ): + # Open the session and chat log first, so their checks run before + # the text leaves Home Assistant. + with ( + chat_session.async_get_chat_session( + self.hass, user_input.conversation_id, context=user_input.context + ) as session, + conversation.async_get_chat_log( + self.hass, session, user_input + ) as chat_log, + ): + async with AsyncTcpClient( + self.service.host, self.service.port + ) as client: + await client.write_event( + Transcript( + user_input.text, + context=context, + language=user_input.language, + ).event() + ) intent_response = await self._async_process( user_input, client, chat_log, intent_response ) diff --git a/tests/components/assist_pipeline/test_websocket.py b/tests/components/assist_pipeline/test_websocket.py index 80bdd5d900ad..5a3abfbb1e62 100644 --- a/tests/components/assist_pipeline/test_websocket.py +++ b/tests/components/assist_pipeline/test_websocket.py @@ -25,7 +25,7 @@ from homeassistant.components.assist_pipeline.pipeline import ( async_get_pipelines, async_update_pipeline, ) -from homeassistant.core import HomeAssistant +from homeassistant.core import Context, HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import chat_session, device_registry as dr @@ -579,6 +579,48 @@ async def test_text_pipeline_timeout( assert msg["result"] == {"events": events} +@pytest.mark.usefixtures("init_components") +async def test_intent_unauthorized( + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test continuing a conversation of another user ends with a clean error.""" + await conversation.async_converse( + hass, + "Are the lights on?", + "koala", + Context(user_id="other-user-id"), + ) + + client = await hass_ws_client(hass) + await client.send_json_auto_id( + { + "type": "assist_pipeline/run", + "start_stage": "intent", + "end_stage": "intent", + "input": {"text": "Are the lights on?"}, + "conversation_id": "koala", + } + ) + + msg = await client.receive_json() + assert msg["success"] + + for event_type in ("run-start", "intent-start"): + msg = await client.receive_json() + assert msg["event"]["type"] == event_type + + msg = await client.receive_json() + assert msg["event"]["type"] == "error" + assert msg["event"]["data"]["code"] == "intent-unauthorized" + + msg = await client.receive_json() + assert msg["event"]["type"] == "run-end" + + assert "Unexpected error during intent recognition" not in caplog.text + + async def test_intent_failed( hass: HomeAssistant, hass_ws_client: WebSocketGenerator, diff --git a/tests/components/wyoming/test_conversation.py b/tests/components/wyoming/test_conversation.py index c96f185de363..4d1986153322 100644 --- a/tests/components/wyoming/test_conversation.py +++ b/tests/components/wyoming/test_conversation.py @@ -381,6 +381,42 @@ async def test_handle(hass: HomeAssistant, init_wyoming_handle: ConfigEntry) -> assert result.conversation_id == conversation_id +async def test_handle_other_user( + hass: HomeAssistant, init_wyoming_handle: ConfigEntry +) -> None: + """Test the text of a refused user is not sent to the service.""" + agent_id = "conversation.test_handle" + conversation_id = "conversation-1234" + + with patch( + "homeassistant.components.wyoming.conversation.AsyncTcpClient", + MockAsyncTcpClient([Handled(text="success").event()]), + ): + await conversation.async_converse( + hass=hass, + text="test text", + conversation_id=conversation_id, + context=Context(user_id="owner-user-id"), + agent_id=agent_id, + ) + + client = MockAsyncTcpClient([Handled(text="success").event()]) + with patch( + "homeassistant.components.wyoming.conversation.AsyncTcpClient", + client, + ): + result = await conversation.async_converse( + hass=hass, + text="other text", + conversation_id=conversation_id, + context=Context(user_id="other-user-id"), + agent_id=agent_id, + ) + + assert result.response.response_type is intent.IntentResponseType.ERROR + assert not client.written + + async def test_not_handled( hass: HomeAssistant, init_wyoming_handle: ConfigEntry ) -> None: