Print: an IPv6 print server is parsed as a host, not split on its last colon

Endpoint::parse split the server string on its last ':', which is wrong for an IPv6 literal: "::1" became host ":" on port 1, and "[::1]:631" kept its brackets inside the host, which to_socket_addrs cannot resolve on Linux or macOS. An IPv6 CUPS endpoint was therefore always refused by detect(), even when CUPS was listening on it.

A bracketed server now takes its host from inside the brackets and its port from after them. A bare address with more than one ':' is the host with the default port, because its last ':' cannot be told apart from a port separator; a zone id (fe80::1%eth0) carries no ':', so a port can still follow it. host:port, a bare host and a socket path are unchanged.

Test: "::1", "[::1]", "[::1]:631", "[fe80::1]:6310", "fe80::1%eth0:631", and the cases that already worked. Fail before, pass after.
This commit is contained in:
Codex committed 2026-10-19 11:22:33 -07:00
1 parent 2ef8a9d3f5
commit ababe94f23
2 files changed
+34 -2

No files matched your search

+15 -2
View File
@@ -111,12 +111,25 @@ impl Endpoint {
.find(Endpoint::reachable)
}
/// `server` as a socket path (starts with `/`) or `host[:port]` (port 631 by default).
/// `server` as a socket path (starts with `/`) or `host[:port]` (port 631 by default). An IPv6
/// server is `[address]` or `[address]:port`; written bare (more than one `:`) it keeps the
/// default port, because its last `:` cannot be told apart from a port separator.
pub fn parse(server: &str) -> Endpoint {
if server.starts_with('/') {
return Endpoint::Unix(PathBuf::from(server));
}
let (host, port) = server.rsplit_once(':').and_then(|(h, p)| p.parse().ok().map(|p| (h.to_string(), p))).unwrap_or((server.to_string(), 631));
// `[IPv6]:port` - the brackets tell the address from the port, and are not part of the host.
if let Some(rest) = server.strip_prefix('[')
&& let Some((host, tail)) = rest.split_once(']')
{
let port = tail.strip_prefix(':').and_then(|p| p.parse().ok()).unwrap_or(631);
return Endpoint::Tcp(host.to_string(), port);
}
// A bare IPv6 literal has more than one ':', so splitting on the last one would cut the
// address in half: it is the host, and the port stays the default. A zone id (`fe80::1%eth0`)
// carries no ':', so there a port can still follow the address.
let host_port = if server.matches(':').count() > 1 && !server.contains('%') { None } else { server.rsplit_once(':') };
let (host, port) = host_port.and_then(|(h, p)| p.parse().ok().map(|p| (h.to_string(), p))).unwrap_or((server.to_string(), 631));
Endpoint::Tcp(host, port)
}
+19
View File
@@ -232,6 +232,25 @@ fn the_unix_socket_endpoint_works_and_detect_honours_cups_server() {
let _ = std::fs::remove_dir_all(&dir);
}
/// An IPv6 print server is not a `host:port` split: a bare address has more than one ':', and the
/// brackets of `[address]:port` must not end up inside the host, where no resolver can read them.
#[test]
fn an_ipv6_print_server_is_parsed_as_a_host_not_as_host_port() {
assert_eq!(Endpoint::parse("::1"), Endpoint::Tcp("::1".into(), 631));
assert_eq!(Endpoint::parse("fe80::1"), Endpoint::Tcp("fe80::1".into(), 631));
assert_eq!(Endpoint::parse("2001:db8::1"), Endpoint::Tcp("2001:db8::1".into(), 631));
assert_eq!(Endpoint::parse("[::1]"), Endpoint::Tcp("::1".into(), 631));
assert_eq!(Endpoint::parse("[::1]:631"), Endpoint::Tcp("::1".into(), 631));
assert_eq!(Endpoint::parse("[fe80::1]:6310"), Endpoint::Tcp("fe80::1".into(), 6310));
// A zone id carries no ':', so a port can still follow it without brackets.
assert_eq!(Endpoint::parse("fe80::1%eth0:631"), Endpoint::Tcp("fe80::1%eth0".into(), 631));
// What already worked is unchanged.
assert_eq!(Endpoint::parse("cups.example"), Endpoint::Tcp("cups.example".into(), 631));
assert_eq!(Endpoint::parse("cups.example:6310"), Endpoint::Tcp("cups.example".into(), 6310));
assert_eq!(Endpoint::parse("127.0.0.1:6310"), Endpoint::Tcp("127.0.0.1".into(), 6310));
assert_eq!(Endpoint::parse("/var/run/cups/cups.sock"), Endpoint::Unix("/var/run/cups/cups.sock".into()));
}
#[test]
fn requests_start_with_the_mandatory_attributes() {
let r = Request::new(OP_PRINT_JOB, 9).end();