feat: collect native HIP facts and log redacted XML responses

This commit is contained in:
Kevin Yue committed 2026-11-18 13:56:12 +08:00
1 parent f2ca674b88
commit bde11186f8
26 files changed
+1567 -206

No files matched your search

+1 -1
View File
@@ -98,7 +98,7 @@ uzers = { workspace = true, optional = true }
[target.'cfg(windows)'.dependencies]
file-rotate = { version = "0.8.0", optional = true }
windows-sys = { version = "0.61", features = ["Win32_NetworkManagement_IpHelper", "Win32_NetworkManagement_Ndis", "Win32_Networking_WinSock", "Win32_Foundation", "Win32_Security", "Win32_Security_Authorization", "Win32_Storage_FileSystem", "Win32_System_Pipes", "Win32_System_Threading", "Win32_System_Services", "Win32_Globalization", "Win32_System_Environment", "Win32_System_JobObjects", "Win32_System_IO"] }
windows-sys = { version = "0.61", features = ["Win32_NetworkManagement_IpHelper", "Win32_NetworkManagement_Ndis", "Win32_Networking_WinSock", "Win32_Foundation", "Win32_Security", "Win32_Security_Authorization", "Win32_Storage_FileSystem", "Win32_System_Pipes", "Win32_System_Threading", "Win32_System_Services", "Win32_Globalization", "Win32_System_Environment", "Win32_System_JobObjects", "Win32_System_IO", "Win32_System_Registry", "Win32_System_SystemInformation"] }
[target.'cfg(windows)'.dev-dependencies]
windows-sys = { version = "0.61", features = ["Win32_UI_Shell"] }
+5 -10
View File
@@ -111,7 +111,7 @@ pub async fn extend_session(ctx: &SessionContext) -> anyhow::Result<()> {
let response = client.post(&url).form(&form).send().await?;
let response = parse_gp_response(response).await?;
let root = Element::parse(response.as_bytes())?;
let status = parse_extend_session_status(&root, &response)?;
let status = parse_extend_session_status(&root)?;
info!("Extend-session gateway response status: {}", status);
@@ -151,11 +151,11 @@ fn build_extend_session_form(ctx: &SessionContext) -> anyhow::Result<HashMap<Str
Ok(form)
}
fn parse_extend_session_status(root: &Element, response: &str) -> anyhow::Result<String> {
fn parse_extend_session_status(root: &Element) -> anyhow::Result<String> {
match root.attr("status") {
Some(status) => Ok(status.to_string()),
None => {
warn!("Malformed extend-session response body: {}", response.trim());
warn!("Extend-session response is missing status");
bail!("Malformed extend session response")
}
}
@@ -231,10 +231,7 @@ mod tests {
fn parses_extend_session_status_attribute() {
let root = Element::parse(r#"<response status="success"></response>"#.as_bytes()).unwrap();
assert_eq!(
parse_extend_session_status(&root, r#"<response status="success"></response>"#).unwrap(),
"success"
);
assert_eq!(parse_extend_session_status(&root).unwrap(), "success");
}
#[test]
@@ -242,9 +239,7 @@ mod tests {
let root = Element::parse("<response><result>ok</result></response>".as_bytes()).unwrap();
assert_eq!(
parse_extend_session_status(&root, "<response><result>ok</result></response>")
.unwrap_err()
.to_string(),
parse_extend_session_status(&root).unwrap_err().to_string(),
"Malformed extend session response"
);
}
+8 -1
View File
@@ -173,6 +173,12 @@ pub fn collect_profile_with_control(
Some(identity) => identity.clone(),
None => collect_with_host_id_and_control(host_id_override, control)?,
};
#[cfg(windows)]
let identity = if builder.client_os == ClientOs::Windows {
windows::native_profile_identity(identity, host_id_override)?
} else {
identity
};
let runtime_os = runtime_client_os();
// Preserve the old behavior: only probe the native version when reporting it.
let (os_version, software_version) = if builder.client_os == runtime_os {
@@ -181,7 +187,8 @@ pub fn collect_profile_with_control(
(defaults::os_version(runtime_os), defaults::software_version(runtime_os))
};
control.check()?;
Ok(builder.build(HostSnapshot {
// The builder may carry cached hardware identity; use the resolved profile identity.
Ok(builder.host_identity(identity.clone()).build(HostSnapshot {
runtime_os,
identity,
os_version,
+85 -2
View File
@@ -137,14 +137,24 @@ fn assert_mac(value: &str, separator: char) {
}
#[test]
fn builder_delegates_default_identity_to_host_identity() {
fn builder_preserves_supplied_hardware_fields() {
let expected = collect_identity();
let profile = collect_profile(
OsProfileBuilder::new(runtime_client_os()).host_identity(expected.clone()),
None,
);
assert_eq!(profile.host_identity(), &expected);
assert!(
profile
.host_identity()
.computer()
.eq_ignore_ascii_case(expected.computer())
);
assert_eq!(profile.host_identity().serialno(), expected.serialno());
assert_eq!(profile.host_identity().mac_addr(), expected.mac_addr());
if runtime_client_os() != ClientOs::Windows {
assert_eq!(profile.host_identity().host_id(), expected.host_id());
}
}
#[test]
@@ -178,3 +188,76 @@ fn controlled_profile_collection_propagates_cancellation_and_deadline() {
let error = collect_profile_with_control(OsProfileBuilder::new(ClientOs::Linux), None, &expired).unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::TimedOut);
}
#[cfg(windows)]
#[test]
fn native_windows_profile_uses_installation_id_and_emulation_keeps_its_seed() {
let identity = HostIdentity::from_parts(
"test-host".into(),
"hardware-seed".into(),
"serial".into(),
"00:11:22:33:44:55".into(),
);
let control = CollectionBudget::new(Duration::from_secs(60));
let native = collect_profile_with_control(
OsProfile::builder(ClientOs::Windows).host_identity(identity.clone()),
None,
&control,
)
.unwrap();
// Read independently through the registry CLI to check the native API's source and view.
let output = std::process::Command::new("reg")
.args([
"query",
r"HKLM\SOFTWARE\Microsoft\Cryptography",
"/v",
"MachineGuid",
"/reg:64",
])
.output()
.unwrap();
assert!(output.status.success());
let text = String::from_utf8(output.stdout).unwrap();
let expected = text
.lines()
.find_map(|line| {
let mut parts = line.split_whitespace();
(parts.next()? == "MachineGuid").then(|| parts.last().unwrap())
})
.unwrap();
assert_eq!(native.host_id(), expected);
assert_eq!(native.host_identity().host_id(), expected);
assert!(native.computer().eq_ignore_ascii_case(&fallback_hostname()));
let with_host_id = collect_profile_with_control(
OsProfile::builder(ClientOs::Windows).host_identity(identity.clone()),
Some("explicit-host-id"),
&control,
)
.unwrap();
assert_eq!(with_host_id.host_id(), "explicit-host-id");
assert_eq!(with_host_id.computer(), native.computer());
let overridden = collect_profile_with_control(
OsProfile::builder(ClientOs::Windows)
.host_identity(identity.clone())
.computer_name_override("Custom-Host"),
None,
&control,
)
.unwrap();
assert_eq!(overridden.computer(), "Custom-Host");
for client_os in [ClientOs::Mac, ClientOs::Linux] {
let builder = OsProfile::builder(client_os).host_identity(identity.clone());
let expected = builder.clone().build(HostSnapshot {
runtime_os: ClientOs::Windows,
identity: identity.clone(),
os_version: String::new(),
software_version: String::new(),
});
let actual = collect_profile_with_control(builder, None, &control).unwrap();
assert_eq!(actual.host_identity(), &identity);
assert_eq!(actual.host_id(), expected.host_id());
assert_eq!(actual.serialno(), expected.serialno());
assert_eq!(actual.mac_addr(), expected.mac_addr());
}
}
+59 -19
View File
@@ -1,4 +1,6 @@
use std::{os::windows::process::CommandExt, process::Command};
use std::{io, os::windows::process::CommandExt, process::Command};
use windows_sys::Win32::System::Registry::{HKEY_LOCAL_MACHINE, RRF_RT_REG_SZ, RRF_SUBKEY_WOW6464KEY, RegGetValueW};
use windows_sys::Win32::System::SystemInformation::{ComputerNamePhysicalNetBIOS, GetComputerNameExW};
use windows_sys::Win32::System::Threading::CREATE_NO_WINDOW;
use super::{ClientOs, NativeHostIdentitySnapshot, fallback_hostname, primary_mac_colon, resolve_host_id};
@@ -52,7 +54,7 @@ fn collect_windows_serial_number() -> Option<String> {
}
fn windows_machine_seed() -> Option<String> {
collect_windows_csproduct_uuid().or_else(collect_windows_machine_guid)
collect_windows_csproduct_uuid().or_else(|| collect_windows_machine_guid().ok())
}
fn collect_windows_csproduct_uuid() -> Option<String> {
@@ -71,25 +73,63 @@ fn collect_windows_csproduct_uuid() -> Option<String> {
.next()
}
fn collect_windows_machine_guid() -> Option<String> {
let output = Command::new("reg")
.creation_flags(CREATE_NO_WINDOW)
.args(["query", r"HKLM\SOFTWARE\Microsoft\Cryptography", "/v", "MachineGuid"])
.output()
.ok()?;
if !output.status.success() {
return None;
// Native Windows uses the OS installation ID and physical NetBIOS name.
// Keep the hardware identity used to seed other OS profiles unchanged.
pub(super) fn native_profile_identity(
identity: super::HostIdentity,
host_id_override: Option<&str>,
) -> io::Result<super::HostIdentity> {
let host_id = match host_id_override.filter(|id| !id.trim().is_empty()) {
Some(id) => id.to_owned(),
None => collect_windows_machine_guid()?,
};
Ok(super::HostIdentity::from_parts(
collect_windows_computer_name()?,
host_id,
identity.serialno().into(),
identity.mac_addr().into(),
))
}
fn collect_windows_computer_name() -> io::Result<String> {
let mut name = [0u16; 256];
let mut size = name.len() as u32;
// SAFETY: the buffer and character count describe writable UTF-16 storage.
if unsafe { GetComputerNameExW(ComputerNamePhysicalNetBIOS, name.as_mut_ptr(), &mut size) } == 0 {
return Err(io::Error::last_os_error());
}
let stdout = String::from_utf8(output.stdout).ok()?;
String::from_utf16(&name[..size as usize]).map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))
}
stdout.lines().find_map(|line| {
let mut parts = line.split_whitespace();
if parts.next()? != "MachineGuid" {
return None;
}
parts.last().map(str::to_string)
})
fn collect_windows_machine_guid() -> io::Result<String> {
let key: Vec<u16> = "SOFTWARE\\Microsoft\\Cryptography\0".encode_utf16().collect();
let value: Vec<u16> = "MachineGuid\0".encode_utf16().collect();
let mut buffer = [0u16; 128];
let mut size = std::mem::size_of_val(&buffer) as u32;
// SAFETY: both names are NUL-terminated; buffer and byte length describe writable storage.
let status = unsafe {
RegGetValueW(
HKEY_LOCAL_MACHINE,
key.as_ptr(),
value.as_ptr(),
RRF_RT_REG_SZ | RRF_SUBKEY_WOW6464KEY,
std::ptr::null_mut(),
buffer.as_mut_ptr().cast(),
&mut size,
)
};
if status != 0 {
return Err(io::Error::from_raw_os_error(status as i32));
}
let end = buffer.iter().position(|value| *value == 0).unwrap_or(buffer.len());
let guid = String::from_utf16(&buffer[..end]).map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
if guid.trim().is_empty() {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Windows MachineGuid is empty",
));
}
Ok(guid)
}
impl RuntimeNativeHostIdentity {
+1 -2
View File
@@ -124,8 +124,7 @@ impl OsProfile {
self.runtime_os == self.client_os
}
/// Default software version string for the target OS, matching the
/// official client values.
/// Software version for the target OS profile.
pub fn software_version(&self) -> &str {
&self.software_version
}
+6 -6
View File
@@ -18,14 +18,14 @@ fn negotiated_os_version_is_used_in_profile_and_user_agent() {
}
#[test]
fn csc_support_defaults_match_official_profiles() {
fn csc_support_defaults_match_platform_profiles() {
assert!(!ClientOs::Linux.default_csc_support());
assert!(ClientOs::Mac.default_csc_support());
assert!(ClientOs::Windows.default_csc_support());
}
#[test]
fn prelogin_param_location_matches_official_profiles() {
fn prelogin_param_location_matches_platform_profiles() {
assert_eq!(
OsProfileBuilder::new(ClientOs::Linux)
.build(test_snapshot())
@@ -91,7 +91,7 @@ fn embedded_gateway_default_browser_is_zero_for_all() {
}
#[test]
fn external_default_browser_values_match_official_profiles() {
fn external_default_browser_values_match_platform_profiles() {
assert_eq!(
OsProfileBuilder::new(ClientOs::Linux)
.build(test_snapshot())
@@ -132,7 +132,7 @@ fn external_default_browser_values_match_official_profiles() {
}
#[test]
fn saml_password_matches_official_profiles() {
fn saml_password_matches_platform_profiles() {
assert_eq!(
OsProfileBuilder::new(ClientOs::Linux)
.build(test_snapshot())
@@ -173,7 +173,7 @@ fn gateway_server_uses_resolved_ipv4_for_all() {
}
#[test]
fn kerberos_support_in_query_matches_official_profiles() {
fn kerberos_support_in_query_matches_platform_profiles() {
assert!(
!OsProfileBuilder::new(ClientOs::Linux)
.build(test_snapshot())
@@ -192,7 +192,7 @@ fn kerberos_support_in_query_matches_official_profiles() {
}
#[test]
fn os_vendor_matches_official_profiles() {
fn os_vendor_matches_platform_profiles() {
assert_eq!(
OsProfileBuilder::new(ClientOs::Linux)
.build(test_snapshot())
+1 -3
View File
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use anyhow::bail;
use log::{debug, info, warn};
use log::{info, warn};
use reqwest::{Client, StatusCode};
use serde::Serialize;
use specta::Type;
@@ -232,7 +232,6 @@ pub async fn retrieve_config(
bail!(PortalError::ConfigError("Empty portal config response".to_string()))
}
debug!("Portal config response received: {} bytes", res_xml.len());
let root = Element::parse(res_xml.as_bytes()).map_err(|e| PortalError::ConfigError(e.to_string()))?;
if csc::is_config_criteria(&root) {
@@ -247,7 +246,6 @@ pub async fn retrieve_config(
_ = cancellation.cancelled() => bail!("Portal configuration cancelled"),
result = retrieve_csc_config(&client, &portal, &root, cred.username(), gp_params) => result?,
};
debug!("Portal CSC config response received: {} bytes", csc_xml.len());
let root = Element::parse(csc_xml.as_bytes()).map_err(|e| PortalError::ConfigError(e.to_string()))?;
return parse_portal_policy(&server, cred, root, cancellation).await;
}
+2 -4
View File
@@ -1,5 +1,5 @@
use anyhow::{anyhow, bail};
use log::{debug, info, warn};
use log::{info, warn};
use reqwest::{Client, StatusCode};
use serde::Serialize;
use specta::Type;
@@ -174,10 +174,8 @@ pub async fn prelogin(portal: &str, gp_params: &GpParams, options: PreloginOptio
Err(anyhow!(PortalError::PreloginError(err.reason)))
})?;
debug!("Prelogin response XML: {}", res_xml);
let prelogin = parse_prelogin_response(&res_xml, is_gateway).map_err(|err| {
warn!("Parse response error, response: {}", res_xml);
warn!("Failed to parse prelogin response: {err}");
PortalError::PreloginError(err.to_string())
})?;
@@ -164,6 +164,7 @@ async fn post(
crate::utils::request::ResponseBodyError::Transport(error) => GatewayRequestError::Transport(error),
crate::utils::request::ResponseBodyError::TooLarge => GatewayRequestError::ResponseTooLarge,
})?;
crate::utils::log_response_xml(&format!("/ssl-vpn/{endpoint}"), status, &body);
parse_response(&body)
}
+20 -14
View File
@@ -13,6 +13,8 @@ mod identity_encoding;
#[cfg(feature = "desktop")]
pub mod lock_file;
pub mod redact;
mod response_log;
pub(crate) use response_log::log_response_xml;
pub mod request;
#[cfg(feature = "tauri")]
pub mod window;
@@ -53,11 +55,10 @@ pub fn remove_url_scheme(s: &str) -> String {
}
#[derive(Error, Debug)]
#[error("GP response error: reason={reason}, status={status}, body={body}")]
#[error("GP response error: reason={reason}, status={status}")]
pub struct GpError {
pub status: StatusCode,
pub reason: String,
body: String,
#[source]
source: Option<reqwest::Error>,
}
@@ -79,16 +80,20 @@ pub async fn parse_gp_response(res: Response) -> anyhow::Result<String, GpError>
return Err(parse_gp_error(res).await);
}
res.text().await.map_err(|err| {
let path = log::log_enabled!(log::Level::Debug).then(|| res.url().path().to_owned());
let body = res.text().await.map_err(|err| {
warn!("Failed to read response: {}", err);
GpError {
status,
reason: "failed to read response".to_string(),
body: "<failed to read response>".to_string(),
source: Some(err),
}
})
})?;
if let Some(path) = path {
log_response_xml(&path, status, body.as_bytes());
}
Ok(body)
}
async fn parse_gp_error(res: Response) -> GpError {
@@ -99,15 +104,16 @@ async fn parse_gp_error(res: Response) -> GpError {
.map_or_else(|| "<none>", |v| v.to_str().unwrap_or("<invalid header>"))
.to_string();
let (body, source) = match res.text().await {
Ok(body) => (if body.is_empty() { "<empty>".to_string() } else { body }, None),
Err(error) => ("<failed to read response>".to_string(), Some(error)),
let path = log::log_enabled!(log::Level::Debug).then(|| res.url().path().to_owned());
let source = match res.text().await {
Ok(body) => {
if let Some(path) = path {
log_response_xml(&path, status, body.as_bytes());
}
None
}
Err(error) => Some(error),
};
GpError {
status,
reason,
body,
source,
}
GpError { status, reason, source }
}
+221
View File
@@ -0,0 +1,221 @@
use reqwest::StatusCode;
use xmltree::{Element, EmitterConfig, XMLNode};
const REDACTED: &str = "[redacted]";
const MAX_XML_BYTES: usize = 1024 * 1024;
pub(crate) fn log_response_xml(path: &str, status: StatusCode, body: &[u8]) {
if !log::log_enabled!(log::Level::Debug) {
return;
}
// Redirect paths can themselves contain authentication tokens.
let endpoint = match path {
"/global-protect/prelogin.esp"
| "/ssl-vpn/prelogin.esp"
| "/global-protect/getconfig.esp"
| "/global-protect/getconfig_csc.esp"
| "/ssl-vpn/login.esp"
| "/ssl-vpn/getconfig.esp"
| "/ssl-vpn/agentmessage.esp"
| "/ssl-vpn/hipreportcheck.esp"
| "/ssl-vpn/hipreport.esp"
| "/ssl-vpn/logout.esp" => path,
_ => "[response endpoint]",
};
// Individual XML lines remain readable in macOS unified logs with limited record sizes.
for line in redact_xml(body).lines() {
log::debug!("{endpoint} response XML (HTTP {}): {line}", status.as_u16());
}
}
fn redact_xml(body: &[u8]) -> String {
if body.len() > MAX_XML_BYTES {
return "[XML omitted: exceeds 1 MiB]".into();
}
let Ok(text) = std::str::from_utf8(body) else {
return "[XML omitted: invalid UTF-8]".into();
};
if text.to_ascii_lowercase().contains("<!doctype") {
return "[XML omitted: DTD]".into();
}
let Ok(nodes) = Element::parse_all(body) else {
return "[XML omitted: malformed or non-XML response]".into();
};
let mut roots = nodes.into_iter().filter_map(|node| match node {
XMLNode::Element(element) => Some(element),
_ => None,
});
let Some(mut root) = roots.next() else {
return "[XML omitted: no document element]".into();
};
if roots.next().is_some() || root.name.eq_ignore_ascii_case("html") {
return "[XML omitted: unsupported response document]".into();
}
redact_element(&mut root);
let mut output = Vec::new();
if root
.write_with_config(
&mut output,
EmitterConfig::new()
.perform_indent(true)
.write_document_declaration(false),
)
.is_err()
{
return "[XML omitted: serialization failed]".into();
}
String::from_utf8(output).unwrap_or_else(|_| "[XML omitted: serialization failed]".into())
}
fn sensitive_name(name: &str) -> bool {
let name: String = name
.chars()
.filter(char::is_ascii_alphanumeric)
.map(|c| c.to_ascii_lowercase())
.collect();
if name.ends_with("label") {
return false;
}
name.contains("cookie")
|| name.contains("token")
|| name.contains("password")
|| name.contains("passphrase")
|| name.contains("secret")
|| name.starts_with("ekey")
|| matches!(
name.as_str(),
"argument"
| "samlrequest"
| "samlresponse"
| "samlrequestid"
| "assertion"
| "encryptedassertion"
| "authnrequest"
| "challenge"
| "privatekey"
| "encryptionkey"
| "enckey"
| "enchskey"
| "tunnelopaque"
| "nonce"
| "user"
| "username"
| "ccusername"
| "credential"
)
}
fn redact_element(element: &mut Element) {
let named_secret = matches!(element.name.as_str(), "field" | "param" | "property" | "setting")
&& ["name", "key"]
.iter()
.any(|name| element.attributes.get(*name).is_some_and(|value| sensitive_name(value)));
let secret = sensitive_name(&element.name) || named_secret;
for (name, value) in &mut element.attributes {
if sensitive_name(name) || (secret && !(named_secret && matches!(name.as_str(), "name" | "key"))) {
*value = REDACTED.into();
} else {
*value = redact_url(value);
}
}
if secret {
element.children = vec![XMLNode::Text(REDACTED.into())];
return;
}
element
.children
.retain(|node| matches!(node, XMLNode::Element(_) | XMLNode::Text(_) | XMLNode::CData(_)));
for node in &mut element.children {
match node {
XMLNode::Element(child) => redact_element(child),
XMLNode::Text(value) | XMLNode::CData(value) => *value = redact_url(value),
_ => {}
}
}
}
fn redact_url(value: &str) -> String {
let Ok(mut url) = url::Url::parse(value) else {
return value.into();
};
if !matches!(url.scheme(), "http" | "https") {
return value.into();
}
if !url.username().is_empty() {
let _ = url.set_username(REDACTED);
}
if url.password().is_some() {
let _ = url.set_password(Some(REDACTED));
}
if url.query().is_some() {
let pairs: Vec<_> = url
.query_pairs()
.map(|(name, _)| (name.into_owned(), REDACTED))
.collect();
url.query_pairs_mut().clear().extend_pairs(pairs);
}
if url.fragment().is_some() {
url.set_fragment(Some(REDACTED));
}
url.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn preserves_policy_checks_and_redacts_authentication_material() {
let input = include_str!("../../tests/fixtures/portal-response.xml");
let xml = redact_xml(input.as_bytes());
let root = Element::parse(xml.as_bytes()).unwrap();
assert!(xml.contains("custom-checks") && xml.contains("HipID") && xml.contains("NV Domain"));
assert!(xml.contains("Corp.Example.test"));
assert!(xml.contains("com.1password.1password") && xml.contains("Version"));
for secret in [
"fixture-user-cookie",
"fixture-prelogon-cookie",
"fixture-csc-cookie",
"fixture-username",
"fixture-saml",
"fixture-attribute-token",
"fixture-property-password",
"fixture-cookie-name",
"fixture-query-token",
"fixture-url-password",
"fixture-comment",
"fixture-pi",
] {
assert!(!xml.contains(secret), "secret retained: {secret}");
}
assert_eq!(root.name, "policy");
}
#[test]
fn redacts_positional_login_arguments_and_session_keys() {
let xml = redact_xml(br#"<response><jnlp><application-desc><argument>fixture-auth-cookie</argument><argument>fixture-user</argument></application-desc></jnlp><ipsec><ekey-c2s><bits>256</bits><val>fixture-key-c2s</val></ekey-c2s><ekey-s2c>fixture-key-s2c</ekey-s2c></ipsec><enc-hs-key>fixture-hs-key</enc-hs-key><PrivateKey>fixture-private-key</PrivateKey><SAMLResponse>fixture-saml-response</SAMLResponse><tunnel-opaque><val>fixture-opaque</val></tunnel-opaque><mtu>1400</mtu></response>"#);
assert!(!xml.contains("fixture-"));
assert!(xml.contains("<mtu>1400</mtu>"));
Element::parse(xml.as_bytes()).unwrap();
}
#[test]
fn omits_unparseable_html_dtd_and_oversized_bodies_without_echoing_them() {
for body in [
b"not XML fixture-secret".as_slice(),
b"<response><cookie>fixture-secret",
b"<html><body>fixture-secret</body></html>",
b"<!DOCTYPE response [<!ENTITY secret 'fixture-secret'>]><response>&secret;</response>",
b"<response/>fixture-secret",
b"\xfffixture-secret",
] {
let xml = redact_xml(body);
assert!(xml.starts_with("[XML omitted:"));
assert!(!xml.contains("fixture-secret"));
}
assert_eq!(
redact_xml(&vec![b'x'; MAX_XML_BYTES + 1]),
"[XML omitted: exceeds 1 MiB]"
);
}
}
+23
View File
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<?fixture fixture-pi?>
<policy>
<!--fixture-comment-->
<portal-userauthcookie>fixture-user-cookie</portal-userauthcookie>
<portal-prelogonuserauthcookie><![CDATA[fixture-prelogon-cookie]]></portal-prelogonuserauthcookie>
<portal-csc-auth-cookie>fixture-csc-cookie</portal-csc-auth-cookie>
<username>fixture-username</username>
<saml-request>fixture-saml</saml-request>
<property name="password" value="fixture-property-password"/>
<cookie name="fixture-cookie-name"/>
<settings auth_token="fixture-attribute-token"/>
<download href="https://user:[email protected]/client?auth=fixture-query-token"/>
<hip-collection>
<hip-report-interval>3600</hip-report-interval>
<collect-hip-data>yes</collect-hip-data>
<custom-checks>
<mac-os><plist><entry name="com.paloaltonetworks.GlobalProtect.settings"><key><member>HipID</member></key></entry><entry name="com.1password.1password"><key><member>Version</member></key></entry></plist></mac-os>
<windows><registry-key><entry name="HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"><registry-value><member>Domain</member><member>NV Domain</member></registry-value></entry></registry-key></windows>
</custom-checks>
</hip-collection>
<domain>Corp.Example.test</domain>
</policy>
+123
View File
@@ -0,0 +1,123 @@
use std::sync::Mutex;
use gpapi::utils::parse_gp_response;
use log::{LevelFilter, Log, Metadata, Record};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::TcpListener,
};
struct Capture(Mutex<Vec<String>>);
static LOGS: Capture = Capture(Mutex::new(Vec::new()));
impl Log for Capture {
fn enabled(&self, _: &Metadata<'_>) -> bool {
true
}
fn log(&self, record: &Record<'_>) {
if record.target() == "gpapi::utils::response_log" {
self.0.lock().unwrap().push(record.args().to_string());
}
}
fn flush(&self) {}
}
async fn response(path: &str, status: &str, body: &str) -> reqwest::Response {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let data = format!(
"HTTP/1.1 {status}\r\nContent-Type: application/xml\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
body.len()
);
tokio::spawn(async move {
let (mut socket, _) = listener.accept().await.unwrap();
let mut request = [0; 4096];
socket.read(&mut request).await.unwrap();
socket.write_all(data.as_bytes()).await.unwrap();
});
reqwest::Client::builder()
.no_proxy()
.build()
.unwrap()
.get(format!("http://{address}{path}"))
.send()
.await
.unwrap()
}
#[tokio::test]
async fn logs_redacted_xml_at_debug_without_changing_responses_or_exposing_error_bodies() {
rustls::crypto::ring::default_provider().install_default().unwrap();
log::set_logger(&LOGS).unwrap();
log::set_max_level(LevelFilter::Debug);
let portal = include_str!("fixtures/portal-response.xml");
for path in [
"/global-protect/getconfig.esp",
"/global-protect/getconfig_csc.esp",
"/global-protect/prelogin.esp",
"/ssl-vpn/login.esp",
] {
let result = parse_gp_response(response(path, "200 OK", portal).await).await.unwrap();
assert_eq!(result, portal);
}
let logs = LOGS.0.lock().unwrap().join("\n");
assert!(logs.contains("getconfig.esp response XML"));
assert!(logs.contains("getconfig_csc.esp response XML"));
assert!(logs.contains("prelogin.esp response XML"));
assert!(logs.contains("login.esp response XML"));
assert!(logs.contains("custom-checks") && logs.contains("HipID"));
for secret in [
"fixture-user-cookie",
"fixture-prelogon-cookie",
"fixture-csc-cookie",
"fixture-username",
"fixture-saml",
"fixture-attribute-token",
"fixture-property-password",
"fixture-cookie-name",
"fixture-query-token",
"fixture-url-password",
"fixture-comment",
"fixture-pi",
] {
assert!(!logs.contains(secret), "logged secret: {secret}");
}
let error = parse_gp_response(response("/global-protect/getconfig.esp", "403 Forbidden", portal).await)
.await
.unwrap_err();
assert_eq!(error.status, reqwest::StatusCode::FORBIDDEN);
assert!(!format!("{error:?} {error}").contains("fixture-"));
assert!(LOGS.0.lock().unwrap().iter().any(|line| line.contains("HTTP 403")));
let error = parse_gp_response(
response(
"/global-protect/getconfig.esp",
"500 Internal Server Error",
"<html><body>fixture-error-secret</body></html>",
)
.await,
)
.await
.unwrap_err();
assert!(!format!("{error:?} {error}").contains("fixture-error-secret"));
assert!(!LOGS.0.lock().unwrap().join("\n").contains("fixture-error-secret"));
LOGS.0.lock().unwrap().clear();
let body = "<jnlp><application-desc><argument>fixture-login-cookie</argument></application-desc></jnlp>";
parse_gp_response(response("/redirect/fixture-path-token?token=fixture-query-token", "200 OK", body).await)
.await
.unwrap();
let logs = LOGS.0.lock().unwrap().join("\n");
assert!(logs.contains("[response endpoint]"));
assert!(!logs.contains("fixture-"));
LOGS.0.lock().unwrap().clear();
log::set_max_level(LevelFilter::Info);
assert_eq!(
parse_gp_response(response("/global-protect/getconfig.esp", "200 OK", portal).await)
.await
.unwrap(),
portal
);
assert!(LOGS.0.lock().unwrap().is_empty());
}