Skip to content

fix: escape credentials and options when rebuilding the MongoDB URI - #41

Merged
passren merged 1 commit into
passren:mainfrom
msyavuz:msyavuz/fix/escape-uri-credentials
Sep 10, 2026
Merged

passren merged 1 commit into
passren:mainfrom
msyavuz:msyavuz/fix/escape-uri-credentials

Conversation

@msyavuz

@msyavuz msyavuz commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Summary

create_connect_args rebuilt the MongoDB URI from url.username / url.password / url.query, which SQLAlchemy has already percent-decoded. A password with a reserved character (e.g. @) was re-injected unescaped, so pymongo failed with "Username and password must be escaped according to RFC 3986" even when the user had correctly encoded it as %40.

Changes

  • Re-escape username, password and query keys/values with quote_plus when reassembling the URI.
  • Emit repeated query keys (e.g. readPreferenceTags) once per value instead of stringifying SQLAlchemy's tuple.
  • Unit tests for escaped credentials (round-tripped through pymongo.uri_parser.parse_uri), username-only, escaped option values, and repeated keys.

Repro

mongodb://user:p%40ssword@host/db → dialect previously produced mongodb://user:p@ssword@host/db; now produces mongodb://user:p%40ssword@host/db.

SQLAlchemy percent-decodes username, password and query values when
parsing the URL. create_connect_args reassembled the MongoDB URI from
those decoded values without re-escaping them, so a password containing
a reserved character such as @ broke pymongo's userinfo parsing even
when the user had correctly encoded it as %40.

Re-escape credentials and query keys/values with quote_plus, and emit
repeated query keys once per value instead of stringifying the tuple.
@msyavuz
msyavuz force-pushed the msyavuz/fix/escape-uri-credentials branch from d11600c to 573d425 Compare September 7, 2026 12:44
@passren
passren merged commit 6d47c92 into passren:main Sep 10, 2026
24 checks passed
@passren

passren commented Sep 11, 2026

Copy link
Copy Markdown
Owner

@msyavuz Thank you for the contribution. This change has been released through v0.7.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants