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): treat unescaped connection-string credentials as non-retryable (#64011)

Tom Owersd3450d81980f
diff --git a/posthog/temporal/data_imports/sources/mongodb/source.py b/posthog/temporal/data_imports/sources/mongodb/source.py
index eb48ec0baae3..6af80349dbf3 100644
--- a/posthog/temporal/data_imports/sources/mongodb/source.py
+++ b/posthog/temporal/data_imports/sources/mongodb/source.py
@@ -34,6 +34,12 @@ _MONGO_UNREACHABLE_MESSAGE = (
"IP addresses are allowlisted in your database's network access settings."
)
+_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 "
+ "example with urllib.parse.quote_plus — and update the connection string."
+)
+
_MONGO_ATLAS_SQL_MESSAGE = (
"This connection string points at a MongoDB Atlas SQL / Data Federation endpoint "
"(its host ends in .query.mongodb.net), which PostHog can't import from — those endpoints "
@@ -52,6 +58,15 @@ class MongoDBSource(SimpleSource[MongoDBSourceConfig], ValidateDatabaseHostMixin
auth_failed_msg = "MongoDB authentication failed. Please check the username and password for this source."
return {
"The DNS query name does not exist": None,
+ # pymongo raises InvalidURI("Username and password must be escaped according to RFC 3986,
+ # use urllib.parse.quote_plus") before any network call when the credentials in the
+ # connection string contain unescaped reserved characters (e.g. ':', '/', '@', '%' in the
+ # password). The same RFC-3986 hint also appears on the "Port contains non-digit characters"
+ # variant, which is the same unescaped-credential mistake. This is a malformed connection
+ # string the user must fix — we can't safely percent-encode it ourselves because the
+ # reserved characters are ambiguous with the URI's own delimiters — so retrying never
+ # recovers. Match the stable RFC-3986 fragment, not the volatile surrounding text.
+ "must be escaped according to RFC 3986": _MONGO_UNESCAPED_CREDENTIALS_MESSAGE,
# pymongo raises OperationFailure with codeName 'AuthenticationFailed' (code 18) and
# errmsg 'Authentication failed.' when the credentials are wrong. Non-retryable error
# matching is case-sensitive, so the previous lowercase "authentication failed" never
diff --git a/posthog/temporal/data_imports/sources/mongodb/test_mongo.py b/posthog/temporal/data_imports/sources/mongodb/test_mongo.py
index 17febd56d75e..19417a0a04c7 100644
--- a/posthog/temporal/data_imports/sources/mongodb/test_mongo.py
+++ b/posthog/temporal/data_imports/sources/mongodb/test_mongo.py
@@ -345,6 +345,19 @@ class TestMongoDBNonRetryableErrors(SimpleTestCase):
),
("dns_failure", "The DNS query name does not exist: example.mongodb.net."),
("ssl_failure", "SSL handshake failed: certificate verify failed"),
+ # pymongo InvalidURI raised before any network call when credentials in the connection
+ # string contain unescaped reserved characters — a malformed string the user must fix.
+ (
+ "unescaped_credentials",
+ "Username and password must be escaped according to RFC 3986, use urllib.parse.quote_plus",
+ ),
+ # Same unescaped-credential mistake surfacing via the "Port contains non-digit characters"
+ # variant, which carries the identical RFC-3986 hint.
+ (
+ "unescaped_credentials_port_variant",
+ "Port contains non-digit characters. Hint: username and password must be escaped "
+ "according to RFC 3986, use urllib.parse.quote_plus",
+ ),
# ServerSelectionTimeoutError variants — cluster unreachable for the whole selection
# timeout. All carry the "Topology Description:" suffix regardless of the per-reason text.
("no_servers", "No servers found yet, Timeout: 5.0s, Topology Description: ..."),
@@ -411,6 +424,7 @@ class TestMongoDBNonRetryableErrors(SimpleTestCase):
("atlas_bad_auth", "bad auth", "password"),
("unreachable_topology", "Topology Description:", "allowlist"),
("atlas_sql_endpoint", "query.mongodb.net", "connection string"),
+ ("unescaped_credentials", "must be escaped according to RFC 3986", "connection string"),
]
)
def test_pattern_has_friendly_message(self, _name, pattern, expected_substring):