xiaozhi-esp32-server/main/xiaozhi-server/core/api/base_handler.py
hrz 7e70484540 跨域默认不放行,陌生来源交浏览器拦
跨域默认放开所有来源等于放行任意网站带凭证读响应,比改之前固定回通
配符更糟:那个组合浏览器本来会拦,改成回显来源就真能读了。默认改回空
名单,不在名单里就不下发跨域头,交给浏览器拦。填星号不放行任何来
源,只在启动时提醒一次。
2026-10-22 01:42:38 +00:00

48 lines
2.1 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"
)
origin = self._resolve_cors_origin(
request.headers.get("Origin") if request is not None else None
)
if origin:
response.headers["Access-Control-Allow-Origin"] = origin
response.headers["Access-Control-Allow-Credentials"] = "true"
response.headers["Vary"] = "Origin"
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