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
Open
CSHARP-6207: Connection-option injection via unescaped settings in the canonical MongoDB URL builder (port to main branch)#2114papafe wants to merge 2 commits into
papafe wants to merge 2 commits into
Conversation
…e canonical MongoDB URL builder (port to main branch)
Contributor
There was a problem hiding this comment.
🟡 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
MongoServerAddresshost validation to reject characters that can break server lists / URLs, and updateTryParseto 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 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()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
https://jira.mongodb.org/browse/CSHARP-6207