httpx-secure 1.1.0__tar.gz → 1.2.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/PKG-INFO +4 -4
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/README.md +1 -1
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/httpx_secure/__init__.py +22 -21
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/pyproject.toml +1 -1
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/tests/test___init__.py +4 -4
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/uv.lock +2 -2
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/.editorconfig +0 -0
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/.envrc +0 -0
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/.github/workflows/workflow.yaml +0 -0
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/.gitignore +0 -0
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/.vscode/settings.json +0 -0
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/LICENSE +0 -0
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/httpx_secure/py.typed +0 -0
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/shell.nix +0 -0
- {httpx_secure-1.1.0 → httpx_secure-1.2.0}/tests/conftest.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: httpx-secure
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.2.0
|
|
4
4
|
Summary: Drop-in SSRF protection for httpx
|
|
5
5
|
Project-URL: Repository, https://github.com/Zaczero/httpx-secure
|
|
6
6
|
Project-URL: Issues, https://github.com/Zaczero/httpx-secure/issues
|
|
@@ -29,8 +29,8 @@ Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
|
29
29
|
Classifier: Topic :: System :: Networking
|
|
30
30
|
Classifier: Typing :: Typed
|
|
31
31
|
Requires-Python: >=3.9
|
|
32
|
-
Requires-Dist: anyio
|
|
33
|
-
Requires-Dist: httpx
|
|
32
|
+
Requires-Dist: anyio>=3.0.0
|
|
33
|
+
Requires-Dist: httpx>=0.20.0
|
|
34
34
|
Description-Content-Type: text/markdown
|
|
35
35
|
|
|
36
36
|
# httpx-secure
|
|
@@ -65,7 +65,7 @@ from httpx_secure import httpx_ssrf_protection
|
|
|
65
65
|
client = httpx_ssrf_protection(
|
|
66
66
|
httpx.AsyncClient(),
|
|
67
67
|
dns_cache_size=1000, # Cache up to 1000 DNS resolutions
|
|
68
|
-
dns_cache_ttl=
|
|
68
|
+
dns_cache_ttl=600, # Cache for 10 minutes
|
|
69
69
|
)
|
|
70
70
|
|
|
71
71
|
await client.get("https://public.domain") # Allowed
|
|
@@ -30,7 +30,7 @@ from httpx_secure import httpx_ssrf_protection
|
|
|
30
30
|
client = httpx_ssrf_protection(
|
|
31
31
|
httpx.AsyncClient(),
|
|
32
32
|
dns_cache_size=1000, # Cache up to 1000 DNS resolutions
|
|
33
|
-
dns_cache_ttl=
|
|
33
|
+
dns_cache_ttl=600, # Cache for 10 minutes
|
|
34
34
|
)
|
|
35
35
|
|
|
36
36
|
await client.get("https://public.domain") # Allowed
|
|
@@ -6,8 +6,8 @@ from ipaddress import ip_address
|
|
|
6
6
|
from time import monotonic
|
|
7
7
|
from weakref import WeakValueDictionary
|
|
8
8
|
|
|
9
|
-
from anyio import
|
|
10
|
-
from httpx import RequestError
|
|
9
|
+
from anyio import Lock, connect_tcp, fail_after
|
|
10
|
+
from httpx import ConnectError, ConnectTimeout, RequestError
|
|
11
11
|
|
|
12
12
|
TYPE_CHECKING = False
|
|
13
13
|
if TYPE_CHECKING:
|
|
@@ -23,7 +23,7 @@ if TYPE_CHECKING:
|
|
|
23
23
|
|
|
24
24
|
_AsyncClientT = TypeVar("_AsyncClientT", bound=AsyncClient)
|
|
25
25
|
|
|
26
|
-
__version__ = "1.
|
|
26
|
+
__version__ = "1.2.0"
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
class SSRFProtectionError(RequestError):
|
|
@@ -95,6 +95,7 @@ class _SSRFProtectionHook:
|
|
|
95
95
|
"_cache",
|
|
96
96
|
"_locks",
|
|
97
97
|
"check_globally_reachable",
|
|
98
|
+
"connect_timeout",
|
|
98
99
|
"custom_validator",
|
|
99
100
|
"happy_eyeballs_delay",
|
|
100
101
|
)
|
|
@@ -104,6 +105,7 @@ class _SSRFProtectionHook:
|
|
|
104
105
|
*,
|
|
105
106
|
cache: _DNSCache,
|
|
106
107
|
check_globally_reachable: bool,
|
|
108
|
+
connect_timeout: float | None,
|
|
107
109
|
custom_validator: (
|
|
108
110
|
Callable[
|
|
109
111
|
[str, _IPAddress, int],
|
|
@@ -116,26 +118,23 @@ class _SSRFProtectionHook:
|
|
|
116
118
|
self._cache = cache
|
|
117
119
|
self._locks: WeakValueDictionary[_CacheKey, Lock] = WeakValueDictionary()
|
|
118
120
|
self.check_globally_reachable = check_globally_reachable
|
|
121
|
+
self.connect_timeout = connect_timeout
|
|
119
122
|
self.custom_validator = custom_validator
|
|
120
123
|
self.happy_eyeballs_delay = happy_eyeballs_delay
|
|
121
124
|
|
|
122
|
-
async def _resolve(self, hostname: str, port: int) -> str:
|
|
125
|
+
async def _resolve(self, request: Request, hostname: str, port: int) -> str:
|
|
123
126
|
try:
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
port=port,
|
|
136
|
-
) from e
|
|
137
|
-
|
|
138
|
-
return addr_info[0]
|
|
127
|
+
with fail_after(self.connect_timeout):
|
|
128
|
+
async with await connect_tcp(
|
|
129
|
+
hostname,
|
|
130
|
+
port,
|
|
131
|
+
happy_eyeballs_delay=self.happy_eyeballs_delay,
|
|
132
|
+
) as stream:
|
|
133
|
+
return stream._raw_socket.getpeername()[0]
|
|
134
|
+
except TimeoutError as e:
|
|
135
|
+
raise ConnectTimeout(str(e), request=request) from e
|
|
136
|
+
except OSError as e:
|
|
137
|
+
raise ConnectError(str(e), request=request) from e
|
|
139
138
|
|
|
140
139
|
async def _validate(
|
|
141
140
|
self,
|
|
@@ -175,6 +174,7 @@ class _SSRFProtectionHook:
|
|
|
175
174
|
|
|
176
175
|
async def _process(
|
|
177
176
|
self,
|
|
177
|
+
request: Request,
|
|
178
178
|
hostname: str,
|
|
179
179
|
port: int,
|
|
180
180
|
) -> str:
|
|
@@ -196,7 +196,7 @@ class _SSRFProtectionHook:
|
|
|
196
196
|
raise result
|
|
197
197
|
return result
|
|
198
198
|
|
|
199
|
-
ip_str = await self._resolve(hostname, port)
|
|
199
|
+
ip_str = await self._resolve(request, hostname, port)
|
|
200
200
|
|
|
201
201
|
try:
|
|
202
202
|
await self._validate(ip_str, port, hostname)
|
|
@@ -212,7 +212,7 @@ class _SSRFProtectionHook:
|
|
|
212
212
|
hostname = url.host
|
|
213
213
|
port = url.port or (443 if url.scheme in {"https", "wss"} else 80)
|
|
214
214
|
|
|
215
|
-
ip_str = await self._process(hostname, port)
|
|
215
|
+
ip_str = await self._process(request, hostname, port)
|
|
216
216
|
|
|
217
217
|
request.url = url.copy_with(host=ip_str)
|
|
218
218
|
request.headers["Host"] = hostname
|
|
@@ -257,6 +257,7 @@ def httpx_ssrf_protection(
|
|
|
257
257
|
_SSRFProtectionHook(
|
|
258
258
|
cache=_DNSCache(dns_cache_size, dns_cache_ttl),
|
|
259
259
|
check_globally_reachable=check_globally_reachable,
|
|
260
|
+
connect_timeout=client.timeout.connect,
|
|
260
261
|
custom_validator=custom_validator,
|
|
261
262
|
happy_eyeballs_delay=happy_eyeballs_delay,
|
|
262
263
|
),
|
|
@@ -4,8 +4,8 @@ from ipaddress import IPv4Address, IPv6Address, ip_address
|
|
|
4
4
|
from unittest.mock import AsyncMock, Mock, patch
|
|
5
5
|
|
|
6
6
|
import pytest
|
|
7
|
-
from anyio import
|
|
8
|
-
from httpx import AsyncClient, Request
|
|
7
|
+
from anyio import getaddrinfo
|
|
8
|
+
from httpx import AsyncClient, ConnectError, Request
|
|
9
9
|
|
|
10
10
|
from httpx_secure import SSRFProtectionError, httpx_ssrf_protection
|
|
11
11
|
|
|
@@ -177,10 +177,10 @@ async def test_custom_validator_blocks_specific_ips():
|
|
|
177
177
|
|
|
178
178
|
|
|
179
179
|
async def test_dns_resolution_failure_raises_error(mock_tcp_fixture):
|
|
180
|
-
mock_tcp_fixture.side_effect =
|
|
180
|
+
mock_tcp_fixture.side_effect = OSError("Name or service not known")
|
|
181
181
|
|
|
182
182
|
async with httpx_ssrf_protection(AsyncClient()) as client:
|
|
183
|
-
with pytest.raises(
|
|
183
|
+
with pytest.raises(ConnectError, match="Name or service not known"):
|
|
184
184
|
await client.get(
|
|
185
185
|
"http://this-hostname-definitely-does-not-exist/",
|
|
186
186
|
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|