[eric] backend: cross-origin 401 from the auth middleware now carries CORS headers for allow-listed origins, so a token-less dev request reads as an honest 401 instead of an opaque 'CORS blocked'

This commit is contained in:
ciregenz
2026-06-25 03:51:28 -07:00
parent 48f475aecf
commit 407915ec9b
+6
View File
@@ -132,9 +132,15 @@ async def p_auth_middleware(request: Request, call_next):
f"auth: rejecting {request.method} {request.url.path} "
f"(origin={headers.get('origin', '-')}, no valid token)"
)
# This middleware sits OUTSIDE CORSMiddleware, so an early 401 skips its headers and a cross-origin caller (e.g. the vite dev server) sees an opaque "CORS blocked" instead of an honest 401. Echo the origin ONLY when it matches the CORS allow-list (same regex), so the 401 is legible without re-opening CORS to arbitrary origins.
import re as p_re
p_origin = headers.get("origin")
p_allowed = bool(p_origin) and bool(p_re.match(r"^(file://.*|http://localhost:\d+|http://127\.0\.0\.1:\d+|https://(api\.)?openswarm\.com)$", p_origin))
p_cors = {"Access-Control-Allow-Origin": p_origin, "Access-Control-Allow-Credentials": "true"} if p_allowed else {}
return JSONResponse(
{"error": "unauthorized", "detail": "missing or invalid token"},
status_code=401,
headers=p_cors,
)
response = await call_next(request)