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(mongodb): give an actionable error for an unparseable connection string (#88251)

Tom Owersbfe5d0a4820d
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py
index 740d97cccefd..c784f36b9500 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/source.py
@@ -36,6 +36,16 @@ _MONGO_UNREACHABLE_MESSAGE = (
"IP addresses are allowlisted in your database's network access settings."
)
+# `_parse_connection_string` raises a ValueError when the string isn't a usable MongoDB URI: a
+# wrong or missing scheme, or a host/port that urlparse rejects. The raw reason gives the user
+# little to act on, so point at the expected scheme and format instead.
+_MONGO_INVALID_CONNECTION_STRING_MESSAGE = (
+ # nosemgrep: trailofbits.generic.mongodb-insecure-transport.mongodb-insecure-transport
+ "PostHog couldn't read your MongoDB connection string. It must start with mongodb:// or "
+ "mongodb+srv:// and follow the standard format, for example "
+ "mongodb+srv://user:[email protected]/database. Check the connection string and try again."
+)
+
_MONGO_UNESCAPED_CREDENTIALS_MESSAGE = (
"Your MongoDB connection string is invalid: the username and password must be percent-encoded "
"per RFC 3986. Escape any reserved characters (e.g. : / ? # [ ] @ %) in your credentials — for "
@@ -227,8 +237,8 @@ class MongoDBSource(SimpleSource[MongoDBSourceConfig], ValidateDatabaseHostMixin
try:
connection_params = _parse_connection_string(config.connection_string, config.database_name)
- except:
- return False, "Invalid connection string"
+ except Exception:
+ return False, _MONGO_INVALID_CONNECTION_STRING_MESSAGE
if not connection_params.get("database"):
return False, DATABASE_NAME_REQUIRED_ERROR
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_database_name.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_database_name.py
index 7b642a652f03..580b02c40408 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_database_name.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_database_name.py
@@ -15,6 +15,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.mongodb.so
_MONGO_AUTHENTICATION_FAILED_MESSAGE,
_MONGO_CONNECT_FAILED_MESSAGE,
_MONGO_HOST_UNRESOLVED_MESSAGE,
+ _MONGO_INVALID_CONNECTION_STRING_MESSAGE,
_MONGO_NO_COLLECTIONS_MESSAGE,
_MONGO_NOT_AUTHORIZED_MESSAGE,
_MONGO_UNESCAPED_CREDENTIALS_MESSAGE,
@@ -45,6 +46,21 @@ class TestParseConnectionStringDatabaseOverride:
class TestMongoValidateCredentialsDatabaseName:
+ @pytest.mark.parametrize(
+ "connection_string",
+ [
+ "https://cluster.example.com/db", # wrong scheme, rejected by our own check
+ "mongodb+srv://host:not-a-port/db", # urlparse rejects the non-numeric port
+ ],
+ )
+ def test_unparseable_connection_string_returns_actionable_error(self, connection_string):
+ config = MongoDBSourceConfig.from_dict({"connection_string": connection_string})
+
+ ok, err = MongoDBSource().validate_credentials(config, team_id=1)
+
+ assert ok is False
+ assert err == _MONGO_INVALID_CONNECTION_STRING_MESSAGE
+
def test_missing_database_everywhere_returns_actionable_error(self):
config = MongoDBSourceConfig.from_dict({"connection_string": _SRV_NO_DB})