Personal AI, On Personal Devices
Python
feat: non-loopback auth enforcement and CORS origin passthrough
Jon Saad-Falcon8e75fcb2321f
diff --git a/src/openjarvis/server/app.py b/src/openjarvis/server/app.pyindex 819fc85e..549e589c 100644--- a/src/openjarvis/server/app.py+++ b/src/openjarvis/server/app.py@@ -153,6 +153,7 @@ def create_app(agent_scheduler=None,api_key: str = "",webhook_config: dict | None = None,+ cors_origins: list[str] | None = None,) -> FastAPI:"""Create and configure the FastAPI application.@@ -179,9 +180,10 @@ def create_app(from fastapi.middleware.cors import CORSMiddleware+ _origins = cors_origins if cors_origins is not None else ["*"]app.add_middleware(CORSMiddleware,- allow_origins=["*"],+ allow_origins=_origins,allow_credentials=True,allow_methods=["*"],allow_headers=["*"],diff --git a/src/openjarvis/server/auth_middleware.py b/src/openjarvis/server/auth_middleware.pyindex 7489428d..52e79b59 100644--- a/src/openjarvis/server/auth_middleware.py+++ b/src/openjarvis/server/auth_middleware.py@@ -56,3 +56,26 @@ class AuthMiddleware(BaseHTTPMiddleware):def generate_api_key() -> str:"""Generate a new API key with ``oj_sk_`` prefix."""return f"oj_sk_{secrets.token_urlsafe(32)}"+++def check_bind_safety(host: str, *, api_key: str) -> None:+ """Refuse to bind non-loopback without an API key.++ Raises ``SystemExit`` if *host* is not a loopback address and+ *api_key* is empty.+ """+ import ipaddress+ import sys++ try:+ is_loop = ipaddress.ip_address(host).is_loopback+ except ValueError:+ is_loop = host in ("localhost", "")++ if not is_loop and not api_key:+ logger.error(+ "Binding to %s requires OPENJARVIS_API_KEY to be set. "+ "Run: jarvis auth generate-key",+ host,+ )+ sys.exit(1)