Skip to content

"Vibe-Trading: Your Personal Trading Agent"

[security] fix(api): require explicit opt-in for agent shell tools (#243)

Hinotobi0568527cd675
diff --git a/agent/api_server.py b/agent/api_server.py
index 420d07ad8..a23a15d94 100644
--- a/agent/api_server.py
+++ b/agent/api_server.py
@@ -842,7 +842,12 @@ def _env_shell_tools_enabled() -> bool:
def _shell_tools_enabled_for_request(request: Request) -> bool:
"""Return whether this API request may expose shell tools to the agent."""
- return _is_local_client(request) or _env_shell_tools_enabled()
+ # Shell-capable tools execute commands on the host as the API process user.
+ # Do not infer that privilege from peer IP alone: browser DNS rebinding can
+ # make attacker-controlled pages appear as loopback clients. Operators who
+ # intentionally want API-started agents or swarm workers to receive shell
+ # tools must opt in explicitly.
+ return _env_shell_tools_enabled()
async def require_local_or_auth(
diff --git a/agent/tests/test_security_auth_api.py b/agent/tests/test_security_auth_api.py
index 331052ca2..55bd97e3c 100644
--- a/agent/tests/test_security_auth_api.py
+++ b/agent/tests/test_security_auth_api.py
@@ -275,10 +275,10 @@ def test_session_event_stream_accepts_query_token_for_browser_eventsource(
assert response.status_code in {404, 501}
-def test_shell_tools_allowed_for_loopback_api_request() -> None:
+def test_shell_tools_disabled_for_loopback_api_request_by_default() -> None:
request = SimpleNamespace(client=SimpleNamespace(host="127.0.0.1"))
- assert api_server._shell_tools_enabled_for_request(request)
+ assert not api_server._shell_tools_enabled_for_request(request)
def test_shell_tools_disabled_for_remote_api_request_by_default() -> None:
@@ -287,15 +287,82 @@ def test_shell_tools_disabled_for_remote_api_request_by_default() -> None:
assert not api_server._shell_tools_enabled_for_request(request)
-def test_shell_tools_remote_api_request_accepts_explicit_opt_in(
+def test_shell_tools_api_request_accepts_explicit_opt_in(
monkeypatch: pytest.MonkeyPatch,
) -> None:
- request = SimpleNamespace(client=SimpleNamespace(host="203.0.113.10"))
+ request = SimpleNamespace(client=SimpleNamespace(host="127.0.0.1"))
monkeypatch.setenv("VIBE_TRADING_ENABLE_SHELL_TOOLS", "1")
assert api_server._shell_tools_enabled_for_request(request)
+def test_dns_rebound_swarm_run_does_not_enable_shell_tools_by_default(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ captured: dict[str, object] = {}
+
+ class FakeRuntime:
+ def start_run(self, preset_name: str, user_vars: dict, include_shell_tools: bool = False):
+ captured["preset_name"] = preset_name
+ captured["user_vars"] = user_vars
+ captured["include_shell_tools"] = include_shell_tools
+ return SimpleNamespace(
+ id="swarm-test-no-shell",
+ status=SimpleNamespace(value="running"),
+ preset_name=preset_name,
+ )
+
+ monkeypatch.setattr(api_server, "_get_swarm_runtime", lambda: FakeRuntime())
+ monkeypatch.setenv("API_AUTH_KEY", "secret")
+ monkeypatch.setattr(api_server, "_API_KEY", "secret")
+
+ response = _local_client().post(
+ "/swarm/runs",
+ headers={
+ "Host": "attacker.example:8899",
+ "Origin": "http://attacker.example:8899",
+ },
+ json={
+ "preset_name": "technical_analysis_panel",
+ "user_vars": {"target": "NVDA", "timeframe": "1d"},
+ },
+ )
+
+ assert response.status_code == 200
+ assert captured["include_shell_tools"] is False
+
+
+def test_dns_rebound_session_message_does_not_enable_shell_tools_by_default(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ captured: dict[str, object] = {}
+
+ class FakeSessionService:
+ async def send_message(self, session_id: str, content: str, include_shell_tools: bool = False):
+ captured["session_id"] = session_id
+ captured["content"] = content
+ captured["include_shell_tools"] = include_shell_tools
+ return {"message_id": "msg-test", "attempt_id": "attempt-test"}
+
+ monkeypatch.setattr(api_server, "_get_session_service", lambda: FakeSessionService())
+ monkeypatch.setenv("API_AUTH_KEY", "secret")
+ monkeypatch.setattr(api_server, "_API_KEY", "secret")
+
+ response = _local_client().post(
+ "/sessions/abcdef012345/messages",
+ headers={
+ "Host": "attacker.example:8899",
+ "Origin": "http://attacker.example:8899",
+ },
+ json={"content": "SESSION_DNS_REBIND_PROOF_PAYLOAD"},
+ )
+
+ assert response.status_code == 200
+ assert captured["session_id"] == "abcdef012345"
+ assert captured["content"] == "SESSION_DNS_REBIND_PROOF_PAYLOAD"
+ assert captured["include_shell_tools"] is False
+
+
def test_default_cors_origins_are_loopback_only() -> None:
origins = api_server._parse_cors_origins(None)