Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(postgres): handle malformed host names during host validation (#73735)

Tom Owersfda8b6e91148
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/mixins.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/mixins.py
index a0e2fbd5edbf..fdfe604e60d4 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/mixins.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/mixins.py
@@ -97,7 +97,10 @@ def _is_host_safe(host: str, team_id: int) -> tuple[bool, str | None]:
if not _is_safe_public_ip(resolved_ip):
_log("block", "resolved_ip", _INTERNAL_IP_ERROR, resolved_ips)
return False, _INTERNAL_IP_ERROR
- except socket.gaierror:
+ except (socket.gaierror, UnicodeError):
+ # getaddrinfo IDNA-encodes the host, so a malformed hostname (e.g. a DNS label over 63
+ # bytes) raises UnicodeError ("label too long") instead of gaierror. Either way the host
+ # can't be resolved — return the actionable message rather than crashing.
_log("block", "dns_failure", _DNS_FAILURE_ERROR)
return (
False,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test_mixins.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test_mixins.py
index dfda3ace901a..33912fbd1f43 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/test_mixins.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/test_mixins.py
@@ -140,6 +140,15 @@ class TestIsHostSafe(SimpleTestCase):
assert "nonexistent.invalid" in error
assert "resolve" in error
+ @override_settings(CLOUD_DEPLOYMENT="US")
+ def test_malformed_host_label_blocked(self):
+ # A single DNS label over 63 bytes makes getaddrinfo's IDNA encoding raise UnicodeError,
+ # not gaierror — this must be handled gracefully instead of crashing.
+ valid, error = _is_host_safe("a" * 92, team_id=999)
+ assert not valid
+ assert error is not None
+ assert "resolve" in error
+
@override_settings(CLOUD_DEPLOYMENT="US")
def test_blocked_host_logs_warning(self):
with patch(