Fix critical and high audit findings
Fixed findings from the audit reports (SECURITY, QUALITY, PERFORMANCE): - P-001 · Dashboard re-downloads the entire dataset every 5 seconds Re-synced the vendored shared/ snapshot from shared commit 199a5ef. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7dd0d34383
commit
a84a36349b
31 files changed
+653
-6055
No files matched your search
@@ -94,6 +94,7 @@ ERR_FEATURE_REQUIRES_AUTHGUARD = "This feature requires AUTHGUARD_URL to be conf
|
||||
# rather than a generic "internal error" indistinguishable from any other bug.
|
||||
ERR_AUTHGUARD_UNAVAILABLE = "Unable to reach the authentication service. Please try again shortly."
|
||||
ERR_SECRET_NOT_SET = "SECRET environment variable is not set."
|
||||
ERR_ADMIN_REQUIRED = "Administrator access required."
|
||||
|
||||
# _LOCAL_PROFILE is the fixed placeholder identity every handler reports when
|
||||
# authentication is disabled, either because AUTHGUARD_URL is disabled or
|
||||
@@ -338,6 +339,23 @@ def build_auth_router(
|
||||
raise HTTPException(status_code=401, detail=ERR_AUTH_REQUIRED)
|
||||
return token
|
||||
|
||||
async def require_admin(
|
||||
request: Request,
|
||||
client: Optional[AuthClient] = Depends(get_client), # noqa: B008
|
||||
) -> PublicUser:
|
||||
"""FastAPI dependency gating a route behind an administrator session
|
||||
(the Python counterpart of Go's RequireAdmin). Unlike require_session it
|
||||
never falls back to _LOCAL_PROFILE, so a route that changes the
|
||||
login-required flag cannot be called anonymously, even while login is
|
||||
already disabled."""
|
||||
if not _enabled(client):
|
||||
raise HTTPException(status_code=401, detail=ERR_AUTH_REQUIRED)
|
||||
assert client is not None
|
||||
profile, _ = await _current_profile_or_401(request, client)
|
||||
if not can_manage_users(profile.role):
|
||||
raise HTTPException(status_code=403, detail=ERR_ADMIN_REQUIRED)
|
||||
return profile
|
||||
|
||||
# --- status / me -------------------------------------------------------
|
||||
|
||||
@router.get("/me")
|
||||
@@ -561,6 +579,7 @@ def build_auth_router(
|
||||
async def update_access_settings(
|
||||
req: UpdateAccessRequest,
|
||||
store: ProxyStore = Depends(get_store), # noqa: B008
|
||||
_admin: PublicUser = Depends(require_admin), # noqa: B008
|
||||
) -> dict[str, bool]:
|
||||
nonlocal login_required_override
|
||||
await store.set_login_required(req.loginRequired)
|
||||
|
||||
@@ -4,7 +4,13 @@ from fastapi import FastAPI, Request
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from shared_backend import authproxy as ap
|
||||
from shared_backend.authclient import APIError, AuthClient, PublicUser, SessionResult
|
||||
from shared_backend.authclient import (
|
||||
DEFAULT_COOKIE_NAME,
|
||||
APIError,
|
||||
AuthClient,
|
||||
PublicUser,
|
||||
SessionResult,
|
||||
)
|
||||
|
||||
|
||||
class _FakeStore(ap.ProxyStore):
|
||||
@@ -113,14 +119,32 @@ class AuthDisabledModeTests(unittest.TestCase):
|
||||
|
||||
|
||||
class AccessSettingsTests(unittest.TestCase):
|
||||
def test_get_and_put_round_trip_through_the_store(self) -> None:
|
||||
def test_get_is_public_and_put_requires_an_admin_session(self) -> None:
|
||||
store = _FakeStore(login_required=True)
|
||||
client = TestClient(_build_app(store))
|
||||
client = TestClient(_build_app(store, _RoleClient()))
|
||||
self.assertEqual(client.get("/api/auth/access").json(), {"loginRequired": True})
|
||||
|
||||
response = client.put("/api/auth/access", json={"loginRequired": False})
|
||||
self.assertEqual(response.status_code, 401)
|
||||
self.assertEqual(client.get("/api/auth/access").json(), {"loginRequired": True})
|
||||
|
||||
client.cookies.set(DEFAULT_COOKIE_NAME, "user-token")
|
||||
response = client.put("/api/auth/access", json={"loginRequired": False})
|
||||
self.assertEqual(response.status_code, 403)
|
||||
self.assertEqual(client.get("/api/auth/access").json(), {"loginRequired": True})
|
||||
|
||||
client.cookies.set(DEFAULT_COOKIE_NAME, "admin-token")
|
||||
response = client.put("/api/auth/access", json={"loginRequired": False})
|
||||
self.assertEqual(response.json(), {"loginRequired": False})
|
||||
self.assertEqual(client.get("/api/auth/access").json(), {"loginRequired": False})
|
||||
|
||||
def test_anonymous_put_is_refused_when_authguard_is_not_configured(self) -> None:
|
||||
store = _FakeStore(login_required=True)
|
||||
client = TestClient(_build_app(store))
|
||||
response = client.put("/api/auth/access", json={"loginRequired": False})
|
||||
self.assertEqual(response.status_code, 401)
|
||||
self.assertEqual(client.get("/api/auth/access").json(), {"loginRequired": True})
|
||||
|
||||
def test_internal_update_disables_login_and_uses_admin_identity(self) -> None:
|
||||
store = _FakeStore(login_required=True)
|
||||
client = TestClient(
|
||||
@@ -180,6 +204,16 @@ class _FakeClient(AuthClient):
|
||||
return ["auth_session=; Path=/; Max-Age=0"]
|
||||
|
||||
|
||||
class _RoleClient(_FakeClient):
|
||||
"""Resolves two fixed session tokens to an administrator and an ordinary user."""
|
||||
|
||||
async def me(self, token: str) -> PublicUser:
|
||||
roles = {"admin-token": "superadmin", "user-token": "user"}
|
||||
if token not in roles:
|
||||
raise APIError(401, "unauthenticated")
|
||||
return PublicUser(id=1, username="a", email="a@b.com", role=roles[token])
|
||||
|
||||
|
||||
class EnabledModeSetupAndLoginTests(unittest.TestCase):
|
||||
"""Exercises the real (client is not None) path, including the
|
||||
setup-claim flag's interaction with the store."""
|
||||
|
||||
Reference in new issue
Block a user