Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(server): require API key for repo health coordinator route (#1405)

Akshat malik90d1b6bb63d1
diff --git a/packages/server/src/repowise/server/routers/health.py b/packages/server/src/repowise/server/routers/health.py
index 2a64b02c1..4e52f66ef 100644
--- a/packages/server/src/repowise/server/routers/health.py
+++ b/packages/server/src/repowise/server/routers/health.py
@@ -14,7 +14,7 @@ from repowise.core.persistence.coordinator import AtomicStorageCoordinator
from repowise.core.persistence.database import get_session
from repowise.core.persistence.models import GenerationJob, Page
from repowise.server import __version__
-from repowise.server.deps import get_db_session, get_vector_store
+from repowise.server.deps import get_db_session, get_vector_store, verify_api_key
from repowise.server.schemas import CoordinatorHealthResponse, HealthResponse
router = APIRouter(tags=["health"])
@@ -79,7 +79,9 @@ async def metrics(request: Request) -> str:
return Response(content="\n".join(lines) + "\n", media_type="text/plain")
-_repo_health_router = APIRouter(prefix="/api/repos", tags=["health"])
+_repo_health_router = APIRouter(
+ prefix="/api/repos", tags=["health"], dependencies=[Depends(verify_api_key)]
+)
@_repo_health_router.get("/{repo_id}/health/coordinator", response_model=CoordinatorHealthResponse)
diff --git a/tests/unit/server/test_auth.py b/tests/unit/server/test_auth.py
index 69036c864..26faaa938 100644
--- a/tests/unit/server/test_auth.py
+++ b/tests/unit/server/test_auth.py
@@ -120,3 +120,70 @@ async def test_health_bypasses_auth(client: AsyncClient) -> None:
assert resp.status_code == 200
finally:
deps_mod._API_KEY = original
+
+
+# ---------------------------------------------------------------------------
+# Regression tests for fix/coordinator-health-auth (#1399)
+# Verify that /api/repos/{id}/health/coordinator is protected by verify_api_key.
+# ---------------------------------------------------------------------------
+
+
+async def test_coordinator_health_requires_api_key(
+ remote_client: AsyncClient,
+) -> None:
+ """GET /api/repos/{id}/health/coordinator returns 401 when no token is sent.
+
+ Regression guard for #1399: _repo_health_router must carry
+ Depends(verify_api_key) so that this route shares the same auth posture as
+ every other /api/ route and cannot be read anonymously from the network.
+
+ A fake repo_id is intentional: verify_api_key is a router-level dependency
+ that runs before the route handler, so the auth rejection happens before the
+ DB is ever consulted. The repo does not need to exist.
+ """
+ import repowise.server.deps as deps_mod
+
+ original = deps_mod._API_KEY
+ deps_mod._API_KEY = "test-secret-key"
+ try:
+ resp = await remote_client.get("/api/repos/nonexistent-repo/health/coordinator")
+ assert resp.status_code == 401, resp.text
+ assert "Missing API key" in resp.json()["detail"]
+ finally:
+ deps_mod._API_KEY = original
+
+
+async def test_coordinator_health_accepts_correct_key(
+ app,
+ remote_client: AsyncClient,
+ tmp_path,
+) -> None:
+ """GET /api/repos/{id}/health/coordinator returns 200 with the correct bearer token.
+
+ Companion to test_coordinator_health_requires_api_key: ensures the route is
+ usable once a valid key is presented, so the auth guard does not
+ inadvertently break legitimate callers.
+ """
+ import repowise.server.deps as deps_mod
+ from tests.unit.server.conftest import create_test_repo
+
+ original = deps_mod._API_KEY
+ deps_mod._API_KEY = "test-secret-key"
+ try:
+ async with AsyncClient(
+ transport=ASGITransport(app=app),
+ base_url="http://test",
+ headers={"Authorization": "Bearer test-secret-key"},
+ ) as local:
+ repo = await create_test_repo(local, tmp_path)
+ repo_id = repo["id"]
+
+ resp = await remote_client.get(
+ f"/api/repos/{repo_id}/health/coordinator",
+ headers={"Authorization": "Bearer test-secret-key"},
+ )
+ assert resp.status_code == 200, resp.text
+ finally:
+ deps_mod._API_KEY = original