xiaozhi-esp32-server/main/xiaozhi-server/core/api/base_handler.py
tuanaiseo 6044d0e42f fix(security): cors misconfiguration allows credentialed cross-or
The API base handler sets `Access-Control-Allow-Credentials: true` together with `Access-Control-Allow-Origin: *`. Browsers treat this combination as invalid/insecure and it often leads to unsafe CORS behavior when later changed to reflected origins. This is especially risky because `authorization` is explicitly allowed in request headers.

Affected files: base_handler.py

Signed-off-by: tuanaiseo <221258316+tuanaiseo@users.noreply.github.com>
2026-04-08 06:32:11 +07:00

34 lines
1.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()
def _add_cors_headers(self, response, request=None):
"""添加CORS头信息"""
response.headers["Access-Control-Allow-Headers"] = (
"client-id, content-type, device-id, authorization"
)
trusted_origins = self.config.get("trusted_origins") or self.config.get(
"cors_allowed_origins"
) or []
if isinstance(trusted_origins, str):
trusted_origins = [trusted_origins]
origin = request.headers.get("Origin") if request else None
if origin in trusted_origins:
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