Skip to content

Personal AI, On Personal Devices

Python

security(server): require auth for /openapi.json, /docs and /redoc (#1014)

clawdovil-agent-bvef005703e5f4
diff --git a/src/openjarvis/server/auth_middleware.py b/src/openjarvis/server/auth_middleware.py
index 0b555373..1eae310d 100644
--- a/src/openjarvis/server/auth_middleware.py
+++ b/src/openjarvis/server/auth_middleware.py
@@ -72,17 +72,27 @@ class AuthMiddleware(BaseHTTPMiddleware):
@staticmethod
def _requires_auth(path: str) -> bool:
- """Protect API routes and operational metrics; leave the UI/health open.
+ """Protect API routes, metrics and API docs; leave the UI/health open.
``/metrics`` exposes request/token counters that should not be readable
by unauthenticated clients, so it is gated alongside ``/v1`` and
``/api``. ``/health`` stays open for liveness probes.
+
+ The OpenAPI schema and its viewers (``/openapi.json``, ``/docs``,
+ ``/redoc``) are gated too: on an exposed deployment they hand an
+ unauthenticated client a complete map of every route, parameter and
+ payload shape — useful reconnaissance for probing the authenticated
+ endpoints. This only applies when an API key is configured at all, so
+ keyless local use is unchanged.
"""
return (
path.startswith("/v1/")
or path.startswith("/api/")
or path == "/metrics"
or path.startswith("/metrics/")
+ or path in ("/openapi.json", "/docs", "/redoc")
+ or path.startswith("/docs/")
+ or path.startswith("/redoc/")
)
diff --git a/tests/server/test_auth_middleware.py b/tests/server/test_auth_middleware.py
index fa13b087..bfad89f2 100644
--- a/tests/server/test_auth_middleware.py
+++ b/tests/server/test_auth_middleware.py
@@ -107,3 +107,37 @@ class TestAuthMiddleware:
def test_non_preflight_options_still_requires_auth(self, client, headers):
resp = client.options("/v1/models", headers=headers)
assert resp.status_code == 401
+
+
+class TestApiDocsAreGated:
+ """The OpenAPI schema and its viewers must not be readable unauthenticated.
+
+ On an exposed deployment they hand an attacker a complete map of every
+ route and payload shape, which is useful reconnaissance even though the
+ endpoints themselves are protected.
+ """
+
+ @pytest.mark.parametrize("path", ["/openapi.json", "/docs", "/redoc"])
+ def test_docs_require_auth(self, path):
+ app = _make_app("oj_sk_test123")
+ with TestClient(app) as c:
+ assert c.get(path).status_code == 401
+
+ @pytest.mark.parametrize("path", ["/openapi.json", "/docs"])
+ def test_docs_reachable_with_key(self, path):
+ app = _make_app("oj_sk_test123")
+ with TestClient(app) as c:
+ r = c.get(path, headers={"Authorization": "Bearer oj_sk_test123"})
+ assert r.status_code == 200
+
+ @pytest.mark.parametrize("path", ["/openapi.json", "/docs"])
+ def test_docs_open_when_no_key_configured(self, path):
+ """Keyless local use is unchanged — the middleware no-ops without a key."""
+ app = _make_app("")
+ with TestClient(app) as c:
+ assert c.get(path).status_code == 200
+
+ def test_health_stays_open(self):
+ app = _make_app("oj_sk_test123")
+ with TestClient(app) as c:
+ assert c.get("/health").status_code == 200