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()) == []