Skip to content

Commit fc15fee

Browse files
authored
fix: allow ip hosts and hostnames (#275)
1 parent 72c7a2b commit fc15fee

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

pytest_socket.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,24 @@ def socket_allow_hosts(allowed=None, allow_unix_socket=False):
227227
if not isinstance(allowed, list):
228228
return
229229

230-
allowed_hosts_by_host = normalize_allowed_hosts(allowed)
231-
allowed_hosts = set(itertools.chain(*allowed_hosts_by_host.values()))
230+
allowed_ip_hosts_by_host = normalize_allowed_hosts(allowed)
231+
allowed_ip_hosts_and_hostnames = set(
232+
itertools.chain(*allowed_ip_hosts_by_host.values())
233+
) | set(allowed_ip_hosts_by_host.keys())
232234
allowed_list = sorted(
233235
[
234236
(
235237
host
236238
if len(normalized) == 1 and next(iter(normalized)) == host
237239
else f"{host} ({','.join(sorted(normalized))})"
238240
)
239-
for host, normalized in allowed_hosts_by_host.items()
241+
for host, normalized in allowed_ip_hosts_by_host.items()
240242
]
241243
)
242244

243245
def guarded_connect(inst, *args):
244246
host = host_from_connect_args(args)
245-
if host in allowed_hosts or (
247+
if host in allowed_ip_hosts_and_hostnames or (
246248
_is_unix_socket(inst.family) and allow_unix_socket
247249
):
248250
return _true_connect(inst, *args)

tests/test_restrict_hosts.py

+8
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ def test_single_cli_arg_connect_enabled_localhost_resolved(assert_connect):
123123
assert_connect(True, cli_arg="localhost")
124124

125125

126+
def test_single_cli_arg_127_0_0_1_hostname_localhost_connect_disabled(assert_connect):
127+
assert_connect(False, cli_arg=localhost, host="localhost")
128+
129+
130+
def test_single_cli_arg_localhost_hostname_localhost_connect_enabled(assert_connect):
131+
assert_connect(True, cli_arg="localhost", host="localhost")
132+
133+
126134
def test_single_cli_arg_connect_disabled_hostname_resolved(assert_connect):
127135
result = assert_connect(
128136
False,

0 commit comments

Comments
 (0)