Check the conversation owner before Wyoming sends text (#188181)

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
Paulus Schoutsen
2026-10-26 18:45:59 +00:00
committed by GitHub
co-authored by Claude
parent 9475eecaab
commit 68170bb861
4 changed files with 105 additions and 20 deletions
@@ -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(
@@ -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
)
@@ -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,
@@ -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: