@devries-cameron20 / composio-1 / commits / 727cfafb0
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.

test(security): add SEC-316 edge cases (subdir-traversal, empty name)

Two regression tests for `FileDownloadable.download()`: - `test_basename_collapse_through_subdir_is_rejected`: `name="foo/.."` exercises the case where `Path(name).name` collapses to `..`. The plain `name=".."` test already covers the same `is_relative_to()` rejection, but the through-subdir form makes the basename normalization explicit. Also asserts (per the P3.1 reorder) that `outdir` is not created as a side effect of a rejected payload. - `test_empty_name_safe_fails_at_write_time`: `name=""` makes `outfile == outdir`, which passes the containment check (a path is relative to itself). The download then safe-fails at `outfile.open("wb")` with `IsADirectoryError`. Pinning this documented behavior locks in the safe-fail and stops a future change from silently weakening the check without a test failure.

jkomyno committed May 6, 2026 at 11:55 UTC 727cfafb0d0e815a96b1fead36618cfdc94b6d08
1 file changed +47
python/tests/test_files.py
+47
@@ -2076,3 +2076,50 @@ class TestFileDownloadablePathTraversal:
2076
2077 assert written == outdir / "report.pdf"
2078 assert written.read_bytes() == b"%PDF-1.4"
2079 +
2080 + def test_basename_collapse_through_subdir_is_rejected(self, tmp_path):
2081 + """`Path('foo/..').name == '..'` — the basename strip of a name that
2082 + traverses *through* a subdir collapses to `..`, then the
2083 + `is_relative_to()` check rejects it. Explicit coverage of the
2084 + residual-`..` path through basename normalization (the plain `..`
2085 + test covers the no-subdir case)."""
2086 + from composio.exceptions import ErrorDownloadingFile
2087 +
2088 + outdir = tmp_path / "safe"
2089 + f = FileDownloadable(
2090 + name="foo/..",
2091 + mimetype="application/octet-stream",
2092 + s3url="https://example.com/file",
2093 + )
2094 + with patch(
2095 + "composio.core.models._files.requests.get",
2096 + return_value=self._mock_response(),
2097 + ):
2098 + with pytest.raises(ErrorDownloadingFile, match="Path traversal detected"):
2099 + f.download(outdir)
2100 + # SEC-316 P3.1: check runs before mkdir, so outdir is not created
2101 + # as a side effect of a rejected payload.
2102 + assert not outdir.exists()
2103 +
2104 + def test_empty_name_safe_fails_at_write_time(self, tmp_path):
2105 + """`Path('').name == ''` — `outdir / ''` resolves to `outdir` itself,
2106 + which passes the containment check (a path is relative to itself).
2107 + The write then fails with `IsADirectoryError` because the target is
2108 + the directory. Documents the safe-fail behavior so a future change
2109 + to the check cannot silently weaken it without breaking this test."""
2110 + outdir = tmp_path / "safe"
2111 + f = FileDownloadable(
2112 + name="",
2113 + mimetype="application/octet-stream",
2114 + s3url="https://example.com/file",
2115 + )
2116 + with patch(
2117 + "composio.core.models._files.requests.get",
2118 + return_value=self._mock_response(b"x"),
2119 + ):
2120 + with pytest.raises(IsADirectoryError):
2121 + f.download(outdir)
2122 + # outdir got created (mkdir runs after the check, which passed),
2123 + # but no file was written inside it.
2124 + assert outdir.is_dir()
2125 + assert list(outdir.iterdir()) == []