sync update from LAN repo (source master @ ca48c4ec)
This commit is contained in:
1 parent
8bc8b0e082
commit
ea9dcff9c0
873 files changed
+107866
-14011
No files matched your search
@@ -5,6 +5,7 @@ State-changing operations require CSRF protection.
|
||||
"""
|
||||
|
||||
import secrets
|
||||
import re
|
||||
from collections.abc import Callable
|
||||
|
||||
from fastapi import Request, Response
|
||||
@@ -25,6 +26,24 @@ CSRF_HEADER_NAME = "X-CSRF-Token"
|
||||
CSRF_TOKEN_LENGTH = 64 # bytes
|
||||
|
||||
|
||||
# Must stay as narrow as AuthMiddleware's credential-free force-situation
|
||||
# detail integration route: only GET/PUT of the current detail document are
|
||||
# public. The version history and other force-situation endpoints keep their
|
||||
# normal authentication and CSRF policy.
|
||||
_FORCE_SITUATION_DETAIL_RE = re.compile(
|
||||
r"^/api/force-situation/tasks/[^/]+/(?:detail|versions)$"
|
||||
)
|
||||
|
||||
|
||||
def _is_force_situation_detail_external_request(method: str, path: str) -> bool:
|
||||
normalized = path.rstrip("/")
|
||||
if not _FORCE_SITUATION_DETAIL_RE.fullmatch(normalized):
|
||||
return False
|
||||
return method.upper() == "GET" or (
|
||||
method.upper() == "PUT" and normalized.endswith("/detail")
|
||||
)
|
||||
|
||||
|
||||
def is_secure_request(request: Request) -> bool:
|
||||
"""Detect whether the original client request was made over HTTPS."""
|
||||
return request.headers.get("x-forwarded-proto", request.url.scheme) == "https"
|
||||
@@ -66,6 +85,17 @@ def should_check_csrf(request: Request) -> bool:
|
||||
# 双提交 Cookie 既无 token 可比、也无会话可被伪造,豁免之。
|
||||
if path.startswith("/api/open/"):
|
||||
return False
|
||||
# b5-1 task details are written by a non-browser integration over a
|
||||
# credential-free open endpoint (see AuthMiddleware): there is no cookie
|
||||
# session for a cross-site request to ride, and the write itself is open
|
||||
# anyway, so the Double-Submit Cookie check is meaningless here.
|
||||
if request.method == "PUT" and path.startswith("/api/task-deeplink-details/"):
|
||||
return False
|
||||
# The force-situation current-detail API is a credential-free internal
|
||||
# integration surface. With no browser login session to protect, CSRF
|
||||
# validation would only reject existing scheduler/skill writers.
|
||||
if _is_force_situation_detail_external_request(request.method, path):
|
||||
return False
|
||||
if is_coze_studio_compat_path(path):
|
||||
return False
|
||||
if is_weknora_embed_proxy_request(request):
|
||||
|
||||
Reference in new issue
Block a user