Skip to content

Agent Zero AI framework

Fix attachment replay metadata sanitization

TerminallyLazye9d527406403
diff --git a/plugins/_a0_connector/api/ws_connector.py b/plugins/_a0_connector/api/ws_connector.py
index 5c3a16925..2ea8b9a58 100644
--- a/plugins/_a0_connector/api/ws_connector.py
+++ b/plugins/_a0_connector/api/ws_connector.py
@@ -3,7 +3,7 @@ from __future__ import annotations
import asyncio
from typing import TYPE_CHECKING, Any, ClassVar
-from urllib.parse import urlsplit
+from urllib.parse import unquote, urlsplit
from helpers.print_style import PrintStyle
from helpers.ws import WsHandler
@@ -82,8 +82,12 @@ def _attachment_log_metadata(attachments: list[str]) -> dict[str, list[str]]:
normalized = str(attachment or "").strip().replace("\\", "/")
if not normalized:
continue
- parsed = urlsplit(normalized)
+ try:
+ parsed = urlsplit(normalized)
+ except ValueError:
+ continue
path = parsed.path if parsed.scheme else normalized.split("?", 1)[0].split("#", 1)[0]
+ path = unquote(path).replace("\\", "/")
if path.endswith("/"):
continue
name = path.rstrip("/").rsplit("/", 1)[-1]
diff --git a/tests/test_a0_connector_attachment_metadata.py b/tests/test_a0_connector_attachment_metadata.py
index 73a628fcc..6c589a2aa 100644
--- a/tests/test_a0_connector_attachment_metadata.py
+++ b/tests/test_a0_connector_attachment_metadata.py
@@ -34,6 +34,15 @@ def test_attachment_log_metadata_omits_empty_metadata() -> None:
assert _attachment_log_metadata(["", "/"]) == {}
+def test_attachment_log_metadata_decodes_encoded_separators() -> None:
+ assert _attachment_log_metadata(
+ [
+ "https://host/%2Fhome%2Falice%2Fsecret.png",
+ "https://host/C:%5CUsers%5CAlice%5Csecret.png",
+ ]
+ ) == {"attachments": ["secret.png", "secret.png"]}
+
+
class RecordingLog:
def __init__(self) -> None:
self.calls: list[dict[str, object]] = []
@@ -139,6 +148,52 @@ async def test_websocket_text_only_message_keeps_empty_kvps(
assert log.calls[0]["kvps"] == {}
+async def test_websocket_malformed_attachment_url_keeps_message_delivery(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ log = RecordingLog()
+ context = SimpleNamespace(log=log)
+ handler = WsConnector(None, None)
+ monkeypatch.setattr(
+ handler,
+ "_resolve_context",
+ AsyncMock(return_value=(context, "ctx-1")),
+ )
+ monkeypatch.setattr(
+ ws_module,
+ "subscribed_contexts_for_sid",
+ lambda sid: {"ctx-1"} if sid == "sid-cli" else set(),
+ )
+
+ scheduled: list[bool] = []
+
+ def close_scheduled(coroutine: object) -> SimpleNamespace:
+ getattr(coroutine, "close")()
+ scheduled.append(True)
+ return SimpleNamespace()
+
+ monkeypatch.setattr(asyncio, "create_task", close_scheduled)
+
+ result = await handler._handle_send_message(
+ {
+ "context_id": "ctx-1",
+ "message": "Review this",
+ "attachments": ["http://["],
+ "client_message_id": "client-malformed",
+ },
+ "sid-cli",
+ )
+
+ assert result == {
+ "context_id": "ctx-1",
+ "status": "accepted",
+ "client_message_id": "client-malformed",
+ }
+ assert scheduled == [True]
+ assert log.calls[0]["kvps"] == {}
+
+
@pytest.mark.asyncio
@pytest.mark.parametrize(
("payload", "code"),