@devries-cameron20 / composio Mirror · read-only / commits / 54d07dc5f
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.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 14:45 UTC

fix(python): cap automatic file download size at 100 MiB

`FileDownloadable.download` streamed the response straight to disk with no byte accounting, so an untrusted `s3url` could fill the disk. Add the same `Content-Length` pre-check plus authoritative streamed-byte counter the sibling `_fetch_file_from_url` already uses, capped at `_MAX_RESPONSE_SIZE` and overridable per call via `max_size`. Also close two gaps the write loop left open: an `OSError` from `fd.write` (disk full, permissions) escaped the documented `ErrorDownloadingFile` contract, and any failure left a partial file on disk that no caller was told about. Every failure path now unlinks the partial file; `ResponseTooLargeError` still propagates uncaught so callers see the limit. Claude-Session: https://claude.ai/code/session_01K1hH9PMmd6KPKdkACX553z

jkomyno committed Aug 28, 2026 at 09:51 UTC 54d07dc5f53a0a7ccaa7ac7fa70b2e94546a764b
2 files changed +110 -1
python/composio/core/models/_files.py
+39 -1
@@ -676,6 +676,7 @@ class FileDownloadable(BaseModel):
676 chunk_size: int = _DEFAULT_CHUNK_SIZE,
677 *,
678 root: Path,
679 + max_size: int = _MAX_RESPONSE_SIZE,
680 ) -> Path:
681 """Fetch the file into ``outdir``.
682
@@ -686,6 +687,10 @@ class FileDownloadable(BaseModel):
687 checking containment against a directory that untrusted input has
688 already relocated is not a check at all, and ``outdir`` may be
689 exactly such a directory. Callers must name the anchor explicitly.
690 + :param max_size: Maximum number of bytes to write to disk. ``s3url`` is
691 + an API-response field, so the body behind it is untrusted: the
692 + streamed byte count is authoritative because ``Content-Length`` can
693 + be absent or dishonest.
694 """
695 # SEC-316: `self.name` also comes from the (potentially compromised or
696 # MITM'd) API response. Collapsed to a bare filename and checked against
@@ -712,6 +717,18 @@ class FileDownloadable(BaseModel):
717 f"Error downloading file: {_sanitize_url_for_logging(self.s3url)}"
718 )
719
720 + # Early abort for a self-declared oversized body. The header is only a
721 + # hint — `parse_content_length` returns None for anything untrustworthy
722 + # and the streaming counter below is the authoritative limit.
723 + content_length = parse_content_length(response.headers.get("Content-Length"))
724 + if content_length is not None and content_length > max_size:
725 + response.close()
726 + raise ResponseTooLargeError(
727 + f"File size ({content_length} bytes) exceeds maximum allowed "
728 + f"size ({max_size} bytes)"
729 + )
730 +
731 + total_bytes = 0
732 try:
733 # Only once the fetch is validated and connected, so a blocked URL
734 # leaves no directory behind — and inside the `try`, so a failure
@@ -719,8 +736,29 @@ class FileDownloadable(BaseModel):
736 outdir.mkdir(exist_ok=True, parents=True)
737 with outfile.open("wb") as fd:
738 for chunk in response.iter_content(chunk_size=chunk_size):
722 - fd.write(chunk)
739 + if chunk:
740 + total_bytes += len(chunk)
741 + if total_bytes > max_size:
742 + raise ResponseTooLargeError(
743 + "Response size exceeds maximum allowed size "
744 + f"({max_size} bytes)"
745 + )
746 + fd.write(chunk)
747 + except ResponseTooLargeError:
748 + # Propagates uncaught — callers must see the limit hit — but the
749 + # truncated file must not be left behind as if it were the download.
750 + outfile.unlink(missing_ok=True)
751 + raise
752 except requests.exceptions.RequestException as e:
753 + outfile.unlink(missing_ok=True)
754 + raise ErrorDownloadingFile(
755 + "Error downloading file: "
756 + f"{_sanitize_url_for_logging(self.s3url)}. Error: {type(e).__name__}"
757 + ) from e
758 + except OSError as e:
759 + # A failing `fd.write` (disk full, permissions) would otherwise
760 + # escape the `ErrorDownloadingFile` contract this method documents.
761 + outfile.unlink(missing_ok=True)
762 raise ErrorDownloadingFile(
763 "Error downloading file: "
764 f"{_sanitize_url_for_logging(self.s3url)}. Error: {type(e).__name__}"
python/tests/test_files.py
+71
@@ -2630,6 +2630,77 @@ class TestResponseSizeLimit:
2630 assert mimetype == "image/jpeg"
2631
2632
2633 +class TestDownloadSizeLimit:
2634 + """``FileDownloadable.download`` streams an untrusted body to disk."""
2635 +
2636 + @staticmethod
2637 + def _downloadable() -> FileDownloadable:
2638 + return FileDownloadable(
2639 + name="report.bin",
2640 + mimetype="application/octet-stream",
2641 + s3url="https://example.com/report.bin",
2642 + )
2643 +
2644 + @patch("composio.core.models._files.safe_get")
2645 + def test_download_rejects_oversized_content_length(self, mock_get, tmp_path):
2646 + """A self-declared oversized body is rejected before any bytes are read."""
2647 + mock_response = MagicMock()
2648 + mock_response.status_code = 200
2649 + mock_response.headers = {"Content-Length": "200000000"}
2650 + mock_response.close = MagicMock()
2651 + mock_get.return_value = mock_response
2652 +
2653 + with pytest.raises(ResponseTooLargeError):
2654 + self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024)
2655 +
2656 + mock_response.iter_content.assert_not_called()
2657 +
2658 + @patch("composio.core.models._files.safe_get")
2659 + def test_download_rejects_oversized_during_streaming(self, mock_get, tmp_path):
2660 + """A dishonest (here, absent) Content-Length cannot bypass the cap."""
2661 + mock_response = MagicMock()
2662 + mock_response.status_code = 200
2663 + mock_response.headers = {}
2664 + mock_response.iter_content.return_value = [b"x" * 512 for _ in range(4)]
2665 + mock_response.close = MagicMock()
2666 + mock_get.return_value = mock_response
2667 +
2668 + with pytest.raises(ResponseTooLargeError):
2669 + self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024)
2670 +
2671 + @patch("composio.core.models._files.safe_get")
2672 + def test_download_removes_partial_file_on_failure(self, mock_get, tmp_path):
2673 + """A truncated download must not be left behind as if it succeeded."""
2674 + mock_response = MagicMock()
2675 + mock_response.status_code = 200
2676 + mock_response.headers = {}
2677 + mock_response.iter_content.return_value = [b"x" * 512 for _ in range(4)]
2678 + mock_response.close = MagicMock()
2679 + mock_get.return_value = mock_response
2680 +
2681 + with pytest.raises(ResponseTooLargeError):
2682 + self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024)
2683 +
2684 + assert list(tmp_path.iterdir()) == []
2685 +
2686 + @patch("composio.core.models._files.safe_get")
2687 + def test_download_accepts_file_within_limit(self, mock_get, tmp_path):
2688 + """A body under the cap is written through unchanged."""
2689 + mock_response = MagicMock()
2690 + mock_response.status_code = 200
2691 + mock_response.headers = {}
2692 + mock_response.iter_content.return_value = [b"x" * 256, b"y" * 256]
2693 + mock_response.close = MagicMock()
2694 + mock_get.return_value = mock_response
2695 +
2696 + outfile = self._downloadable().download(
2697 + outdir=tmp_path, root=tmp_path, max_size=1024
2698 + )
2699 +
2700 + assert outfile.exists()
2701 + assert outfile.read_bytes() == b"x" * 256 + b"y" * 256
2702 +
2703 +
2704 class TestRedirectHandling:
2705 """Test redirect handling (redirects should be rejected)."""
2706