Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(security): keep WhatsApp media files in the workspace and check download URLs
trohitg276d833b1178
diff --git a/docs-internal/media_transport.md b/docs-internal/media_transport.mdindex 65c733f0..27124008 100644--- a/docs-internal/media_transport.md+++ b/docs-internal/media_transport.md@@ -137,6 +137,11 @@ user-supplied path onto the workspace root with no check at all, souploaded it to a third-party provider. `coerce_file_param` closes that byconstruction for every node that adopts it.+`whatsappSend` (and `socialSend` to WhatsApp) had the same hole until+2026-10-25: a `file_path` went to plain `open()`, so an agent could send any+readable host file to any number. It now reads through `coerce_file_param`+with the sending node's context.+---## 4. The APIdiff --git a/docs-internal/node-logic-flows/google_workspace/googleDrive.md b/docs-internal/node-logic-flows/google_workspace/googleDrive.mdindex cc8e9e75..a6e220e2 100644--- a/docs-internal/node-logic-flows/google_workspace/googleDrive.md+++ b/docs-internal/node-logic-flows/google_workspace/googleDrive.md@@ -129,7 +129,7 @@ flowchart TD- **Database writes**: `api_usage_metrics` row per call via `track_google_usage` -> `save_api_usage_metric` with `service='google_drive'`.- **Broadcasts**: none from the operation; executor emits standard `node_status`.-- **External API calls**: Drive API v3 - `files().create/get/list`, `permissions().create`, `files().get_media`. Plus `httpx.AsyncClient.get(file_url)` on upload-from-URL.+- **External API calls**: Drive API v3 - `files().create/get/list`, `permissions().create`, `files().get_media`. Plus `httpx.AsyncClient.get(file_url)` on upload-from-URL, after `netpolicy.resolved_url_block_reason` accepts the URL on every address it resolves to (`fetch_policy`); a refused URL raises `NodeUserError`.- **File I/O**: in-memory `io.BytesIO` buffers only; no disk writes.- **Subprocess**: none.diff --git a/docs-internal/node-logic-flows/whatsapp/whatsappSend.md b/docs-internal/node-logic-flows/whatsapp/whatsappSend.mdindex 90a8ef7a..8ecd0bd1 100644--- a/docs-internal/node-logic-flows/whatsapp/whatsappSend.md+++ b/docs-internal/node-logic-flows/whatsapp/whatsappSend.md@@ -38,8 +38,8 @@ Key parameters (see frontend definition for the full list and `displayOptions.sh| `format_markdown` | boolean | `true` | no | When true and text message, convert GFM to WhatsApp syntax via `markdown_formatter.to_whatsapp` || `media_source` | options | `base64` | conditional | `base64`, `file`, or `url` for media types || `media_data` | string | `""` | conditional | Base64 data when `media_source == 'base64'` |-| `file_path` | string/object | `""` | conditional | Either a path string or upload dict `{type: 'upload', filename, mimeType}` |-| `media_url` | string | `""` | conditional | URL when `media_source == 'url'` |+| `file_path` | string/object | `""` | conditional | A file in this workflow's workspace (a path or a file reference, read through `coerce_file_param`), or an upload dict `{type: 'upload', filename, mimeType}`. A path outside the workspace is refused |+| `media_url` | string | `""` | conditional | URL when `media_source == 'url'`. Checked with `services/netpolicy.py` on every address it resolves to before the download: cloud metadata and OpenCompany's own ports are refused || `mime_type` | string | `""` | no | Optional override || `caption` | string | `""` | no | Caption for media messages || `filename` | string | `""` | no | Filename for document messages |@@ -119,12 +119,13 @@ flowchart TD- **Markdown transform**: only runs when `message_type == 'text'` AND `format_markdown` truthy. Overwrites `parameters['message']` in place before RPC call.- **Type-specific result fields**: `match message_type` adds `preview`, `media_source`, `caption`, `filename`, `mime_type`, `uploaded_file`, `location`, or `contact_name` based on message type.- **File upload detection**: if `file_path` is a dict with `type == 'upload'`, the `_service.py` send handler uploads bytes and the result includes `uploaded_file` and `mime_type` from the upload dict.+- **File containment**: any other `file_path` is read with `coerce_file_param` from the workflow's workspace (the node passes its `NodeContext`). A path outside it, or a caller with no workspace (the `whatsapp_send` WebSocket command), gets a `success=false` envelope; nothing is read.## Side Effects- **Database writes**: none directly from this handler. (API-usage cost is recorded by the `@Operation` `cost` metadata via `BaseNode.execute()`.)- **Broadcasts**: none directly - standard node_status transitions come from `BaseNode.execute()`.-- **External calls**: WebSocket RPC to the Go `whatsapp-rpc` service via [`nodes/whatsapp/_service.py::whatsapp_rpc_call`](../../../server/nodes/whatsapp/_service.py). Default URL `ws://localhost:5683/ws/rpc` (overridable via `WHATSAPP_RPC_URL`). For media URLs, `_service.py` additionally downloads via `httpx`.+- **External calls**: WebSocket RPC to the Go `whatsapp-rpc` service via [`nodes/whatsapp/_service.py::whatsapp_rpc_call`](../../../server/nodes/whatsapp/_service.py). Default URL `ws://localhost:5683/ws/rpc` (overridable via `WHATSAPP_RPC_URL`). For media URLs, `_service.py` additionally downloads via `httpx`, after `netpolicy.resolved_url_block_reason` accepts the URL (`fetch_policy`: the owner's network is allowed, cloud metadata and OpenCompany's own ports never).- **File I/O**: none in this handler - file uploads are handled in `_service.py`.- **Subprocess**: none.diff --git a/server/nodes/google/drive/__init__.py b/server/nodes/google/drive/__init__.pyindex 040c03b7..0e6927a4 100644--- a/server/nodes/google/drive/__init__.py+++ b/server/nodes/google/drive/__init__.py@@ -10,7 +10,7 @@ import httpxfrom googleapiclient.http import MediaIoBaseDownload, MediaIoBaseUploadfrom pydantic import BaseModel, ConfigDict, Field-from services.plugin import ActionNode, NodeContext, Operation, TaskQueue+from services.plugin import ActionNode, NodeContext, NodeUserError, Operation, TaskQueuefrom services.plugin.approval import ApprovalSpecfrom .._credentials import GoogleCredential@@ -144,6 +144,11 @@ class DriveNode(ActionNode):mime_type = params.mime_typeif params.file_url:+ from services.netpolicy import fetch_policy, resolved_url_block_reason++ reason = await resolved_url_block_reason(params.file_url, fetch_policy())+ if reason:+ raise NodeUserError(f"Cannot download {params.file_url}: {reason}.")async with httpx.AsyncClient() as client:resp = await client.get(params.file_url, timeout=60.0)resp.raise_for_status()diff --git a/server/nodes/whatsapp/_base.py b/server/nodes/whatsapp/_base.pyindex 0cf39978..b19629ca 100644--- a/server/nodes/whatsapp/_base.py+++ b/server/nodes/whatsapp/_base.py@@ -114,18 +114,19 @@ async def _enrich_messages_with_media(messages: List[Dict[str, Any]], rpc_call,return messages-async def handle_whatsapp_send(node_id: str, node_type: str, parameters: Dict[str, Any], context: Dict[str, Any]) -> Dict[str, Any]:+async def handle_whatsapp_send(node_id: str, node_type: str, parameters: Dict[str, Any], context: Dict[str, Any], *, ctx: Any = None) -> Dict[str, Any]:"""Handle WhatsApp send message node via Go RPC service.Supports all message types: text, image, video, audio, document, sticker, location, contactRecipients: phone number, group_id, or channel_jid (newsletter)- Media sources: base64, file path, or URL+ Media sources: base64, a file in the workflow's workspace, or URLArgs:node_id: The node IDnode_type: The node type (whatsappSend)parameters: Resolved parameterscontext: Execution context+ ctx: The node's context, which names the workspace a file is read fromReturns:Execution result dict@@ -171,7 +172,7 @@ async def handle_whatsapp_send(node_id: str, node_type: str, parameters: Dict[stparameters["message"] = to_whatsapp(parameters["message"])# Call WhatsApp Go RPC service via handler - pass full params- data = await whatsapp_send_handler(parameters)+ data = await whatsapp_send_handler(parameters, ctx=ctx)success = data.get("success", False)if not success:diff --git a/server/nodes/whatsapp/_service.py b/server/nodes/whatsapp/_service.pyindex c7a31a07..6b9b0652 100644--- a/server/nodes/whatsapp/_service.py+++ b/server/nodes/whatsapp/_service.py@@ -473,9 +473,12 @@ async def handle_whatsapp_qr() -> dict:return {"success": False, "connected": False, "error": str(e)}-async def handle_whatsapp_send(params: dict) -> dict:+async def handle_whatsapp_send(params: dict, *, ctx: Any = None) -> dict:"""Send a WhatsApp message via direct RPC - supports all message types.+ ``ctx`` is the sending node's context. A file is read only from its+ workflow's workspace, so a caller without one cannot send a file by path.+Uses _send_lock to serialize sends - Go service processes sequentially.Params from frontend node (snake_case):@@ -556,23 +559,31 @@ async def handle_whatsapp_send(params: dict) -> dict:mime_type = mime_type or file_param.get("mimeType")filename = filename or file_param.get("filename")elif file_param:- import base64 as b64+ from services.media.workspace import coerce_file_param+ from services.plugin import NodeUserError+ # The model can choose this path, so it is read only+ # from the workflow's workspace, never anywhere on the host.try:- with open(file_param, "rb") as f:- media_data = b64.b64encode(f.read()).decode("utf-8")- except Exception as e:- return {"success": False, "error": f"Failed to read file: {e}"}+ name, blob = coerce_file_param(file_param, ctx=ctx)+ except NodeUserError as e:+ return {"success": False, "error": str(e)}+ media_data = base64.b64encode(blob).decode("utf-8")+ filename = filename or nameelif media_source == "url":media_url = params.get("media_url")if media_url:import httpx- import base64 as b64+ from services.netpolicy import fetch_policy, resolved_url_block_reason++ reason = await resolved_url_block_reason(media_url, fetch_policy())+ if reason:+ return {"success": False, "error": f"Cannot download media from {media_url}: {reason}."}try:async with httpx.AsyncClient() as http:resp = await http.get(media_url, timeout=30)- media_data = b64.b64encode(resp.content).decode("utf-8")+ media_data = base64.b64encode(resp.content).decode("utf-8")except Exception as e:return {"success": False, "error": f"Failed to download media: {e}"}diff --git a/server/nodes/whatsapp/_social.py b/server/nodes/whatsapp/_social.pyindex 46ada2ca..7de5e201 100644--- a/server/nodes/whatsapp/_social.py+++ b/server/nodes/whatsapp/_social.py@@ -94,7 +94,7 @@ async def social_send_adapter(payload: Dict[str, Any], ctx: Any) -> Dict[str, Anparams["is_reply"] = Trueparams["reply_message_id"] = payload.get("reply_message_id", "")- return await handle_whatsapp_send(params)+ return await handle_whatsapp_send(params, ctx=ctx)__all__ = ["social_send_adapter"]diff --git a/server/nodes/whatsapp/whatsapp_send.py b/server/nodes/whatsapp/whatsapp_send.pyindex 999f725a..1df83ba8 100644--- a/server/nodes/whatsapp/whatsapp_send.py+++ b/server/nodes/whatsapp/whatsapp_send.py@@ -119,7 +119,7 @@ class WhatsAppSendParams(BaseModel):)file_path: str = Field(default="",- description="Server file path or uploaded filename",+ description="A file in this workflow's workspace, or an uploaded file",json_schema_extra={"widget": "file","accept": "*/*",@@ -303,6 +303,7 @@ class WhatsAppSendNode(ActionNode):node_type=self.type,parameters=params.model_dump(by_alias=False),context=ctx.raw,+ ctx=ctx,)if response.get("success"):return response.get("result") or responsediff --git a/server/services/netpolicy.py b/server/services/netpolicy.pyindex 403e1741..6b8d41ee 100644--- a/server/services/netpolicy.py+++ b/server/services/netpolicy.py@@ -1,11 +1,15 @@"""Where an outbound connection made for an agent may go.-Two callers: the agent's browser (``nodes/browser``: each navigation, and the-egress proxy on the addresses it resolved itself), and the custom MCP-connectors (``nodes/mcp_connector``: the server the owner added, on every-address its host resolves to). What they reach is untrusted, and an agent-follows untrusted instructions (prompt injection), so these stay out of-reach:+Callers:+- the agent's browser (``nodes/browser``): each navigation, and the egress+ proxy on the addresses it resolved itself;+- the custom MCP connectors (``nodes/mcp_connector``): the server the owner+ added, on every address its host resolves to;+- nodes that download a URL they were given, such as a WhatsApp media URL+ or a Google Drive upload's source (:func:`resolved_url_block_reason`).++What they reach is untrusted, and an agent follows untrusted instructions+(prompt injection), so these stay out of reach:- **OpenCompany itself**: its ports on this machine (the app, the codesidecar, the WhatsApp bridge, Temporal) and the managed Chromes' debugging@@ -18,15 +22,17 @@ reach:protected ports above.Pure functions, except :func:`local_addresses`, which reads this machine's-interface addresses (no network).+interface addresses (no network), and :func:`resolve_addresses`, a DNS lookup."""from __future__ import annotations+import asyncioimport ipaddressimport os+import socketfrom dataclasses import dataclass, field-from typing import FrozenSet, Iterable, Optional, Tuple, Union+from typing import FrozenSet, Iterable, List, Optional, Tuple, Unionfrom urllib.parse import urlsplitIPAddress = Union[ipaddress.IPv4Address, ipaddress.IPv6Address]@@ -197,16 +203,69 @@ def url_block_reason(url: str, policy: NetPolicy) -> Optional[str]:return None+def fetch_policy() -> NetPolicy:+ """The policy for a URL a node downloads itself: the owner's own network+ is allowed, OpenCompany's own ports and cloud metadata never are."""+ return NetPolicy(allow_private_network=True, blocked_local_ports=own_ports_from_env(), local_addresses=local_addresses())+++async def resolve_addresses(host: str, port: int) -> List[IPAddress]:+ """Every address ``host`` resolves to; empty when it does not resolve."""+ try:+ infos = await asyncio.get_running_loop().getaddrinfo(host, port, type=socket.SOCK_STREAM)+ except (socket.gaierror, UnicodeError):+ return []+ found = []+ for info in infos:+ try:+ found.append(ipaddress.ip_address(str(info[4][0]).split("%", 1)[0]))+ except ValueError:+ continue+ return found+++async def resolved_url_block_reason(url: str, policy: NetPolicy) -> Optional[str]:+ """Why a node may not download ``url``, or ``None``.++ :func:`url_block_reason`, plus a check of every address the host resolves+ to, because the node connects itself and nothing else checks it.+ """+ reason = url_block_reason(url, policy)+ if reason:+ return reason+ parts = urlsplit(url.strip())+ host = parts.hostname or ""+ if not host:+ return "no host"+ if literal_ip(host) is not None:+ return None+ try:+ port = parts.port or (443 if parts.scheme.lower() == "https" else 80)+ except ValueError:+ return "not a valid port"+ addresses = await resolve_addresses(host, port)+ if not addresses:+ return f"{host} could not be found"+ for address in addresses:+ reason = address_block_reason(address, port, policy)+ if reason:+ return reason+ return None++__all__ = ["NAVIGABLE_SCHEMES","NetPolicy","address_block_reason","domain_allowed",+ "fetch_policy","host_block_reason","is_local_network","literal_ip","local_addresses","own_ports_from_env","parse_allowed_domains",+ "resolve_addresses",+ "resolved_url_block_reason","url_block_reason",]diff --git a/server/tests/nodes/test_google_workspace.py b/server/tests/nodes/test_google_workspace.pyindex 00c6910e..67b6ec59 100644--- a/server/tests/nodes/test_google_workspace.py+++ b/server/tests/nodes/test_google_workspace.py@@ -482,6 +482,19 @@ class TestDrive:harness.assert_envelope(result, success=False)assert "file_url" in result["error"].lower() or "file_content" in result["error"].lower()+ async def test_upload_refuses_a_metadata_url_without_downloading(self, harness):+ service = MagicMock(name="DriveService")+ with _patch_creds("googleDrive"), _patch_build("googleDrive", service), patch("httpx.AsyncClient") as http:+ result = await harness.execute(+ "googleDrive",+ {"operation": "upload", "filename": "keys.json", "file_url": "http://169.254.169.254/latest/meta-data/"},+ )++ harness.assert_envelope(result, success=False)+ assert "metadata" in result["error"]+ http.assert_not_called()+ service.files.return_value.create.assert_not_called()+async def test_unknown_operation_returns_error(self, harness):with _patch_creds("googleDrive"), _patch_build("googleDrive", MagicMock()):result = await harness.execute(diff --git a/server/tests/nodes/test_whatsapp.py b/server/tests/nodes/test_whatsapp.pyindex 25eef783..b3371e19 100644--- a/server/tests/nodes/test_whatsapp.py+++ b/server/tests/nodes/test_whatsapp.py@@ -90,7 +90,7 @@ class TestWhatsappSend:"""When format_markdown=true, parameters.message is rewritten before RPC call."""captured = {}- async def fake_send(params):+ async def fake_send(params, *, ctx=None):captured["message"] = params.get("message")return {"success": True}diff --git a/server/tests/nodes/test_whatsapp_media_sources.py b/server/tests/nodes/test_whatsapp_media_sources.pynew file mode 100644index 00000000..ce181f61--- /dev/null+++ b/server/tests/nodes/test_whatsapp_media_sources.py@@ -0,0 +1,97 @@+"""Where a WhatsApp send reads its media from (nodes/whatsapp/_service.py).++whatsappSend is an AI tool, so the model can choose a media path or URL. A+file is read only from the workflow's workspace, and a URL is downloaded only+after services/netpolicy.py accepts it.+"""++from __future__ import annotations++import base64+import ipaddress+from types import SimpleNamespace+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++from nodes.whatsapp import _service+from services import netpolicy+++def rpc():+ client = MagicMock(name="WhatsAppRpc")+ client.call = AsyncMock(return_value={"message_id": "m1"})+ with patch.object(_service, "get_client", AsyncMock(return_value=client)):+ yield client+++def _media(message_type: str = "document", **extra) -> dict:+ return {"recipient_type": "phone", "phone": "15550001111", "message_type": message_type, **extra}+++def _ctx(workspace) -> SimpleNamespace:+ return SimpleNamespace(workspace_dir=str(workspace), node_id="send")+++async def test_reads_a_file_from_the_workspace(tmp_path, rpc):+ (tmp_path / "report.pdf").write_bytes(b"%PDF-1.4 fixture")+ result = await _service.handle_whatsapp_send(_media(media_source="file", file_path="report.pdf"), ctx=_ctx(tmp_path))+ assert result["success"] is True+ media = rpc.call.await_args.args[1]["media_data"]+ assert base64.b64decode(media["data"]) == b"%PDF-1.4 fixture"+ assert media["filename"] == "report.pdf"++[email protected]("relative", [True, False])+async def test_refuses_a_file_outside_the_workspace(tmp_path, rpc, relative):+ workspace = tmp_path / "workspace"+ workspace.mkdir()+ secret = tmp_path / "credentials.db"+ secret.write_bytes(b"secret")+ path = "../credentials.db" if relative else str(secret)+ result = await _service.handle_whatsapp_send(_media(media_source="file", file_path=path), ctx=_ctx(workspace))+ assert result["success"] is False+ assert "outside this workflow's workspace" in result["error"]+ rpc.call.assert_not_awaited()+++async def test_refuses_a_path_when_the_caller_has_no_workspace(tmp_path, rpc):+ secret = tmp_path / "credentials.db"+ secret.write_bytes(b"secret")+ result = await _service.handle_whatsapp_send(_media(media_source="file", file_path=str(secret)))+ assert result["success"] is False+ rpc.call.assert_not_awaited()+++async def test_an_uploaded_file_still_sends(rpc):+ upload = {"type": "upload", "data": base64.b64encode(b"png").decode(), "mimeType": "image/png", "filename": "a.png"}+ result = await _service.handle_whatsapp_send(_media("image", media_source="file", file_path=upload))+ assert result["success"] is True+ assert rpc.call.await_args.args[1]["media_data"]["mime_type"] == "image/png"+++async def test_refuses_a_metadata_url_without_downloading(rpc):+ with patch("httpx.AsyncClient") as http:+ result = await _service.handle_whatsapp_send(+ _media(media_source="url", media_url="http://169.254.169.254/latest/meta-data/"))+ assert result["success"] is False+ assert "metadata" in result["error"]+ http.assert_not_called()+ rpc.call.assert_not_awaited()+++async def test_downloads_a_public_url(monkeypatch, rpc):+ async def resolve(host, port):+ return [ipaddress.ip_address("93.184.216.34")]++ monkeypatch.setattr(netpolicy, "resolve_addresses", resolve)+ http = MagicMock(name="AsyncClient")+ http.__aenter__ = AsyncMock(return_value=http)+ http.__aexit__ = AsyncMock(return_value=False)+ http.get = AsyncMock(return_value=MagicMock(content=b"jpeg"))+ with patch("httpx.AsyncClient", return_value=http):+ result = await _service.handle_whatsapp_send(+ _media("image", media_source="url", media_url="https://cdn.example.com/a.jpg"))+ assert result["success"] is True+ assert base64.b64decode(rpc.call.await_args.args[1]["media_data"]["data"]) == b"jpeg"diff --git a/server/tests/services/test_netpolicy_fetch.py b/server/tests/services/test_netpolicy_fetch.pynew file mode 100644index 00000000..75efc75a--- /dev/null+++ b/server/tests/services/test_netpolicy_fetch.py@@ -0,0 +1,68 @@+"""A node's own download of a URL it was given (services/netpolicy.py)."""++from __future__ import annotations++import ipaddress++import pytest++from services import netpolicy+from services.netpolicy import NetPolicy, fetch_policy, resolved_url_block_reason++APP_PORT = 5678+POLICY = NetPolicy(allow_private_network=True, blocked_local_ports=frozenset({APP_PORT}),+ local_addresses=frozenset({ipaddress.ip_address("127.0.0.1")}))+++def _resolving_to(monkeypatch, *addresses: str) -> None:+ async def resolve(host, port):+ return [ipaddress.ip_address(address) for address in addresses]++ monkeypatch.setattr(netpolicy, "resolve_addresses", resolve)++[email protected]("url", [+ "http://169.254.169.254/latest/meta-data/",+ "http://metadata.google.internal/computeMetadata/v1/",+ f"http://127.0.0.1:{APP_PORT}/api/credentials",+ "file:///etc/passwd",+ "ftp://example.com/file",+])+async def test_refused_before_any_lookup(url, monkeypatch):+ async def resolve(host, port):+ raise AssertionError("no lookup expected")++ monkeypatch.setattr(netpolicy, "resolve_addresses", resolve)+ assert await resolved_url_block_reason(url, POLICY)+++async def test_a_name_that_resolves_to_an_own_port_is_refused(monkeypatch):+ _resolving_to(monkeypatch, "93.184.216.34", "127.0.0.1")+ assert "OpenCompany" in await resolved_url_block_reason(f"http://sneaky.example:{APP_PORT}/", POLICY)+++async def test_a_name_that_resolves_to_metadata_is_refused(monkeypatch):+ _resolving_to(monkeypatch, "169.254.169.254")+ assert "metadata" in await resolved_url_block_reason("https://rebind.example/latest", POLICY)+++async def test_a_public_name_is_allowed(monkeypatch):+ _resolving_to(monkeypatch, "93.184.216.34")+ assert await resolved_url_block_reason("https://cdn.example.com/a.jpg", POLICY) is None+++async def test_the_owners_network_is_allowed(monkeypatch):+ _resolving_to(monkeypatch, "192.168.1.20")+ assert await resolved_url_block_reason("http://nas.local/photo.jpg", POLICY) is None+++async def test_a_name_that_does_not_resolve_is_refused(monkeypatch):+ _resolving_to(monkeypatch)+ assert "could not be found" in await resolved_url_block_reason("https://nowhere.invalid/", POLICY)+++def test_the_fetch_policy_refuses_the_configured_ports(monkeypatch):+ monkeypatch.setenv("PYTHON_BACKEND_PORT", "5999")+ policy = fetch_policy()+ assert 5999 in policy.blocked_local_ports+ assert policy.allow_private_network