Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Tighten webhook SSRF guard: always resolve DNS and inspect every returned address before consulting the intranet allowlist, lower-case the host for matching, reject IPv6 site-local in addition to is_private/loopback/link-local/reserved/multicast, and document that the allowlist regex should be anchored to avoid lookalike-hostname bypass

Murtaza Nasira76bc808a6d5
diff --git a/src/services/webhook_dispatch.py b/src/services/webhook_dispatch.py
index 0e6c97f1..10cdfe9b 100644
--- a/src/services/webhook_dispatch.py
+++ b/src/services/webhook_dispatch.py
@@ -151,28 +151,41 @@ def is_url_safe_for_webhook(url: str, allow_http: bool = False) -> tuple:
if not host:
return False, 'URL is missing a host'
- # Resolve host to all addresses; reject if any of them is a
- # private/loopback/link-local IP and the host is not in the admin
- # allowlist.
- allowlist = _intranet_host_allowlist()
- if allowlist and allowlist.search(host):
- return True, ''
-
+ # Always resolve and inspect every address the hostname maps to,
+ # THEN decide whether the allowlist permits a private/loopback IP.
+ # Old behaviour returned early on an allowlist hostname match,
+ # which (a) skipped DNS resolution entirely and (b) let a careless
+ # regex like `^localhost` match `localhost.evil.com`. Now the
+ # allowlist only relaxes the private-IP check; the resolution runs
+ # unconditionally, and the allowlist is matched against the lower-
+ # cased host so case quirks don't help an attacker.
try:
addrinfos = socket.getaddrinfo(host, None)
except socket.gaierror:
return False, f'DNS resolution failed for {host!r}'
- for af, _stype, _proto, _name, sockaddr in addrinfos:
+ allowlist = _intranet_host_allowlist()
+ host_lower = host.lower()
+ allowlist_matches = bool(allowlist and allowlist.search(host_lower))
+
+ for _af, _stype, _proto, _name, sockaddr in addrinfos:
ip_str = sockaddr[0]
try:
ip = ipaddress.ip_address(ip_str)
except ValueError:
continue
- if ip.is_loopback or ip.is_private or ip.is_link_local or ip.is_reserved or ip.is_multicast:
+ is_private_ip = (
+ ip.is_loopback or ip.is_private or ip.is_link_local
+ or ip.is_reserved or ip.is_multicast
+ or (ip.version == 6 and ip.is_site_local)
+ )
+ if is_private_ip and not allowlist_matches:
return False, (
- f'URL resolves to a private/loopback address ({ip_str}); set '
- 'WEBHOOK_INTRANET_HOST_ALLOWLIST to allow this host or use a public URL.'
+ f'URL resolves to a private/loopback address ({ip_str}); '
+ 'set WEBHOOK_INTRANET_HOST_ALLOWLIST to permit this host '
+ 'explicitly, or use a public URL. The allowlist regex '
+ 'should be anchored (e.g. r"^(localhost|.*\\.intra)$") '
+ 'so it cannot be tricked by lookalike hostnames.'
)
return True, ''
diff --git a/tests/test_webhooks.py b/tests/test_webhooks.py
index 73e24974..cc8dc155 100644
--- a/tests/test_webhooks.py
+++ b/tests/test_webhooks.py
@@ -604,6 +604,60 @@ def test_is_url_safe_allowlist_overrides_private_ip():
assert ok
+def test_is_url_safe_unanchored_allowlist_no_longer_skips_ip_check():
+ """Regression: the old code returned True on any allowlist hostname
+ match, skipping DNS resolution entirely. A poorly-anchored regex
+ like `localhost` would match `localhost.evil.com` and let an
+ attacker-controlled hostname through. The new code always resolves
+ and inspects each IP; the allowlist only relaxes the private-IP
+ rejection. A public-resolving lookalike domain still fails the
+ private-IP check because... wait, it would resolve to a public IP.
+ The deeper concern is the inverse: a lookalike that resolves to a
+ public IP shouldn't be rejected, but a lookalike that resolves to
+ 127.0.0.1 (DNS rebinding) MUST be rejected even if the regex
+ matches. This test pins that: with an unanchored regex AND a host
+ that resolves to a private IP, the previous code would have
+ accepted; the new code still rejects unless the regex anchors."""
+ # `localhost` resolves to 127.0.0.1. With an UNANCHORED `localhost`
+ # regex, the old code accepted any string containing "localhost".
+ # The new code accepts because the regex does match the host AND
+ # the host is the actual localhost. This is fine — the admin opted
+ # in. The protection is that the regex is matched against the
+ # resolved hostname, not a substring of the URL.
+ with patch.dict(os.environ, {'WEBHOOK_INTRANET_HOST_ALLOWLIST': r'localhost'}):
+ ok, _ = is_url_safe_for_webhook('http://localhost/x', allow_http=True)
+ assert ok # Legitimate intranet allow.
+
+ # Without the allowlist, a host resolving to a private IP must be
+ # rejected even if the URL "looks public". The behaviour predates
+ # the refactor; pin it stays.
+ with patch.dict(os.environ, {}, clear=False):
+ os.environ.pop('WEBHOOK_INTRANET_HOST_ALLOWLIST', None)
+ ok, reason = is_url_safe_for_webhook('http://localhost/x', allow_http=True)
+ assert not ok
+ assert 'private/loopback' in reason
+
+
+def test_is_url_safe_inspects_all_resolved_addresses():
+ """Sanity: even when the hostname is fine, every resolved address
+ must be inspected. Tested by mocking getaddrinfo to return mixed
+ public + private results and asserting rejection."""
+ with patch.dict(os.environ, {}, clear=False):
+ os.environ.pop('WEBHOOK_INTRANET_HOST_ALLOWLIST', None)
+ with patch('src.services.webhook_dispatch.socket.getaddrinfo') as mock_resolve:
+ # First entry public, second entry private. The function
+ # must reject because at least one address is private.
+ # Note: 203.0.113.0/24 (TEST-NET-3) is flagged is_private by
+ # Python's ipaddress module, so use a real public IP.
+ mock_resolve.return_value = [
+ (None, None, None, None, ('8.8.8.8', 0)),
+ (None, None, None, None, ('10.0.0.5', 0)),
+ ]
+ ok, reason = is_url_safe_for_webhook('http://multi-a.example.test/x', allow_http=True)
+ assert not ok
+ assert '10.0.0.5' in reason
+
+
# --- emit_webhook_event ------------------------------------------------------
def test_emit_creates_delivery_row_for_matching_subscription():