79 lines
2.6 KiB
Rust
79 lines
2.6 KiB
Rust
use std::io;
|
|
|
|
use rcom::fs::FsError;
|
|
|
|
#[test]
|
|
fn io_errors_are_mapped_without_leaking_paths() {
|
|
let secret_path = "/tmp/private-fixture/secret.txt";
|
|
let error = io::Error::new(io::ErrorKind::PermissionDenied, secret_path);
|
|
let mapped = FsError::from(error);
|
|
|
|
assert_eq!(mapped, FsError::PermissionDenied);
|
|
assert!(!mapped.to_string().contains(secret_path));
|
|
assert!(!mapped.user_message().contains(secret_path));
|
|
assert_eq!(
|
|
FsError::from(io::Error::new(io::ErrorKind::NotFound, "x")),
|
|
FsError::NotFound
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn backend_credentials_are_redacted_from_user_messages() {
|
|
let messages = [
|
|
"token=super-secret",
|
|
"password: correct-horse",
|
|
"Bearer abc.def.ghi",
|
|
"access_token=abc123",
|
|
"api_key=private",
|
|
"/tmp/token=super-secret/file",
|
|
"https://example.invalid/?access_token=abc123&mode=copy",
|
|
];
|
|
for message in messages {
|
|
for error in [
|
|
FsError::Other(message.into()),
|
|
FsError::Unsupported(message.into()),
|
|
] {
|
|
let displayed = error.user_message();
|
|
assert!(displayed.contains("<redacted>"), "{displayed}");
|
|
for secret in [
|
|
"super-secret",
|
|
"correct-horse",
|
|
"abc.def.ghi",
|
|
"abc123",
|
|
"private",
|
|
] {
|
|
assert!(!displayed.contains(secret), "{displayed}");
|
|
}
|
|
}
|
|
}
|
|
let path = FsError::Other("/tmp/token=super-secret/file".into()).user_message();
|
|
assert!(path.contains("/tmp/token=<redacted>/file"));
|
|
let url = FsError::Other("https://example.invalid/?access_token=abc123&mode=copy".into())
|
|
.user_message();
|
|
assert!(url.contains("access_token=<redacted>&mode=copy"));
|
|
let pem = "-----BEGIN PRIVATE KEY-----\nsuper-secret\n-----END PRIVATE KEY-----";
|
|
let displayed = FsError::Other(pem.into()).user_message();
|
|
assert!(displayed.contains("<redacted private key>"));
|
|
assert!(!displayed.contains("super-secret"));
|
|
assert!(!displayed.contains("BEGIN PRIVATE KEY"));
|
|
}
|
|
|
|
#[test]
|
|
fn every_displayable_error_has_a_safe_actionable_message() {
|
|
let errors = [
|
|
FsError::NotFound,
|
|
FsError::PermissionDenied,
|
|
FsError::NotADirectory,
|
|
FsError::Timeout,
|
|
FsError::Cancelled,
|
|
FsError::Disconnected,
|
|
FsError::AuthFailed,
|
|
FsError::HostKeyRejected,
|
|
FsError::Unsupported("SFTP subsystem".into()),
|
|
FsError::Other("I/O error".into()),
|
|
];
|
|
|
|
for error in errors {
|
|
assert!(!error.user_message().is_empty());
|
|
}
|
|
}
|