diff --git a/.jules/sentinel.md b/.jules/sentinel.md index 986d7ff..dcab261 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -7,3 +7,8 @@ **Vulnerability:** The `maybe_write_text` utility function was using `std::fs::write`, which resulted in sensitive data (like PSBT files and offers) being saved with insecure default file permissions, making them readable by other users on a shared system. **Learning:** Even generic utility functions used for saving user-requested command outputs must use secure file permissions (`0o600`) if the data they handle (like PSBTs and offers) is sensitive. **Prevention:** Always use `crate::paths::write_secure_file` instead of `std::fs::write` for all file writing operations that might contain sensitive material in this codebase. + +## 2024-04-15 - Path Traversal in Snapshot Commands +**Vulnerability:** The snapshot `save` and `restore` commands allowed path traversal because the user-provided snapshot `name` was joined directly to the base snapshot directory path without validation. This allowed writing or reading files outside of the intended directory using `../` sequences in the name. +**Learning:** File paths constructed using user input must be strictly validated to prevent path traversal attacks, especially in CLI tools where user input is directly used for file operations. +**Prevention:** Implement and use a strict allowlist validation function (e.g., `validate_file_name`) that only permits alphanumeric characters, dashes, and underscores for any user-provided string used as a filename. diff --git a/src/commands/snapshot.rs b/src/commands/snapshot.rs index 7be1d9e..d4b1143 100644 --- a/src/commands/snapshot.rs +++ b/src/commands/snapshot.rs @@ -12,6 +12,7 @@ pub async fn run(cli: &Cli, args: &SnapshotArgs) -> Result { + crate::utils::validate_file_name(name)?; let source = read_profile(&profile_path)?; let destination = snap_dir.join(format!("{name}.json")); if destination.exists() && !(*overwrite || cli.yes) { @@ -27,6 +28,7 @@ pub async fn run(cli: &Cli, args: &SnapshotArgs) -> Result { + crate::utils::validate_file_name(name)?; if !confirm(&format!("Are you sure you want to restore snapshot '{name}'? This will overwrite your current profile."), cli) { return Err(AppError::Internal("aborted by user".to_string())); } diff --git a/src/utils.rs b/src/utils.rs index 8e3996f..5ca73eb 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -219,3 +219,18 @@ pub fn parse_indices(s: Option<&str>) -> Result, AppError> { } Ok(indices) } + +pub fn validate_file_name(name: &str) -> Result<(), AppError> { + if name.is_empty() { + return Err(AppError::Invalid("file name cannot be empty".to_string())); + } + for c in name.chars() { + if !c.is_ascii_alphanumeric() && c != '_' && c != '-' { + return Err(AppError::Invalid(format!( + "invalid character in file name: '{}'", + c + ))); + } + } + Ok(()) +}