From e3d794d77445109f91753631dc3cf75132737576 Mon Sep 17 00:00:00 2001 From: Jan Zachmann <50990105+JanZachmann@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:26:10 +0100 Subject: [PATCH 1/3] fix(ssh): show HTTP status and body of a failed tunnel request When the backend rejects the tunnel request without an `internalMsg` JSON body, e.g. a 403 from a gateway, the error now shows the HTTP status and the start of the body instead of "unknown error type". Server text is cut to 200 characters and control characters are replaced, so an HTML error page or escape sequences do not flood or change the terminal. Signed-off-by: Jan Zachmann 50990105+JanZachmann@users.noreply.github.com --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/ssh.rs | 118 ++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 109 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 17e8f55..aeb8457 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1748,7 +1748,7 @@ dependencies = [ [[package]] name = "omnect-cli" -version = "0.29.0" +version = "0.29.1" dependencies = [ "actix-web", "anyhow", diff --git a/Cargo.toml b/Cargo.toml index d9077bc..e138ec1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0" name = "omnect-cli" readme = "README.md" repository = "https://github.com/omnect/omnect-cli" -version = "0.29.0" +version = "0.29.1" [dependencies] actix-web = { version = "4.17", default-features = false, features = ["macros"] } diff --git a/src/ssh.rs b/src/ssh.rs index fc7badc..2b5a4e0 100644 --- a/src/ssh.rs +++ b/src/ssh.rs @@ -13,6 +13,7 @@ use url::Url; static BACKEND_API_ENDPOINT: &str = "/api/devices/prepareSSHConnection"; static SSH_KEY_FORMAT: &str = "ed25519"; +const MAX_ERROR_BODY_CHARS: usize = 200; static BASTION_CERT_NAME: &str = "bastion-cert.pub"; static DEVICE_CERT_NAME: &str = "device-cert.pub"; @@ -187,22 +188,47 @@ async fn unpack_response(response: reqwest::Response) -> Re .map_err(|err| anyhow!("could not read response body: {err}"))?; if !status.is_success() { - #[derive(Deserialize)] - struct ErrorMessage { - #[serde(rename = "internalMsg")] - internal_message: String, - } - - anyhow::bail!( - serde_json::from_str::(&body) - .map(|err| err.internal_message) - .unwrap_or_else(|_| "unknown error type".to_string()) - ) + anyhow::bail!(error_message(status, &body)) } else { serde_json::from_str(&body).map_err(|_| anyhow!("unsupported reply.")) } } +fn error_message(status: reqwest::StatusCode, body: &str) -> String { + #[derive(Deserialize)] + struct ErrorMessage { + #[serde(rename = "internalMsg")] + internal_message: String, + } + + if let Ok(err) = serde_json::from_str::(body) { + let message = printable(&err.internal_message); + if !message.is_empty() { + return message; + } + } + + let status = match status.canonical_reason() { + Some(reason) => format!("HTTP {} {reason}", status.as_u16()), + None => format!("HTTP {}", status.as_u16()), + }; + + match printable(body) { + shown if shown.is_empty() => status, + shown => format!("{status}: {shown}"), + } +} + +/// Server text can be a long HTML error page or hold terminal escape sequences. +fn printable(text: &str) -> String { + let mut chars = text.chars().map(|c| if c.is_control() { ' ' } else { c }); + let mut shown: String = chars.by_ref().take(MAX_ERROR_BODY_CHARS).collect(); + if chars.next().is_some() { + shown.push_str("..."); + } + shown.trim().to_string() +} + async fn request_ssh_tunnel( backend: &Url, device_id: &str, @@ -467,6 +493,76 @@ pub async fn ssh_create_tunnel( mod test { use super::*; + #[test] + fn error_message_takes_internal_msg() { + assert_eq!( + error_message( + reqwest::StatusCode::BAD_REQUEST, + r#"{"internalMsg":"Device may not be online"}"# + ), + "Device may not be online" + ); + } + + #[test] + fn error_message_shows_status_and_body_of_unknown_reply() { + assert_eq!( + error_message(reqwest::StatusCode::FORBIDDEN, "Forbidden\n"), + "HTTP 403 Forbidden: Forbidden" + ); + } + + #[test] + fn error_message_shows_status_of_empty_reply() { + assert_eq!( + error_message(reqwest::StatusCode::BAD_GATEWAY, ""), + "HTTP 502 Bad Gateway" + ); + } + + #[test] + fn error_message_ignores_empty_internal_msg() { + assert_eq!( + error_message( + reqwest::StatusCode::INTERNAL_SERVER_ERROR, + r#"{"internalMsg":" "}"# + ), + "HTTP 500 Internal Server Error: {\"internalMsg\":\" \"}" + ); + } + + #[test] + fn error_message_drops_control_chars_of_internal_msg() { + assert_eq!( + error_message( + reqwest::StatusCode::BAD_REQUEST, + r#"{"internalMsg":"Device\u001b[2J offline\n"}"# + ), + "Device [2J offline" + ); + } + + #[test] + fn error_message_shows_status_without_reason() { + assert_eq!( + error_message(reqwest::StatusCode::from_u16(599).unwrap(), ""), + "HTTP 599" + ); + } + + #[test] + fn error_message_shortens_long_body_and_drops_control_chars() { + let body = format!("a\x1b[2J{}", "x".repeat(MAX_ERROR_BODY_CHARS)); + let message = error_message(reqwest::StatusCode::BAD_GATEWAY, &body); + + assert!(!message.contains('\x1b')); + assert!(message.ends_with("...")); + assert_eq!( + message.chars().count(), + "HTTP 502 Bad Gateway: ".len() + MAX_ERROR_BODY_CHARS + "...".len() + ); + } + fn test_query_yes_no_for_result( input: &str, expected_result: bool, From 7811ec06713d8f17aea74b9083ce001686a0c04d Mon Sep 17 00:00:00 2001 From: Jan Zachmann <50990105+JanZachmann@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:28:40 +0100 Subject: [PATCH 2/3] fix(ssh): show the status on every failed tunnel reply, hint on 403 The body-read error now names the HTTP status, and a 2xx reply that is not the expected JSON shows the serde error, the status and the body. A 403 without the backend's error format gets a hint: the account may lack access to the device, or a network filter in front of the backend, e.g. one that allows only a VPN, refused the request. Signed-off-by: Jan Zachmann 50990105+JanZachmann@users.noreply.github.com --- src/ssh.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/src/ssh.rs b/src/ssh.rs index 2b5a4e0..23db2e8 100644 --- a/src/ssh.rs +++ b/src/ssh.rs @@ -182,15 +182,22 @@ struct SshTunnelInfo { async fn unpack_response(response: reqwest::Response) -> Result { let status = response.status(); - let body = response - .text() - .await - .map_err(|err| anyhow!("could not read response body: {err}"))?; + let body = response.text().await.map_err(|err| { + anyhow!( + "{}: could not read response body: {err}", + status_text(status) + ) + })?; if !status.is_success() { anyhow::bail!(error_message(status, &body)) } else { - serde_json::from_str(&body).map_err(|_| anyhow!("unsupported reply.")) + serde_json::from_str(&body).map_err(|err| { + anyhow!( + "unsupported reply: {err}: {}", + with_body(status_text(status), &body) + ) + }) } } @@ -208,11 +215,27 @@ fn error_message(status: reqwest::StatusCode, body: &str) -> String { } } - let status = match status.canonical_reason() { + let message = with_body(status_text(status), body); + + // A 403 without the backend's error format can also come from a network + // filter in front of the backend, e.g. one that only allows a VPN. + if status == reqwest::StatusCode::FORBIDDEN { + format!( + "{message} (check that your account has access to the device, and that you are on a network the backend allows, e.g. a VPN)" + ) + } else { + message + } +} + +fn status_text(status: reqwest::StatusCode) -> String { + match status.canonical_reason() { Some(reason) => format!("HTTP {} {reason}", status.as_u16()), None => format!("HTTP {}", status.as_u16()), - }; + } +} +fn with_body(status: String, body: &str) -> String { match printable(body) { shown if shown.is_empty() => status, shown => format!("{status}: {shown}"), @@ -507,11 +530,40 @@ mod test { #[test] fn error_message_shows_status_and_body_of_unknown_reply() { assert_eq!( - error_message(reqwest::StatusCode::FORBIDDEN, "Forbidden\n"), - "HTTP 403 Forbidden: Forbidden" + error_message(reqwest::StatusCode::UNAUTHORIZED, "Unauthorized\n"), + "HTTP 401 Unauthorized: Unauthorized" ); } + #[test] + fn error_message_names_access_and_network_on_forbidden() { + let message = error_message(reqwest::StatusCode::FORBIDDEN, "Forbidden"); + + assert!(message.starts_with("HTTP 403 Forbidden: Forbidden (")); + assert!(message.contains("VPN")); + } + + #[tokio::test] + async fn unpack_response_shows_status_and_body_of_unsupported_reply() { + let server = httpmock::MockServer::start_async().await; + server + .mock_async(|when, then| { + when.any_request(); + then.status(200).body("login"); + }) + .await; + let response = reqwest::get(server.url("/")).await.unwrap(); + + let err = unpack_response::(response) + .await + .err() + .unwrap() + .to_string(); + + assert!(err.starts_with("unsupported reply: ")); + assert!(err.ends_with("HTTP 200 OK: login")); + } + #[test] fn error_message_shows_status_of_empty_reply() { assert_eq!( From 53f9d519b3800b5bb0eaae2b482b70478c8a3447 Mon Sep 17 00:00:00 2001 From: Jan Zachmann <50990105+JanZachmann@users.noreply.github.com> Date: Thu, 19 Nov 2026 10:28:54 +0100 Subject: [PATCH 3/3] fix(deps): update actix-http and rustix for RUSTSEC advisories actix-http 3.22.0 fixes RUSTSEC-2026-0552, RUSTSEC-2026-0553 and RUSTSEC-2026-0554; rustix 1.1.6 fixes RUSTSEC-2026-0486. Signed-off-by: Jan Zachmann 50990105+JanZachmann@users.noreply.github.com --- Cargo.lock | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aeb8457..c12947b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,9 +21,9 @@ dependencies = [ [[package]] name = "actix-http" -version = "3.20.3" +version = "3.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cd4ef186188fed2d4f8177d712ad623c751c6e84f45b31da694cc53f483f320b" +checksum = "50f98f47b03911915aa3f0b856749aa3d3915033afabb295ab6ac5ff3b8bd5c1" dependencies = [ "actix-codec", "actix-service", @@ -847,7 +847,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2239,15 +2239,15 @@ dependencies = [ [[package]] name = "rustix" -version = "1.1.5" +version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" dependencies = [ "bitflags", "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2651,7 +2651,7 @@ dependencies = [ "getrandom 0.4.3", "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]]