Repository navigation
Description
fix(security): add require_admin to mutation endpoints in topology, catalog, and config routers
Symptom
In the default Rackscope configuration (auth.enabled=false, auth.trusted_networks=[]), any client that can reach the backend API can perform destructive mutations on the topology, catalog, and setup wizard without any authentication or IP-based gating.
Concretely, the following call succeeds today against a default-config instance:
curl -X DELETE http://<host>/api/sites/foo
curl -X PUT http://<host>/api/catalog/templates -d '...'
curl -X POST http://<host>/api/setup/wizard/disableThis means a network-adjacent attacker (or a curious user, or a misbehaving script) can wipe the entire topology, corrupt the hardware template catalog, or silently disable the setup wizard.
Technical Analysis
The pattern for protecting mutation endpoints already exists in the codebase and is correctly applied in some routers:
| Router | require_admin used? |
|---|---|
src/rackscope/api/routers/config.py |
✅ on PUT /api/config (line 84) |
src/rackscope/api/routers/auth.py |
✅ on credential changes |
src/rackscope/api/routers/system.py |
✅ on POST /api/system/restart |
src/rackscope/api/routers/logs.py |
✅ on all log endpoints |
src/rackscope/api/routers/topology.py |
❌ 0 occurrences (14 mutation endpoints exposed) |
src/rackscope/api/routers/catalog.py |
❌ 0 occurrences (4 mutation endpoints exposed) |
src/rackscope/api/routers/config.py wizard/disable |
❌ missing on POST /api/setup/wizard/disable (line 213) |
The require_admin dependency (src/rackscope/api/dependencies.py:163) is intentionally non-breaking in default config:
def require_admin(request: Request) -> None:
if APP_CONFIG and APP_CONFIG.auth.enabled:
if not getattr(request.state, "user", None):
raise HTTPException(status_code=401, detail="Not authenticated")
return
networks = APP_CONFIG.auth.trusted_networks if APP_CONFIG else []
if not networks:
return # ← default config: allowWhen auth.enabled=false and trusted_networks=[] (defaults), require_admin returns None without raising. Adding the dependency to mutation endpoints is therefore non-breaking for existing default-config users, while immediately providing protection for users who flip auth.enabled=true or configure trusted_networks.
Proposed Fix
1. src/rackscope/api/routers/topology.py — add dependency to all 14 mutation endpoints
Apply the pattern already used in config.py:84. Each mutation decorator gains a dependencies=[Depends(require_admin)] argument:
from rackscope.api.dependencies import get_app_config, get_catalog, get_topology, require_admin
@router.post("/api/sites", dependencies=[Depends(require_admin)])
async def create_site(...):
...Endpoints to update:
POST /api/sitesPUT /api/sites/{site_id}DELETE /api/sites/{site_id}POST /api/sites/{site_id}/roomsPUT /api/sites/{site_id}/rooms/{room_id}DELETE /api/sites/{site_id}/rooms/{room_id}POST /api/sites/{site_id}/rooms/{room_id}/aislesPUT /api/sites/{site_id}/rooms/{room_id}/aisles/{aisle_id}DELETE /api/sites/{site_id}/rooms/{room_id}/aisles/{aisle_id}POST /api/racksPUT /api/racks/{rack_id}DELETE /api/racks/{rack_id}PUT /api/racks/{rack_id}/devices- (verify the actual list against
grep -E "@router\.(post|put|delete)" src/rackscope/api/routers/topology.pybefore merge — 14 was the count at audit time, may have evolved)
2. src/rackscope/api/routers/catalog.py — add dependency to all 4 mutation endpoints
POST /api/catalog/templatesPUT /api/catalog/templatesDELETE /api/catalog/templates/device/{id}- (and any other mutation endpoint present at merge time)
3. src/rackscope/api/routers/config.py:213 — add dependency to disable_setup_wizard
@router.post("/setup/wizard/disable", dependencies=[Depends(require_admin)])
async def disable_setup_wizard(...):4. src/rackscope/api/routers/checks.py — add dependency on mutation endpoints
Although not in the original SEC-01/SEC-03 scope, the audit confirmed checks.py also exposes mutation endpoints (PUT /api/checks/files/{name}, DELETE /api/checks/files/{name}) without require_admin. Include them in the same fix to avoid leaving an asymmetric protection surface.
Test Checklist
-
pytest tests/passes unchanged (no regression on existing tests) - Default config (
auth.enabled=false,trusted_networks=[]):curl -X DELETE http://localhost/api/sites/fooreturns 200/404 as before (non-regression) - Default config +
trusted_networks=["10.0.0.0/8"]from a non-listed IP: returns 403 on all newly protected endpoints -
auth.enabled=true+ no Bearer token: returns 401 on all newly protected endpoints -
auth.enabled=true+ valid admin JWT: succeeds on all newly protected endpoints -
make lintreturns 0 errors -
make typecheckreturns 0 errors - New explicit test added:
tests/security/test_require_admin_coverage.pyenumerates all@router.{post,put,delete}decorators acrosstopology.py,catalog.py,checks.py,config.pyand assertsrequire_adminis present (prevents regression when new endpoints are added)
Impact and Severity
- Audience affected: every deployment that exposes the Rackscope API to a network where untrusted clients exist — i.e., realistically anything beyond a single-user laptop demo. The audit established that the production
docker-compose.prod.ymlexposes the backend on port 8000 directly to the host network, multiplying the attack surface. - Severity: critical. Trivial topology destruction without authentication in the default configuration.
- Priority: must ship before any public exposure of v1.0.0. Recommended as the first PR of the v1.0.0 security hardening series.
Breaking Changes
None for default-config users. require_admin is a no-op when auth.enabled=false and trusted_networks=[] (current default). For users who have already configured trusted_networks or auth.enabled=true, the change is also non-breaking — those users already expect mutation endpoints to be gated; the fix simply makes the gating actually happen on the routers where it was missing.
Related
- Audit ref: SEC-01, SEC-03 in
AUDIT_ARCHITECTURAL.md - Audit ref: SEC-04 (TLS paths exposed in
GET /api/config) is a separate concern, not included in this PR — file-level exclusion in Pydantic dump, not an auth fix. - Blocks: Issue C1 (RBAC) and Issue B (API tokens) build on
require_adminand will later replace it withrequire_role(). This issue ensures the gating exists in the first place.
Out of Scope
- Adding
require_adminto read endpoints (intentionally left public — Rackscope's read-only API is a feature in default config). - Adding any new permission granularity. Roles and per-section permissions arrive in Issue C1.
- Refactoring
require_adminitself.
Activity