代码提交

This commit is contained in:
董宏凯 committed 2026-08-30 01:25:20 +08:00
1 parent 4ac7bc21cd
commit 17cf849361
110 files changed
+17007 -518

No files matched your search

@@ -4,25 +4,25 @@ Per RFC-001:
State-changing operations require CSRF protection.
"""
import secrets
from collections.abc import Callable
from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import JSONResponse
from starlette.types import ASGIApp
from app.gateway.auth.disabled_mode import is_auth_disabled
from app.gateway.proxy_path import app_relative_path
from app.gateway.weknora_embed import (
WEKNORA_EMBED_COOKIE,
has_valid_weknora_embed_cookie,
is_weknora_embed_proxy_path,
)
CSRF_COOKIE_NAME = "csrf_token"
CSRF_HEADER_NAME = "X-CSRF-Token"
CSRF_TOKEN_LENGTH = 64 # bytes
import secrets
from collections.abc import Callable
from fastapi import Request, Response
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import JSONResponse
from starlette.types import ASGIApp
from app.gateway.auth.disabled_mode import is_auth_disabled
from app.gateway.proxy_path import app_relative_path
from app.gateway.weknora_embed import (
WEKNORA_EMBED_COOKIE,
has_valid_weknora_embed_cookie,
is_weknora_embed_proxy_path,
)
CSRF_COOKIE_NAME = "csrf_token"
CSRF_HEADER_NAME = "X-CSRF-Token"
CSRF_TOKEN_LENGTH = 64 # bytes
def is_secure_request(request: Request) -> bool:
@@ -35,7 +35,7 @@ def generate_csrf_token() -> str:
return secrets.token_urlsafe(CSRF_TOKEN_LENGTH)
def should_check_csrf(request: Request) -> bool:
def should_check_csrf(request: Request) -> bool:
"""Determine if a request needs CSRF validation.
CSRF is checked for state-changing methods (POST, PUT, DELETE, PATCH).
@@ -49,12 +49,12 @@ def should_check_csrf(request: Request) -> bool:
path = app_relative_path(request)
stripped = path.rstrip("/")
# Exempt /api/v1/auth/me endpoint
if stripped == "/api/v1/auth/me":
return False
if stripped == "/api/knowledge/vector-search":
return False
if stripped == "/api/writing/export/docx":
return False
if stripped == "/api/v1/auth/me":
return False
if stripped == "/api/knowledge/vector-search":
return False
if stripped == "/api/writing/export/docx":
return False
# Anonymous public endpoints (mirror auth_middleware._PUBLIC_PATH_PREFIXES).
# These intentionally have no cookie session, so the Double-Submit Cookie
# pattern is both impossible (no token to compare) and unnecessary (no
@@ -64,18 +64,37 @@ def should_check_csrf(request: Request) -> bool:
return False
# 开放问答接口同样是匿名、无会话的(见 auth_middleware._PUBLIC_PATH_PREFIXES),
# 双提交 Cookie 既无 token 可比、也无会话可被伪造,豁免之。
if path.startswith("/api/open/"):
return False
if is_weknora_embed_proxy_request(request):
return False
return True
def is_weknora_embed_proxy_request(request: Request) -> bool:
path = app_relative_path(request)
if not is_weknora_embed_proxy_path(path):
return False
return has_valid_weknora_embed_cookie(request.cookies.get(WEKNORA_EMBED_COOKIE))
if path.startswith("/api/open/"):
return False
if is_coze_studio_compat_path(path):
return False
if is_weknora_embed_proxy_request(request):
return False
return True
# Coze playground 自己的 axios 不会带 X-CSRF-Token;独立编排页经
# localhost:8080 同源代理访问这些接口。Cookie 会话仍由 AuthMiddleware 校验。
_COZE_STUDIO_PREFIXES: tuple[str, ...] = (
"/api/passport/",
"/api/playground_api/",
"/api/workflow_api/",
"/api/space/",
)
def is_coze_studio_compat_path(path: str) -> bool:
stripped = path.rstrip("/")
if stripped == "/api/bot/get_type_list":
return True
return any(path.startswith(prefix) for prefix in _COZE_STUDIO_PREFIXES)
def is_weknora_embed_proxy_request(request: Request) -> bool:
path = app_relative_path(request)
if not is_weknora_embed_proxy_path(path):
return False
return has_valid_weknora_embed_cookie(request.cookies.get(WEKNORA_EMBED_COOKIE))
_AUTH_EXEMPT_PATHS: frozenset[str] = frozenset(
@@ -90,7 +109,7 @@ _AUTH_EXEMPT_PATHS: frozenset[str] = frozenset(
"/api/v1/auth/register",
"/api/v1/auth/initialize",
# 并行多智能体面板专用取 token 端点(公共 + CSRF 豁免)。
"/api/parallel-agents/auth/token",
"/api/parallel-agents/auth/token",
}
)