Skip to content

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(web): SPA 路径注入缺失 + 部署工具 host 写死修复

huben4bde324434d4
diff --git a/deploy/enterprise/patch_handler.sh b/deploy/enterprise/patch_handler.sh
index a848792577..e10d499b0f 100755
--- a/deploy/enterprise/patch_handler.sh
+++ b/deploy/enterprise/patch_handler.sh
@@ -50,6 +50,7 @@ install_agentserver_patch() {
fi
local runtime_port="${DEPLOY_VARS["AGENT_RUNTIME_NODE_PORT"]}"
+ local host="${DEPLOY_VARS["CURRENT_NODE_IP"]:-127.0.0.1}"
local yaml_file="${CONFIG["AS_ENV_YAML_FILE"]}"
local json_file="${CONFIG["AS_JSON_FILE"]}"
@@ -59,7 +60,7 @@ install_agentserver_patch() {
# 下发agentserver服务配置
info "Pushing AgentServer template"
set +e
- local response=$(curl -s --max-time 20 -X POST "http://127.0.0.1:${runtime_port}/api/session/config_sync" \
+ local response=$(curl -s --max-time 20 -X POST "http://${host}:${runtime_port}/api/session/config_sync" \
-H "Content-Type: application/json" \
-d @"${json_file}" 2>&1)
local rc=$?
@@ -77,6 +78,7 @@ install_model_patch() {
fi
local gw_node_port="${DEPLOY_VARS["GATEWAY_CONFIG_HTTP_NODE_PORT"]:-}"
+ local host="${DEPLOY_VARS["CURRENT_NODE_IP"]:-127.0.0.1}"
local bot_id="${MODEL_BOT_ID:-default}"
local agent_template_id="${AGENT_TEMPLATE_ID:-default-agent}"
local model_template_id="default-model"
@@ -84,7 +86,7 @@ install_model_patch() {
# 1. 下发 model_template(数据一:模型凭据)
info "Pushing model template (template_id=${model_template_id})"
set +e
- local response=$(curl -s --max-time 20 -X POST "http://127.0.0.1:${gw_node_port}/api/v1/model-templates" \
+ local response=$(curl -s --max-time 20 -X POST "http://${host}:${gw_node_port}/api/v1/model-templates" \
-H "Content-Type: application/json" \
-d "{
\"template_id\":\"${model_template_id}\",
@@ -112,7 +114,7 @@ install_model_patch() {
# 2. 下发 agent_template(数据二:template_ref 指向 model_template)
info "Pushing agent template (template_id=${agent_template_id}, ref→${model_template_id})"
set +e
- response=$(curl -s --max-time 20 -X POST "http://127.0.0.1:${gw_node_port}/api/v1/agent-templates" \
+ response=$(curl -s --max-time 20 -X POST "http://${host}:${gw_node_port}/api/v1/agent-templates" \
-H "Content-Type: application/json" \
-d "{\"template_id\":\"${agent_template_id}\",\"template_name\":\"default agent\",\"template_ref\":{\"default_model\":[\"${model_template_id}\"]},\"enabled\":true}" 2>&1)
rc=$?
@@ -125,7 +127,7 @@ install_model_patch() {
# 3. 下发 instance_agent_resource(数据三:resource_id=bot_id, ref_template_id 指向 agent_template)
info "Pushing instance agent resource (resource_id=${bot_id}, ref→${agent_template_id})"
set +e
- response=$(curl -s --max-time 20 -X POST "http://127.0.0.1:${gw_node_port}/api/v1/instance-agent-resources" \
+ response=$(curl -s --max-time 20 -X POST "http://${host}:${gw_node_port}/api/v1/instance-agent-resources" \
-H "Content-Type: application/json" \
-d "{\"resource_id\":\"${bot_id}\",\"ref_template_id\":\"${agent_template_id}\",\"resource_name\":\"default agent\",\"match_expr\":[],\"enabled\":true}" 2>&1)
rc=$?
diff --git a/jiuwenswarm/channels/web/app_web.py b/jiuwenswarm/channels/web/app_web.py
index a99900abee..a41d513a66 100644
--- a/jiuwenswarm/channels/web/app_web.py
+++ b/jiuwenswarm/channels/web/app_web.py
@@ -749,7 +749,18 @@ class _SpaStaticHandler(SimpleHTTPRequestHandler):
def _is_document_request(self) -> bool:
path = urlparse(self.path).path
- return path in ("/", "/index.html") and "text/html" in self.headers.get("Accept", "")
+ if "text/html" not in self.headers.get("Accept", ""):
+ return False
+ if path in ("/", "/index.html"):
+ return True
+ rel_path = unquote(path).lstrip("/")
+ if not rel_path:
+ return True
+ base_dir = Path(self.directory or os.getcwd()).resolve()
+ target = (base_dir / rel_path).resolve()
+ if os.path.commonpath([str(base_dir), str(target)]) != str(base_dir):
+ return False
+ return not target.exists()
def send_head(self):
parsed = urlparse(self.path)
diff --git a/tests/unit_tests/test_app_web_gateway_api_proxy.py b/tests/unit_tests/test_app_web_gateway_api_proxy.py
index ecb195c4d9..3f54a86df3 100644
--- a/tests/unit_tests/test_app_web_gateway_api_proxy.py
+++ b/tests/unit_tests/test_app_web_gateway_api_proxy.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+from email.message import EmailMessage
from pathlib import Path
import pytest
@@ -18,6 +19,17 @@ def _handler(path: str) -> _SpaStaticHandler:
return handler
+def _document_handler(path: str, accept: str, directory: Path) -> _SpaStaticHandler:
+ handler = object.__new__(_SpaStaticHandler)
+ handler.path = path
+ handler.directory = str(directory)
+ headers = EmailMessage()
+ if accept:
+ headers["Accept"] = accept
+ handler.headers = headers
+ return handler
+
+
def test_gateway_api_is_classified_as_gateway_web_http_route() -> None:
assert _handler("/gateway-api/v1/chat/completions")._is_web_http_route()
@@ -73,3 +85,33 @@ def test_login_auth_simulate_config_is_strict_and_defaults_to_true() -> None:
assert _parse_login_auth_simulate("false") is False
with pytest.raises(ValueError, match="期望 true 或 false"):
_parse_login_auth_simulate("yes")
+
+def test_document_request_for_root_and_index(tmp_path: Path) -> None:
+ assert _document_handler("/", "text/html", tmp_path)._is_document_request()
+ assert _document_handler("/index.html", "text/html", tmp_path)._is_document_request()
+
+
+def test_document_request_skips_when_accept_header_lacks_html(tmp_path: Path) -> None:
+ assert not _document_handler("/", "*/*", tmp_path)._is_document_request()
+ assert not _document_handler("/", "", tmp_path)._is_document_request()
+
+
+def test_document_request_treats_spa_routes_as_document(tmp_path: Path) -> None:
+ for path in ("/chat", "/agents", "/sessions/123", "/teams"):
+ assert _document_handler(path, "text/html", tmp_path)._is_document_request(), path
+
+
+def test_document_request_serves_existing_static_files_as_is(tmp_path: Path) -> None:
+ (tmp_path / "logo.svg").write_text("<svg/>", encoding="utf-8")
+ (tmp_path / "assets").mkdir()
+ (tmp_path / "assets" / "index-abc.js").write_text("console.log(1)", encoding="utf-8")
+ assert not _document_handler("/logo.svg", "text/html", tmp_path)._is_document_request()
+ assert not _document_handler(
+ "/assets/index-abc.js", "text/html", tmp_path
+ )._is_document_request()
+
+
+def test_document_request_rejects_path_traversal_as_document(tmp_path: Path) -> None:
+ assert not _document_handler(
+ "/../etc/passwd", "text/html", tmp_path
+ )._is_document_request()