From 70e28e5813af8b45019d3e33b4a296b5300aa705 Mon Sep 17 00:00:00 2001 From: Harshit saini Date: Thu, 3 Dec 2026 18:20:48 +0530 Subject: [PATCH] clnrest: accept IPv6 in clnrest-host, with or without brackets 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:`. Changelog-Fixed: clnrest: `clnrest-host` now accepts IPv6 addresses (with or without brackets), and the generated certificate includes the address. --- plugins/rest-plugin/src/certs.rs | 49 +++++++++++++++++++++- plugins/rest-plugin/src/options.rs | 66 ++++++++++++++++++++++++------ 2 files changed, 100 insertions(+), 15 deletions(-) diff --git a/plugins/rest-plugin/src/certs.rs b/plugins/rest-plugin/src/certs.rs index b48e35c2ca52..f292e4a1b27f 100644 --- a/plugins/rest-plugin/src/certs.rs +++ b/plugins/rest-plugin/src/certs.rs @@ -1,7 +1,7 @@ use anyhow::Error; use rcgen::{CertificateParams, DistinguishedName, Issuer, KeyPair}; use std::fs; -use std::net::IpAddr; +use std::net::{IpAddr, Ipv6Addr}; use std::path::{Path, PathBuf}; fn write_private_key(path: PathBuf, contents: &[u8]) -> Result<(), Error> { @@ -16,6 +16,20 @@ fn write_private_key(path: PathBuf, contents: &[u8]) -> Result<(), Error> { Ok(()) } +/// The IP address in the `clnrest-host` option, if it is one. +/// An IPv6 address may be given with or without brackets. Brackets are only +/// valid around IPv6, so `[127.0.0.1]` is not accepted. +pub fn host_ip(rest_host: &str) -> Option { + if let Some(bare) = rest_host + .strip_prefix('[') + .and_then(|h| h.strip_suffix(']')) + { + bare.parse::().ok().map(IpAddr::V6) + } else { + rest_host.parse::().ok() + } +} + pub fn generate_certificates(certs_path: &PathBuf, rest_host: &str) -> Result<(), Error> { /* Generate the CA certificate */ let mut ca_params = CertificateParams::new(vec![ @@ -61,7 +75,7 @@ pub fn generate_certificates(certs_path: &PathBuf, rest_host: &str) -> Result<() server_params .distinguished_name .push(rcgen::DnType::CommonName, "cln rest server"); - if let Ok(ip) = rest_host.parse::() { + if let Some(ip) = host_ip(rest_host) { server_params .subject_alt_names .push(rcgen::SanType::IpAddress(ip)); @@ -113,3 +127,34 @@ pub fn do_certificates_exist(cert_dir: &Path) -> bool { path.exists() && path.metadata().map(|m| m.len() > 0).unwrap_or(false) }) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn host_ip_ipv4() { + assert_eq!(host_ip("127.0.0.1"), Some("127.0.0.1".parse().unwrap())); + } + + #[test] + fn host_ip_ipv6_bare() { + assert_eq!(host_ip("::1"), Some("::1".parse().unwrap())); + } + + #[test] + fn host_ip_ipv6_bracketed() { + assert_eq!(host_ip("[::1]"), Some("::1".parse().unwrap())); + } + + #[test] + fn host_ip_rejects_bracketed_ipv4() { + assert_eq!(host_ip("[127.0.0.1]"), None); + } + + #[test] + fn host_ip_not_an_ip() { + assert_eq!(host_ip("localhost"), None); + assert_eq!(host_ip("[::1"), None); + } +} diff --git a/plugins/rest-plugin/src/options.rs b/plugins/rest-plugin/src/options.rs index 0c13d20e8b2b..a688b51659eb 100644 --- a/plugins/rest-plugin/src/options.rs +++ b/plugins/rest-plugin/src/options.rs @@ -17,6 +17,7 @@ use tower_http::cors::{Any, CorsLayer}; use crate::{ PluginState, + certs::host_ip, structs::{ClnrestOptions, ClnrestProtocol}, }; @@ -67,19 +68,15 @@ pub fn parse_options( } }; - let address_str = format!("{}:{}", plugin.option(&OPT_CLNREST_HOST)?, port); - let address: SocketAddr = match plugin.option(&OPT_CLNREST_HOST)? { - i if i.eq("localhost") => address_str - .to_socket_addrs()? - .next() - .ok_or(anyhow!("No address found for localhost"))?, - _ => { - if let Ok(addr) = address_str.parse() { - addr - } else { - return Err(anyhow!("`clnrest-host` should be a valid IP.")); - } - } + let host = plugin.option(&OPT_CLNREST_HOST)?; + let address = parse_address(&host, port)?; + // Keep `localhost` as given, so the address in the log and the + // certificate name still match. IP literals are printed from the parsed + // address, which puts brackets around IPv6. + let address_str = if host == "localhost" { + format!("{}:{}", host, port) + } else { + address.to_string() }; let cors = create_cors_layer(&plugin.option(&OPT_CLNREST_CORS)?)?; @@ -110,6 +107,20 @@ pub fn parse_options( }) } +/// Turn the `clnrest-host` option and the port into a socket address. +pub fn parse_address(host: &str, port: u16) -> Result { + if host == "localhost" { + return format!("{}:{}", host, port) + .to_socket_addrs()? + .next() + .ok_or(anyhow!("No address found for localhost")); + } + match host_ip(host) { + Some(ip) => Ok(SocketAddr::new(ip, port)), + None => Err(anyhow!("`clnrest-host` should be a valid IP.")), + } +} + fn create_cors_layer(allowed_origins: &[String]) -> Result { if allowed_origins.is_empty() { return Err(anyhow!("`clnrest-cors-origins` must not be empty!")); @@ -138,3 +149,32 @@ fn create_cors_layer(allowed_origins: &[String]) -> Result