Skip to content

CSHARP-6207: Connection-option injection via unescaped settings in the canonical MongoDB URL builder (port to main branch) - #2114

Open
papafe wants to merge 2 commits into
mongodb:mainfrom
papafe:csharp6207
Open

CSHARP-6207: Connection-option injection via unescaped settings in the canonical MongoDB URL builder (port to main branch)#2114
papafe wants to merge 2 commits into
mongodb:mainfrom
papafe:csharp6207

Conversation

@papafe

@papafe papafe commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

…e canonical MongoDB URL builder (port to main branch)
Copilot AI lite review requested due to automatic review settings September 2, 2026 09:23
@papafe
papafe requested a review from a team as a code owner September 2, 2026 09:23
@papafe
papafe requested a review from sanych-sun September 2, 2026 09:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

There are test and validation edge cases (non-deterministic hash code assertion; host validation still permits whitespace/control chars without test coverage) that should be corrected to ensure reliable security hardening.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR hardens the driver’s canonical MongoDB URL building and host handling to prevent connection-option injection (CSHARP-6207), primarily by percent-encoding user-controlled URL components and tightening server host validation, with accompanying tests.

Changes:

  • Percent-encode additional MongoUrlBuilder.ToString() components (database name and multiple query option values) to prevent query-string injection.
  • Add MongoServerAddress host validation to reject characters that can break server lists / URLs, and update TryParse to use compiled regexes.
  • Add/extend unit tests covering injection scenarios and expected percent-encoding behavior.
File summaries
File Description
src/MongoDB.Driver/MongoUrlBuilder.cs Escapes additional URL components and option values when building canonical URLs.
src/MongoDB.Driver/MongoServerAddress.cs Adds stricter host validation and compiled regex parsing for server addresses.
tests/MongoDB.Driver.Tests/MongoUrlTests.cs Adds tests ensuring injected settings are distinguishable from genuine settings.
tests/MongoDB.Driver.Tests/MongoUrlBuilderTests.cs Adds round-trip tests verifying percent-encoding for multiple URL parts/options.
tests/MongoDB.Driver.Tests/MongoServerAddressTests.cs Adds invalid-host test coverage for constructor/parse/TryParse behaviors.
Review details
  • Files reviewed: 5/5 changed files
  • Comments generated: 3
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/MongoDB.Driver/MongoServerAddress.cs Outdated
Comment on lines +25 to +35
public static IEnumerable<object[]> InvalidHosts =>
[
["host,evil.example.com"],
["host/database"],
["host?tls=false"],
["user@host"],
["host#fragment"],
["host:extra"],
["a[]b"],
[""]
];
var genuine = new MongoUrlBuilder("mongodb://localhost") { ApplicationName = "app", ReplicaSetName = "rs0" }.ToMongoUrl();

injected.Equals(genuine).Should().BeFalse();
injected.GetHashCode().Should().NotBe(genuine.GetHashCode());
@papafe
papafe requested review from adelinowona and removed request for sanych-sun September 2, 2026 09:42
@papafe papafe added the bug Fixes issues or unintended behavior. label Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Fixes issues or unintended behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants