|
| 1 | +use crate::error::{Result, SofosError}; |
| 2 | +use std::path::PathBuf; |
| 3 | +use std::process::Command; |
| 4 | + |
| 5 | +pub struct BashExecutor { |
| 6 | + workspace: PathBuf, |
| 7 | +} |
| 8 | + |
| 9 | +impl BashExecutor { |
| 10 | + pub fn new(workspace: PathBuf) -> Result<Self> { |
| 11 | + Ok(Self { workspace }) |
| 12 | + } |
| 13 | + |
| 14 | + pub fn execute(&self, command: &str) -> Result<String> { |
| 15 | + if !self.is_safe_command(command) { |
| 16 | + return Err(SofosError::ToolExecution( |
| 17 | + "Command is not allowed. Only read-only commands are permitted.".to_string(), |
| 18 | + )); |
| 19 | + } |
| 20 | + |
| 21 | + let output = Command::new("sh") |
| 22 | + .arg("-c") |
| 23 | + .arg(command) |
| 24 | + .current_dir(&self.workspace) |
| 25 | + .output() |
| 26 | + .map_err(|e| SofosError::ToolExecution(format!("Failed to execute command: {}", e)))?; |
| 27 | + |
| 28 | + let stdout = String::from_utf8_lossy(&output.stdout); |
| 29 | + let stderr = String::from_utf8_lossy(&output.stderr); |
| 30 | + |
| 31 | + if !output.status.success() { |
| 32 | + return Ok(format!( |
| 33 | + "Command failed with exit code: {}\nSTDOUT:\n{}\nSTDERR:\n{}", |
| 34 | + output.status.code().unwrap_or(-1), |
| 35 | + stdout, |
| 36 | + stderr |
| 37 | + )); |
| 38 | + } |
| 39 | + |
| 40 | + let mut result = String::new(); |
| 41 | + if !stdout.is_empty() { |
| 42 | + result.push_str("STDOUT:\n"); |
| 43 | + result.push_str(&stdout); |
| 44 | + } |
| 45 | + if !stderr.is_empty() { |
| 46 | + if !result.is_empty() { |
| 47 | + result.push_str("\n"); |
| 48 | + } |
| 49 | + result.push_str("STDERR:\n"); |
| 50 | + result.push_str(&stderr); |
| 51 | + } |
| 52 | + |
| 53 | + if result.is_empty() { |
| 54 | + result = "Command executed successfully (no output)".to_string(); |
| 55 | + } |
| 56 | + |
| 57 | + Ok(result) |
| 58 | + } |
| 59 | + |
| 60 | + fn is_safe_command(&self, command: &str) -> bool { |
| 61 | + let command_lower = command.to_lowercase(); |
| 62 | + |
| 63 | + if command_lower.starts_with("sudo") || command_lower.contains(" sudo ") { |
| 64 | + return false; |
| 65 | + } |
| 66 | + |
| 67 | + if command.contains("..") { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + let directory_commands = ["cd ", "cd\t", "cd;", "cd&", "cd|", |
| 72 | + "pushd ", "pushd\t", "pushd;", "pushd&", "pushd|", |
| 73 | + "popd ", "popd\t", "popd;", "popd&", "popd|"]; |
| 74 | + for cmd in &directory_commands { |
| 75 | + if command_lower.starts_with(cmd.trim_end()) |
| 76 | + || command_lower.contains(&format!(" {}", cmd.trim_end())) |
| 77 | + || command_lower.contains(&format!(";{}", cmd.trim_end())) |
| 78 | + || command_lower.contains(&format!("&&{}", cmd.trim_end())) |
| 79 | + || command_lower.contains(&format!("||{}", cmd.trim_end())) |
| 80 | + || command_lower.contains(&format!("|{}", cmd.trim_end())) |
| 81 | + { |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if command.starts_with('/') { |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + // Catches: cat /etc/passwd, ls /tmp, etc. |
| 91 | + if command.contains(" /") { |
| 92 | + return false; |
| 93 | + } |
| 94 | + |
| 95 | + // Check absolute paths after pipes, semicolons, and logical operators |
| 96 | + if command.contains("|/") || command.contains(";/") |
| 97 | + || command.contains("&&/") || command.contains("||/") { |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + let forbidden_commands = [ |
| 102 | + "rm", "mv", "cp", "chmod", "chown", "chgrp", |
| 103 | + "mkdir", "rmdir", "touch", "ln", "dd", |
| 104 | + "mkfs", "mount", "umount", "kill", "killall", |
| 105 | + "pkill", "shutdown", "reboot", "halt", "poweroff", |
| 106 | + "useradd", "userdel", "groupadd", "groupdel", |
| 107 | + "passwd", "systemctl", "service", "fdisk", |
| 108 | + "parted", "mkswap", "swapon", "swapoff", |
| 109 | + ]; |
| 110 | + |
| 111 | + for forbidden in &forbidden_commands { |
| 112 | + if command_lower.starts_with(forbidden) |
| 113 | + || command_lower.starts_with(&format!("{} ", forbidden)) |
| 114 | + { |
| 115 | + return false; |
| 116 | + } |
| 117 | + // Check chained commands |
| 118 | + if command_lower.contains(&format!("| {}", forbidden)) |
| 119 | + || command_lower.contains(&format!("; {}", forbidden)) |
| 120 | + || command_lower.contains(&format!("&& {}", forbidden)) |
| 121 | + || command_lower.contains(&format!("|| {}", forbidden)) |
| 122 | + { |
| 123 | + return false; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + if command.contains('>') || command.contains(">>") { |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + // Here-doc can be used for malicious input |
| 132 | + if command.contains("<<") { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + true |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +#[cfg(test)] |
| 141 | +mod tests { |
| 142 | + use super::*; |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn test_safe_commands() { |
| 146 | + let executor = BashExecutor::new(PathBuf::from(".")).unwrap(); |
| 147 | + |
| 148 | + assert!(executor.is_safe_command("ls -la")); |
| 149 | + assert!(executor.is_safe_command("cat file.txt")); |
| 150 | + assert!(executor.is_safe_command("grep pattern file.txt")); |
| 151 | + assert!(executor.is_safe_command("cargo test")); |
| 152 | + assert!(executor.is_safe_command("cargo build")); |
| 153 | + assert!(executor.is_safe_command("echo hello")); |
| 154 | + assert!(executor.is_safe_command("pwd")); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn test_unsafe_commands() { |
| 159 | + let executor = BashExecutor::new(PathBuf::from(".")).unwrap(); |
| 160 | + |
| 161 | + assert!(!executor.is_safe_command("sudo ls")); |
| 162 | + assert!(!executor.is_safe_command("rm file.txt")); |
| 163 | + assert!(!executor.is_safe_command("mv file1 file2")); |
| 164 | + assert!(!executor.is_safe_command("chmod 777 file")); |
| 165 | + assert!(!executor.is_safe_command("echo hello > file.txt")); |
| 166 | + assert!(!executor.is_safe_command("cat file.txt >> output.txt")); |
| 167 | + assert!(!executor.is_safe_command("ls | rm file.txt")); |
| 168 | + assert!(!executor.is_safe_command("ls && rm file.txt")); |
| 169 | + } |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn test_path_traversal_blocked() { |
| 173 | + let executor = BashExecutor::new(PathBuf::from(".")).unwrap(); |
| 174 | + |
| 175 | + assert!(!executor.is_safe_command("cat ../file.txt")); |
| 176 | + assert!(!executor.is_safe_command("ls ../../etc")); |
| 177 | + assert!(!executor.is_safe_command("cat ../../../etc/passwd")); |
| 178 | + assert!(!executor.is_safe_command("cat file.txt && ls ..")); |
| 179 | + assert!(!executor.is_safe_command("ls | cat ../secret")); |
| 180 | + } |
| 181 | + |
| 182 | + #[test] |
| 183 | + fn test_absolute_paths_blocked() { |
| 184 | + let executor = BashExecutor::new(PathBuf::from(".")).unwrap(); |
| 185 | + |
| 186 | + assert!(!executor.is_safe_command("/bin/ls")); |
| 187 | + assert!(!executor.is_safe_command("/etc/passwd")); |
| 188 | + assert!(!executor.is_safe_command("cat /etc/passwd")); |
| 189 | + assert!(!executor.is_safe_command("ls /tmp")); |
| 190 | + assert!(!executor.is_safe_command("cat /home/user/secret")); |
| 191 | + assert!(!executor.is_safe_command("ls && cat /etc/passwd")); |
| 192 | + assert!(!executor.is_safe_command("echo test || cat /etc/passwd")); |
| 193 | + assert!(!executor.is_safe_command("ls | grep /etc/passwd")); |
| 194 | + assert!(!executor.is_safe_command("true;/bin/bash")); |
| 195 | + } |
| 196 | + |
| 197 | + #[test] |
| 198 | + fn test_directory_change_blocked() { |
| 199 | + let executor = BashExecutor::new(PathBuf::from(".")).unwrap(); |
| 200 | + |
| 201 | + assert!(!executor.is_safe_command("cd /tmp")); |
| 202 | + assert!(!executor.is_safe_command("cd ..")); |
| 203 | + assert!(!executor.is_safe_command("cd / && ls")); |
| 204 | + assert!(!executor.is_safe_command("ls && cd /tmp")); |
| 205 | + assert!(!executor.is_safe_command("ls | cd /tmp")); |
| 206 | + assert!(!executor.is_safe_command("pushd /tmp")); |
| 207 | + assert!(!executor.is_safe_command("popd")); |
| 208 | + assert!(!executor.is_safe_command("ls && pushd ..")); |
| 209 | + } |
| 210 | +} |
0 commit comments