|
| 1 | +"""Tests for first-message WebSocket authentication in sockets.py.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import json |
| 5 | +from unittest.mock import AsyncMock, MagicMock, patch |
| 6 | +from uuid import uuid4 |
| 7 | + |
| 8 | +import pytest |
| 9 | +from fastapi import WebSocketDisconnect |
| 10 | + |
| 11 | +from openhands.agent_server.sockets import _accept_authenticated_websocket |
| 12 | + |
| 13 | + |
| 14 | +def _make_mock_websocket(*, headers=None): |
| 15 | + """Build a mock WebSocket with configurable query params and headers.""" |
| 16 | + ws = MagicMock() |
| 17 | + ws.accept = AsyncMock() |
| 18 | + ws.receive_text = AsyncMock() |
| 19 | + ws.receive_json = AsyncMock() |
| 20 | + ws.send_json = AsyncMock() |
| 21 | + ws.close = AsyncMock() |
| 22 | + ws.headers = headers or {} |
| 23 | + return ws |
| 24 | + |
| 25 | + |
| 26 | +# -- No auth configured (empty session_api_keys) -- |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.asyncio |
| 30 | +async def test_no_auth_configured_accepts_immediately(): |
| 31 | + ws = _make_mock_websocket() |
| 32 | + with patch("openhands.agent_server.sockets.get_default_config") as mock_config: |
| 33 | + mock_config.return_value.session_api_keys = [] |
| 34 | + result = await _accept_authenticated_websocket(ws, session_api_key=None) |
| 35 | + |
| 36 | + assert result is True |
| 37 | + ws.accept.assert_called_once() |
| 38 | + ws.receive_text.assert_not_called() |
| 39 | + |
| 40 | + |
| 41 | +# -- Legacy query param auth (deprecated) -- |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.asyncio |
| 45 | +async def test_legacy_query_param_valid_key(): |
| 46 | + ws = _make_mock_websocket() |
| 47 | + with patch("openhands.agent_server.sockets.get_default_config") as mock_config: |
| 48 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 49 | + result = await _accept_authenticated_websocket(ws, session_api_key="sk-oh-valid") |
| 50 | + |
| 51 | + assert result is True |
| 52 | + ws.accept.assert_called_once() |
| 53 | + ws.receive_text.assert_not_called() |
| 54 | + |
| 55 | + |
| 56 | +@pytest.mark.asyncio |
| 57 | +async def test_legacy_query_param_invalid_key(): |
| 58 | + ws = _make_mock_websocket() |
| 59 | + with patch("openhands.agent_server.sockets.get_default_config") as mock_config: |
| 60 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 61 | + result = await _accept_authenticated_websocket(ws, session_api_key="sk-oh-wrong") |
| 62 | + |
| 63 | + assert result is False |
| 64 | + ws.close.assert_called_once_with(code=4001, reason="Authentication failed") |
| 65 | + ws.accept.assert_not_called() |
| 66 | + |
| 67 | + |
| 68 | +# -- Legacy header auth (deprecated) -- |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.asyncio |
| 72 | +async def test_legacy_header_valid_key(): |
| 73 | + ws = _make_mock_websocket(headers={"x-session-api-key": "sk-oh-valid"}) |
| 74 | + with patch("openhands.agent_server.sockets.get_default_config") as mock_config: |
| 75 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 76 | + result = await _accept_authenticated_websocket(ws, session_api_key=None) |
| 77 | + |
| 78 | + assert result is True |
| 79 | + ws.accept.assert_called_once() |
| 80 | + |
| 81 | + |
| 82 | +@pytest.mark.asyncio |
| 83 | +async def test_legacy_header_invalid_key(): |
| 84 | + ws = _make_mock_websocket(headers={"x-session-api-key": "sk-oh-wrong"}) |
| 85 | + with patch("openhands.agent_server.sockets.get_default_config") as mock_config: |
| 86 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 87 | + result = await _accept_authenticated_websocket(ws, session_api_key=None) |
| 88 | + |
| 89 | + assert result is False |
| 90 | + ws.close.assert_called_once_with(code=4001, reason="Authentication failed") |
| 91 | + |
| 92 | + |
| 93 | +# -- First-message auth -- |
| 94 | + |
| 95 | + |
| 96 | +@pytest.mark.asyncio |
| 97 | +async def test_first_message_auth_valid_key(): |
| 98 | + ws = _make_mock_websocket() |
| 99 | + ws.receive_text.return_value = json.dumps( |
| 100 | + {"type": "auth", "session_api_key": "sk-oh-valid"} |
| 101 | + ) |
| 102 | + with patch("openhands.agent_server.sockets.get_default_config") as mock_config: |
| 103 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 104 | + result = await _accept_authenticated_websocket(ws, session_api_key=None) |
| 105 | + |
| 106 | + assert result is True |
| 107 | + ws.accept.assert_called_once() |
| 108 | + ws.receive_text.assert_called_once() |
| 109 | + |
| 110 | + |
| 111 | +@pytest.mark.asyncio |
| 112 | +async def test_first_message_auth_invalid_key(): |
| 113 | + ws = _make_mock_websocket() |
| 114 | + ws.receive_text.return_value = json.dumps( |
| 115 | + {"type": "auth", "session_api_key": "sk-oh-wrong"} |
| 116 | + ) |
| 117 | + with patch("openhands.agent_server.sockets.get_default_config") as mock_config: |
| 118 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 119 | + result = await _accept_authenticated_websocket(ws, session_api_key=None) |
| 120 | + |
| 121 | + assert result is False |
| 122 | + ws.accept.assert_called_once() # accepted before reading first message |
| 123 | + ws.close.assert_called_once_with(code=4001, reason="Authentication failed") |
| 124 | + |
| 125 | + |
| 126 | +@pytest.mark.asyncio |
| 127 | +async def test_first_message_auth_wrong_type_field(): |
| 128 | + ws = _make_mock_websocket() |
| 129 | + ws.receive_text.return_value = json.dumps( |
| 130 | + {"type": "message", "session_api_key": "sk-oh-valid"} |
| 131 | + ) |
| 132 | + with patch("openhands.agent_server.sockets.get_default_config") as mock_config: |
| 133 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 134 | + result = await _accept_authenticated_websocket(ws, session_api_key=None) |
| 135 | + |
| 136 | + assert result is False |
| 137 | + |
| 138 | + |
| 139 | +@pytest.mark.asyncio |
| 140 | +async def test_first_message_auth_missing_key_field(): |
| 141 | + ws = _make_mock_websocket() |
| 142 | + ws.receive_text.return_value = json.dumps({"type": "auth"}) |
| 143 | + with patch("openhands.agent_server.sockets.get_default_config") as mock_config: |
| 144 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 145 | + result = await _accept_authenticated_websocket(ws, session_api_key=None) |
| 146 | + |
| 147 | + assert result is False |
| 148 | + |
| 149 | + |
| 150 | +@pytest.mark.asyncio |
| 151 | +async def test_first_message_auth_malformed_json(): |
| 152 | + ws = _make_mock_websocket() |
| 153 | + ws.receive_text.return_value = "not json at all" |
| 154 | + with patch("openhands.agent_server.sockets.get_default_config") as mock_config: |
| 155 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 156 | + result = await _accept_authenticated_websocket(ws, session_api_key=None) |
| 157 | + |
| 158 | + assert result is False |
| 159 | + ws.close.assert_called_once_with(code=4001, reason="Authentication failed") |
| 160 | + |
| 161 | + |
| 162 | +@pytest.mark.asyncio |
| 163 | +async def test_first_message_auth_client_disconnects(): |
| 164 | + ws = _make_mock_websocket() |
| 165 | + ws.receive_text.side_effect = WebSocketDisconnect() |
| 166 | + with patch("openhands.agent_server.sockets.get_default_config") as mock_config: |
| 167 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 168 | + result = await _accept_authenticated_websocket(ws, session_api_key=None) |
| 169 | + |
| 170 | + assert result is False |
| 171 | + |
| 172 | + |
| 173 | +@pytest.mark.asyncio |
| 174 | +async def test_first_message_auth_timeout(): |
| 175 | + ws = _make_mock_websocket() |
| 176 | + |
| 177 | + async def slow_receive(): |
| 178 | + await asyncio.sleep(60) |
| 179 | + |
| 180 | + ws.receive_text.side_effect = slow_receive |
| 181 | + |
| 182 | + with ( |
| 183 | + patch("openhands.agent_server.sockets.get_default_config") as mock_config, |
| 184 | + patch( |
| 185 | + "openhands.agent_server.sockets._FIRST_MESSAGE_AUTH_TIMEOUT_SECONDS", 0.05 |
| 186 | + ), |
| 187 | + ): |
| 188 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 189 | + result = await _accept_authenticated_websocket(ws, session_api_key=None) |
| 190 | + |
| 191 | + assert result is False |
| 192 | + ws.close.assert_called_once_with(code=4001, reason="Authentication failed") |
| 193 | + |
| 194 | + |
| 195 | +# -- End-to-end: first-message auth through events_socket -- |
| 196 | + |
| 197 | + |
| 198 | +@pytest.mark.asyncio |
| 199 | +async def test_events_socket_first_message_auth_e2e(): |
| 200 | + """First-message auth works end-to-end through the events_socket endpoint.""" |
| 201 | + from openhands.agent_server.event_service import EventService |
| 202 | + from openhands.agent_server.sockets import events_socket |
| 203 | + |
| 204 | + ws = _make_mock_websocket() |
| 205 | + # First call: auth message (read by _accept_authenticated_websocket via receive_text) |
| 206 | + # Then receive_json calls: disconnect |
| 207 | + ws.receive_text.return_value = json.dumps( |
| 208 | + {"type": "auth", "session_api_key": "sk-oh-valid"} |
| 209 | + ) |
| 210 | + ws.receive_json.side_effect = WebSocketDisconnect() |
| 211 | + |
| 212 | + mock_event_service = MagicMock(spec=EventService) |
| 213 | + mock_event_service.subscribe_to_events = AsyncMock(return_value=uuid4()) |
| 214 | + mock_event_service.unsubscribe_from_events = AsyncMock(return_value=True) |
| 215 | + |
| 216 | + with ( |
| 217 | + patch( |
| 218 | + "openhands.agent_server.sockets.conversation_service" |
| 219 | + ) as mock_conv_service, |
| 220 | + patch("openhands.agent_server.sockets.get_default_config") as mock_config, |
| 221 | + ): |
| 222 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 223 | + mock_conv_service.get_event_service = AsyncMock( |
| 224 | + return_value=mock_event_service |
| 225 | + ) |
| 226 | + |
| 227 | + await events_socket(uuid4(), ws, session_api_key=None) |
| 228 | + |
| 229 | + ws.accept.assert_called_once() |
| 230 | + mock_event_service.subscribe_to_events.assert_called_once() |
| 231 | + mock_event_service.unsubscribe_from_events.assert_called_once() |
| 232 | + |
| 233 | + |
| 234 | +@pytest.mark.asyncio |
| 235 | +async def test_events_socket_first_message_auth_rejected(): |
| 236 | + """events_socket returns early when first-message auth fails.""" |
| 237 | + from openhands.agent_server.sockets import events_socket |
| 238 | + |
| 239 | + ws = _make_mock_websocket() |
| 240 | + ws.receive_text.return_value = json.dumps( |
| 241 | + {"type": "auth", "session_api_key": "sk-oh-wrong"} |
| 242 | + ) |
| 243 | + |
| 244 | + with patch("openhands.agent_server.sockets.get_default_config") as mock_config: |
| 245 | + mock_config.return_value.session_api_keys = ["sk-oh-valid"] |
| 246 | + |
| 247 | + await events_socket(uuid4(), ws, session_api_key=None) |
| 248 | + |
| 249 | + ws.accept.assert_called_once() |
| 250 | + # Should not proceed to subscribe |
| 251 | + ws.receive_json.assert_not_called() |
0 commit comments