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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: httpx-secure
3
- Version: 1.1.0
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=300, # Cache for 5 minutes
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=300, # Cache for 5 minutes
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 ConnectionFailed, Lock, connect_tcp
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.1.0"
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
- async with await connect_tcp(
125
- hostname,
126
- port,
127
- happy_eyeballs_delay=self.happy_eyeballs_delay,
128
- ) as stream:
129
- addr_info = stream._raw_socket.getpeername()
130
- except ConnectionFailed as e:
131
- raise SSRFProtectionError(
132
- f"Failed to resolve hostname {hostname!r}: {e}",
133
- hostname=hostname,
134
- ip_addr=None,
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
  ),
@@ -1,7 +1,7 @@
1
1
  [project]
2
2
  name = "httpx-secure"
3
3
  requires-python = ">=3.9"
4
- dependencies = ["httpx", "anyio"]
4
+ dependencies = ["httpx>=0.20.0", "anyio>=3.0.0"]
5
5
  description = "Drop-in SSRF protection for httpx"
6
6
  readme = "README.md"
7
7
  license = "0BSD"
@@ -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 ConnectionFailed, getaddrinfo
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 = ConnectionFailed(["Name or service not known"])
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(SSRFProtectionError, match="Failed to resolve hostname"):
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
  )
@@ -109,8 +109,8 @@ dev = [
109
109
 
110
110
  [package.metadata]
111
111
  requires-dist = [
112
- { name = "anyio" },
113
- { name = "httpx" },
112
+ { name = "anyio", specifier = ">=3.0.0" },
113
+ { name = "httpx", specifier = ">=0.20.0" },
114
114
  ]
115
115
 
116
116
  [package.metadata.requires-dev]
File without changes
File without changes
File without changes
File without changes
File without changes