Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions stdlib/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,21 @@ impl StdlibRegistry {
));
}
let cmd = args[0].try_into_string()?;
if cmd.contains("..") || cmd.contains('\0') {
let shell_interpreters = [
"sh", "bash", "zsh", "csh", "ksh", "dash", "ash", "tcsh",
"powershell", "pwsh", "cmd", "cmd.exe", "bat", "ps1"
];
let cmd_lower = cmd.to_lowercase();
let is_shell = shell_interpreters.iter().any(|&s| {
cmd_lower == s
|| cmd_lower.ends_with(&format!("/{}", s))
|| cmd_lower.ends_with(&format!("\\{}", s))
});

if cmd.contains("..") || cmd.contains('\0') || is_shell {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Path traversal or null bytes in command are denied".to_string(),
"Security policy violation: Path traversal, null bytes, or shell interpreters in command are denied".to_string(),
),
None,
None,
Expand Down Expand Up @@ -124,10 +135,21 @@ impl StdlibRegistry {
}
};

if cmd.contains("..") || cmd.contains('\0') {
let shell_interpreters = [
"sh", "bash", "zsh", "csh", "ksh", "dash", "ash", "tcsh",
"powershell", "pwsh", "cmd", "cmd.exe", "bat", "ps1"
];
let cmd_lower = cmd.to_lowercase();
let is_shell = shell_interpreters.iter().any(|&s| {
cmd_lower == s
|| cmd_lower.ends_with(&format!("/{}", s))
|| cmd_lower.ends_with(&format!("\\{}", s))
});

if cmd.contains("..") || cmd.contains('\0') || is_shell {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Path traversal or null bytes in command are denied".to_string(),
"Security policy violation: Path traversal, null bytes, or shell interpreters in command are denied".to_string(),
),
None,
None,
Expand Down
24 changes: 19 additions & 5 deletions stdlib/tests/stdlib_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,14 +564,28 @@ fn test_system_diagnostics_and_process_execution() {
panic!("disk did not return a Map");
}

// 1.5 Verify command injection protection
let run = process.exports.get("run").unwrap();
let res = run.call(
&mut ctx,
vec![
RuntimeValue::Str("sh".to_string()),
RuntimeValue::List {
items: std::rc::Rc::new(std::cell::RefCell::new(vec![
RuntimeValue::Str("-c".to_string()),
RuntimeValue::Str("echo hacked".to_string()),
])),
is_const: false,
},
],
);
assert!(res.is_err());

// 2. Process run under granted Capability::Process
let run = process.exports.get("run").unwrap();
let cmd = if cfg!(windows) { "cmd" } else { "echo" };
let cmd = if cfg!(windows) { "whoami" } else { "echo" };
let args = if cfg!(windows) {
vec![
RuntimeValue::Str("/c".to_string()),
RuntimeValue::Str("echo hello".to_string()),
]
vec![]
} else {
vec![RuntimeValue::Str("hello".to_string())]
};
Expand Down