diff --git a/crates/gpapi/src/portal/config.rs b/crates/gpapi/src/portal/config.rs index ea60a74..203a74f 100644 --- a/crates/gpapi/src/portal/config.rs +++ b/crates/gpapi/src/portal/config.rs @@ -13,7 +13,7 @@ use crate::{ error::PortalError, gateway::{Gateway, parse_gateways}, gp_params::GpParams, - utils::{normalize_server, parse_gp_response, remove_url_scheme, xml::ElementExt}, + utils::{normalize_server, parse_gp_response, redact::redact_form_params, remove_url_scheme, xml::ElementExt}, }; use super::csc; @@ -131,6 +131,7 @@ pub async fn retrieve_config(portal: &str, cred: &Credential, gp_params: &GpPara let params = build_config_params(cred, gp_params, &server, &swg_nonce); info!("Retrieve the portal config, user_agent: {}", gp_params.user_agent()); + info!("Portal config request params: {}", redact_params(¶ms)); let res = client.post(&url).form(¶ms).send().await.map_err(|e| { warn!("Network error: {:?}", e); @@ -158,13 +159,14 @@ pub async fn retrieve_config(portal: &str, cred: &Credential, gp_params: &GpPara let root = Element::parse(res_xml.as_bytes()).map_err(|e| PortalError::ConfigError(e.to_string()))?; if csc::is_config_criteria(&root) { - info!("Portal returned CSC criteria, retrieving CSC portal config"); + info!("Portal returned CSC criteria: {}", csc_criteria_summary(&root)); let csc_xml = retrieve_csc_config(&client, &portal, &root, cred.username(), gp_params).await?; debug!("Portal CSC config response: {}", csc_xml); let root = Element::parse(csc_xml.as_bytes()).map_err(|e| PortalError::ConfigError(e.to_string()))?; return parse_portal_config(&server, cred, root); } + info!("Portal did not return CSC criteria"); parse_portal_config(&server, cred, root) } @@ -201,6 +203,8 @@ async fn retrieve_csc_config( let params = csc::csc_params(&csc_req, username, gp_params, &swg_nonce); let url = format!("{}/global-protect/getconfig_csc.esp", portal); + info!("Portal CSC config request params: {}", redact_params(¶ms)); + let res = client.post(&url).form(¶ms).send().await.map_err(|e| { warn!("Network error: {:?}", e); anyhow::anyhow!(PortalError::NetworkError(e)) @@ -220,6 +224,29 @@ async fn retrieve_csc_config( }) } +fn redact_params(params: &HashMap<&str, &str>) -> String { + let params = params.iter().map(|(key, value)| (*key, *value)).collect::>(); + redact_form_params(¶ms) +} + +fn csc_criteria_summary(root: &Element) -> String { + let auth_cookie = present(root.descendant_text("portal-csc-auth-cookie").as_deref()); + let config_digest = present(root.descendant_text("config-digest").as_deref()); + let custom_check_entries = root + .descendant("custom-checks") + .map(|custom_checks| custom_checks.descendants("entry").len()) + .unwrap_or_default(); + + format!("auth_cookie={auth_cookie}, config_digest={config_digest}, custom_check_entries={custom_check_entries}") +} + +fn present(value: Option<&str>) -> &'static str { + match value { + Some(value) if !value.is_empty() => "present", + _ => "empty", + } +} + fn parse_portal_config(server: &str, cred: &Credential, root: Element) -> anyhow::Result { let mut ihd_enabled = false; let mut prefer_internal = false; diff --git a/crates/gpapi/src/utils/redact.rs b/crates/gpapi/src/utils/redact.rs index 1864a2e..712c847 100644 --- a/crates/gpapi/src/utils/redact.rs +++ b/crates/gpapi/src/utils/redact.rs @@ -123,6 +123,46 @@ pub fn redact_uri(uri: &str) -> String { url.to_string() } +pub fn redact_form_params(params: &[(&str, &str)]) -> String { + let mut pairs = params + .iter() + .map(|(key, value)| (*key, redact_form_value(key, value))) + .collect::>(); + + pairs.sort_by(|(left, _), (right, _)| left.cmp(right)); + + form_urlencoded::Serializer::new(String::new()) + .extend_pairs(pairs) + .finish() +} + +fn redact_form_value(key: &str, value: &str) -> String { + if value.is_empty() || is_safe_form_param(key) { + return value.to_string(); + } + + format!("redacted-{}", value.len()) +} + +fn is_safe_form_param(key: &str) -> bool { + matches!( + key, + "clientVer" + | "clientos" + | "config-digest" + | "csc-digest" + | "csc-support" + | "direct" + | "ipv6-support" + | "jnlpReady" + | "ok" + | "os-version" + | "prot" + | "swg-auth-token" + | "swg-nonce" + ) +} + fn redact_query(query: Option<&str>) -> Option { let query = query?; @@ -190,6 +230,20 @@ mod tests { assert_eq!(redact_uri(uri), "https://f**********r/baz?qux=q**********x"); } + #[test] + fn it_should_redact_form_params() { + let params = [ + ("portal-userauthcookie", "secret-cookie"), + ("csc-support", "yes"), + ("prelogin-cookie", ""), + ]; + + assert_eq!( + redact_form_params(¶ms), + "csc-support=yes&portal-userauthcookie=redacted-13&prelogin-cookie=" + ); + } + #[test] fn it_should_redact_data_uri() { let uri = "data:text/plain;a";