xiaozhi-esp32-server/main/xiaozhi-server/core/api/base_handler.py
hrz 340c3698d2 跨域响应一律标来源,配置改成逐键合并
陌生来源的响应之前不带 Vary,共享缓存可能把带放行头的响应喂给别的来源。
改成只要请求带了 Origin 就无条件写。

从智控台拉配置时原本把整个 server 段换成五个本地键,接口返回的其余字段
全被吞掉,包括这个 PR 新加的跨域配置和白名单。改成逐键覆盖:本地显式
写过的覆盖接口值,接口返回的其余字段保留。认证段仍以智控台为准。
2026-10-22 02:25:17 +00:00

52 lines
2.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from aiohttp import web
from config.logger import setup_logging
class BaseHandler:
def __init__(self, config: dict):
self.config = config
self.logger = setup_logging()
self._cors_wildcard_warned = False
def _resolve_cors_origin(self, origin: str | None) -> str | None:
"""回显名单内的 Origin;其余情况不下发 CORS 头,由浏览器拦截
这里刻意不支持通配:回显任意来源并带上 Allow-Credentials 会让任意网站
都能带凭证读响应,而固定写 "*" 配 Allow-Credentials 属于浏览器明确
拒绝的组合,反而挡得更严。宁可什么都不放,也不放宽。
"""
if not origin:
return None
cors = self.config.get("server", {}).get("cors", {}) or {}
allowed = cors.get("allowed_origins", []) or []
if "*" in allowed and not self._cors_wildcard_warned:
self._cors_wildcard_warned = True
self.logger.bind(tag="BaseHandler").warning(
"cors.allowed_origins 里的 * 不会放行任意来源,请填写具体来源"
)
return origin if origin in allowed else None
def _add_cors_headers(self, response, request=None):
"""添加CORS头信息"""
response.headers["Access-Control-Allow-Headers"] = (
"client-id, content-type, device-id, authorization"
)
requested_origin = (
request.headers.get("Origin") if request is not None else None
)
origin = self._resolve_cors_origin(requested_origin)
if requested_origin:
# 只要请求带了 Origin 就标 Vary,哪怕这次没放行,
# 否则共享缓存会把带放行头的响应喂给别的来源
response.headers["Vary"] = "Origin"
if origin:
response.headers["Access-Control-Allow-Origin"] = origin
response.headers["Access-Control-Allow-Credentials"] = "true"
async def handle_options(self, request):
"""处理OPTIONS请求,添加CORS头信息"""
response = web.Response(body=b"", content_type="text/plain")
self._add_cors_headers(response, request)
# 添加允许的方法
response.headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"
return response