2659
s3url="https://example.com/report.bin",
2660
)
2661
2662
- @patch("composio.core.models._files.safe_get")
2662
+ @patch("composio.core.models._files.safe_request")
2663
def test_download_rejects_oversized_content_length(self, mock_get, tmp_path):
2664
"""A self-declared oversized body is rejected before any bytes are read."""
2665
mock_response = MagicMock()
2673
2674
mock_response.iter_content.assert_not_called()
2675
2676
- @patch("composio.core.models._files.safe_get")
2676
+ @patch("composio.core.models._files.safe_request")
2677
def test_download_rejects_oversized_during_streaming(self, mock_get, tmp_path):
2678
"""A dishonest (here, absent) Content-Length cannot bypass the cap."""
2679
mock_response = MagicMock()
2686
with pytest.raises(ResponseTooLargeError):
2687
self._downloadable().download(outdir=tmp_path, root=tmp_path, max_size=1024)
2688
2689
- @patch("composio.core.models._files.safe_get")
2689
+ @patch("composio.core.models._files.safe_request")
2690
def test_download_removes_partial_file_on_failure(self, mock_get, tmp_path):
2691
"""A truncated download must not be left behind as if it succeeded."""
2692
mock_response = MagicMock()
2701
2702
assert list(tmp_path.iterdir()) == []
2703
2704
- @patch("composio.core.models._files.safe_get")
2704
+ @patch("composio.core.models._files.safe_request")
2705
def test_download_accepts_file_within_limit(self, mock_get, tmp_path):
2706
"""A body under the cap is written through unchanged."""
2707
mock_response = MagicMock()
2718
assert outfile.exists()
2719
assert outfile.read_bytes() == b"x" * 256 + b"y" * 256
2720
2721
- @patch("composio.core.models._files.safe_get")
2721
+ @patch("composio.core.models._files.safe_request")
2722
def test_download_wraps_stream_failure_and_removes_partial_file(
2723
self, mock_get, tmp_path
2724
):
2740
2741
assert list(tmp_path.iterdir()) == []
2742
2743
- @patch("composio.core.models._files.safe_get")
2743
+ @patch("composio.core.models._files.safe_request")
2744
def test_download_wraps_write_failure_and_removes_partial_file(
2745
self, mock_get, tmp_path
2746
):
2942
s3url="https://example.com/file",
2943
)
2944
with patch(
2945
- "composio.core.models._files.safe_get",
2945
+ "composio.core.models._files.safe_request",
2946
return_value=self._mock_response(b"#!/bin/sh\n"),
2947
):
2948
written = f.download(outdir, root=outdir)
2961
s3url="https://example.com/file",
2962
)
2963
with patch(
2964
- "composio.core.models._files.safe_get",
2964
+ "composio.core.models._files.safe_request",
2965
return_value=self._mock_response(b"x"),
2966
):
2967
written = f.download(outdir, root=outdir)
2982
s3url="https://example.com/file",
2983
)
2984
with patch(
2985
- "composio.core.models._files.safe_get",
2985
+ "composio.core.models._files.safe_request",
2986
return_value=self._mock_response(),
2987
):
2988
with pytest.raises(ErrorDownloadingFile, match="Path traversal detected"):
2998
s3url="https://example.com/file",
2999
)
3000
with patch(
3001
- "composio.core.models._files.safe_get",
3001
+ "composio.core.models._files.safe_request",
3002
return_value=self._mock_response(b"%PDF-1.4"),
3003
):
3004
written = f.download(outdir, root=outdir)
3014
s3url="https://example.com/file",
3015
)
3016
with patch(
3017
- "composio.core.models._files.safe_get",
3017
+ "composio.core.models._files.safe_request",
3018
return_value=self._mock_response(b"%PDF-1.4"),
3019
) as mock_get:
3020
f.download(outdir, root=outdir)
3021
3022
mock_get.assert_called_once_with(
3023
+ "GET",
3024
"https://example.com/file",
3025
stream=True,
3026
timeout=(5, 60),
3034
s3url="https://example.com/file?token=abc",
3035
)
3036
with patch(
3036
- "composio.core.models._files.safe_get",
3037
+ "composio.core.models._files.safe_request",
3038
side_effect=requests.exceptions.Timeout(
3039
"Max retries exceeded with url: /file?token=abc"
3040
),
3059
s3url="https://example.com/file?token=abc",
3060
)
3061
with patch(
3061
- "composio.core.models._files.safe_get",
3062
+ "composio.core.models._files.safe_request",
3063
return_value=response,
3064
):
3065
with pytest.raises(ErrorDownloadingFile) as exc_info:
3079
s3url="https://example.com/file?token=abc",
3080
)
3081
with patch(
3081
- "composio.core.models._files.safe_get",
3082
+ "composio.core.models._files.safe_request",
3083
return_value=response,
3084
):
3085
with pytest.raises(ErrorDownloadingFile) as exc_info:
3103
s3url="https://example.com/file",
3104
)
3105
with patch(
3105
- "composio.core.models._files.safe_get",
3106
+ "composio.core.models._files.safe_request",
3107
return_value=self._mock_response(),
3108
):
3109
with pytest.raises(ErrorDownloadingFile, match="Path traversal detected"):
3130
s3url="https://example.com/file",
3131
)
3132
with patch(
3132
- "composio.core.models._files.safe_get",
3133
+ "composio.core.models._files.safe_request",
3134
return_value=self._mock_response(b"x"),
3135
):
3136
with pytest.raises(ErrorDownloadingFile, match="no usable basename"):
3139
# Rejected before `mkdir`, so nothing was created on disk at all.
3140
assert not outdir.exists()
3141
3142
+ def test_nul_byte_filename_is_rejected(self, tmp_path):
3143
+ """`self.name` is untrusted by the same rule as the slugs. Without this
3144
+ check a NUL byte escapes as a raw ValueError from `resolve()`."""
3145
+ from composio.exceptions import ErrorDownloadingFile
3146
+
3147
+ outdir = tmp_path / "safe"
3148
+ f = FileDownloadable(
3149
+ name="a\x00b",
3150
+ mimetype="application/octet-stream",
3151
+ s3url="https://example.com/file",
3152
+ )
3153
+ with patch(
3154
+ "composio.core.models._files.safe_request",
3155
+ return_value=self._mock_response(b"x"),
3156
+ ):
3157
+ with pytest.raises(ErrorDownloadingFile, match="NUL byte"):
3158
+ f.download(outdir, root=outdir)
3159
+ assert not outdir.exists()
3160
+
3161
@pytest.mark.parametrize(
3142
- "name,reason",
3162
+ "name,expected",
3163
[
3144
- ("NUL", "reserved device name"),
3145
- ("nul.txt", "reserved device name"),
3146
- ("NUL.tar.gz", "reserved device name"),
3147
- ("COM1", "reserved device name"),
3148
- ("report.txt:payload", "reserved by Windows"),
3149
- ("a\x00b", "NUL byte"),
3150
- ("😀" * 128, "longer than"),
3151
- ("x" * 300, "longer than"),
3164
+ ("report_2026-09-29T10:30:00.csv", "report_2026-09-29T10_30_00.csv"),
3165
+ ("What is this?.png", "What is this_.png"),
3166
+ ('invoice "final".pdf', "invoice _final_.pdf"),
3167
+ ("NUL", "_NUL"),
3168
+ ("NUL.tar.gz", "_NUL.tar.gz"),
3169
+ ("x" * 300 + ".pdf", "x" * 124 + ".pdf"),
3170
+ ("請" * 70 + ".pdf", "請" * 41 + ".pdf"),
3171
],
3172
)
3154
- def test_unsafe_filenames_are_rejected(self, name, reason, tmp_path):
3155
- """`self.name` is untrusted by the same rule as the slugs. Without these
3156
- checks a NUL byte escapes as a raw ValueError from `resolve()`, an
3157
- over-long name as a raw OSError mid-write, and `NUL` opens the Windows
3158
- null device — silently discarding the payload while returning a path."""
3159
- from composio.exceptions import ErrorDownloadingFile
3160
-
3173
+ def test_unportable_filenames_are_made_portable(self, name, expected, tmp_path):
3174
+ """Ordinary names with a `:` or `?`, reserved device names, and names over
3175
+ the byte limit are written under a portable name rather than failing
3176
+ the download after the tool has already run."""
3177
outdir = tmp_path / "safe"
3178
f = FileDownloadable(
3179
name=name,
3181
s3url="https://example.com/file",
3182
)
3183
with patch(
3168
- "composio.core.models._files.safe_get",
3184
+ "composio.core.models._files.safe_request",
3185
return_value=self._mock_response(b"x"),
3186
):
3171
- with pytest.raises(ErrorDownloadingFile, match=reason):
3172
- f.download(outdir, root=outdir)
3173
- assert not outdir.exists()
3187
+ outfile = f.download(outdir, root=outdir)
3188
+ assert outfile == (outdir / expected).resolve()
3189
+ assert outfile.read_bytes() == b"x"
3190
3191
3192
class TestDownloadDirSlugTraversal:
3236
response.status_code = 200
3237
response.iter_content = lambda chunk_size: [content]
3238
response.close = MagicMock()
3223
- return patch("composio.core.models._files.safe_get", return_value=response)
3239
+ return patch("composio.core.models._files.safe_request", return_value=response)
3240
3241
@pytest.mark.parametrize("slug", TRAVERSALS)
3242
def test_hostile_tool_slug_is_rejected(self, slug, tmp_path):
3485
s3url="https://s3.example.com/file",
3486
)
3487
with patch(
3472
- "composio.core.models._files.safe_get",
3488
+ "composio.core.models._files.safe_request",
3489
return_value=self._download_response(),
3490
) as mock_get:
3491
f.download(tmp_path / "out", root=tmp_path / "out")
3492
3477
- # `safe_get` is the guard: it validates the target and connects to the
3478
- # address it validated, rather than re-resolving the hostname.
3479
- assert mock_get.call_args.args == ("https://s3.example.com/file",)
3493
+ # `safe_request` is the guard: it validates each hop and connects to
3494
+ # the address it validated, rather than re-resolving the hostname.
3495
+ assert mock_get.call_args.args == ("GET", "https://s3.example.com/file")
3496
3497
def test_download_blocked_url_never_reaches_the_network(self, tmp_path):
3498
outdir = tmp_path / "out"
3514
mock_send.assert_not_called()
3515
assert not outdir.exists()
3516
3501
- def test_download_refuses_to_follow_redirects(self, tmp_path):
3517
+ def test_download_follows_a_validated_redirect(self, tmp_path):
3518
+ """A presigned URL may answer with a redirect (an S3 region redirect,
3519
+ say). The download follows it, and every hop goes through the pinned,
3520
+ validated request rather than a plain ``requests`` redirect."""
3521
+ redirect = MagicMock()
3522
+ redirect.status_code = 307
3523
+ redirect.headers = {"Location": "https://s3.eu.example.com/file"}
3524
f = FileDownloadable(
3525
name="report.pdf",
3526
mimetype="application/pdf",
3527
s3url="https://s3.example.com/file",
3528
)
3529
with patch(
3508
- "composio.core.models._files.safe_get",
3509
- return_value=self._download_response(),
3510
- ) as mock_get:
3511
- f.download(tmp_path / "out", root=tmp_path / "out")
3512
-
3513
- # `safe_get` never follows redirects, and passing `allow_redirects`
3514
- # through to it would be a way to turn that off.
3515
- assert "allow_redirects" not in mock_get.call_args.kwargs
3530
+ "composio.utils.url_safety._pinned_request",
3531
+ side_effect=[redirect, self._download_response()],
3532
+ ) as mock_pinned:
3533
+ outfile = f.download(tmp_path / "out", root=tmp_path / "out")
3534
+
3535
+ assert outfile.read_bytes() == b"%PDF-1.4"
3536
+ assert [call.args[:2] for call in mock_pinned.call_args_list] == [
3537
+ ("GET", "https://s3.example.com/file"),
3538
+ ("GET", "https://s3.eu.example.com/file"),
3539
+ ]
3540
3541
def test_upload_bytes_to_s3_goes_through_safe_request(self):
3542
client = self._s3_client("https://s3.example.com/upload")