Skip to content

Commit 2c7aad3

Browse files
committed
Stop reflecting agent task proxy payloads
1 parent 05a7e3f commit 2c7aad3

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

services/ui/ui/app.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2992,6 +2992,16 @@ def _json_safe(value):
29922992
return value
29932993

29942994

2995+
def _agent_status_response(status):
2996+
try:
2997+
status_code = int(status)
2998+
except (TypeError, ValueError):
2999+
status_code = 502
3000+
if status_code < 100 or status_code > 599:
3001+
status_code = 502
3002+
return jsonify({"ok": 200 <= status_code < 300, "status_code": status_code}), status_code
3003+
3004+
29953005
# ---------------------------------------------------------------------------
29963006
# Agent mode endpoints (proxy to agent service)
29973007
# ---------------------------------------------------------------------------
@@ -3021,8 +3031,8 @@ def agent_get_task(task_id):
30213031
if task_id is None:
30223032
return jsonify({"error": "invalid task id"}), 400
30233033
try:
3024-
data, status = _agent_request("GET", _agent_task_path(task_id))
3025-
return jsonify(_json_safe(data)), status
3034+
_, status = _agent_request("GET", _agent_task_path(task_id))
3035+
return _agent_status_response(status)
30263036
except Exception:
30273037
log.exception("agent service unavailable")
30283038
return jsonify({"error": "agent service unavailable"}), 503
@@ -3036,10 +3046,10 @@ def agent_approve_steps(task_id):
30363046
return jsonify({"error": "invalid task id"}), 400
30373047
body = request.get_json(silent=True) or {}
30383048
try:
3039-
data, status = _agent_request("POST", _agent_task_path(task_id, "/approve"), json_body=body)
3049+
_, status = _agent_request("POST", _agent_task_path(task_id, "/approve"), json_body=body)
30403050
event = "agent_steps_approved" if 200 <= status < 300 else "agent_steps_approve_failed"
30413051
_ui_audit.append(event, {"task_id": task_id, "status_code": status})
3042-
return jsonify(_json_safe(data)), status
3052+
return _agent_status_response(status)
30433053
except Exception:
30443054
log.exception("agent service unavailable")
30453055
return jsonify({"error": "agent service unavailable"}), 503
@@ -3053,10 +3063,10 @@ def agent_deny_steps(task_id):
30533063
return jsonify({"error": "invalid task id"}), 400
30543064
body = request.get_json(silent=True) or {}
30553065
try:
3056-
data, status = _agent_request("POST", _agent_task_path(task_id, "/deny"), json_body=body)
3066+
_, status = _agent_request("POST", _agent_task_path(task_id, "/deny"), json_body=body)
30573067
event = "agent_steps_denied" if 200 <= status < 300 else "agent_steps_deny_failed"
30583068
_ui_audit.append(event, {"task_id": task_id, "status_code": status})
3059-
return jsonify(_json_safe(data)), status
3069+
return _agent_status_response(status)
30603070
except Exception:
30613071
log.exception("agent service unavailable")
30623072
return jsonify({"error": "agent service unavailable"}), 503
@@ -3069,10 +3079,10 @@ def agent_cancel_task(task_id):
30693079
if task_id is None:
30703080
return jsonify({"error": "invalid task id"}), 400
30713081
try:
3072-
data, status = _agent_request("POST", _agent_task_path(task_id, "/cancel"), json_body={})
3082+
_, status = _agent_request("POST", _agent_task_path(task_id, "/cancel"), json_body={})
30733083
event = "agent_task_cancelled" if 200 <= status < 300 else "agent_task_cancel_failed"
30743084
_ui_audit.append(event, {"task_id": task_id, "status_code": status})
3075-
return jsonify(_json_safe(data)), status
3085+
return _agent_status_response(status)
30763086
except Exception:
30773087
log.exception("agent service unavailable")
30783088
return jsonify({"error": "agent service unavailable"}), 503

tests/test_ui.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,11 +606,14 @@ def test_emergency_panic_reports_unsupported_in_sandbox_without_body(self, clien
606606
assert resp.get_json()["feature"] == "emergency_panic"
607607

608608
def test_agent_approve_logs_failure_when_agent_rejects(self, client):
609-
with patch("ui.app._agent_request", return_value=({"error": "conflict"}, 409)), \
609+
upstream_payload = {"error": "<script>alert(1)</script>", "task_id": "task-123"}
610+
with patch("ui.app._agent_request", return_value=(upstream_payload, 409)), \
610611
patch("ui.app._ui_audit.append") as mock_append:
611612
resp = client.post("/api/agent/task/task-123/approve", json={"approve_all": True})
612613

613614
assert resp.status_code == 409
615+
assert resp.get_json() == {"ok": False, "status_code": 409}
616+
assert "<script>" not in resp.get_data(as_text=True)
614617
mock_append.assert_called_once_with(
615618
"agent_steps_approve_failed",
616619
{"task_id": "task-123", "status_code": 409},

0 commit comments

Comments
 (0)