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(python): cap automatic file download size at 100 MiB

jkomyno54d07dc5f53a
diff --git a/python/composio/core/models/_files.py b/python/composio/core/models/_files.py
index 87616c4df..4f9400c46 100644
--- a/python/composio/core/models/_files.py
+++ b/python/composio/core/models/_files.py
@@ -676,6 +676,7 @@ class FileDownloadable(BaseModel):
chunk_size: int = _DEFAULT_CHUNK_SIZE,
*,
root: Path,
+ max_size: int = _MAX_RESPONSE_SIZE,
) -> Path:
"""Fetch the file into ``outdir``.
@@ -686,6 +687,10 @@ class FileDownloadable(BaseModel):
checking containment against a directory that untrusted input has
already relocated is not a check at all, and ``outdir`` may be
exactly such a directory. Callers must name the anchor explicitly.
+ :param max_size: Maximum number of bytes to write to disk. ``s3url`` is
+ an API-response field, so the body behind it is untrusted: the
+ streamed byte count is authoritative because ``Content-Length`` can
+ be absent or dishonest.
"""
# SEC-316: `self.name` also comes from the (potentially compromised or
# MITM'd) API response. Collapsed to a bare filename and checked against
@@ -712,6 +717,18 @@ class FileDownloadable(BaseModel):
f"Error downloading file: {_sanitize_url_for_logging(self.s3url)}"
)
+ # Early abort for a self-declared oversized body. The header is only a
+ # hint — `parse_content_length` returns None for anything untrustworthy
+ # and the streaming counter below is the authoritative limit.
+ content_length = parse_content_length(response.headers.get("Content-Length"))
+ if content_length is not None and content_length > max_size:
+ response.close()
+ raise ResponseTooLargeError(
+ f"File size ({content_length} bytes) exceeds maximum allowed "
+ f"size ({max_size} bytes)"
+ )
+
+ total_bytes = 0
try:
# Only once the fetch is validated and connected, so a blocked URL
# leaves no directory behind — and inside the `try`, so a failure
@@ -719,8 +736,29 @@ class FileDownloadable(BaseModel):
outdir.mkdir(exist_ok=True, parents=True)
with outfile.open("wb") as fd:
for chunk in response.iter_content(chunk_size=chunk_size):
- fd.write(chunk)
+ if chunk:
+ total_bytes += len(chunk)
+ if total_bytes > max_size:
+ raise ResponseTooLargeError(
+ "Response size exceeds maximum allowed size "
+ f"({max_size} bytes)"
+ )
+ fd.write(chunk)
+ except ResponseTooLargeError:
+ # Propagates uncaught — callers must see the limit hit — but the
+ # truncated file must not be left behind as if it were the download.
+ outfile.unlink(missing_ok=True)
+ raise
except requests.exceptions.RequestException as e:
+ outfile.unlink(missing_ok=True)
+ raise ErrorDownloadingFile(
+ "Error downloading file: "
+ f"{_sanitize_url_for_logging(self.s3url)}. Error: {type(e).__name__}"
+ ) from e
+ except OSError as e:
+ # A failing `fd.write` (disk full, permissions) would otherwise
+ # escape the `ErrorDownloadingFile` contract this method documents.
+ outfile.unlink(missing_ok=True)
raise ErrorDownloadingFile(
"Error downloading file: "
f"{_sanitize_url_for_logging(self.s3url)}. Error: {type(e).__name__}"
diff --git a/python/tests/test_files.py b/python/tests/test_files.py
index bdefd38ad..8f9731929 100644
--- a/python/tests/test_files.py
+++ b/python/tests/test_files.py
@@ -2630,6 +2630,77 @@ class TestResponseSizeLimit:
assert mimetype == "image/jpeg"
+class TestDownloadSizeLimit:
+ """``FileDownloadable.download`` streams an untrusted body to disk."""
+
+ @staticmethod
+ def _downloadable() -> FileDownloadable:
+ return FileDownloadable(
+ name="report.bin",
+ mimetype="application/octet-stream",
+ s3url="https://example.com/report.bin",
+ )
+
+ @patch("composio.core.models._files.safe_get")
+ def test_download_rejects_oversized_content_length(self, mock_get, tmp_path):
+ """A self-declared oversized body is rejected before any bytes are read."""
+ mock_response = MagicMock()
+ mock_response.status_code = 200
+ mock_response.headers = {"Content-Length": "200000000"}
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ with pytest.raises(ResponseTooLargeError):
+ self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024)
+
+ mock_response.iter_content.assert_not_called()
+
+ @patch("composio.core.models._files.safe_get")
+ def test_download_rejects_oversized_during_streaming(self, mock_get, tmp_path):
+ """A dishonest (here, absent) Content-Length cannot bypass the cap."""
+ mock_response = MagicMock()
+ mock_response.status_code = 200
+ mock_response.headers = {}
+ mock_response.iter_content.return_value = [b"x" * 512 for _ in range(4)]
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ with pytest.raises(ResponseTooLargeError):
+ self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024)
+
+ @patch("composio.core.models._files.safe_get")
+ def test_download_removes_partial_file_on_failure(self, mock_get, tmp_path):
+ """A truncated download must not be left behind as if it succeeded."""
+ mock_response = MagicMock()
+ mock_response.status_code = 200
+ mock_response.headers = {}
+ mock_response.iter_content.return_value = [b"x" * 512 for _ in range(4)]
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ with pytest.raises(ResponseTooLargeError):
+ self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024)
+
+ assert list(tmp_path.iterdir()) == []
+
+ @patch("composio.core.models._files.safe_get")
+ def test_download_accepts_file_within_limit(self, mock_get, tmp_path):
+ """A body under the cap is written through unchanged."""
+ mock_response = MagicMock()
+ mock_response.status_code = 200
+ mock_response.headers = {}
+ mock_response.iter_content.return_value = [b"x" * 256, b"y" * 256]
+ mock_response.close = MagicMock()
+ mock_get.return_value = mock_response
+
+ outfile = self._downloadable().download(
+ outdir=tmp_path, root=tmp_path, max_size=1024
+ )
+
+ assert outfile.exists()
+ assert outfile.read_bytes() == b"x" * 256 + b"y" * 256
+
+
class TestRedirectHandling:
"""Test redirect handling (redirects should be rejected)."""