Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
TypeScript agentic-aiagentsaiai-agentsaiagentsdeveloper-toolsfunction-callinggpt-4javascriptjsllmllmopsmcppythonremote-mcp-serverssetypescript
fix(security): prevent path traversal in FileDownloadable.download() (SEC-316)
Zen Agentf60cdf1ebfe3
diff --git a/python/composio/core/models/_files.py b/python/composio/core/models/_files.pyindex bef1095b6..b3f6215ce 100644--- a/python/composio/core/models/_files.py+++ b/python/composio/core/models/_files.py@@ -547,8 +547,19 @@ class FileDownloadable(BaseModel):s3url: str = Field(..., description="URL of the file.")def download(self, outdir: Path, chunk_size: int = _DEFAULT_CHUNK_SIZE) -> Path:- outfile = outdir / self.name+ # SEC-316: `self.name` comes from the (potentially compromised or+ # MITM'd) Composio API response. Strip directory components with+ # `Path(...).name` so traversal sequences like `../../../foo` collapse+ # to `foo`, then verify the resolved output stays under `outdir` so a+ # name like `output_evil/foo` (sibling-prefix attack) is also rejected.+ safe_name = Path(self.name).name+ outfile = outdir / safe_nameoutdir.mkdir(exist_ok=True, parents=True)+ if not outfile.resolve().is_relative_to(outdir.resolve()):+ raise ErrorDownloadingFile(+ f"Path traversal detected: filename '{self.name}' resolves "+ "outside the intended output directory."+ )response = requests.get(url=self.s3url, stream=True)if response.status_code != 200:raise ErrorDownloadingFile(f"Error downloading file: {self.s3url}")diff --git a/python/tests/test_files.py b/python/tests/test_files.pyindex 1d26fa958..08672564d 100644--- a/python/tests/test_files.py+++ b/python/tests/test_files.py@@ -10,6 +10,7 @@ import pytestfrom composio.client.types import Tool, tool_list_responsefrom composio.core.models._files import (+ FileDownloadable,FileHelper,FileUploadable,_is_url,@@ -1992,3 +1993,78 @@ class TestUrlSanitization:url = "https://example.com/file.jpg"sanitized = _sanitize_url_for_logging(url)assert "[REDACTED]" not in sanitized+++class TestFileDownloadablePathTraversal:+ """SEC-316: server-controlled `name` must not escape the output dir."""++ def _mock_response(self, content: bytes = b"data") -> MagicMock:+ response = MagicMock()+ response.status_code = 200+ response.iter_content = lambda chunk_size: [content]+ return response++ def test_relative_traversal_is_neutralized(self, tmp_path):+ outdir = tmp_path / "safe"+ # outdir does not need to exist yet — download() creates it.+ f = FileDownloadable(+ name="../../../PWNED.sh",+ mimetype="application/octet-stream",+ s3url="https://example.com/file",+ )+ with patch("composio.core.models._files.requests.get",+ return_value=self._mock_response(b"#!/bin/sh\n")):+ written = f.download(outdir)++ # Traversal sequence collapsed to basename and stayed inside outdir.+ assert written == outdir / "PWNED.sh"+ assert written.resolve().is_relative_to(outdir.resolve())+ assert not (tmp_path / "PWNED.sh").exists()+ assert written.read_bytes() == b"#!/bin/sh\n"++ def test_absolute_path_is_neutralized(self, tmp_path):+ outdir = tmp_path / "safe"+ f = FileDownloadable(+ name="/etc/passwd",+ mimetype="application/octet-stream",+ s3url="https://example.com/file",+ )+ with patch("composio.core.models._files.requests.get",+ return_value=self._mock_response(b"x")):+ written = f.download(outdir)++ # `Path('/etc/passwd').name == 'passwd'` — absolute path is stripped.+ assert written == outdir / "passwd"+ assert written.resolve().is_relative_to(outdir.resolve())++ def test_dotdot_only_name_is_rejected(self, tmp_path):+ """`Path('..').name == '..'`. Basename strip alone wouldn't catch it,+ but the second-line `is_relative_to()` check does."""+ from composio.exceptions import ErrorDownloadingFile++ outdir = tmp_path / "safe"+ f = FileDownloadable(+ name="..",+ mimetype="application/octet-stream",+ s3url="https://example.com/file",+ )+ with patch("composio.core.models._files.requests.get",+ return_value=self._mock_response()):+ with pytest.raises(ErrorDownloadingFile, match="Path traversal detected"):+ f.download(outdir)+ # No file was written under the parent.+ assert not (tmp_path / "x").exists()++ def test_safe_filename_passes_through(self, tmp_path):+ outdir = tmp_path / "safe"+ f = FileDownloadable(+ name="report.pdf",+ mimetype="application/pdf",+ s3url="https://example.com/file",+ )+ with patch("composio.core.models._files.requests.get",+ return_value=self._mock_response(b"%PDF-1.4")):+ written = f.download(outdir)++ assert written == outdir / "report.pdf"+ assert written.read_bytes() == b"%PDF-1.4"