Skip to content

Commit ce8e85e

Browse files
committed
fix(cli): preserve external TLS for SSH forwarding
Resolve the external scheme together with its authority when replacing an internal bind address with the registered gateway endpoint. Ported from #3714. Fixes: #3674 Signed-off-by: Daneyon Hansen <[email protected]>
1 parent 0e981be commit ce8e85e

3 files changed

Lines changed: 113 additions & 53 deletions

File tree

‎crates/openshell-cli/src/ssh.rs‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,13 @@ async fn ssh_session_config(
169169
// to 127.0.0.1 but the cluster is actually running on a remote host.
170170
#[allow(clippy::cast_possible_truncation)]
171171
let gateway_port_u16 = session.gateway_port as u16;
172-
let (gateway_host, gateway_port) =
173-
resolve_ssh_gateway(&session.gateway_host, gateway_port_u16, server);
174-
format_gateway_url(&session.gateway_scheme, &gateway_host, gateway_port)
172+
let (gateway_scheme, gateway_host, gateway_port) = resolve_ssh_gateway(
173+
&session.gateway_scheme,
174+
&session.gateway_host,
175+
gateway_port_u16,
176+
server,
177+
);
178+
format_gateway_url(&gateway_scheme, &gateway_host, gateway_port)
175179
};
176180
let gateway_name = tls
177181
.gateway_name()

‎crates/openshell-core/src/forward.rs‎

Lines changed: 85 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -826,50 +826,64 @@ fn lsof_listeners(port: u16) -> Option<String> {
826826
// SSH utility functions (shared between CLI and TUI)
827827
// ---------------------------------------------------------------------------
828828

829-
/// Resolve the SSH gateway host and port for a sandbox connection.
829+
/// Resolve the SSH gateway scheme, host, and port for a sandbox connection.
830830
///
831831
/// If the server-provided gateway host is a loopback address, use the host
832832
/// and port from the cluster endpoint instead so the client connects to the
833-
/// right machine. The server returns its internal bind address (e.g. 0.0.0.0:8080)
834-
/// which may not be reachable from outside — the cluster URL has the actual
835-
/// Docker-mapped or tunnel port.
833+
/// right machine. The server returns its internal bind address (e.g.
834+
/// `http://0.0.0.0:8080`) which may not be reachable from outside — the
835+
/// cluster URL has the actual scheme and Docker-mapped or tunnel authority.
836836
pub fn resolve_ssh_gateway(
837+
gateway_scheme: &str,
837838
gateway_host: &str,
838839
gateway_port: u16,
839840
cluster_url: &str,
840-
) -> (String, u16) {
841+
) -> (String, String, u16) {
841842
let is_loopback = gateway_host == "127.0.0.1"
842843
|| gateway_host == "0.0.0.0"
843844
|| gateway_host == "localhost"
845+
|| gateway_host == "::"
844846
|| gateway_host == "::1";
845847

846848
if !is_loopback {
847-
return (gateway_host.to_string(), gateway_port);
849+
return (
850+
gateway_scheme.to_string(),
851+
gateway_host.to_string(),
852+
gateway_port,
853+
);
848854
}
849855

850-
// Extract host and port from the cluster URL. The cluster URL represents
851-
// the externally reachable endpoint (e.g. Docker port-mapped address).
856+
// Extract the scheme, host, and port from the cluster URL. The cluster URL
857+
// represents the externally reachable endpoint (e.g. a TLS-terminating
858+
// proxy or Docker port-mapped address).
852859
if let Ok(url) = url::Url::parse(cluster_url)
860+
&& matches!(url.scheme(), "http" | "https")
853861
&& let Some(host) = url.host_str()
854862
{
863+
let cluster_scheme = url.scheme().to_string();
855864
let cluster_port = url.port_or_known_default().unwrap_or(gateway_port);
856865
let cluster_is_loopback =
857866
host == "127.0.0.1" || host == "0.0.0.0" || host == "localhost" || host == "::1";
858867
if !cluster_is_loopback {
859-
// Remote cluster: use the remote host but keep the cluster URL port.
860-
return (host.to_string(), cluster_port);
868+
// Remote cluster: use the complete externally reachable endpoint.
869+
return (cluster_scheme, host.to_string(), cluster_port);
861870
}
862871
// Unspecified addresses are bind-only, and tonic cannot use an IPv6
863872
// literal as a TLS DNS name. In those cases, keep the cluster URL's
864873
// already-reachable authority. Other loopback addresses retain the
865-
// gateway-reported host.
874+
// gateway-reported host, but use the cluster scheme and port because
875+
// those describe the externally reachable listener.
866876
if matches!(gateway_host, "0.0.0.0" | "::" | "::1") {
867-
return (host.to_string(), cluster_port);
877+
return (cluster_scheme, host.to_string(), cluster_port);
868878
}
869-
return (gateway_host.to_string(), cluster_port);
879+
return (cluster_scheme, gateway_host.to_string(), cluster_port);
870880
}
871881

872-
(gateway_host.to_string(), gateway_port)
882+
(
883+
gateway_scheme.to_string(),
884+
gateway_host.to_string(),
885+
gateway_port,
886+
)
873887
}
874888

875889
/// Bracket a bare IPv6 literal (e.g. `::1` → `[::1]`) so it can be embedded in
@@ -1106,68 +1120,98 @@ mod tests {
11061120

11071121
#[test]
11081122
fn resolve_ssh_gateway_keeps_non_loopback() {
1109-
let (host, port) = resolve_ssh_gateway("10.0.0.5", 8080, "https://spark.local");
1110-
assert_eq!(host, "10.0.0.5");
1111-
assert_eq!(port, 8080);
1123+
let resolved = resolve_ssh_gateway("http", "10.0.0.5", 8080, "https://spark.local");
1124+
assert_eq!(resolved, ("http".to_string(), "10.0.0.5".to_string(), 8080));
11121125
}
11131126

11141127
#[test]
11151128
fn resolve_ssh_gateway_overrides_loopback_with_cluster_host() {
1116-
let (host, port) = resolve_ssh_gateway("127.0.0.1", 8080, "https://spark.local");
1117-
assert_eq!(host, "spark.local");
1118-
assert_eq!(port, 443);
1129+
let resolved = resolve_ssh_gateway("http", "127.0.0.1", 8080, "https://spark.local");
1130+
assert_eq!(
1131+
resolved,
1132+
("https".to_string(), "spark.local".to_string(), 443)
1133+
);
11191134
}
11201135

11211136
#[test]
11221137
fn resolve_ssh_gateway_overrides_zeros_with_cluster_host() {
1123-
let (host, port) = resolve_ssh_gateway("0.0.0.0", 8080, "https://10.0.0.5:443");
1124-
assert_eq!(host, "10.0.0.5");
1125-
assert_eq!(port, 443);
1138+
let resolved = resolve_ssh_gateway("http", "0.0.0.0", 8080, "https://10.0.0.5:443");
1139+
assert_eq!(resolved, ("https".to_string(), "10.0.0.5".to_string(), 443));
11261140
}
11271141

11281142
#[test]
11291143
fn resolve_ssh_gateway_uses_known_default_http_port() {
1130-
let (host, port) = resolve_ssh_gateway("0.0.0.0", 8080, "http://gateway.example.test");
1131-
assert_eq!(host, "gateway.example.test");
1132-
assert_eq!(port, 80);
1144+
let resolved = resolve_ssh_gateway("https", "0.0.0.0", 8080, "http://gateway.example.test");
1145+
assert_eq!(
1146+
resolved,
1147+
("http".to_string(), "gateway.example.test".to_string(), 80)
1148+
);
11331149
}
11341150

11351151
#[test]
11361152
fn resolve_ssh_gateway_overrides_localhost() {
1137-
let (host, port) = resolve_ssh_gateway("localhost", 8080, "https://remote-host:443");
1138-
assert_eq!(host, "remote-host");
1139-
assert_eq!(port, 443);
1153+
let resolved = resolve_ssh_gateway("http", "localhost", 8080, "https://remote-host:443");
1154+
assert_eq!(
1155+
resolved,
1156+
("https".to_string(), "remote-host".to_string(), 443)
1157+
);
11401158
}
11411159

11421160
#[test]
11431161
fn resolve_ssh_gateway_no_override_when_cluster_is_also_loopback() {
1144-
let (host, port) = resolve_ssh_gateway("127.0.0.1", 8080, "https://127.0.0.1:443");
1145-
assert_eq!(host, "127.0.0.1");
1146-
assert_eq!(port, 443);
1162+
let resolved = resolve_ssh_gateway("http", "127.0.0.1", 8080, "https://127.0.0.1:443");
1163+
assert_eq!(
1164+
resolved,
1165+
("https".to_string(), "127.0.0.1".to_string(), 443)
1166+
);
11471167
}
11481168

11491169
#[test]
11501170
fn resolve_ssh_gateway_preserves_loopback_tls_authority() {
1151-
let (host, port) = resolve_ssh_gateway("::1", 8080, "https://localhost:8443");
1152-
assert_eq!(host, "localhost");
1153-
assert_eq!(port, 8443);
1171+
let resolved = resolve_ssh_gateway("http", "::1", 8080, "https://localhost:8443");
1172+
assert_eq!(
1173+
resolved,
1174+
("https".to_string(), "localhost".to_string(), 8443)
1175+
);
11541176
}
11551177

11561178
#[test]
11571179
fn resolve_ssh_gateway_swaps_zeros_for_loopback_cluster_host() {
11581180
// The gateway binds 0.0.0.0 but advertises that bind address via the
11591181
// SSH session response. 0.0.0.0 is not a valid connect target and is
11601182
// not in any TLS cert SAN; fall through to the cluster URL's host.
1161-
let (host, port) = resolve_ssh_gateway("0.0.0.0", 8080, "https://127.0.0.1:9000");
1162-
assert_eq!(host, "127.0.0.1");
1163-
assert_eq!(port, 9000);
1183+
let resolved = resolve_ssh_gateway("http", "0.0.0.0", 8080, "https://127.0.0.1:9000");
1184+
assert_eq!(
1185+
resolved,
1186+
("https".to_string(), "127.0.0.1".to_string(), 9000)
1187+
);
1188+
}
1189+
1190+
#[test]
1191+
fn resolve_ssh_gateway_swaps_unspecified_ipv6_for_cluster_host() {
1192+
let resolved = resolve_ssh_gateway("http", "::", 8080, "https://localhost:18443");
1193+
assert_eq!(
1194+
resolved,
1195+
("https".to_string(), "localhost".to_string(), 18443)
1196+
);
11641197
}
11651198

11661199
#[test]
11671200
fn resolve_ssh_gateway_handles_invalid_cluster_url() {
1168-
let (host, port) = resolve_ssh_gateway("127.0.0.1", 8080, "not-a-url");
1169-
assert_eq!(host, "127.0.0.1");
1170-
assert_eq!(port, 8080);
1201+
let resolved = resolve_ssh_gateway("https", "127.0.0.1", 8080, "not-a-url");
1202+
assert_eq!(
1203+
resolved,
1204+
("https".to_string(), "127.0.0.1".to_string(), 8080)
1205+
);
1206+
}
1207+
1208+
#[test]
1209+
fn resolve_ssh_gateway_ignores_non_http_cluster_url() {
1210+
let resolved = resolve_ssh_gateway("https", "127.0.0.1", 8080, "ssh://remote-host:22");
1211+
assert_eq!(
1212+
resolved,
1213+
("https".to_string(), "127.0.0.1".to_string(), 8080)
1214+
);
11711215
}
11721216

11731217
#[test]

‎crates/openshell-tui/src/lib.rs‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -978,9 +978,13 @@ async fn handle_shell_connect(
978978
// Step 3: Resolve gateway address (handle loopback override).
979979
#[allow(clippy::cast_possible_truncation)]
980980
let gateway_port_u16 = session.gateway_port as u16;
981-
let (gateway_host, gateway_port) =
982-
resolve_ssh_gateway(&session.gateway_host, gateway_port_u16, &app.endpoint);
983-
let gateway_url = format_gateway_url(&session.gateway_scheme, &gateway_host, gateway_port);
981+
let (gateway_scheme, gateway_host, gateway_port) = resolve_ssh_gateway(
982+
&session.gateway_scheme,
983+
&session.gateway_host,
984+
gateway_port_u16,
985+
&app.endpoint,
986+
);
987+
let gateway_url = format_gateway_url(&gateway_scheme, &gateway_host, gateway_port);
984988

985989
// Step 4: Build the ProxyCommand using our own binary.
986990
let exe = match std::env::current_exe() {
@@ -1114,9 +1118,13 @@ async fn handle_exec_command(
11141118
// Step 2: Resolve gateway and build ProxyCommand (same as handle_shell_connect).
11151119
#[allow(clippy::cast_possible_truncation)]
11161120
let gateway_port_u16 = session.gateway_port as u16;
1117-
let (gateway_host, gateway_port) =
1118-
resolve_ssh_gateway(&session.gateway_host, gateway_port_u16, &app.endpoint);
1119-
let gateway_url = format_gateway_url(&session.gateway_scheme, &gateway_host, gateway_port);
1121+
let (gateway_scheme, gateway_host, gateway_port) = resolve_ssh_gateway(
1122+
&session.gateway_scheme,
1123+
&session.gateway_host,
1124+
gateway_port_u16,
1125+
&app.endpoint,
1126+
);
1127+
let gateway_url = format_gateway_url(&gateway_scheme, &gateway_host, gateway_port);
11201128

11211129
let exe = match std::env::current_exe() {
11221130
Ok(p) => p,
@@ -1577,9 +1585,13 @@ async fn start_port_forwards(
15771585
// Resolve gateway address.
15781586
#[allow(clippy::cast_possible_truncation)]
15791587
let gateway_port_u16 = session.gateway_port as u16;
1580-
let (gateway_host, gateway_port) =
1581-
resolve_ssh_gateway(&session.gateway_host, gateway_port_u16, endpoint);
1582-
let gateway_url = format_gateway_url(&session.gateway_scheme, &gateway_host, gateway_port);
1588+
let (gateway_scheme, gateway_host, gateway_port) = resolve_ssh_gateway(
1589+
&session.gateway_scheme,
1590+
&session.gateway_host,
1591+
gateway_port_u16,
1592+
endpoint,
1593+
);
1594+
let gateway_url = format_gateway_url(&gateway_scheme, &gateway_host, gateway_port);
15831595

15841596
// Build ProxyCommand.
15851597
let exe = match std::env::current_exe() {

0 commit comments

Comments
 (0)