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): reject an out-of-range database port before connecting (#112390)

Tom Owersc4b9fea3f0ae
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.py
index f26e2d97dcaa..6884f689094f 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
@@ -94,6 +94,10 @@ _HOST_HAS_PORT_ERROR = (
"in the port field instead."
)
+_PORT_OUT_OF_RANGE_ERROR = (
+ "The port must be between 1 and 65535. Enter the port your database listens on, usually 5432."
+)
+
# Railway's DATABASE_URL points at the service's private-network host, so it is the value customers
# paste most often. The name only resolves inside Railway's own network, and the DNS failure that
# follows asks them to check a spelling that is already correct, so name the public host instead.
@@ -1701,6 +1705,11 @@ class PostgresSource(
if host_value.count(":") == 1 and not host_value.startswith("["):
return False, _HOST_HAS_PORT_ERROR
+ # Out of range, the port reaches sshtunnel as a bare AssertionError or libpq as a connection
+ # failure, and both end in the generic "check all connection details" message.
+ if not 1 <= config.port <= 65535:
+ return False, _PORT_OUT_OF_RANGE_ERROR
+
# A bastion inside the customer's Railway project can reach the private host, so only reject
# it for a direct connection.
if not self.ssh_tunnel_enabled(config) and host_value.lower().endswith(_RAILWAY_INTERNAL_HOST_SUFFIX):
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.py
index 0f134f0ad9e0..73b59630a1ad 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
@@ -5427,6 +5427,35 @@ class TestValidateCredentialsErrorMapping:
assert host not in (error or "")
assert "port field" in (error or "")
+ @pytest.mark.parametrize("port", [-5432, 0, 65536])
+ @pytest.mark.parametrize("ssh_tunnel_enabled", [False, True])
+ def test_out_of_range_port_rejected_before_connecting(self, source, port, ssh_tunnel_enabled):
+ config = source.parse_config(
+ {
+ "host": "db.example.com",
+ "port": port,
+ "database": "postgres",
+ "user": "postgres",
+ "password": "postgres",
+ "schema": "public",
+ "ssh_tunnel": {
+ "enabled": ssh_tunnel_enabled,
+ "host": "bastion.example.com",
+ "port": "22",
+ "auth": {"selection": "password", "username": "tunnel", "password": "tunnel"},
+ },
+ }
+ )
+ with (
+ mock.patch.object(source, "ssh_tunnel_is_valid", return_value=(True, None)),
+ mock.patch.object(source, "is_database_host_valid", return_value=(True, None)),
+ 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
+ assert "between 1 and 65535" in (error or "")
+
def test_railway_private_host_named_as_such_instead_of_a_spelling_error(self, source):
config = source.parse_config(
{