Credential exposure via sensitive connection string values included in parse-error messages
Description
When the MongoDB .NET/C# Driver failed to parse or validate a connection string, the resulting exception messages could include the offending value from the connection string. Because connection strings carry the username, password and other sensitive option values, these values could end up in application logs and error output. In addition, the ToString() implementations of MongoUrl, MongoUrlBuilder and ConnectionString returned the full connection string including credentials, and MongoServerAddress.ToString() / MongoCredential.ToString() exposed host, port and username.
Impact
Sensitive information, including database credentials, can be exposed to logs, error output or telemetry if exceptions from connection string parsing, or the ToString() output of the types above, are logged or displayed. This can occur from an inadvertent error in a connection string and does not require an attacker-controlled string.
Patches
Fixed in MongoDB.Driver 3.14.1:
ToString()onMongoUrl,MongoUrlBuilderandConnectionStringnow returns a redacted URL. Usernames, passwords and other sensitive values are replaced with<redacted>.MongoServerAddress.ToString()andMongoCredential.ToString()now return fixed type names.- Exception messages from connection-string parsing, SRV validation, address parsing and authentication no longer include user-supplied values.
Breaking change / action required: code that uses these ToString() results as a connection string, key or identifier will break.
MongoUrl: useurl.Urlinstead ofurl.ToString().MongoUrlBuilder: usebuilder.ToMongoUrl().Urlinstead ofbuilder.ToString(), or passbuilder.ToMongoUrl()directly tonew MongoClient(...).ConnectionString: there is no public accessor for the unredacted string; keep the original string.MongoServerAddress: use$"{address.Host}:{address.Port}".MongoCredential: readUsernameandSource.
Workarounds
Do not log exception messages or ToString() output of connection-string related types, or redact credentials from them before logging, until upgrading.