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(a2a): trust forwarded HTTPS from configured proxies
NickFuryXXXb9fa4099c47e
diff --git a/packages/jiuwenclaw-ee/gateway/extensions/manager_config_receiver/http/app.py b/packages/jiuwenclaw-ee/gateway/extensions/manager_config_receiver/http/app.pyindex f1895098f8..21a90b4a33 100644--- a/packages/jiuwenclaw-ee/gateway/extensions/manager_config_receiver/http/app.py+++ b/packages/jiuwenclaw-ee/gateway/extensions/manager_config_receiver/http/app.py@@ -3,7 +3,9 @@from __future__ import annotationsfrom fastapi import APIRouter, FastAPI+from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware+from ..infrastructure.config import get_settingsfrom ..routers.application_config_routers import application_config_routerfrom ..routers.instance_resource_routers import instance_resource_routerfrom ..routers.instance_routers import instance_router@@ -13,6 +15,10 @@ from ..routers.template_routers import templates_routerdef create_app() -> FastAPI:"""Gateway 本机配置接收接口(每网关独立 DB,无路径级实例段)。"""app = FastAPI(title="Gateway Manager Config Receiver", docs_url="/docs", redoc_url=None)+ app.add_middleware(+ ProxyHeadersMiddleware,+ trusted_hosts=get_settings().gateway_config_forwarded_allow_ips,+ )@app.get("/api/health", tags=["System"])async def system_health() -> dict[str, str]:diff --git a/packages/jiuwenclaw-ee/gateway/extensions/manager_config_receiver/http/server.py b/packages/jiuwenclaw-ee/gateway/extensions/manager_config_receiver/http/server.pyindex 56bfe03071..f73036a731 100644--- a/packages/jiuwenclaw-ee/gateway/extensions/manager_config_receiver/http/server.py+++ b/packages/jiuwenclaw-ee/gateway/extensions/manager_config_receiver/http/server.py@@ -5,7 +5,6 @@ from __future__ import annotationsimport asyncioimport logging-import uvicornfrom uvicorn import Config, Serverfrom ..infrastructure.config import get_settings@@ -38,6 +37,8 @@ class ConfigReceiverServer:loop="asyncio",log_config=None,access_log=False,+ # Proxy headers are handled by create_app() with the configured trust list.+ proxy_headers=False,)self._server = Server(config)diff --git a/packages/jiuwenclaw-ee/gateway/extensions/manager_config_receiver/infrastructure/config.py b/packages/jiuwenclaw-ee/gateway/extensions/manager_config_receiver/infrastructure/config.pyindex 7cdb4038f4..ddf392651f 100644--- a/packages/jiuwenclaw-ee/gateway/extensions/manager_config_receiver/infrastructure/config.py+++ b/packages/jiuwenclaw-ee/gateway/extensions/manager_config_receiver/infrastructure/config.py@@ -67,6 +67,11 @@ class Settings(BaseSettings):default=8775,validation_alias="GATEWAY_CONFIG_HTTP_PORT",)+ gateway_config_forwarded_allow_ips: str = Field(+ default="127.0.0.1",+ validation_alias="GATEWAY_CONFIG_FORWARDED_ALLOW_IPS",+ description="允许提供 X-Forwarded-* 的可信反向代理 IP/CIDR 列表",+ )# 对外可被 Manager 访问的地址;HTTPS 通常由反向代理终止。gateway_config_public_host: str = Field(default="",@@ -119,6 +124,14 @@ class Settings(BaseSettings):# ===================== 核心校验逻辑 =====================@model_validator(mode="after")def validate_db_fields(self) -> "Settings":+ trusted_proxies = {+ value.strip()+ for value in self.gateway_config_forwarded_allow_ips.split(",")+ if value.strip()+ }+ if "*" in trusted_proxies:+ raise ValueError("GATEWAY_CONFIG_FORWARDED_ALLOW_IPS must not trust all hosts")+# 如果是 SQLite,不需要校验连接参数if is_sqlite(self.gateway_db_type):# 如果没传路径,自动设置默认值diff --git a/tests/unit_tests/gateway/test_manager_config_receiver_a2a.py b/tests/unit_tests/gateway/test_manager_config_receiver_a2a.pyindex 01615504e8..d5e7d6f0b3 100644--- a/tests/unit_tests/gateway/test_manager_config_receiver_a2a.py+++ b/tests/unit_tests/gateway/test_manager_config_receiver_a2a.py@@ -391,6 +391,46 @@ def test_a2a_credential_replace_requires_https(assert secrets.values == {}+def test_a2a_credential_replace_accepts_trusted_https_proxy(+ a2a_receiver: tuple[TestClient, dict[str, Any], _Secrets],+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ _, _, secrets = a2a_receiver+ monkeypatch.setenv("GATEWAY_CONFIG_FORWARDED_ALLOW_IPS", "testclient")+ app_module = import_manager_config_receiver_module("http.app")+ with TestClient(app_module.create_app(), base_url="http://gateway.test") as client:+ response = client.post(+ "/api/v1/a2a-outbound-templates",+ json=_outbound_payload(),+ headers={"X-Forwarded-Proto": "https"},+ )+ assert response.status_code == 200+ assert secrets.values == {"a2a/outbound/a2a-weather.api_key": "secret"}+++def test_a2a_credential_replace_rejects_untrusted_https_proxy_header(+ a2a_receiver: tuple[TestClient, dict[str, Any], _Secrets],+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ _, _, secrets = a2a_receiver+ monkeypatch.setenv("GATEWAY_CONFIG_FORWARDED_ALLOW_IPS", "192.0.2.1")+ app_module = import_manager_config_receiver_module("http.app")+ with TestClient(app_module.create_app(), base_url="http://gateway.test") as client:+ response = client.post(+ "/api/v1/a2a-outbound-templates",+ json=_outbound_payload(),+ headers={"X-Forwarded-Proto": "https"},+ )+ assert response.status_code == 400+ assert secrets.values == {}+++def test_a2a_receiver_rejects_wildcard_trusted_proxy_config() -> None:+ config_module = import_manager_config_receiver_module("infrastructure.config")+ with pytest.raises(ValueError, match="must not trust all hosts"):+ config_module.Settings(GATEWAY_CONFIG_FORWARDED_ALLOW_IPS="*")++def test_a2a_credential_is_restored_when_projection_update_fails(a2a_receiver: tuple[TestClient, dict[str, Any], _Secrets],monkeypatch: pytest.MonkeyPatch,