Skip to content

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(security): apply SEC-316 defense-in-depth to RemoteFile.save()

jkomyno7a3d30a37c9b
diff --git a/python/composio/core/models/tool_router_session_files.py b/python/composio/core/models/tool_router_session_files.py
index 78b2e6dcd..0d9f3c584 100644
--- a/python/composio/core/models/tool_router_session_files.py
+++ b/python/composio/core/models/tool_router_session_files.py
@@ -142,13 +142,22 @@ class RemoteFile:
If path is omitted, saves to ~/.composio/files/ using the filename.
"""
content = self.buffer()
- home = Path.home()
save_path: Path
if path is not None:
save_path = Path(path)
else:
- save_path = home / COMPOSIO_DIR / TEMP_FILES_DIRECTORY_NAME / self.filename
+ # SEC-316 defense-in-depth: `self.filename` is `Path(mount_relative_path).name`,
+ # so directory components are already stripped — but verify the resolved
+ # default-location path stays inside the cache dir to reject residual `..`
+ # cases (e.g. ``mount_relative_path == ".."`` keeps ``filename == ".."``).
+ default_dir = Path.home() / COMPOSIO_DIR / TEMP_FILES_DIRECTORY_NAME
+ save_path = default_dir / self.filename
+ if not save_path.resolve().is_relative_to(default_dir.resolve()):
+ raise ValidationError(
+ f"Path traversal detected: filename {self.filename!r} resolves "
+ "outside the intended output directory."
+ )
save_path.parent.mkdir(parents=True, exist_ok=True)
save_path.write_bytes(content)
diff --git a/python/tests/test_tool_router_session_files.py b/python/tests/test_tool_router_session_files.py
index 18d66d4ff..8f00d4927 100644
--- a/python/tests/test_tool_router_session_files.py
+++ b/python/tests/test_tool_router_session_files.py
@@ -234,3 +234,23 @@ class TestRemoteFile:
expected = tmp_path / ".composio" / "files" / "report.pdf"
assert Path(out_path) == expected
assert expected.read_bytes() == b"pdf content"
+
+ def test_save_default_location_rejects_dotdot_filename(self, tmp_path):
+ """SEC-316 defense-in-depth: a server-controlled ``mount_relative_path``
+ whose basename is ``..`` (e.g. ``"foo/.."``) must be rejected before
+ any bytes touch the disk, not silently fail with ``IsADirectoryError``."""
+ rf = RemoteFile(
+ expires_at="2026-01-01",
+ mount_relative_path="foo/..",
+ sandbox_mount_prefix="/mnt/files",
+ download_url="https://example.com/file",
+ )
+ assert rf.filename == ".." # `Path("foo/..").name == ".."`
+
+ with patch.object(rf, "buffer", return_value=b"should not be written"):
+ with patch("pathlib.Path.home", return_value=tmp_path):
+ with pytest.raises(ValidationError, match="Path traversal detected"):
+ rf.save()
+
+ # The check raises before mkdir/write, so nothing was written under tmp_path.
+ assert not (tmp_path / ".composio").exists()