-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Major - P3
-
Affects Version/s: None
-
Component/s: Connection String
-
None
Description
Cleartext connection-string passwords reach Mongo::Error::InvalidURI messages, and from there application logs and error reporters. Verifies SECBUG-4324, which is REPRODUCED — and the leak is broader than the original report describes.
Two independent defects combine. Either alone would leak; together they mean the full password is emitted.
1. Raw URI fragments are interpolated into the error details.
lib/mongo/uri.rb:414 splits the post-scheme string on the first /, so an unescaped / inside the password puts the password tail plus @host/db into db. Line 415 interpolates that fragment verbatim:
hosts, db = hosts_and_db.split('/', 2) raise_invalid_error!("Database name contains an unescaped slash (/): #{db}") if db && db.index('/')
Mongo::Error::InvalidURI#initialize (lib/mongo/error/invalid_uri.rb:36) appends details without redaction, so the fragment reaches the message.
2. The Bad URI: line is not actually redacted.
This was missed by SECBUG-4324. lib/mongo/error/invalid_uri.rb:35 passes the raw URI through Mongo::URI.redact, but the regex at lib/mongo/uri.rb:155 cannot match a userinfo section containing a slash:
USERINFO_REDACTION_REGEX = %r{\A(mongodb(?:\+srv)?://)[^/?#]*@}i.freeze
[^/?#] excludes /, so the pattern never reaches the @ and sub is a no-op. The exact precondition that triggers defect 1 also disables the redaction meant to contain it. Because this line carries the whole URI, it leaks the *full password (s3cr3t/p4ss), not just the tail after the slash.
Impact
Every InvalidURI raised on a URI whose password contains /, ?, or # emits the cleartext password in the exception message, regardless of which validation trips first. The message is destined for logs, error-tracking systems, and backtraces, so credentials usable for database authentication become recoverable by anyone with read access to those sinks.
Mongo::URI::SRVProtocol inherits parse!, so mongodb+srv:// is affected identically.
Nobody controls the trigger — the secret and the trigger are both the deployment's own connection string, and the precondition is a common URI-encoding mistake. Mongo::URI.redact is called from only this one site, so the blast radius is limited to InvalidURI messages.
Steps to Reproduce
require 'mongo' Mongo::URI.get('mongodb://admin:s3cr3t/p4ss@example.com/mydb')
Raises Mongo::Error::InvalidURI:
Bad URI: mongodb://admin:s3cr3t/p4ss@example.com/mydb
Database name contains an unescaped slash (/): p4ss@example.com/mydb
Both the Bad URI: line and the details line leak. Redaction can be shown to fail in isolation:
Mongo::URI.redact('mongodb://admin:s3cr3t@example.com/mydb') # => "mongodb://<credentials>@example.com/mydb" Mongo::URI.redact('mongodb://admin:s3cr3t/p4ss@example.com/mydb') # => "mongodb://admin:s3cr3t/p4ss@example.com/mydb" <-- unredacted
Additional trigger paths, no trailing database name required. These reach a different validation but leak via the same unredacted Bad URI: line:
| Input | Details line |
|---|---|
| mongodb://admin:s3cr3t/p4ss@example.com | Invalid port: s3cr3t. ... |
| mongodb+srv://admin:s3cr3t/p4ss@example.com/mydb | Database name contains an unescaped slash |
| mongodb://admin:s3cr3t?p4ss@example.com/mydb | Invalid port: s3cr3t. ... |
The ? case is a second metacharacter variant: split('?', 2) at lib/mongo/uri.rb:409 cuts the password, and ? is likewise excluded from the redaction character class. The general bug is therefore not "slash in password" but "any userinfo containing /, ?, or # both misparses and escapes redaction."
Proposed Fix
Both halves are needed; fixing one leaves the other leaking.
- Make USERINFO_REDACTION_REGEX match userinfo containing URI metacharacters — match greedily to the last @ preceding the host portion rather than stopping at the first /, ?, or #. Take care that the pattern still terminates and still leaves slash-free URIs (and URIs with no userinfo at all) behaving as they do today.
- Stop interpolating raw URI fragments into details at lib/mongo/uri.rb:415. The message should name the problem without echoing the offending substring, since that substring may be credential material. Audit the other raise_invalid_error! call sites in lib/mongo/uri.rb for the same pattern, including the Error::InvalidAddress rescue at line 436-437, which forwards a message built from a host fragment that may be password material.
Acceptance Criteria
- Mongo::URI.redact redacts userinfo containing /, ?, and #.
- No InvalidURI message raised from any URI in the Steps to Reproduce table contains s3cr3t or p4ss.
- Regression tests cover redaction of each metacharacter, and assert the absence of password material in the full InvalidURI message (not just the Bad URI: line).
- Existing URI parsing specs still pass; error messages remain useful for diagnosing genuine malformed-URI mistakes.
- No RuboCop offenses introduced.
Notes
Filed from verification of SECBUG-4324 (Aegis finding 28fe866d5b4a). That ticket identified defect 1 only and described the leak as the "password tail"; the redaction failure in defect 2 means the full password is exposed. Reproduced against master at 547d37580.
Fix and tests should land as a single change, since the redaction repair is what makes the details-string repair verifiable.