Spaces:
Sleeping
Sleeping
Donne120
Claude Opus 5
Require auth on admin read routes (was serving student PII anonymously)
a1054d8 Download test_admin_reads_require_auth.py from StudentCompanion/alu-chatbot: direct link, hf CLI and curl.
- Browser
- Download file 4.23 kB
-
https://huggingface.co/spaces/StudentCompanion/alu-chatbot/resolve/main/test_admin_reads_require_auth.py
- Command line
-
hf download hf://spaces/StudentCompanion/alu-chatbot/test_admin_reads_require_auth.py
-
curl -L -o test_admin_reads_require_auth.py https://huggingface.co/spaces/StudentCompanion/alu-chatbot/resolve/main/test_admin_reads_require_auth.py
4.23 kB
| """Admin read routes must not serve data to anonymous callers. | |
| The bug this guards, found live on 2026-09-09: `current_org_read` fell back to | |
| the default organization when no credentials were presented, documented as | |
| acceptable because these were "non-sensitive analytics reads". They were not β | |
| an unauthenticated GET to the production Space returned: | |
| /api/admin/students -> student names and email addresses | |
| /api/admin/curators -> staff names and calendar links | |
| /api/admin/overview -> organization id, name, student counts | |
| The fallback existed because the dashboard's Server Components fetched without | |
| a token. That constraint is gone: every screen is now a client component | |
| specifically so the admin's Firebase token is attached. | |
| These tests assert the property directly β no credentials, no data β rather | |
| than checking the implementation, so they survive a refactor of how auth is | |
| resolved. | |
| """ | |
| import pytest | |
| from fastapi.testclient import TestClient | |
| import admin_routes | |
| # Every admin GET that reads tenant data. /organizations is included even | |
| # though it already required a platform admin: it belongs to the same surface, | |
| # and a future edit could weaken it the same way. | |
| READ_ROUTES = [ | |
| "/api/admin/overview", | |
| "/api/admin/opportunities", | |
| "/api/admin/students", | |
| "/api/admin/curators", | |
| "/api/admin/inquiries", | |
| "/api/admin/bookings", | |
| "/api/admin/office-hours", | |
| "/api/admin/organizations", | |
| ] | |
| def client(): | |
| import app as app_module | |
| return TestClient(app_module.app) | |
| def test_anonymous_read_is_rejected(client, route): | |
| """No token must mean no data β not a fallback to the default org. | |
| 503 counts as refused: routes gated on a platform admin check reach the | |
| "auth unavailable" path when Firebase is unconfigured, as it is here. | |
| Production returns 401 for these β verified against the live Space. The | |
| forbidden outcome is 200, which is what the old fallback produced. | |
| """ | |
| response = client.get(route) | |
| assert response.status_code in (401, 403, 503), ( | |
| f"{route} returned {response.status_code} to an anonymous caller. " | |
| f"Admin reads expose student names and email addresses; an " | |
| f"unauthenticated caller must be refused, never served the default " | |
| f"organization's rows." | |
| ) | |
| assert response.status_code != 200, ( | |
| f"{route} SERVED DATA to an anonymous caller β this is the leak." | |
| ) | |
| def test_garbage_token_is_rejected(client, route): | |
| """A malformed bearer token must fail closed, not fall through to the | |
| anonymous path and get served anyway. | |
| Accepts 503 alongside 401/403: with no Firebase credentials configured | |
| (the case in this test environment) token verification cannot run and the | |
| route refuses with "auth unavailable". That is still failing closed, which | |
| is the property under test. Production, where Firebase IS configured, | |
| returns 401 here β verified against the live Space. | |
| What must never appear is 200: that would mean an unverifiable token was | |
| treated as good enough to serve tenant data. | |
| """ | |
| response = client.get(route, headers={"Authorization": "Bearer not-a-real-token"}) | |
| assert response.status_code in (401, 403, 503), ( | |
| f"{route} accepted an invalid bearer token ({response.status_code})" | |
| ) | |
| assert response.status_code != 200 | |
| def test_read_resolver_has_no_anonymous_fallback(): | |
| """The specific regression: current_org_read must refuse anonymous | |
| callers, exactly as current_org does. | |
| Asserted at the resolver rather than only through routes so the reason a | |
| route is protected stays legible β and so adding a new admin GET that uses | |
| this dependency inherits the guarantee. | |
| """ | |
| from fastapi import HTTPException | |
| with pytest.raises(HTTPException) as exc: | |
| admin_routes.current_org_read(cred=None, x_admin_token=None) | |
| assert exc.value.status_code in (401, 403), ( | |
| "current_org_read returned an organization to a caller with no " | |
| "credentials β this is the exact fallback that leaked student data." | |
| ) | |