fix(auth): CSRF/鉴权中间件兼容反向代理路径前缀 + 无会话请求豁免
清空浏览器缓存后(尤其 token 换登录)报「CSRF token missing」。根因: 反向代理把请求转发到带前缀的路径(如 /xxx/api/v1/auth/login/token), FastAPI 用 root_path 剥前缀做路由,但 Starlette 中间件跑在路由之前, 看到的仍是带前缀的 path,导致 CSRF 豁免名单与公开路径白名单(精确/前缀 匹配)全部失配。清缓存前靠 localStorage 残留 token 当 Bearer 绕过 CSRF, 清掉后裸奔才暴露。 - 新增 proxy_path.app_relative_path:匹配前按 root_path / X-Forwarded-Prefix 剥掉代理前缀(无前缀时 no-op)。 - csrf_middleware:should_check_csrf / is_auth_endpoint 改用应用相对路径; 补上漏收的 /api/v1/auth/login/token 豁免;新增「无 access_token 会话 cookie 则跳过 CSRF」兜底(无会话请求不存在可被跨站伪造利用的会话)。 - auth_middleware:公开路径判断改用应用相对路径,避免 /xxx/api/public/* 分享页被误判 401。 - langgraph_auth:无 access_token cookie 时跳过 CSRF,与网关链路一致。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
413f92e270
commit
3178b4c45e
4 files changed
+82
-5
No files matched your search
@@ -8,6 +8,7 @@ import secrets
|
||||
from collections.abc import Callable
|
||||
|
||||
from app.gateway.auth.disabled_mode import is_auth_disabled
|
||||
from app.gateway.proxy_path import app_relative_path
|
||||
from fastapi import Request, Response
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from starlette.responses import JSONResponse
|
||||
@@ -37,7 +38,9 @@ def should_check_csrf(request: Request) -> bool:
|
||||
if request.method not in ("POST", "PUT", "DELETE", "PATCH"):
|
||||
return False
|
||||
|
||||
path = request.url.path
|
||||
# Match the application-relative path so a reverse-proxy prefix
|
||||
# (e.g. /xxx/api/v1/auth/me) does not defeat these whitelists.
|
||||
path = app_relative_path(request)
|
||||
stripped = path.rstrip("/")
|
||||
# Exempt /api/v1/auth/me endpoint
|
||||
if stripped == "/api/v1/auth/me":
|
||||
@@ -56,6 +59,10 @@ _AUTH_EXEMPT_PATHS: frozenset[str] = frozenset(
|
||||
{
|
||||
"/api/v1/auth/login/local",
|
||||
"/api/v1/auth/login/username",
|
||||
# Token-exchange login is sessionless on first call (no CSRF cookie yet),
|
||||
# exactly like the other login endpoints — mirror auth_middleware's
|
||||
# _PUBLIC_EXACT_PATHS, which already whitelists it.
|
||||
"/api/v1/auth/login/token",
|
||||
"/api/v1/auth/logout",
|
||||
"/api/v1/auth/register",
|
||||
"/api/v1/auth/initialize",
|
||||
@@ -69,8 +76,10 @@ def is_auth_endpoint(request: Request) -> bool:
|
||||
"""Check if the request is to an auth endpoint.
|
||||
|
||||
Auth endpoints don't need CSRF validation on first call (no token).
|
||||
Matches the application-relative path so a reverse-proxy prefix does not
|
||||
defeat the whitelist.
|
||||
"""
|
||||
return request.url.path.rstrip("/") in _AUTH_EXEMPT_PATHS
|
||||
return app_relative_path(request).rstrip("/") in _AUTH_EXEMPT_PATHS
|
||||
|
||||
|
||||
def _has_bearer_auth(request: Request) -> bool:
|
||||
@@ -82,6 +91,22 @@ def _has_bearer_auth(request: Request) -> bool:
|
||||
return scheme.lower() == "bearer" and bool(token.strip())
|
||||
|
||||
|
||||
def _has_cookie_session(request: Request) -> bool:
|
||||
"""Return True when the request carries a cookie-based session.
|
||||
|
||||
Double-Submit-Cookie CSRF only protects requests the browser authenticates
|
||||
**automatically via cookies** — that is the exact surface a cross-site
|
||||
forgery can ride. A request with no ``access_token`` session cookie has no
|
||||
session for an attacker to exploit, so enforcing CSRF on it is both
|
||||
pointless and harmful: a freshly-loaded browser (e.g. one that just cleared
|
||||
its cache/cookies) hitting the login endpoint would otherwise be rejected
|
||||
with "CSRF token missing" before it can even sign in. Such an
|
||||
unauthenticated request, if forged, is rejected by the auth layer (401)
|
||||
anyway.
|
||||
"""
|
||||
return bool(request.cookies.get("access_token"))
|
||||
|
||||
|
||||
class CSRFMiddleware(BaseHTTPMiddleware):
|
||||
"""Middleware that implements CSRF protection using Double Submit Cookie pattern."""
|
||||
|
||||
@@ -95,8 +120,11 @@ class CSRFMiddleware(BaseHTTPMiddleware):
|
||||
return await call_next(request)
|
||||
|
||||
# Double-submit-cookie CSRF applies to cookie-based browser sessions.
|
||||
# API clients using explicit Bearer tokens are not CSRF-prone.
|
||||
if should_check_csrf(request) and not _is_auth and not _has_bearer_auth(request):
|
||||
# API clients using explicit Bearer tokens are not CSRF-prone, and a
|
||||
# request with no session cookie at all (e.g. a fresh browser signing
|
||||
# in) has no session to forge — only enforce when an ``access_token``
|
||||
# cookie is actually present.
|
||||
if should_check_csrf(request) and not _is_auth and not _has_bearer_auth(request) and _has_cookie_session(request):
|
||||
cookie_token = request.cookies.get(CSRF_COOKIE_NAME)
|
||||
header_token = request.headers.get(CSRF_HEADER_NAME)
|
||||
|
||||
|
||||
Reference in new issue
Block a user