: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(postgres): give actionable error when a URL is entered as the host (#68368)
Tom Owers6f25e2fb2b99
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.pyindex 00267a2fca74..395e4837edb6 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/source.py@@ -63,6 +63,12 @@ from products.warehouse_sources.backend.types import ExternalDataSourceType, Inclog = logging.getLogger(__name__)+_HOST_IS_URL_ERROR = (+ "Enter just the hostname in the host field (for example, db.example.com), not a full URL or "+ "connection string. Remove any scheme (like http:// or postgres://) and any username, "+ "password, port, or path."+)+PostgresErrors = {"password authentication failed for user": "Invalid user or password",# libpq reports a bad password via SCRAM with a different wording than the line above.@@ -828,6 +834,12 @@ class PostgresSource(SQLSource[PostgresSourceConfig], SSHTunnelMixin, ValidateDaif not is_ssh_valid:return is_ssh_valid, ssh_valid_errors+ # A pasted URL or connection string in the host field otherwise fails DNS resolution with a+ # misleading "check the spelling" message that echoes the raw value back (which can embed+ # credentials). Catch it early with an actionable message that never reflects the input.+ if "://" in config.host:+ return False, _HOST_IS_URL_ERROR+valid_host, host_errors = self.is_database_host_valid(config.host, team_id, using_ssh_tunnel=config.ssh_tunnel.enabled if config.ssh_tunnel else False)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.pyindex 9bbf51bac8a5..356572251dda 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postgres/test_postgres.py@@ -2648,6 +2648,36 @@ class TestValidateCredentialsErrorMapping:assert valid is Falseassert error == expected+ @pytest.mark.parametrize(+ "host",+ [+ "https://db.example.com/",+ "postgres://user:[email protected]:5432/mydb",+ ],+ )+ def test_url_in_host_field_rejected_without_echoing_input(self, source, host):+ config = source.parse_config(+ {+ "host": host,+ "port": 5432,+ "database": "postgres",+ "user": "postgres",+ "password": "postgres",+ "schema": "public",+ }+ )+ with (+ mock.patch.object(source, "ssh_tunnel_is_valid", return_value=(True, None)),+ mock.patch.object(source, "is_database_host_valid", side_effect=AssertionError("should not resolve")),+ mock.patch.object(source, "get_schemas", side_effect=AssertionError("should not connect")),+ ):+ valid, error = source.validate_credentials(config, team_id=1)++ assert valid is False+ # The raw host may embed credentials, so it must never be reflected back.+ assert host not in (error or "")+ assert "hostname" in (error or "")+class TestPostgresSchemaDiscovery:def _mock_connection(self, *fetchall_results: list[tuple[object, ...]]):