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,
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"