fix: replace panics reachable from user input with proper error handling

This commit is contained in:
Christian Visintin
2026-02-27 22:19:17 +01:00
parent a252caa66b
commit be237c39a6
7 changed files with 85 additions and 37 deletions

View File

@@ -190,8 +190,8 @@ impl BookmarksClient {
) {
let name: String = name.as_ref().to_string();
if name.is_empty() {
error!("Fatal error; bookmark name is empty");
panic!("Bookmark name can't be empty");
error!("Bookmark name is empty; ignoring add_bookmark request");
return;
}
// Make bookmark
info!("Added bookmark {}", name);
@@ -557,15 +557,13 @@ mod tests {
}
#[test]
#[should_panic]
fn test_system_bookmarks_bad_bookmark_name() {
let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
// Initialize a new bookmarks client
let mut client: BookmarksClient =
BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
// Add bookmark
// Add bookmark with empty name should be silently ignored
client.add_bookmark(
"",
make_generic_ftparams(
@@ -577,6 +575,8 @@ mod tests {
),
true,
);
// No bookmark should have been added
assert_eq!(client.iter_bookmarks().count(), 0);
}
#[test]
@@ -738,14 +738,13 @@ mod tests {
}
#[test]
#[should_panic]
fn test_system_bookmarks_add_bookmark_empty() {
let tmp_dir: tempfile::TempDir = TempDir::new().ok().unwrap();
let (cfg_path, key_path): (PathBuf, PathBuf) = get_paths(tmp_dir.path());
// Initialize a new bookmarks client
let mut client: BookmarksClient =
BookmarksClient::new(cfg_path.as_path(), key_path.as_path(), 16, true).unwrap();
// Add bookmark
// Add bookmark with empty name should be silently ignored
client.add_bookmark(
"",
make_generic_ftparams(
@@ -757,6 +756,8 @@ mod tests {
),
true,
);
// No bookmark should have been added
assert_eq!(client.iter_bookmarks().count(), 0);
}
#[test]

View File

@@ -309,7 +309,7 @@ impl ConfigClient {
None => None,
Some(key_path) => {
// Get host and username
let (host, username): (String, String) = Self::get_ssh_tokens(mkey);
let (host, username) = Self::get_ssh_tokens(mkey)?;
// Return key
Some((host, username, PathBuf::from(key_path)))
}
@@ -389,12 +389,18 @@ impl ConfigClient {
}
/// Get ssh tokens starting from ssh host key
/// Panics if key has invalid syntax
/// Returns: (host, username)
fn get_ssh_tokens(host_key: &str) -> (String, String) {
/// Returns: (host, username) or None if key has invalid syntax
fn get_ssh_tokens(host_key: &str) -> Option<(String, String)> {
let tokens: Vec<&str> = host_key.split('@').collect();
assert!(tokens.len() >= 2);
(String::from(tokens[1]), String::from(tokens[0]))
if tokens.len() >= 2 {
Some((String::from(tokens[1]), String::from(tokens[0])))
} else {
error!(
"Invalid SSH host key format: '{}' (expected 'username@host')",
host_key
);
None
}
}
/// Make serializer error from `std::io::Error`
@@ -711,10 +717,16 @@ mod tests {
);
assert_eq!(
ConfigClient::get_ssh_tokens("pi@192.168.1.31"),
(String::from("192.168.1.31"), String::from("pi"))
Some((String::from("192.168.1.31"), String::from("pi")))
);
}
#[test]
fn test_system_config_get_ssh_tokens_invalid() {
assert!(ConfigClient::get_ssh_tokens("invalid").is_none());
assert!(ConfigClient::get_ssh_tokens("").is_none());
}
#[test]
fn test_system_config_make_io_err() {
let err: SerializerError =

View File

@@ -71,7 +71,10 @@ impl KeyStorage for FileStorage {
return Err(KeyStorageError::ProviderError);
}
// Set file to readonly
let mut permissions: Permissions = file.metadata().unwrap().permissions();
let mut permissions: Permissions = file
.metadata()
.map_err(|_| KeyStorageError::ProviderError)?
.permissions();
permissions.set_readonly(true);
let _ = file.set_permissions(permissions);
Ok(())