mirror of
https://github.com/openswarm-ai/openswarm.git
synced 2026-08-17 18:25:42 +02:00
[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:
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user