Skip to content

Fix IPv6 formatting in host_with_port in evm rpc_url parser #1236

Description

@ryardley

⚠️ Potential issue | 🟠 Major

Fix IPv6 formatting in host_with_port.

format!("{}:{}", hostname, port) produces invalid strings for IPv6 hosts (e.g., ::1:8545). Bracket IPv6 addresses to avoid connection errors.

🐛 Proposed fix
 pub fn host_with_port(&self) -> String {
-    format!("{}:{}", self.hostname(), self.port())
+    let host = self.hostname();
+    if host.contains(':') {
+        format!("[{}]:{}", host, self.port())
+    } else {
+        format!("{}:{}", host, self.port())
+    }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

    pub fn host_with_port(&self) -> String {
        let host = self.hostname();
        if host.contains(':') {
            format!("[{}]:{}", host, self.port())
        } else {
            format!("{}:{}", host, self.port())
        }
    }
🤖 Prompt for AI Agents
In `@crates/config/src/rpc.rs` around lines 77 - 90, The host_with_port() helper
currently formats "{hostname}:{port}" which breaks IPv6 addresses (e.g.,
"::1:8545"); update host_with_port() to detect IPv6 by checking hostname
returned from hostname() for ':' (and not already wrapped) and wrap it in square
brackets before appending the port so IPv6 becomes "[::1]:8545"; keep using the
existing hostname(), port() accessors and ensure you only add brackets when
necessary to avoid double-bracketing.

Originally posted by @coderabbitai[bot] in #1153 (comment)

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions