From 92857fc1ba1a9322bd3547d5c2614494d174b735 Mon Sep 17 00:00:00 2001 From: Kevin Yue Date: Sun, 8 Nov 2026 21:36:31 +0800 Subject: [PATCH] fix: add gateway login diagnostics and scope platform dependencies --- Cargo.toml | 1 - crates/gpapi/Cargo.toml | 13 ++-- crates/gpapi/src/gateway/login.rs | 101 +++++++++++++++++++++++++++++- 3 files changed, 107 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3f910a6..9d5456c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,6 @@ base64 = { version = "0.23", default-features = false, features = ["std"] } chrono = "0.4" clap = { version = "4", features = ["derive"] } clap-verbosity-flag = "3" -ctrlc = "3.4" directories = "6.0" dns-lookup = "4.0.2" env_logger = "0.11" diff --git a/crates/gpapi/Cargo.toml b/crates/gpapi/Cargo.toml index f4d8494..ca6646b 100644 --- a/crates/gpapi/Cargo.toml +++ b/crates/gpapi/Cargo.toml @@ -8,7 +8,6 @@ license = "MIT" [dependencies] nix = { version = "0.31", features = ["fs", "signal", "process"], optional = true } common = { path = "../common" } -directories = { workspace = true, optional = true } anyhow.workspace = true base64.workspace = true dns-lookup = { workspace = true, optional = true } @@ -45,7 +44,6 @@ chrono.workspace = true humantime.workspace = true netdev = { workspace = true, optional = true } socket2 = { version = "0.6", features = ["all"], optional = true } -os_info = { version = "3", default-features = false, optional = true } snow.workspace = true zeroize = { workspace = true, features = ["serde"] } getrandom = "0.4" @@ -66,8 +64,15 @@ oslog = { version = "0.2", default-features = false, optional = true } gtk = { version = "0.18", optional = true } [target.'cfg(target_os="linux")'.dependencies] +directories = { workspace = true, optional = true } libloading = { version = "0.9", optional = true } +[target.'cfg(target_os="macos")'.dependencies] +os_info = { version = "3", default-features = false, optional = true } + +[target.'cfg(target_os="windows")'.dependencies] +os_info = { version = "3", default-features = false } + [build-dependencies] cc = "1" pkg-config = "0.3" @@ -80,8 +85,8 @@ tokio = { workspace = true, features = ["test-util"] } [features] default = ["desktop"] -desktop = ["tokio/process", "tokio/signal", "dep:directories", "dep:nix", "dep:uzers", "dep:netdev", "dep:os_info", "dep:whoami", "dep:dns-lookup", "dep:socket2", "dep:libloading"] -tauri = ["desktop", "dep:tauri", "dep:gtk"] +desktop = ["tokio/process", "tokio/signal", "dep:directories", "dep:nix", "dep:uzers", "dep:netdev", "dep:whoami", "dep:dns-lookup", "dep:socket2", "dep:libloading"] +tauri = ["desktop", "dep:tauri", "dep:gtk", "dep:os_info"] clap = ["desktop", "dep:clap", "dep:clap-verbosity-flag"] webview-auth = [] logger = ["desktop", "dep:env_logger", "dep:log-reload", "dep:oslog"] diff --git a/crates/gpapi/src/gateway/login.rs b/crates/gpapi/src/gateway/login.rs index 90e04b7..d52661d 100644 --- a/crates/gpapi/src/gateway/login.rs +++ b/crates/gpapi/src/gateway/login.rs @@ -18,7 +18,7 @@ use crate::{ gp_params::GpParams, params::gateway_login::{self, GatewayLoginInput}, session::GatewayAuthentication, - utils::{normalize_server, parse_gp_response, remove_url_scheme, xml::ElementExt}, + utils::{normalize_server, parse_gp_response, redact::redact_uri, remove_url_scheme, xml::ElementExt}, }; pub enum GatewayLogin { @@ -181,7 +181,7 @@ async fn receive_gateway_login( log_gateway_login_context(context, client_ip, gp_params); let res = client - .post(login_url) + .post(&login_url) .form(&request_params.body) .send() .await @@ -200,14 +200,30 @@ async fn receive_gateway_login( .map(<[u8]>::to_vec); let endpoint = res.remote_addr(); let url = res.url().clone(); + let http_version = res.version(); + let content_type = res.headers().get(reqwest::header::CONTENT_TYPE).cloned(); let res = parse_gp_response(res).await.map_err(|err| { + // Redirect paths and query strings can contain authentication tokens. Log + // only the redacted origin and whether the response is from the login path. + warn!( + "Gateway login response failed: status={}, reason={}, http={:?}, origin={}, port={:?}, redirected={}, login_endpoint={}, content_type={:?}, client_certificate={}, ignore_tls_errors={}", + err.status, + err.reason, + http_version, + redact_uri(&url.origin().ascii_serialization()), + url.port_or_known_default(), + url.as_str() != login_url, + url.path() == "/ssl-vpn/login.esp", + content_type, + gp_params.has_client_identity(), + gp_params.ignore_tls_errors(), + ); if let Some(tls) = crate::error::ServerTlsError::from_request(origin, &err, gp_params.has_client_identity()) { return tls; } if gp_params.os_profile().client_os() == crate::os_profile::ClientOs::Ios && err.is_authentication_rejection() { return anyhow::anyhow!(crate::error::AuthenticationRejected); } - warn!("Gateway login response failed: {}", err.reason); anyhow::anyhow!("Gateway login error: {}", err.reason) })?; @@ -429,6 +445,85 @@ mod tests { .build() } + #[tokio::test] + async fn failed_login_logs_http_context_without_credentials_or_redirect_tokens() { + use std::cell::RefCell; + + thread_local! { + static MESSAGES: RefCell> = const { RefCell::new(Vec::new()) }; + } + struct Logger; + impl log::Log for Logger { + fn enabled(&self, _: &log::Metadata<'_>) -> bool { + true + } + + fn log(&self, record: &log::Record<'_>) { + let message = record.args().to_string(); + if message.starts_with("Gateway login response failed:") { + MESSAGES.with_borrow_mut(|messages| messages.push(message)); + } + } + + fn flush(&self) {} + } + log::set_logger(&Logger).unwrap(); + log::set_max_level(log::LevelFilter::Warn); + + for (status, reason, redirected) in [ + (403, None, false), + (502, None, true), + (401, Some("access denied"), false), + ] { + let body = "response-body-secret"; + let mut responses = Vec::new(); + if redirected { + responses.push( + "HTTP/1.1 307 Temporary Redirect\r\nLocation: /path-secret?token=query-secret\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".into(), + ); + } + let reason_header = reason + .map(|reason| format!("x-private-pan-globalprotect: {reason}\r\n")) + .unwrap_or_default(); + responses.push(format!( + "HTTP/1.1 {status} Error\r\nContent-Type: text/html\r\nSet-Cookie: cookie-secret\r\n{reason_header}Content-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + )); + let (_, origin, server) = crate::session::transport::tests::login_gateway(responses).await; + let mut client = GatewayLoginClient::new(origin.as_str(), test_params()).unwrap(); + let credential = crate::credential::PasswordCredential::new("username-secret", "password-secret").into(); + let error = client + .login(&credential, None, &CancellationToken::new()) + .await + .err() + .unwrap(); + server.await.unwrap(); + + assert_eq!( + error.to_string(), + format!("Gateway login error: {}", reason.unwrap_or("")) + ); + let messages = MESSAGES.with_borrow_mut(std::mem::take); + assert_eq!(messages.len(), 1); + let message = &messages[0]; + for expected in [ + format!("status={status}"), + format!("reason={}", reason.unwrap_or("")), + format!("port={:?}", origin.port()), + format!("redirected={redirected}"), + format!("login_endpoint={}", !redirected), + "http=HTTP/1.1".into(), + "content_type=Some(\"text/html\")".into(), + "client_certificate=false".into(), + "ignore_tls_errors=false".into(), + ] { + assert!(message.contains(&expected), "missing {expected}: {message}"); + } + assert!(!message.contains("secret"), "sensitive data in diagnostic: {message}"); + assert!(!message.contains("127.0.0.1"), "unredacted origin: {message}"); + } + } + #[tokio::test] async fn gateway_login_cancels_stalled_headers_and_body() { use tokio::io::{AsyncReadExt, AsyncWriteExt};