@devries-cameron20 / composio-1 / commits / 7a3d30a37
Fork 0

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()

`RemoteFile.save()` in `tool_router_session_files.py` shares the same trust-boundary shape as the `FileDownloadable.download()` path traversal fixed in SEC-316: the default save location is built as `~/.composio/files/<self.filename>`, where `self.filename` is `Path(mount_relative_path).name` derived from the (potentially compromised or MITM'd) tool-router API response. Today the basename strip alone makes this safe-by-OS-error — a server returning `mount_relative_path == ".."` produces `filename == ".."`, which collapses the resolved path to `~/.composio/` and `write_bytes()` fails with `IsADirectoryError`. That's a non-security bug surface, but it leaks intent through a confusing error and relies on a happy accident of `Path` semantics. Mirror the SEC-316 pattern: after building the default `save_path`, verify `save_path.resolve().is_relative_to(default_dir.resolve())` and raise `ValidationError` on mismatch. The check only runs on the default-location branch — a caller-supplied `path` is fully trusted and writes wherever the SDK user chooses. Adds `test_save_default_location_rejects_dotdot_filename` covering the `mount_relative_path="foo/.."` case (where `Path("foo/..").name == ".."`) and asserting nothing is written under the cache dir.

jkomyno committed May 6, 2026 at 11:49 UTC 7a3d30a37c9b8f631713b3138b272e71b68b2bcf
2 files changed +31 -2
python/composio/core/models/tool_router_session_files.py
+11 -2
@@ -142,13 +142,22 @@ class RemoteFile:
142 If path is omitted, saves to ~/.composio/files/ using the filename.
143 """
144 content = self.buffer()
145 - home = Path.home()
145 save_path: Path
146
147 if path is not None:
148 save_path = Path(path)
149 else:
151 - save_path = home / COMPOSIO_DIR / TEMP_FILES_DIRECTORY_NAME / self.filename
150 + # SEC-316 defense-in-depth: `self.filename` is `Path(mount_relative_path).name`,
151 + # so directory components are already stripped — but verify the resolved
152 + # default-location path stays inside the cache dir to reject residual `..`
153 + # cases (e.g. ``mount_relative_path == ".."`` keeps ``filename == ".."``).
154 + default_dir = Path.home() / COMPOSIO_DIR / TEMP_FILES_DIRECTORY_NAME
155 + save_path = default_dir / self.filename
156 + if not save_path.resolve().is_relative_to(default_dir.resolve()):
157 + raise ValidationError(
158 + f"Path traversal detected: filename {self.filename!r} resolves "
159 + "outside the intended output directory."
160 + )
161
162 save_path.parent.mkdir(parents=True, exist_ok=True)
163 save_path.write_bytes(content)
python/tests/test_tool_router_session_files.py
+20
@@ -234,3 +234,23 @@ class TestRemoteFile:
234 expected = tmp_path / ".composio" / "files" / "report.pdf"
235 assert Path(out_path) == expected
236 assert expected.read_bytes() == b"pdf content"
237 +
238 + def test_save_default_location_rejects_dotdot_filename(self, tmp_path):
239 + """SEC-316 defense-in-depth: a server-controlled ``mount_relative_path``
240 + whose basename is ``..`` (e.g. ``"foo/.."``) must be rejected before
241 + any bytes touch the disk, not silently fail with ``IsADirectoryError``."""
242 + rf = RemoteFile(
243 + expires_at="2026-01-01",
244 + mount_relative_path="foo/..",
245 + sandbox_mount_prefix="/mnt/files",
246 + download_url="https://example.com/file",
247 + )
248 + assert rf.filename == ".." # `Path("foo/..").name == ".."`
249 +
250 + with patch.object(rf, "buffer", return_value=b"should not be written"):
251 + with patch("pathlib.Path.home", return_value=tmp_path):
252 + with pytest.raises(ValidationError, match="Path traversal detected"):
253 + rf.save()
254 +
255 + # The check raises before mkdir/write, so nothing was written under tmp_path.
256 + assert not (tmp_path / ".composio").exists()