JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
fix:host校验改成通过开关和白名单控制
xiaoyao4280eec50dc638
diff --git a/jiuwenclaw/common/security/ws_origin.py b/jiuwenclaw/common/security/ws_origin.pyindex ed35f96530..33229d4549 100644--- a/jiuwenclaw/common/security/ws_origin.py+++ b/jiuwenclaw/common/security/ws_origin.py@@ -4,25 +4,41 @@from __future__ import annotations+import osfrom http import HTTPStatusfrom typing import Anyfrom urllib.parse import urlsplit-_ALLOWED_WS_ORIGIN_HOSTS = {"127.0.0.1", "localhost"}+_ENABLE_ORIGIN_CHECK_ENV = "JIUWENCLAW_ENABLE_ORIGIN_CHECK"+_ALLOWED_ORIGIN_HOSTS_ENV = "JIUWENCLAW_WS_ALLOWED_ORIGIN_HOSTS"_FORBIDDEN_BODY = b"Forbidden: Origin not allowed\n"+def is_origin_check_enabled() -> bool:+ """Return whether WebSocket Origin validation is enabled."""+ return os.getenv(_ENABLE_ORIGIN_CHECK_ENV, "").strip() == "1"+++def get_allowed_origin_hosts() -> set[str]:+ """Return the global WebSocket Origin hostname allowlist from environment."""+ raw = os.getenv(_ALLOWED_ORIGIN_HOSTS_ENV)+ if raw is None:+ return set()+ return {item.strip().lower() for item in raw.split(",") if item.strip()}++def is_allowed_browser_origin(origin: str | None) -> bool:- """校验浏览器 Origin 是否来自允许的本机地址。"""+ """校验浏览器 Origin 是否允许访问 WebSocket 服务。"""+ allowed_hosts = get_allowed_origin_hosts()if origin is None:- return False+ return "none" in allowed_hoststry:parsed = urlsplit(origin)except ValueError:return False- return parsed.hostname in _ALLOWED_WS_ORIGIN_HOSTS+ return (parsed.hostname or "").lower() in allowed_hostsdef extract_handshake_request(args: tuple[Any, ...]) -> tuple[str, Any]:diff --git a/jiuwenclaw/gateway/channel_manager/web/web_connect.py b/jiuwenclaw/gateway/channel_manager/web/web_connect.pyindex 0baf14151d..f018aacda4 100644--- a/jiuwenclaw/gateway/channel_manager/web/web_connect.py+++ b/jiuwenclaw/gateway/channel_manager/web/web_connect.py@@ -27,6 +27,7 @@ from jiuwenclaw.common.security.ws_origin import (extract_handshake_request,forbidden_origin_response,get_header_value,+ is_origin_check_enabled,is_allowed_browser_origin,)from jiuwenclaw.common.schema.message import Message, Mode, ReqMethod@@ -277,11 +278,23 @@ class WebChannel(BaseChannel):"""在握手阶段执行 Origin 校验,兼容 legacy/new websockets APIs。"""path, request_headers = extract_handshake_request(args)origin = get_header_value(request_headers, "Origin")+ enable_origin_check = is_origin_check_enabled()+ if not enable_origin_check:+ logger.info(+ "WebChannel 握手检查 path=%s origin=%s enable_origin_check=%s allowed=%s",+ path,+ origin,+ enable_origin_check,+ True,+ )+ return None+allowed = is_allowed_browser_origin(origin)logger.info(- "WebChannel 握手检查 path=%s origin=%s allowed=%s",+ "WebChannel 握手检查 path=%s origin=%s enable_origin_check=%s allowed=%s",path,origin,+ enable_origin_check,allowed,)if allowed:diff --git a/jiuwenclaw/resources/.env.template b/jiuwenclaw/resources/.env.templateindex 673b142608..90a41b0d43 100644--- a/jiuwenclaw/resources/.env.template+++ b/jiuwenclaw/resources/.env.template@@ -118,3 +118,9 @@ MEMORY_MODE=# Extension package search dirs, separated by ';'EXTENSION_DIRS=""++# WebSocket Origin validation.+# Set JIUWENCLAW_ENABLE_ORIGIN_CHECK=1 to enable checks.+# Use English commas only; add "none" to allow connections without an Origin header.+JIUWENCLAW_ENABLE_ORIGIN_CHECK=0+JIUWENCLAW_WS_ALLOWED_ORIGIN_HOSTS=127.0.0.1,localhostdiff --git a/jiuwenclaw/server/agent_ws_server.py b/jiuwenclaw/server/agent_ws_server.pyindex 2ba51006b1..23c534f992 100644--- a/jiuwenclaw/server/agent_ws_server.py+++ b/jiuwenclaw/server/agent_ws_server.py@@ -51,6 +51,7 @@ from jiuwenclaw.common.security.ws_origin import (extract_handshake_request,forbidden_origin_response,get_header_value,+ is_origin_check_enabled,is_allowed_browser_origin,)@@ -238,11 +239,23 @@ class AgentWebSocketServer:"""在握手阶段执行 Origin 校验,兼容 legacy/new websockets APIs。"""path, request_headers = extract_handshake_request(args)origin = get_header_value(request_headers, "Origin")+ enable_origin_check = is_origin_check_enabled()+ if not enable_origin_check:+ logger.info(+ "[AgentWebSocketServer] 握手检查 path=%s origin=%s enable_origin_check=%s allowed=%s",+ path,+ origin,+ enable_origin_check,+ True,+ )+ return None+allowed = is_allowed_browser_origin(origin)logger.info(- "[AgentWebSocketServer] 握手检查 path=%s origin=%s allowed=%s",+ "[AgentWebSocketServer] 握手检查 path=%s origin=%s enable_origin_check=%s allowed=%s",path,origin,+ enable_origin_check,allowed,)if allowed: