Skip to content

Self-hosted AI workspace.

Python

Honor AUTH_ENABLED=false in route-level auth gate (#785)

tanmayraut45bc4dcf703b06
diff --git a/src/auth_helpers.py b/src/auth_helpers.py
index 56de954ad..0e9ca23f6 100644
--- a/src/auth_helpers.py
+++ b/src/auth_helpers.py
@@ -1,5 +1,6 @@
"""Shared auth helpers used by all route files."""
+import os
from typing import Optional
from fastapi import Request, HTTPException
@@ -9,11 +10,28 @@ def get_current_user(request: Request) -> Optional[str]:
return getattr(request.state, 'current_user', None)
+def _auth_disabled() -> bool:
+ """True when the operator has explicitly turned off auth via .env.
+ Mirrors the AUTH_ENABLED parse in app.py / core/middleware.py so the
+ three call sites agree on what "off" means."""
+ return os.getenv("AUTH_ENABLED", "true").lower() == "false"
+
+
def require_user(request: Request) -> str:
- """FastAPI dependency: reject unauthenticated callers, even if upstream
- middleware was bypassed (LOCALHOST_BYPASS, AUTH_ENABLED=false, SSRF from
- a sibling service). Returns the resolved username, or "" in unconfigured
- first-run mode when the caller is on loopback.
+ """FastAPI dependency: reject unauthenticated callers when the upstream
+ auth middleware was bypassed unexpectedly (e.g. SSRF from a sibling
+ service). Returns the resolved username, or "" in single-user / anonymous
+ modes where no username is available.
+
+ The three "" cases are:
+ 1. AUTH_ENABLED=false — the operator explicitly turned auth off.
+ The full /login flow is skipped (issue #622), so route-level
+ require_user must let the request through too instead of 401-ing
+ and forcing the browser to /login.
+ 2. Unconfigured first-run + loopback caller — pre-setup access from
+ localhost so the operator can hit the SPA before creating the
+ first admin.
+ 3. LOCALHOST_BYPASS=true + loopback caller — documented dev bypass.
Use this on routes that touch user data so middleware misconfig can't
open them up.
@@ -21,13 +39,27 @@ def require_user(request: Request) -> str:
u = get_current_user(request)
if u:
return u
+ # Operator-disabled auth: honor it at the route layer too. Without this,
+ # routes that depend on require_user 401, the front-end fetch wrapper
+ # redirects to /login, and the user sees a login page despite
+ # AUTH_ENABLED=false (issue #622). Docker / reverse-proxy deployments
+ # hit this because requests arrive from a non-loopback client.host, so
+ # the loopback fall-through below never fires.
+ if _auth_disabled():
+ return ""
auth_mgr = getattr(request.app.state, "auth_manager", None)
+ client = getattr(request, "client", None)
+ host = (client.host if client else "") or ""
+ is_loopback = host in ("127.0.0.1", "::1", "localhost")
+ # LOCALHOST_BYPASS=true is the dev-only "I'm on loopback, skip auth"
+ # switch. Mirror the middleware so routes don't 401 the same caller
+ # the middleware just let through.
+ if is_loopback and os.getenv("LOCALHOST_BYPASS", "false").lower() == "true":
+ return ""
if auth_mgr is not None and getattr(auth_mgr, "is_configured", False):
raise HTTPException(401, "Not authenticated")
# Unconfigured / first-run mode: only allow loopback callers.
- client = getattr(request, "client", None)
- host = (client.host if client else "") or ""
- if host in ("127.0.0.1", "::1", "localhost"):
+ if is_loopback:
return ""
raise HTTPException(401, "Not authenticated")
diff --git a/tests/test_security_regressions.py b/tests/test_security_regressions.py
index 1e5c77c98..4aa144315 100644
--- a/tests/test_security_regressions.py
+++ b/tests/test_security_regressions.py
@@ -537,6 +537,104 @@ def test_require_user_accepts_loopback_when_unconfigured(monkeypatch):
assert auth_helpers.require_user(_LoopReq()) == ""
+def test_require_user_accepts_anyone_when_auth_disabled(monkeypatch):
+ """AUTH_ENABLED=false must let unauthenticated callers through from
+ any host — including the docker bridge / reverse proxy / LAN — so
+ the frontend's global 401 redirect doesn't bounce the user to /login
+ despite the operator turning auth off (issue #622)."""
+ monkeypatch.setenv("AUTH_ENABLED", "false")
+ sys.modules.pop("src.auth_helpers", None)
+ from src import auth_helpers # noqa: WPS433
+
+ class _State:
+ current_user = None
+
+ class _AppState:
+ class _Mgr:
+ # Even with a prior admin account on disk, AUTH_ENABLED=false
+ # must take precedence over is_configured=True.
+ is_configured = True
+ auth_manager = _Mgr()
+
+ class _App:
+ state = _AppState()
+
+ class _DockerClient:
+ host = "172.18.0.1" # docker bridge gateway, not loopback
+
+ class _Req:
+ state = _State()
+ app = _App()
+ client = _DockerClient()
+
+ assert auth_helpers.require_user(_Req()) == ""
+
+
+def test_require_user_localhost_bypass_admits_loopback(monkeypatch):
+ """LOCALHOST_BYPASS=true is the dev-only switch that admits loopback
+ callers without an auth cookie. require_user must mirror the auth
+ middleware so routes don't 401 a caller the middleware already let
+ through."""
+ monkeypatch.setenv("AUTH_ENABLED", "true")
+ monkeypatch.setenv("LOCALHOST_BYPASS", "true")
+ sys.modules.pop("src.auth_helpers", None)
+ from src import auth_helpers # noqa: WPS433
+
+ class _State:
+ current_user = None
+
+ class _AppState:
+ class _Mgr:
+ is_configured = True
+ auth_manager = _Mgr()
+
+ class _App:
+ state = _AppState()
+
+ class _LoopClient:
+ host = "127.0.0.1"
+
+ class _LoopReq:
+ state = _State()
+ app = _App()
+ client = _LoopClient()
+
+ assert auth_helpers.require_user(_LoopReq()) == ""
+
+
+def test_require_user_localhost_bypass_still_rejects_lan(monkeypatch):
+ """LOCALHOST_BYPASS=true must not extend to non-loopback callers —
+ a LAN visitor still needs to authenticate."""
+ from fastapi import HTTPException
+ monkeypatch.setenv("AUTH_ENABLED", "true")
+ monkeypatch.setenv("LOCALHOST_BYPASS", "true")
+ sys.modules.pop("src.auth_helpers", None)
+ from src import auth_helpers # noqa: WPS433
+
+ class _State:
+ current_user = None
+
+ class _AppState:
+ class _Mgr:
+ is_configured = True
+ auth_manager = _Mgr()
+
+ class _App:
+ state = _AppState()
+
+ class _LanClient:
+ host = "192.168.1.42"
+
+ class _LanReq:
+ state = _State()
+ app = _App()
+ client = _LanClient()
+
+ with pytest.raises(HTTPException) as exc:
+ auth_helpers.require_user(_LanReq())
+ assert exc.value.status_code == 401
+
+
def test_require_admin_rejects_unconfigured_public_api(monkeypatch):
"""First-run API mode must not treat "no users yet" as admin access."""
from fastapi import HTTPException