Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Print: an IPv6 endpoint writes its authority in brackets, and a mistyped one stays unreachable

Codex909e1cdd8400
diff --git a/crates/ipp/src/lib.rs b/crates/ipp/src/lib.rs
index fd9eda806..b5863e910 100644
--- a/crates/ipp/src/lib.rs
+++ b/crates/ipp/src/lib.rs
@@ -113,7 +113,9 @@ impl Endpoint {
/// `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.
+ /// default port, because its last `:` cannot be told apart from a port separator. Anything else
+ /// after the bracket (`[::1]:abc`) stays the host, so a mistyped endpoint cannot resolve rather
+ /// than quietly becoming the default one.
pub fn parse(server: &str) -> Endpoint {
if server.starts_with('/') {
return Endpoint::Unix(PathBuf::from(server));
@@ -122,7 +124,16 @@ impl Endpoint {
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);
+ // Nothing after the bracket, or a real `:port`, is understood. Anything else (`[::1]:abc`,
+ // `[::1]:70000`, `[::1]junk`) keeps the whole string as the host, so the mistyped endpoint
+ // stays unresolvable instead of quietly becoming the default one.
+ let port = if tail.is_empty() {
+ 631
+ } else if let Some(port) = tail.strip_prefix(':').and_then(|p| p.parse().ok()) {
+ port
+ } else {
+ return Endpoint::Tcp(server.to_string(), 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
@@ -163,9 +174,12 @@ impl Endpoint {
}
}
+ /// The endpoint as an HTTP authority: an IPv6 host is bracketed, or its last colon reads as the
+ /// port separator and the request carries a host CUPS has never heard of.
fn host(&self) -> String {
match self {
Endpoint::Unix(_) => "localhost".into(),
+ Endpoint::Tcp(host, port) if host.contains(':') => format!("[{host}]:{port}"),
Endpoint::Tcp(host, port) => format!("{host}:{port}"),
}
}
@@ -175,6 +189,7 @@ impl std::fmt::Display for Endpoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Endpoint::Unix(p) => write!(f, "{}", p.display()),
+ Endpoint::Tcp(h, p) if h.contains(':') => write!(f, "[{h}]:{p}"),
Endpoint::Tcp(h, p) => write!(f, "{h}:{p}"),
}
}
diff --git a/crates/ipp/src/tests.rs b/crates/ipp/src/tests.rs
index ce895d81c..bc08594ad 100644
--- a/crates/ipp/src/tests.rs
+++ b/crates/ipp/src/tests.rs
@@ -249,6 +249,27 @@ fn an_ipv6_print_server_is_parsed_as_a_host_not_as_host_port() {
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()));
+ // A mistyped bracketed endpoint is not quietly the default one: the whole string stays the host,
+ // so nothing can resolve it and detect() reports the endpoint unreachable.
+ assert_eq!(Endpoint::parse("[::1]:abc"), Endpoint::Tcp("[::1]:abc".into(), 631));
+ assert_eq!(Endpoint::parse("[::1]:70000"), Endpoint::Tcp("[::1]:70000".into(), 631));
+ assert_eq!(Endpoint::parse("[::1]junk"), Endpoint::Tcp("[::1]junk".into(), 631));
+ assert!(!Endpoint::parse("[::1]:abc").reachable());
+}
+
+/// The authority a request carries (the `Host:` header and the `ipp://` printer-uri) has to bracket an
+/// IPv6 host, or its last colon reads as the port separator and CUPS sees a nonsense host.
+#[test]
+fn an_ipv6_endpoint_writes_its_authority_in_brackets() {
+ assert_eq!(Endpoint::parse("::1").host(), "[::1]:631");
+ assert_eq!(Endpoint::parse("[::1]:631").host(), "[::1]:631");
+ assert_eq!(Endpoint::parse("[fe80::1]:6310").host(), "[fe80::1]:6310");
+ assert_eq!(Endpoint::parse("cups.example").host(), "cups.example:631");
+ assert_eq!(Endpoint::parse("cups.example:6310").host(), "cups.example:6310");
+ assert_eq!(Endpoint::parse("127.0.0.1:6310").host(), "127.0.0.1:6310");
+ // What the user is shown is written the same way.
+ assert_eq!(Endpoint::parse("::1").to_string(), "[::1]:631");
+ assert_eq!(Endpoint::parse("cups.example:6310").to_string(), "cups.example:6310");
}
#[test]