@devries-cameron20 / composio-1 / commits / f60cdf1eb
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): prevent path traversal in FileDownloadable.download() (SEC-316)

The Composio Python SDK's `FileDownloadable.download()` constructed the output path as `outdir / self.name`, where `self.name` is taken directly from the Composio API response without sanitization. A compromised or man-in-the-middle'd API server could return a filename like `../../../../PWNED.sh` and write arbitrary files outside the configured output directory on the user's machine. Fix mirrors the equivalent change made in mercury's legacy FileDownloadable (commit e3915b82c3 + c3d675dbcc): - Strip directory components from `self.name` via `Path(...).name`, collapsing traversal sequences to a basename. - As a second-line defense, verify the resolved output path stays within `outdir` using `Path.is_relative_to()` (correct directory containment semantics, vs. naive `str.startswith()` which is vulnerable to sibling-prefix attacks). Adds 4 unit tests in `TestFileDownloadablePathTraversal` covering relative traversal, absolute paths, dotdot-only names, and the safe pass-through case. Reference: https://cwe.mitre.org/data/definitions/22.html CVSS: 8.2 (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:H/A:N) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> Co-authored-by: Srujan A <srujan@composio.dev>

Zen Agent committed May 6, 2026 at 06:17 UTC f60cdf1ebfe34c65bfb0d8fbca331cacfe0f926e
2 files changed +88 -1
python/composio/core/models/_files.py
+12 -1
@@ -547,8 +547,19 @@ class FileDownloadable(BaseModel):
547 s3url: str = Field(..., description="URL of the file.")
548
549 def download(self, outdir: Path, chunk_size: int = _DEFAULT_CHUNK_SIZE) -> Path:
550 - outfile = outdir / self.name
550 + # SEC-316: `self.name` comes from the (potentially compromised or
551 + # MITM'd) Composio API response. Strip directory components with
552 + # `Path(...).name` so traversal sequences like `../../../foo` collapse
553 + # to `foo`, then verify the resolved output stays under `outdir` so a
554 + # name like `output_evil/foo` (sibling-prefix attack) is also rejected.
555 + safe_name = Path(self.name).name
556 + outfile = outdir / safe_name
557 outdir.mkdir(exist_ok=True, parents=True)
558 + if not outfile.resolve().is_relative_to(outdir.resolve()):
559 + raise ErrorDownloadingFile(
560 + f"Path traversal detected: filename '{self.name}' resolves "
561 + "outside the intended output directory."
562 + )
563 response = requests.get(url=self.s3url, stream=True)
564 if response.status_code != 200:
565 raise ErrorDownloadingFile(f"Error downloading file: {self.s3url}")
python/tests/test_files.py
+76
@@ -10,6 +10,7 @@ import pytest
10
11 from composio.client.types import Tool, tool_list_response
12 from composio.core.models._files import (
13 + FileDownloadable,
14 FileHelper,
15 FileUploadable,
16 _is_url,
@@ -1992,3 +1993,78 @@ class TestUrlSanitization:
1993 url = "https://example.com/file.jpg"
1994 sanitized = _sanitize_url_for_logging(url)
1995 assert "[REDACTED]" not in sanitized
1996 +
1997 +
1998 +class TestFileDownloadablePathTraversal:
1999 + """SEC-316: server-controlled `name` must not escape the output dir."""
2000 +
2001 + def _mock_response(self, content: bytes = b"data") -> MagicMock:
2002 + response = MagicMock()
2003 + response.status_code = 200
2004 + response.iter_content = lambda chunk_size: [content]
2005 + return response
2006 +
2007 + def test_relative_traversal_is_neutralized(self, tmp_path):
2008 + outdir = tmp_path / "safe"
2009 + # outdir does not need to exist yet — download() creates it.
2010 + f = FileDownloadable(
2011 + name="../../../PWNED.sh",
2012 + mimetype="application/octet-stream",
2013 + s3url="https://example.com/file",
2014 + )
2015 + with patch("composio.core.models._files.requests.get",
2016 + return_value=self._mock_response(b"#!/bin/sh\n")):
2017 + written = f.download(outdir)
2018 +
2019 + # Traversal sequence collapsed to basename and stayed inside outdir.
2020 + assert written == outdir / "PWNED.sh"
2021 + assert written.resolve().is_relative_to(outdir.resolve())
2022 + assert not (tmp_path / "PWNED.sh").exists()
2023 + assert written.read_bytes() == b"#!/bin/sh\n"
2024 +
2025 + def test_absolute_path_is_neutralized(self, tmp_path):
2026 + outdir = tmp_path / "safe"
2027 + f = FileDownloadable(
2028 + name="/etc/passwd",
2029 + mimetype="application/octet-stream",
2030 + s3url="https://example.com/file",
2031 + )
2032 + with patch("composio.core.models._files.requests.get",
2033 + return_value=self._mock_response(b"x")):
2034 + written = f.download(outdir)
2035 +
2036 + # `Path('/etc/passwd').name == 'passwd'` — absolute path is stripped.
2037 + assert written == outdir / "passwd"
2038 + assert written.resolve().is_relative_to(outdir.resolve())
2039 +
2040 + def test_dotdot_only_name_is_rejected(self, tmp_path):
2041 + """`Path('..').name == '..'`. Basename strip alone wouldn't catch it,
2042 + but the second-line `is_relative_to()` check does."""
2043 + from composio.exceptions import ErrorDownloadingFile
2044 +
2045 + outdir = tmp_path / "safe"
2046 + f = FileDownloadable(
2047 + name="..",
2048 + mimetype="application/octet-stream",
2049 + s3url="https://example.com/file",
2050 + )
2051 + with patch("composio.core.models._files.requests.get",
2052 + return_value=self._mock_response()):
2053 + with pytest.raises(ErrorDownloadingFile, match="Path traversal detected"):
2054 + f.download(outdir)
2055 + # No file was written under the parent.
2056 + assert not (tmp_path / "x").exists()
2057 +
2058 + def test_safe_filename_passes_through(self, tmp_path):
2059 + outdir = tmp_path / "safe"
2060 + f = FileDownloadable(
2061 + name="report.pdf",
2062 + mimetype="application/pdf",
2063 + s3url="https://example.com/file",
2064 + )
2065 + with patch("composio.core.models._files.requests.get",
2066 + return_value=self._mock_response(b"%PDF-1.4")):
2067 + written = f.download(outdir)
2068 +
2069 + assert written == outdir / "report.pdf"
2070 + assert written.read_bytes() == b"%PDF-1.4"