use deps_core::redact::sanitize_invisible;
use std::path::{Path, PathBuf};
#[must_use]
pub(crate) fn sanitize_path_for_display(path: &Path) -> PathBuf {
PathBuf::from(sanitize_invisible(&path.to_string_lossy()).into_owned())
}
#[must_use]
pub(crate) fn sanitize_message_for_display(message: &str) -> String {
sanitize_invisible(message).into_owned()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sanitize_path_for_display_strips_ansi_and_bidi_but_keeps_legitimate_segments() {
let path = Path::new("src/\u{202E}\x1Bsneaky/Cargo.toml");
let sanitized = sanitize_path_for_display(path)
.to_string_lossy()
.into_owned();
assert!(!sanitized.contains('\u{202E}'));
assert!(!sanitized.contains('\x1B'));
assert!(sanitized.contains("src"));
assert!(sanitized.contains("Cargo.toml"));
}
#[test]
fn sanitize_message_for_display_strips_a_payload_embedded_by_a_third_party_error() {
let message = format!(
"IO error for operation on {}locked: Permission denied (os error 13)",
"ev\u{202E}il\x1B[31m/"
);
let sanitized = sanitize_message_for_display(&message);
assert!(!sanitized.contains('\u{202E}'));
assert!(!sanitized.contains('\x1B'));
assert!(sanitized.contains("Permission denied"));
}
}