Skip to content

Conversation

@guyutongxue
Copy link

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 #5693

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
Copilot AI review requested due to automatic review settings November 21, 2025 06:05
Copy link
Contributor

Copilot AI left a 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_port helper 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.

Comment on lines +140 to +165
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)
Copy link

Copilot AI Nov 21, 2025

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());
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MongoDB client failed to use connectionURL containing an IPv6 host

1 participant