-
Notifications
You must be signed in to change notification settings - Fork 291
fix(mongodb-client): Correctly parse IPv6 addresses #5701
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
The MongoDB connection string parser failed to handle IPv6 addresses because it would split the host and port on the first colon. This change introduces specific logic to handle bracketed IPv6 addresses, ensuring they are parsed correctly while maintaining compatibility with existing hostname and IPv4 formats. - Adds logic to parse bracketed IPv6 addresses. - Refactors port parsing into a helper function to avoid code duplication. - Adds unit tests for various IPv6 address scenarios, including with and without ports, and with port 0. Fixes prisma#5693
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes the MongoDB connection string parser to correctly handle IPv6 addresses by detecting bracketed addresses and parsing them separately from the colon-based splitting used for regular hostnames and IPv4 addresses. The change maintains backward compatibility with existing hostname/IPv4 formats while adding robust IPv6 support.
- Introduces bracket-aware parsing logic to handle IPv6 addresses in the format
[::1]or[::1]:27017 - Refactors port parsing into a reusable
parse_porthelper function to eliminate code duplication - Adds comprehensive test coverage for IPv6 scenarios including standalone addresses, addresses with ports, mixed host lists, and error cases
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| let (hostname, port) = if address.starts_with('[') { | ||
| let end_bracket_idx = match address.rfind(']') { | ||
| Some(end_bracket_idx) => end_bracket_idx, | ||
| None => { | ||
| return Err(ErrorKind::invalid_argument(format!( | ||
| "invalid server address: \"{address}\"; hostname cannot be empty" | ||
| "invalid server address: \"{address}\"; missing closing bracket for IPv6 address" | ||
| )) | ||
| .into()); | ||
| } | ||
| part | ||
| } | ||
| None => { | ||
| return Err( | ||
| ErrorKind::invalid_argument(format!("invalid server address: \"{address}\"")).into(), | ||
| ); | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| let port = match parts.next() { | ||
| Some(part) => { | ||
| let port = u16::from_str(part).map_err(|_| { | ||
| let (host, port_str) = address.split_at(end_bracket_idx + 1); | ||
|
|
||
| let port = if !port_str.is_empty() { | ||
| let port_str = port_str.strip_prefix(':').ok_or_else(|| { | ||
| ErrorKind::invalid_argument(format!( | ||
| "port must be valid 16-bit unsigned integer, instead got: {part}" | ||
| "invalid server address: \"{address}\"; invalid characters after IPv6 address" | ||
| )) | ||
| })?; | ||
|
|
||
| if port == 0 { | ||
| return Err(ErrorKind::invalid_argument(format!( | ||
| "invalid server address: \"{address}\"; port must be non-zero" | ||
| )) | ||
| .into()); | ||
| Some(parse_port(port_str, address)?) | ||
| } else { | ||
| None | ||
| }; | ||
|
|
||
| (host, port) |
Copilot
AI
Nov 21, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing validation for empty IPv6 addresses. The code should check that the content between brackets is not empty to prevent addresses like [] or []:27017 from being accepted. Consider adding a check after line 148 to validate that the extracted IPv6 address (without brackets) is not empty:
let ipv6_addr = &host[1..host.len()-1];
if ipv6_addr.is_empty() {
return Err(ErrorKind::invalid_argument(format!(
"invalid server address: \"{address}\"; IPv6 address cannot be empty"
))
.into());
}
The MongoDB connection string parser failed to handle IPv6 addresses because it would split the host and port on the first colon. This change introduces specific logic to handle bracketed IPv6 addresses, ensuring they are parsed correctly while maintaining compatibility with existing hostname and IPv4 formats.
Fixes #5693