use super::util::non_empty;
pub(super) fn sanitize_token_helper(raw: &str) -> Option<String> {
let trimmed = raw.trim();
if trimmed.is_empty() {
return None;
}
let is_unix_absolute = trimmed.starts_with('/');
let is_windows_absolute = trimmed.starts_with("\\\\")
|| trimmed.as_bytes().get(1).is_some_and(|&b| b == b':')
&& trimmed
.as_bytes()
.first()
.is_some_and(|&b| b.is_ascii_alphabetic())
&& matches!(trimmed.as_bytes().get(2), Some(b'/' | b'\\'));
if !(is_unix_absolute || is_windows_absolute) {
return None;
}
if trimmed.chars().any(|c| {
c.is_ascii_whitespace()
|| matches!(
c,
'"' | '\'' | '`' | '$' | '&' | '|' | ';' | '<' | '>' | '(' | ')' | '*' | '?' | '\0'
)
}) {
return None;
}
Some(trimmed.to_string())
}
pub(crate) fn run_token_helper(command: &str) -> Option<String> {
let output = match std::process::Command::new(command).output() {
Ok(o) => o,
Err(e) => {
tracing::warn!(
code = aube_codes::warnings::WARN_AUBE_TOKEN_HELPER_SPAWN_FAILED,
"tokenHelper {command:?} could not be spawned: {e}"
);
return None;
}
};
if !output.status.success() {
tracing::warn!(
code = aube_codes::warnings::WARN_AUBE_TOKEN_HELPER_NON_ZERO_EXIT,
"tokenHelper {command:?} exited with {}",
output.status
);
return None;
}
let token = String::from_utf8(output.stdout).ok()?;
non_empty(token.lines().next().unwrap_or_default().to_string())
}