sync update from LAN repo (source dhk-1 @ 3b1ec5b4)

This commit is contained in:
gozeros committed 2026-11-05 10:56:34 +08:00
1 parent da6521b6cb
commit 264e2200d6
995 files changed
+148105 -13399

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,15 +85,16 @@ def should_check_csrf(request: Request) -> bool:
# 双提交 Cookie 既无 token 可比、也无会话可被伪造,豁免之。
if path.startswith("/api/open/"):
return False
# b5-1 task details may be written by a non-browser integration using its
# own X-Integration-Key authentication. It has no cookie session for a
# cross-site request to ride; AuthMiddleware validates that key narrowly
# for PUT only.
if (
request.method == "PUT"
and path.startswith("/api/task-deeplink-details/")
and bool(request.headers.get("X-Integration-Key"))
):
# 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