-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathtest_auth_middleware.py
More file actions
392 lines (311 loc) · 14.3 KB
/
Copy pathtest_auth_middleware.py
File metadata and controls
392 lines (311 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
"""Unit tests for auth_middleware allow/deny logic."""
from __future__ import annotations
from unittest.mock import AsyncMock, MagicMock
import pytest
from fastapi.responses import JSONResponse
from starlette.responses import RedirectResponse
from tinyagentos.auth_middleware import (
AuthMiddleware,
_is_agent_canvas_path,
_is_exempt,
_is_loopback_client,
)
def _request(
*,
method: str = "GET",
path: str = "/api/system",
headers: dict[str, str] | None = None,
cookies: dict[str, str] | None = None,
client_host: str | None = "203.0.113.5",
auth_mgr: MagicMock | None = None,
) -> MagicMock:
req = MagicMock()
req.method = method
req.url.path = path
req.headers = headers or {}
req.cookies = cookies or {}
if client_host is None:
req.client = None
else:
req.client = MagicMock(host=client_host)
req.app.state.auth = auth_mgr or MagicMock()
return req
def _default_auth_mgr(*, configured: bool = True) -> MagicMock:
mgr = MagicMock()
mgr.is_configured.return_value = configured
mgr.validate_local_token.return_value = False
mgr.validate_session.return_value = None
mgr.get_primary_user.return_value = None
mgr.get_user_by_id.return_value = None
return mgr
class TestIsExempt:
def test_exact_exempt_paths(self):
for path in ("/api/health", "/auth/login", "/desktop/index.html"):
assert _is_exempt("GET", path) is True
def test_exempt_prefixes(self):
assert _is_exempt("GET", "/static/app.css") is True
assert _is_exempt("GET", "/desktop/bundle.js") is True
assert _is_exempt("GET", "/ws/chat") is True
def test_auth_request_create_exempt(self):
assert _is_exempt("POST", "/api/agents/auth-requests") is True
def test_auth_request_status_poll_exempt(self):
assert _is_exempt("GET", "/api/agents/auth-requests/req-123") is True
def test_auth_request_approve_not_exempt(self):
assert _is_exempt("POST", "/api/agents/auth-requests/req-123/approve") is False
def test_auth_request_list_not_exempt(self):
assert _is_exempt("GET", "/api/agents/auth-requests") is False
def test_cluster_pairing_exempt(self):
assert _is_exempt("POST", "/api/cluster/pairing/announce") is True
assert _is_exempt("POST", "/api/cluster/pairing/claim") is True
def test_cluster_workers_and_heartbeat_exempt(self):
assert _is_exempt("GET", "/api/cluster/workers") is True
assert _is_exempt("POST", "/api/cluster/workers") is True
assert _is_exempt("POST", "/api/cluster/heartbeat") is True
def test_protected_api_not_exempt(self):
assert _is_exempt("GET", "/api/system") is False
class TestIsLoopbackClient:
def test_ipv4_loopback(self):
assert _is_loopback_client(_request(client_host="127.0.0.1")) is True
def test_ipv6_loopback(self):
assert _is_loopback_client(_request(client_host="::1")) is True
def test_remote_client(self):
assert _is_loopback_client(_request(client_host="203.0.113.5")) is False
def test_missing_client(self):
assert _is_loopback_client(_request(client_host=None)) is False
def test_invalid_host(self):
assert _is_loopback_client(_request(client_host="not-an-ip")) is False
class TestAuthMiddlewareDispatch:
@pytest.mark.asyncio
async def test_exempt_path_passes_without_auth(self):
middleware = AuthMiddleware(app=MagicMock())
req = _request(path="/api/health")
call_next = AsyncMock(return_value=JSONResponse({"ok": True}))
resp = await middleware.dispatch(req, call_next)
assert resp.status_code == 200
assert req.state.via == "exempt"
assert req.state.user_id is None
call_next.assert_awaited_once()
@pytest.mark.asyncio
async def test_unconfigured_api_returns_onboarding_401(self):
middleware = AuthMiddleware(app=MagicMock())
req = _request(path="/api/system", auth_mgr=_default_auth_mgr(configured=False))
call_next = AsyncMock()
resp = await middleware.dispatch(req, call_next)
assert resp.status_code == 401
assert resp.body == b'{"error":"onboarding_required","needs_onboarding":true}'
call_next.assert_not_awaited()
@pytest.mark.asyncio
async def test_unconfigured_html_redirects_to_setup(self):
middleware = AuthMiddleware(app=MagicMock())
req = _request(
path="/",
headers={"accept": "text/html"},
auth_mgr=_default_auth_mgr(configured=False),
)
call_next = AsyncMock()
resp = await middleware.dispatch(req, call_next)
assert isinstance(resp, RedirectResponse)
assert resp.status_code == 303
assert resp.headers["location"] == "/auth/setup"
call_next.assert_not_awaited()
@pytest.mark.asyncio
async def test_valid_session_passes(self):
middleware = AuthMiddleware(app=MagicMock())
auth_mgr = _default_auth_mgr()
auth_mgr.validate_session.return_value = "user-1"
auth_mgr.get_user_by_id.return_value = {"id": "user-1", "is_admin": True}
req = _request(
path="/api/system",
cookies={"taos_session": "sess-token"},
auth_mgr=auth_mgr,
)
call_next = AsyncMock(return_value=JSONResponse({"ok": True}))
resp = await middleware.dispatch(req, call_next)
assert resp.status_code == 200
assert req.state.user_id == "user-1"
assert req.state.is_admin is True
assert req.state.via == "session"
call_next.assert_awaited_once()
@pytest.mark.asyncio
async def test_missing_session_api_returns_401(self):
middleware = AuthMiddleware(app=MagicMock())
req = _request(
path="/api/system",
headers={"accept": "application/json"},
auth_mgr=_default_auth_mgr(),
)
call_next = AsyncMock()
resp = await middleware.dispatch(req, call_next)
assert resp.status_code == 401
assert resp.body == b'{"error":"Authentication required"}'
call_next.assert_not_awaited()
@pytest.mark.asyncio
async def test_missing_session_html_redirects_to_login(self):
middleware = AuthMiddleware(app=MagicMock())
req = _request(
path="/settings",
headers={"accept": "text/html"},
auth_mgr=_default_auth_mgr(),
)
call_next = AsyncMock()
resp = await middleware.dispatch(req, call_next)
assert isinstance(resp, RedirectResponse)
assert resp.status_code == 303
assert resp.headers["location"] == "/auth/login?next=/settings"
call_next.assert_not_awaited()
@pytest.mark.asyncio
async def test_valid_local_token_with_primary_user(self):
middleware = AuthMiddleware(app=MagicMock())
auth_mgr = _default_auth_mgr()
auth_mgr.validate_local_token.return_value = True
auth_mgr.get_primary_user.return_value = {"id": "admin-1"}
req = _request(
path="/api/system",
headers={"authorization": "Bearer local-secret"},
auth_mgr=auth_mgr,
)
call_next = AsyncMock(return_value=JSONResponse({"ok": True}))
resp = await middleware.dispatch(req, call_next)
assert resp.status_code == 200
assert req.state.user_id == "admin-1"
assert req.state.is_admin is True
assert req.state.via == "local_token"
call_next.assert_awaited_once()
@pytest.mark.asyncio
async def test_registry_feed_bearer_bypasses_session_gate(self):
middleware = AuthMiddleware(app=MagicMock())
auth_mgr = _default_auth_mgr()
auth_mgr.validate_local_token.return_value = False
req = _request(
path="/api/agents/registry/grants",
headers={"authorization": "Bearer registry-jwt"},
auth_mgr=auth_mgr,
)
call_next = AsyncMock(return_value=JSONResponse({"grants": []}))
resp = await middleware.dispatch(req, call_next)
assert resp.status_code == 200
assert req.state.via == "registry_jwt_candidate"
call_next.assert_awaited_once()
@pytest.mark.asyncio
async def test_prepare_shutdown_allowed_from_loopback(self):
middleware = AuthMiddleware(app=MagicMock())
req = _request(
method="POST",
path="/api/system/prepare-shutdown",
client_host="127.0.0.1",
auth_mgr=_default_auth_mgr(),
)
call_next = AsyncMock(return_value=JSONResponse({"status": "ready"}))
resp = await middleware.dispatch(req, call_next)
assert resp.status_code == 200
assert req.state.via == "loopback"
call_next.assert_awaited_once()
@pytest.mark.asyncio
async def test_prepare_shutdown_denied_from_remote(self):
middleware = AuthMiddleware(app=MagicMock())
req = _request(
method="POST",
path="/api/system/prepare-shutdown",
client_host="203.0.113.5",
headers={"accept": "application/json"},
auth_mgr=_default_auth_mgr(),
)
call_next = AsyncMock()
resp = await middleware.dispatch(req, call_next)
assert resp.status_code == 401
call_next.assert_not_awaited()
class TestIsAgentCanvasPath:
def test_list_elements_get_allowed(self):
assert _is_agent_canvas_path("GET", "/api/projects/proj-1/canvas/elements") is True
def test_create_element_post_allowed(self):
assert _is_agent_canvas_path("POST", "/api/projects/proj-1/canvas/elements") is True
def test_delete_element_allowed(self):
assert _is_agent_canvas_path("DELETE", "/api/projects/proj-1/canvas/elements/el-1") is True
def test_snapshot_png_allowed(self):
assert _is_agent_canvas_path("GET", "/api/projects/proj-1/canvas/snapshot.png") is True
def test_snapshot_tldr_allowed(self):
assert _is_agent_canvas_path("GET", "/api/projects/proj-1/canvas/snapshot.tldr") is True
def test_stream_allowed(self):
assert _is_agent_canvas_path("GET", "/api/projects/proj-1/canvas/stream") is True
def test_update_element_patch_allowed(self):
# canvas_write-bound agents may PATCH an element (create + update +
# delete all live under canvas_write), so the route is on the allowlist.
assert _is_agent_canvas_path("PATCH", "/api/projects/proj-1/canvas/elements/el-1") is True
def test_permissions_patch_not_allowed(self):
assert _is_agent_canvas_path("PATCH", "/api/projects/proj-1/canvas/permissions/agent-1") is False
def test_extra_path_segment_not_allowed(self):
assert _is_agent_canvas_path("GET", "/api/projects/proj-1/canvas/elements/el-1/extra") is False
def test_wrong_method_not_allowed(self):
assert _is_agent_canvas_path("POST", "/api/projects/proj-1/canvas/elements/el-1") is False
def test_nested_element_path_not_allowed(self):
assert _is_agent_canvas_path("GET", "/api/projects/proj-1/canvas/elements/x/y") is False
def test_single_element_get_not_allowed(self):
# There is no GET /elements/{id} route in the allowlist; only the
# collection GET and the DELETE of a single element are permitted.
assert _is_agent_canvas_path("GET", "/api/projects/proj-1/canvas/elements/el-1") is False
def test_snapshot_dot_is_literal_not_wildcard(self):
# The dot in snapshot.png / snapshot.tldr must be a literal, not a regex
# wildcard: a near-miss like snapshotXpng must NOT slip through the
# agent-token allowlist onto a session-only surface.
assert _is_agent_canvas_path("GET", "/api/projects/proj-1/canvas/snapshotXpng") is False
assert _is_agent_canvas_path("GET", "/api/projects/proj-1/canvas/snapshot_png") is False
assert _is_agent_canvas_path("GET", "/api/projects/proj-1/canvas/snapshotXtldr") is False
class TestCanvasAgentTokenDispatch:
@pytest.mark.asyncio
async def test_canvas_list_elements_bearer_passes(self):
middleware = AuthMiddleware(app=MagicMock())
auth_mgr = _default_auth_mgr()
auth_mgr.validate_local_token.return_value = False
req = _request(
method="GET",
path="/api/projects/proj-1/canvas/elements",
headers={"authorization": "Bearer registry-jwt"},
auth_mgr=auth_mgr,
)
call_next = AsyncMock(return_value=JSONResponse({"elements": []}))
resp = await middleware.dispatch(req, call_next)
assert resp.status_code == 200
assert req.state.via == "registry_jwt_candidate"
call_next.assert_awaited_once()
@pytest.mark.asyncio
async def test_canvas_delete_element_bearer_passes(self):
middleware = AuthMiddleware(app=MagicMock())
auth_mgr = _default_auth_mgr()
auth_mgr.validate_local_token.return_value = False
req = _request(
method="DELETE",
path="/api/projects/proj-1/canvas/elements/el-1",
headers={"authorization": "Bearer registry-jwt"},
auth_mgr=auth_mgr,
)
call_next = AsyncMock(return_value=JSONResponse({"ok": True}))
resp = await middleware.dispatch(req, call_next)
assert resp.status_code == 200
assert req.state.via == "registry_jwt_candidate"
call_next.assert_awaited_once()
@pytest.mark.asyncio
async def test_canvas_permissions_patch_requires_session(self):
middleware = AuthMiddleware(app=MagicMock())
req = _request(
method="PATCH",
path="/api/projects/proj-1/canvas/permissions/agent-1",
headers={"authorization": "Bearer registry-jwt"},
auth_mgr=_default_auth_mgr(),
)
call_next = AsyncMock()
resp = await middleware.dispatch(req, call_next)
assert resp.status_code == 401
call_next.assert_not_awaited()
@pytest.mark.asyncio
async def test_canvas_extra_segment_requires_session(self):
middleware = AuthMiddleware(app=MagicMock())
req = _request(
method="GET",
path="/api/projects/proj-1/canvas/elements/el-1/extra",
headers={"authorization": "Bearer registry-jwt"},
auth_mgr=_default_auth_mgr(),
)
call_next = AsyncMock()
resp = await middleware.dispatch(req, call_next)
assert resp.status_code == 401
call_next.assert_not_awaited()