Skip to content

clnrest: accept IPv6 in clnrest-host, with or without brackets - #9799

Merged
daywalker90 merged 1 commit into
ElementsProject:masterfrom
harshitsaini17:fix/9786-clnrest-ipv6-host
Dec 3, 2026
Merged

daywalker90 merged 1 commit into
ElementsProject:masterfrom
harshitsaini17:fix/9786-clnrest-ipv6-host

Conversation

@harshitsaini17

Copy link
Copy Markdown
Contributor

clnrest-host=::1 failed with "should be a valid IP", because options.rs joined host and port as "::1:3010" and parsed that as a socket address. With clnrest-host=[::1] the server started, but certs.rs parsed "[::1]" as an IpAddr, which fails, so the certificate had no IP SAN for it.

Now the host is parsed as an IP once, brackets are stripped, and the socket address is built with SocketAddr::new. The certificate code uses the same helper. I added unit tests for both functions. Two of them fail with the old parsing code and pass with the fix.

I ran cargo test -p clnrest, cargo clippy -p clnrest --all-targets and rustfmt on the two changed files. I did not run the pytest clnrest tests.

Fixes #9786

Copilot AI balanced review requested due to automatic review settings December 2, 2026 21:47

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Comment thread plugins/rest-plugin/src/options.rs Outdated
}
};
let address = parse_address(&plugin.option(&OPT_CLNREST_HOST)?, port)?;
let address_str = address.to_string();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This would turn localhost to the resolved address (usually 127.0.0.1) so it would break with the certificate. Suggested fix: keep format!("{}:{}", host, port) when host == "localhost", and use address.to_string() for IP literals

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, fixed. localhost is kept as localhost:<port> now, and IP addresses still use address.to_string().


/// The IP address in the `clnrest-host` option, if it is one.
/// An IPv6 address may be given with or without brackets.
pub fn host_ip(rest_host: &str) -> Option<IpAddr> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this would accept bracketed ipv4 addresses. Suggested fix:

pub fn host_ip(rest_host: &str) -> Option<IpAddr> {
    if let Some(bare) = rest_host.strip_prefix('[').and_then(|h| h.strip_suffix(']')) {
        bare.parse::<Ipv6Addr>().ok().map(IpAddr::V6)
    } else {
        rest_host.parse::<IpAddr>().ok()
    }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, I used your version. [127.0.0.1] is refused now, and I added a test for it.

@daywalker90

Copy link
Copy Markdown
Collaborator

Also please squash the commits

clnrest-host=::1 was refused as "should be a valid IP", because the
host and port were joined as "::1:3010" and parsed as one socket
address. clnrest-host=[::1] started the server, but the certificate
code parsed "[::1]" as an IpAddr, which fails, so the server
certificate got no IP SAN for it.

Parse the host as an IP once, accepting a bracketed IPv6 address, and
build the socket address with SocketAddr::new. Use the same helper to
add the IP SAN when generating certificates. Brackets are only accepted
around IPv6, and `localhost` is still shown as `localhost:<port>`.

Changelog-Fixed: clnrest: `clnrest-host` now accepts IPv6 addresses (with or without brackets), and the generated certificate includes the address.
@harshitsaini17
harshitsaini17 force-pushed the fix/9786-clnrest-ipv6-host branch from f6bc213 to 70e28e5 Compare December 3, 2026 12:51
@harshitsaini17

Copy link
Copy Markdown
Contributor Author

squashed

@daywalker90
daywalker90 enabled auto-merge (rebase) December 3, 2026 13:14
@daywalker90

Copy link
Copy Markdown
Collaborator

Thanks!

@daywalker90
daywalker90 merged commit 8df1c35 into ElementsProject:master Dec 3, 2026
56 checks passed
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.

clnrest: an IPv6 clnrest-host fails, and a bracketed one gets no IP SAN

3 participants