fix(api): disable CORS credentials for wildcard origins

ALLOWED_HOSTS defaults to ["*"] and the CORS middleware always set
allow_credentials=True. Starlette echoes any request Origin with
Access-Control-Allow-Credentials in that combination, which lets every
site make credentialed cross-origin calls. resolve_cors_policy now
returns credentials=False for wildcard origins and keeps credentials
only when explicit origins are configured.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
freeman
2026-10-09 11:39:12 +08:00
parent 5db2eea5b4
commit 0cfb292cef
3 changed files with 84 additions and 4 deletions

View File

@@ -328,6 +328,20 @@ async def localized_unhandled_exception_handler(
)
def resolve_cors_policy(allowed_hosts: Any) -> tuple[list[str], bool]:
"""
把 ALLOWED_HOSTS 解析为 CORS 的允许来源与凭据开关。
通配来源不得与 allow_credentials 并用:Starlette 在两者同时成立时会把任意
请求 Origin 原样回显并声明允许携带凭据,等于对所有站点开放带 Cookie 的跨域
访问。只有显式列出来源时才允许携带凭据。
"""
origins = [str(item).strip() for item in (allowed_hosts or ["*"]) if str(item).strip()]
if not origins or "*" in origins:
return ["*"], False
return origins, True
def create_app() -> FastAPI:
"""
创建并配置 FastAPI 应用实例。
@@ -365,11 +379,12 @@ def create_app() -> FastAPI:
enabled=lambda: bool(get_runtime_setting("API_DOCS_ENABLE")),
)
# 配置 CORS 中间件
# 配置 CORS 中间件;通配来源时不允许携带凭据
allow_origins, allow_credentials = resolve_cors_policy(get_runtime_setting('ALLOWED_HOSTS'))
_app.add_middleware(
CORSMiddleware, # noqa
allow_origins=get_runtime_setting('ALLOWED_HOSTS'),
allow_credentials=True,
allow_origins=allow_origins,
allow_credentials=allow_credentials,
allow_methods=["*"],
allow_headers=["*"],
)

View File

@@ -204,7 +204,7 @@ class ConfigModel(BaseModel):
SECRET_KEY: str = secrets.token_urlsafe(32)
# RESOURCE密钥
RESOURCE_SECRET_KEY: str = secrets.token_urlsafe(32)
# 允许的域名
# 允许的跨域来源;为 "*" 时浏览器跨域请求不携带凭据,需要跨域带 Cookie 请列出显式来源
ALLOWED_HOSTS: list = Field(default_factory=lambda: ["*"])
# TOKEN过期时间
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8