1
+"""Regression tests for URL file-upload SSRF protections."""
2
+
3
+from __future__ import annotations
4
+
5
+import socket
6
+from unittest.mock import patch
7
+
8
+import pytest
9
+
10
+from composio.exceptions import BlockedInternalUrlError
11
+from composio.utils.url_safety import assert_safe_fetch_target, is_blocked_ip
12
+
13
+
14
+@pytest.mark.parametrize(
15
+ "address",
16
+ [
17
+ "127.0.0.1",
18
+ "10.0.0.5",
19
+ "169.254.169.254",
20
+ "100.64.0.1",
21
+ "::1",
22
+ "fc00::1",
23
+ "::ffff:127.0.0.1",
24
+ "::127.0.0.1",
25
+ "::7f00:1",
26
+ "::169.254.169.254",
27
+ "64:ff9b::7f00:1",
28
+ "64:ff9b::a9fe:a9fe",
29
+ ],
30
+)
31
+def test_blocks_non_public_addresses(address: str) -> None:
32
+ assert is_blocked_ip(address) is True
33
+
34
+
35
+@pytest.mark.parametrize(
36
+ "address",
37
+ [
38
+ "8.8.8.8",
39
+ "93.184.216.34",
40
+ "2606:4700:4700::1111",
41
+ "::8.8.8.8",
42
+ "64:ff9b::8.8.8.8",
43
+ ],
44
+)
45
+def test_allows_public_addresses(address: str) -> None:
46
+ assert is_blocked_ip(address) is False
47
+
48
+
49
+@pytest.mark.parametrize(
50
+ "url", ["file:///etc/passwd", "ftp://example.com/file", "not a url"]
51
+)
52
+def test_rejects_non_http_urls(url: str) -> None:
53
+ with pytest.raises(BlockedInternalUrlError):
54
+ assert_safe_fetch_target(url)
55
+
56
+
57
+@patch("composio.utils.url_safety.socket.getaddrinfo")
58
+def test_rejects_internal_dns_answers(mock_getaddrinfo) -> None:
59
+ mock_getaddrinfo.return_value = [
60
+ (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0)),
61
+ (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("127.0.0.1", 0)),
62
+ ]
63
+
64
+ with pytest.raises(BlockedInternalUrlError):
65
+ assert_safe_fetch_target("https://example.com/file.pdf")
66
+
67
+
68
+@patch("composio.utils.url_safety.socket.getaddrinfo")
69
+def test_allows_public_dns_answers(mock_getaddrinfo) -> None:
70
+ mock_getaddrinfo.return_value = [
71
+ (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("93.184.216.34", 0)),
72
+ ]
73
+
74
+ assert_safe_fetch_target("https://example.com/file.pdf")