Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(cua-agent): bind playground server to localhost and require same-origin (#5502)

Francesco Bonacci20e9cc275692
diff --git a/libs/python/agent/cua_agent/agent.py b/libs/python/agent/cua_agent/agent.py
index ed13c291a7..3fa2ed964d 100644
--- a/libs/python/agent/cua_agent/agent.py
+++ b/libs/python/agent/cua_agent/agent.py
@@ -1095,15 +1095,19 @@ class ComputerAgent:
return self.agent_loop.get_capabilities()
return ["step"] # Default capability
- def open(self, port: Optional[int] = None):
+ def open(self, port: Optional[int] = None, host: str = "127.0.0.1"):
"""
Start the playground server and open it in the browser.
This method starts a local HTTP server that exposes the /responses endpoint
and automatically opens the Cua playground interface in the default browser.
+ The server has no authentication, so it only listens on this machine unless
+ you pass another ``host``.
Args:
port: Port to run the server on. If None, finds an available port automatically.
+ host: Interface to bind. Defaults to loopback; use "0.0.0.0" only on a
+ network you trust.
Example:
>>> agent = ComputerAgent(model="claude-sonnet-4")
@@ -1112,4 +1116,4 @@ class ComputerAgent:
from .playground import PlaygroundServer
server = PlaygroundServer(agent_instance=self)
- server.start(port=port, open_browser=True)
+ server.start(port=port, open_browser=True, host=host)
diff --git a/libs/python/agent/cua_agent/playground/server.py b/libs/python/agent/cua_agent/playground/server.py
index eb24058619..80e521ed30 100644
--- a/libs/python/agent/cua_agent/playground/server.py
+++ b/libs/python/agent/cua_agent/playground/server.py
@@ -4,10 +4,11 @@ import asyncio
import logging
import os
import platform
+import re
import socket
import traceback
import webbrowser
-from typing import Any, Dict, List, Optional, Union
+from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
from urllib.parse import quote
import uvicorn
@@ -17,18 +18,78 @@ from fastapi.responses import JSONResponse
logger = logging.getLogger(__name__)
+DEFAULT_HOST = "127.0.0.1"
+
+# Browser origins served from this machine (any port) are always allowed.
+_LOOPBACK_ORIGIN_RE = re.compile(r"https?://(localhost|127\.0\.0\.1|\[::1\])(:\d+)?")
+
+# agent_kwargs a request may set by default: harmless tuning knobs only.
+_SAFE_AGENT_KWARGS = frozenset(
+ {
+ "only_n_most_recent_images",
+ "use_prompt_caching",
+ "max_retries",
+ "screenshot_delay",
+ "instructions",
+ "verbosity",
+ "temperature",
+ "top_p",
+ "max_tokens",
+ }
+)
+
+# agent_kwargs a request can never set, even with allow_request_overrides:
+# they load code, write to disk or swap the agent's tools and loop.
+_BLOCKED_AGENT_KWARGS = frozenset(
+ {
+ "trust_remote_code",
+ "trajectory_dir",
+ "tools",
+ "callbacks",
+ "custom_loop",
+ }
+)
+
+
+def _is_loopback_host(host: str) -> bool:
+ return host in ("127.0.0.1", "localhost", "::1")
-class PlaygroundServer:
- """Playground server for running Cua agents via HTTP API."""
- def __init__(self, agent_instance=None):
+class PlaygroundServer:
+ """Playground server for running Cua agents via HTTP API.
+
+ Security model: the server has no authentication, so it is only meant to be
+ reachable from the machine it runs on. It binds to loopback by default, only
+ answers browser origins served from loopback (plus any ``allowed_origins``
+ you list), never writes request data into ``os.environ``, and ignores any
+ request field that could redirect the agent's model traffic (``api_base``,
+ ``api_key``, ...) unless ``allow_request_overrides=True`` is set.
+ """
+
+ def __init__(
+ self,
+ agent_instance=None,
+ *,
+ allowed_origins: Optional[Iterable[str]] = None,
+ allow_request_overrides: bool = False,
+ ):
"""
Initialize the playground server.
Args:
agent_instance: Optional pre-configured agent instance to use
+ allowed_origins: Extra browser origins (for example
+ ``"https://dashboard.example.com"``) allowed to call the API, on
+ top of loopback origins. ``"*"`` is not accepted.
+ allow_request_overrides: Let a request set ``agent_kwargs`` that
+ point the agent at another model endpoint or credentials
+ (``api_base``, ``base_url``, ``api_key``, ...). Off by default.
+ Only turn this on if you trust every client that can reach
+ the server.
"""
self.agent_instance = agent_instance
+ self.allow_request_overrides = allow_request_overrides
+ self.allowed_origins = self._normalize_origins(allowed_origins)
self.app = FastAPI(
title="Cua Playground Server",
description="Playground server for Cua agents",
@@ -39,13 +100,46 @@ class PlaygroundServer:
self.server = None
self.port = None
+ @staticmethod
+ def _normalize_origins(origins: Optional[Iterable[str]]) -> List[str]:
+ normalized: List[str] = []
+ for origin in origins or ():
+ origin = origin.strip().rstrip("/").lower()
+ if not origin:
+ continue
+ if "*" in origin:
+ raise ValueError(
+ "Wildcard origins are not allowed for the playground server; "
+ "list each origin explicitly."
+ )
+ normalized.append(origin)
+ return normalized
+
+ def _is_origin_allowed(self, origin: str) -> bool:
+ return (
+ _LOOPBACK_ORIGIN_RE.fullmatch(origin) is not None
+ or origin.rstrip("/").lower() in self.allowed_origins
+ )
+
def _setup_middleware(self):
- """Setup CORS middleware."""
+ """Setup origin checks and CORS (added last, so it runs first)."""
+
+ @self.app.middleware("http")
+ async def reject_foreign_origins(request: Request, call_next):
+ # CORS only stops a page from reading a response; a cross-origin
+ # "simple" POST is still executed. Browsers always send Origin on
+ # cross-origin requests, so refuse them before any route runs.
+ origin = request.headers.get("origin")
+ if origin is not None and not self._is_origin_allowed(origin):
+ return JSONResponse(status_code=403, content={"detail": "Origin not allowed"})
+ return await call_next(request)
+
self.app.add_middleware(
CORSMiddleware,
- allow_origins=["*"],
- allow_credentials=True,
- allow_methods=["*"],
+ allow_origins=self.allowed_origins,
+ allow_origin_regex=_LOOPBACK_ORIGIN_RE.pattern,
+ allow_credentials=False,
+ allow_methods=["GET", "POST", "OPTIONS"],
allow_headers=["*"],
)
@@ -78,9 +172,13 @@ class PlaygroundServer:
{
"model": "...", # required
"input": "... or messages[]", # required
- "agent_kwargs": { ... }, # optional, passed directly to ComputerAgent
- "env": { ... } # optional env overrides for agent
+ "agent_kwargs": { ... }, # optional tuning options for ComputerAgent
+ "env": { ... } # ignored; kept so older clients still work
}
+
+ ``env`` is never applied. ``agent_kwargs`` is limited to harmless tuning
+ options; anything else (``api_base``, ``api_key``, ...) is ignored unless
+ the server was created with ``allow_request_overrides=True``.
"""
# Import here to avoid circular imports
try:
@@ -93,32 +191,27 @@ class PlaygroundServer:
body = await request.json()
except Exception as e:
raise HTTPException(status_code=400, detail=f"Invalid JSON body: {str(e)}")
+ if not isinstance(body, dict):
+ raise HTTPException(status_code=400, detail="Body must be a JSON object")
model = body.get("model")
input_data = body.get("input")
if not model or input_data is None:
raise HTTPException(status_code=400, detail="'model' and 'input' are required")
- agent_kwargs: Dict[str, Any] = body.get("agent_kwargs") or {}
- env_overrides: Dict[str, str] = body.get("env") or {}
-
- # Simple env override context
- class _EnvOverride:
- def __init__(self, overrides: Dict[str, str]):
- self.overrides = overrides
- self._original: Dict[str, Optional[str]] = {}
+ raw_agent_kwargs = body.get("agent_kwargs") or {}
+ if not isinstance(raw_agent_kwargs, dict):
+ raise HTTPException(status_code=400, detail="'agent_kwargs' must be an object")
+ agent_kwargs, ignored = self._filter_agent_kwargs(raw_agent_kwargs)
- def __enter__(self):
- for k, v in (self.overrides or {}).items():
- self._original[k] = os.environ.get(k)
- os.environ[k] = str(v)
-
- def __exit__(self, exc_type, exc, tb):
- for k, old in self._original.items():
- if old is None:
- os.environ.pop(k, None)
- else:
- os.environ[k] = old
+ # Never write request data into the process environment.
+ if body.get("env"):
+ ignored.append("env")
+ if ignored:
+ logger.warning(
+ "Ignored request fields that the playground server does not accept: %s",
+ ", ".join(sorted(set(ignored))),
+ )
# Convert input to messages
def _to_messages(data: Union[str, List[Dict[str, Any]]]) -> List[Dict[str, Any]]:
@@ -132,50 +225,49 @@ class PlaygroundServer:
error = None
- with _EnvOverride(env_overrides):
- # Use pre-configured agent only if model matches, otherwise create new one
- # This ensures the model picker in the UI is respected
- if self.agent_instance and self.agent_instance.model == model:
- agent = self.agent_instance
- else:
- # Model changed or no pre-configured agent - create new agent with requested model
- agent = ComputerAgent(model=model, **agent_kwargs) # type: ignore[arg-type]
-
- total_output: List[Any] = []
- total_usage: Dict[str, Any] = {}
-
- pending_computer_call_ids = set()
- try:
- async for result in agent.run(messages):
- total_output += result["output"]
- # Try to collect usage if present
- if (
- isinstance(result, dict)
- and "usage" in result
- and isinstance(result["usage"], dict)
- ):
- # Merge usage counters
- for k, v in result["usage"].items():
- if isinstance(v, (int, float)):
- total_usage[k] = total_usage.get(k, 0) + v
- else:
- total_usage[k] = v
- for msg in result.get("output", []):
- if msg.get("type") == "computer_call":
- pending_computer_call_ids.add(msg["call_id"])
- elif msg.get("type") == "computer_call_output":
- pending_computer_call_ids.discard(msg["call_id"])
- elif msg.get("type") == "function_call":
- pending_computer_call_ids.add(msg["call_id"])
- elif msg.get("type") == "function_call_output":
- pending_computer_call_ids.discard(msg["call_id"])
- # exit if no pending computer calls
- if not pending_computer_call_ids:
- break
- except Exception as e:
- logger.error(f"Error running agent: {str(e)}")
- logger.error(traceback.format_exc())
- error = str(e)
+ # Use pre-configured agent only if model matches, otherwise create new one
+ # This ensures the model picker in the UI is respected
+ if self.agent_instance and self.agent_instance.model == model:
+ agent = self.agent_instance
+ else:
+ # Model changed or no pre-configured agent - create new agent with requested model
+ agent = ComputerAgent(model=model, **agent_kwargs) # type: ignore[arg-type]
+
+ total_output: List[Any] = []
+ total_usage: Dict[str, Any] = {}
+
+ pending_computer_call_ids = set()
+ try:
+ async for result in agent.run(messages):
+ total_output += result["output"]
+ # Try to collect usage if present
+ if (
+ isinstance(result, dict)
+ and "usage" in result
+ and isinstance(result["usage"], dict)
+ ):
+ # Merge usage counters
+ for k, v in result["usage"].items():
+ if isinstance(v, (int, float)):
+ total_usage[k] = total_usage.get(k, 0) + v
+ else:
+ total_usage[k] = v
+ for msg in result.get("output", []):
+ if msg.get("type") == "computer_call":
+ pending_computer_call_ids.add(msg["call_id"])
+ elif msg.get("type") == "computer_call_output":
+ pending_computer_call_ids.discard(msg["call_id"])
+ elif msg.get("type") == "function_call":
+ pending_computer_call_ids.add(msg["call_id"])
+ elif msg.get("type") == "function_call_output":
+ pending_computer_call_ids.discard(msg["call_id"])
+ # exit if no pending computer calls
+ if not pending_computer_call_ids:
+ break
+ except Exception as e:
+ logger.error(f"Error running agent: {str(e)}")
+ logger.error(traceback.format_exc())
+ error = str(e)
# Build response payload
payload = {
@@ -185,8 +277,9 @@ class PlaygroundServer:
"usage": total_usage,
"status": "completed" if not error else "failed",
}
+ if ignored:
+ payload["ignored_request_fields"] = sorted(set(ignored))
- # CORS: allow any origin
headers = {
"Cache-Control": "no-cache",
"Connection": "keep-alive",
@@ -194,6 +287,19 @@ class PlaygroundServer:
return JSONResponse(content=payload, headers=headers)
+ def _filter_agent_kwargs(self, requested: Dict[str, Any]) -> Tuple[Dict[str, Any], List[str]]:
+ """Split request agent_kwargs into (accepted, ignored key names)."""
+ accepted: Dict[str, Any] = {}
+ ignored: List[str] = []
+ for key, value in requested.items():
+ if key in _BLOCKED_AGENT_KWARGS:
+ ignored.append(f"agent_kwargs.{key}")
+ elif key in _SAFE_AGENT_KWARGS or self.allow_request_overrides:
+ accepted[key] = value
+ else:
+ ignored.append(f"agent_kwargs.{key}")
+ return accepted, ignored
+
def _find_available_port(self, start_port: int = 8000, max_attempts: int = 100) -> int:
"""Find an available port starting from start_port."""
for port in range(start_port, start_port + max_attempts):
@@ -207,25 +313,38 @@ class PlaygroundServer:
f"Could not find an available port in range {start_port}-{start_port + max_attempts}"
)
- async def start_async(self, port: Optional[int] = None, open_browser: bool = False):
+ async def start_async(
+ self,
+ port: Optional[int] = None,
+ open_browser: bool = False,
+ host: str = DEFAULT_HOST,
+ ):
"""
Start the playground server asynchronously.
Args:
port: Port to run the server on. If None, finds an available port.
open_browser: Whether to open the browser automatically.
+ host: Interface to bind. Defaults to loopback. Pass "0.0.0.0" only if
+ you really want other machines to reach this unauthenticated server.
"""
if port is None:
port = self._find_available_port()
self.port = port
- host = f"http://localhost:{port}"
+ base_url = f"http://localhost:{port}"
- logger.info(f"Starting playground server on {host}")
+ if not _is_loopback_host(host):
+ logger.warning(
+ "Playground server is binding to %s and has no authentication; "
+ "anyone who can reach this address can run the agent.",
+ host,
+ )
+ logger.info(f"Starting playground server on {base_url} (bound to {host})")
if open_browser:
# Construct the playground URL
- encoded_host = quote(host, safe="")
+ encoded_host = quote(base_url, safe="")
encoded_model = quote(self.agent_instance.model, safe="")
encoded_vnc_url = quote("http://localhost:8006/?autoconnect=true", safe="")
@@ -249,20 +368,26 @@ class PlaygroundServer:
config = uvicorn.Config(
self.app,
- host="0.0.0.0",
+ host=host,
port=port,
log_level="info",
)
self.server = uvicorn.Server(config)
await self.server.serve()
- def start(self, port: Optional[int] = None, open_browser: bool = False):
+ def start(
+ self,
+ port: Optional[int] = None,
+ open_browser: bool = False,
+ host: str = DEFAULT_HOST,
+ ):
"""
Start the playground server (blocking).
Args:
port: Port to run the server on. If None, finds an available port.
open_browser: Whether to open the browser automatically.
+ host: Interface to bind. Defaults to loopback (see ``start_async``).
"""
# Check if there's already a running event loop
try:
@@ -273,7 +398,7 @@ class PlaygroundServer:
# Run the server in a separate thread to avoid blocking
server_thread = threading.Thread(
target=self._run_in_new_loop,
- args=(port, open_browser),
+ args=(port, open_browser, host),
daemon=True,
)
server_thread.start()
@@ -285,14 +410,21 @@ class PlaygroundServer:
except RuntimeError:
# No running loop, can use asyncio.run() safely
- asyncio.run(self.start_async(port=port, open_browser=open_browser))
-
- def _run_in_new_loop(self, port: Optional[int] = None, open_browser: bool = False):
+ asyncio.run(self.start_async(port=port, open_browser=open_browser, host=host))
+
+ def _run_in_new_loop(
+ self,
+ port: Optional[int] = None,
+ open_browser: bool = False,
+ host: str = DEFAULT_HOST,
+ ):
"""Helper to run server in a new event loop (for threading)."""
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
try:
- new_loop.run_until_complete(self.start_async(port=port, open_browser=open_browser))
+ new_loop.run_until_complete(
+ self.start_async(port=port, open_browser=open_browser, host=host)
+ )
finally:
new_loop.close()
diff --git a/libs/python/agent/tests/test_playground_server.py b/libs/python/agent/tests/test_playground_server.py
new file mode 100644
index 0000000000..bf605cf3d1
--- /dev/null
+++ b/libs/python/agent/tests/test_playground_server.py
@@ -0,0 +1,295 @@
+"""Security tests for PlaygroundServer.
+
+The playground server has no authentication, so it must only be reachable from
+the local machine, only answer local browser origins, never write request data
+into os.environ, and never let a request redirect the agent's model traffic.
+"""
+
+import asyncio
+import os
+from unittest.mock import patch
+
+import pytest
+
+pytest.importorskip("fastapi")
+pytest.importorskip("httpx")
+
+from cua_agent.playground import server as playground_server # noqa: E402
+from cua_agent.playground.server import PlaygroundServer # noqa: E402
+from fastapi.testclient import TestClient # noqa: E402
+
+EVIL = "https://evil.example"
+
+
+class FakeAgent:
+ """Stand-in for ComputerAgent that records how it was built and what it saw."""
+
+ instances = []
+
+ def __init__(self, model, **kwargs):
+ self.model = model
+ self.kwargs = kwargs
+ self.environ_during_run = None
+ FakeAgent.instances.append(self)
+
+ async def run(self, messages):
+ self.environ_during_run = dict(os.environ)
+ yield {
+ "output": [
+ {
+ "type": "message",
+ "role": "assistant",
+ "content": [{"type": "output_text", "text": "ok"}],
+ }
+ ],
+ "usage": {"total_tokens": 2},
+ }
+
+
[email protected](autouse=True)
+def fake_agent():
+ FakeAgent.instances = []
+ with patch("cua_agent.ComputerAgent", FakeAgent):
+ yield FakeAgent
+
+
+def make_client(**server_kwargs) -> TestClient:
+ return TestClient(PlaygroundServer(**server_kwargs).app, base_url="http://localhost")
+
+
+def post_responses(client, body, **headers):
+ return client.post("/responses", json=body, headers=headers)
+
+
+class TestBinding:
+ @pytest.mark.parametrize(
+ "kwargs, expected_host",
+ [({}, "127.0.0.1"), ({"host": "0.0.0.0"}, "0.0.0.0")],
+ )
+ def test_start_async_binds_loopback_unless_host_given(self, kwargs, expected_host):
+ captured = {}
+
+ class FakeServer:
+ def __init__(self, config):
+ captured["host"] = config.host
+
+ async def serve(self):
+ return None
+
+ with patch.object(playground_server.uvicorn, "Server", FakeServer):
+ asyncio.run(PlaygroundServer().start_async(port=18123, **kwargs))
+
+ assert captured["host"] == expected_host
+
+ def test_agent_open_defaults_to_loopback(self):
+ from cua_agent import agent as agent_module
+
+ seen = {}
+
+ class FakeServer:
+ def __init__(self, agent_instance=None):
+ pass
+
+ def start(self, port=None, open_browser=False, host=None):
+ seen["host"] = host
+
+ with patch("cua_agent.playground.PlaygroundServer", FakeServer):
+ agent_module.ComputerAgent.open(object.__new__(agent_module.ComputerAgent), port=1)
+
+ assert seen["host"] == "127.0.0.1"
+
+
+class TestCors:
+ def preflight(self, client, origin):
+ return client.options(
+ "/responses",
+ headers={
+ "Origin": origin,
+ "Access-Control-Request-Method": "POST",
+ "Access-Control-Request-Headers": "content-type",
+ },
+ )
+
+ def test_foreign_origin_preflight_is_not_allowed(self):
+ resp = self.preflight(make_client(), EVIL)
+ assert "access-control-allow-origin" not in resp.headers
+ assert "access-control-allow-credentials" not in resp.headers
+
+ @pytest.mark.parametrize(
+ "origin",
+ [
+ "http://localhost:3000",
+ "http://localhost",
+ "http://127.0.0.1:5173",
+ "https://localhost:8443",
+ "http://[::1]:3000",
+ ],
+ )
+ def test_loopback_origins_are_allowed_without_credentials(self, origin):
+ resp = self.preflight(make_client(), origin)
+ assert resp.status_code == 200
+ assert resp.headers["access-control-allow-origin"] == origin
+ assert "access-control-allow-credentials" not in resp.headers
+
+ @pytest.mark.parametrize(
+ "origin",
+ [
+ "null",
+ "http://localhost.evil.example",
+ "http://127.0.0.1.evil.example",
+ "http://evil.example:3000",
+ "http://localhost:3000.evil.example",
+ "file://",
+ ],
+ )
+ def test_lookalike_origins_are_rejected(self, origin):
+ resp = self.preflight(make_client(), origin)
+ assert "access-control-allow-origin" not in resp.headers
+
+ def test_explicit_extra_origin_can_be_allowed(self):
+ client = make_client(allowed_origins=["https://dash.example.com/"])
+ resp = self.preflight(client, "https://dash.example.com")
+ assert resp.headers["access-control-allow-origin"] == "https://dash.example.com"
+ assert "access-control-allow-credentials" not in resp.headers
+ assert "access-control-allow-origin" not in self.preflight(client, EVIL).headers
+
+ def test_wildcard_origin_is_refused(self):
+ with pytest.raises(ValueError):
+ PlaygroundServer(allowed_origins=["*"])
+
+
+class TestOriginGuard:
+ BODY = {"model": "m", "input": "hi"}
+
+ def test_cross_origin_post_is_rejected_and_never_runs_the_agent(self, fake_agent):
+ # A text/plain body is a CORS "simple request": no preflight happens, so the
+ # server itself must refuse it.
+ resp = make_client().post(
+ "/responses",
+ content='{"model": "m", "input": "hi"}',
+ headers={"Origin": EVIL, "Content-Type": "text/plain"},
+ )
+ assert resp.status_code == 403
+ assert fake_agent.instances == []
+
+ def test_null_origin_post_is_rejected(self, fake_agent):
+ resp = post_responses(make_client(), self.BODY, Origin="null")
+ assert resp.status_code == 403
+ assert fake_agent.instances == []
+
+ def test_get_from_foreign_origin_is_rejected(self):
+ assert make_client().get("/status", headers={"Origin": EVIL}).status_code == 403
+
+ def test_loopback_origin_post_works(self):
+ resp = post_responses(make_client(), self.BODY, Origin="http://localhost:3000")
+ assert resp.status_code == 200
+ assert resp.json()["status"] == "completed"
+ assert resp.headers["access-control-allow-origin"] == "http://localhost:3000"
+
+ def test_request_without_origin_works(self):
+ # curl, scripts and other non-browser local clients send no Origin.
+ resp = post_responses(make_client(), self.BODY)
+ assert resp.status_code == 200
+ assert make_client().get("/status").status_code == 200
+
+ def test_extra_allowed_origin_post_works(self):
+ client = make_client(allowed_origins=["https://dash.example.com"])
+ assert (
+ post_responses(client, self.BODY, Origin="https://dash.example.com").status_code == 200
+ )
+
+
+class TestEnvOverride:
+ def test_request_env_never_reaches_the_process_environment(self, fake_agent, monkeypatch):
+ monkeypatch.setenv("OPENAI_API_KEY", "dev-key")
+ monkeypatch.delenv("OPENAI_API_BASE", raising=False)
+ before = dict(os.environ)
+
+ resp = post_responses(
+ make_client(),
+ {
+ "model": "m",
+ "input": "hi",
+ "env": {
+ "OPENAI_API_BASE": "http://127.0.0.1:9999/v1",
+ "OPENAI_API_KEY": "from-request",
+ "PATH": "/tmp/evil",
+ },
+ },
+ )
+
+ assert resp.status_code == 200
+ agent = fake_agent.instances[0]
+ assert "OPENAI_API_BASE" not in agent.environ_during_run
+ assert agent.environ_during_run["OPENAI_API_KEY"] == "dev-key"
+ assert agent.environ_during_run["PATH"] == before["PATH"]
+ assert dict(os.environ) == before
+ assert resp.json()["ignored_request_fields"] == ["env"]
+
+ def test_env_is_ignored_even_with_request_overrides_enabled(self, fake_agent, monkeypatch):
+ monkeypatch.delenv("OPENAI_API_BASE", raising=False)
+ client = make_client(allow_request_overrides=True)
+ post_responses(
+ client,
+ {"model": "m", "input": "hi", "env": {"OPENAI_API_BASE": "http://127.0.0.1:9999/v1"}},
+ )
+ assert "OPENAI_API_BASE" not in fake_agent.instances[0].environ_during_run
+ assert "OPENAI_API_BASE" not in os.environ
+
+ def test_empty_env_from_older_clients_is_fine(self):
+ resp = post_responses(make_client(), {"model": "m", "input": "hi", "env": {}})
+ assert resp.status_code == 200
+ assert "ignored_request_fields" not in resp.json()
+
+
+class TestAgentKwargs:
+ def run(self, agent_kwargs, **server_kwargs):
+ resp = post_responses(
+ make_client(**server_kwargs),
+ {"model": "m", "input": "hi", "agent_kwargs": agent_kwargs},
+ )
+ assert resp.status_code == 200
+ return FakeAgent.instances[-1].kwargs, resp.json()
+
+ @pytest.mark.parametrize(
+ "key",
+ ["api_base", "base_url", "api_key", "api_version", "extra_headers", "custom_llm_provider"],
+ )
+ def test_endpoint_and_credential_kwargs_are_dropped(self, key):
+ kwargs, payload = self.run({key: "http://127.0.0.1:9999/v1"})
+ assert key not in kwargs
+ assert payload["ignored_request_fields"] == [f"agent_kwargs.{key}"]
+
+ def test_safe_tuning_kwargs_still_pass_through(self):
+ # These are the options the playground UI sends.
+ kwargs, payload = self.run({"use_prompt_caching": False, "only_n_most_recent_images": 3})
+ assert kwargs == {"use_prompt_caching": False, "only_n_most_recent_images": 3}
+ assert "ignored_request_fields" not in payload
+
+ def test_mixed_kwargs_keep_only_the_safe_ones(self):
+ kwargs, _ = self.run(
+ {"only_n_most_recent_images": 3, "api_base": "http://x", "api_key": "k"}
+ )
+ assert kwargs == {"only_n_most_recent_images": 3}
+
+ def test_opt_in_allows_endpoint_overrides(self):
+ kwargs, payload = self.run(
+ {"api_base": "http://127.0.0.1:9999/v1", "api_key": "k"}, allow_request_overrides=True
+ )
+ assert kwargs == {"api_base": "http://127.0.0.1:9999/v1", "api_key": "k"}
+ assert "ignored_request_fields" not in payload
+
+ @pytest.mark.parametrize(
+ "key", ["trust_remote_code", "trajectory_dir", "tools", "callbacks", "custom_loop"]
+ )
+ def test_code_and_disk_kwargs_are_dropped_even_with_opt_in(self, key):
+ kwargs, _ = self.run({key: True}, allow_request_overrides=True)
+ assert key not in kwargs
+
+ @pytest.mark.parametrize("bad", [["api_base"], "api_base", 5])
+ def test_non_object_agent_kwargs_is_a_400(self, bad):
+ resp = post_responses(make_client(), {"model": "m", "input": "hi", "agent_kwargs": bad})
+ assert resp.status_code == 400
+
+ def test_non_object_body_is_a_400(self):
+ assert make_client().post("/responses", json=["x"]).status_code == 400
diff --git a/libs/typescript/playground/src/types/messages.ts b/libs/typescript/playground/src/types/messages.ts
index 4c5ee7abfc..c1d9592a4e 100644
--- a/libs/typescript/playground/src/types/messages.ts
+++ b/libs/typescript/playground/src/types/messages.ts
@@ -25,7 +25,7 @@ export interface AgentRequest {
provider_type?: string;
[key: string]: unknown;
};
- /** Optional per-request environment variable overrides */
+ /** Per-request environment overrides. cua-agent's PlaygroundServer ignores these; set variables on the server process instead. */
env?: Record<string, string>;
}
// #endregion