🔒 Fix command injection vulnerability in command_exists#74
Conversation
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
There was a problem hiding this comment.
Pull request overview
This PR hardens command_exists against shell command injection by avoiding string interpolation into a sh -c command, and adds unit tests to exercise the new behavior.
Changes:
- Updated
command_existsto pass the command name as a positional parameter ($1) instead of formatting it into the shell string. - Added tests for valid/invalid commands and basic injection attempts.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| fn command_exists(command: &str) -> bool { | ||
| std::process::Command::new("sh") | ||
| .arg("-c") | ||
| .arg(format!("command -v {command} >/dev/null 2>&1")) | ||
| .arg("command -v \"$1\" >/dev/null 2>&1") | ||
| .arg("--") | ||
| .arg(command) |
There was a problem hiding this comment.
command_exists now avoids string interpolation (good), but it still hard-depends on invoking sh. On systems without sh (notably native Windows), this will always return false, causing detect_powershell_shell() to report PowerShell missing even when pwsh/powershell is present. Consider making command_exists cross-platform (e.g., PATH scan in Rust and/or a Windows-specific implementation using where) or at least cfg(unix)/cfg(windows) split so PowerShell detection works on Windows without MSYS/Git-Bash.
| #[test] | ||
| fn test_command_exists_valid() { | ||
| assert!(super::command_exists("ls") || super::command_exists("dir")); | ||
| } |
There was a problem hiding this comment.
These new tests assume a Unix-like environment (command_exists shells out to sh) and that ls/dir are available. As written they will fail on Windows (and potentially minimal environments). Consider gating with #[cfg(unix)] or choosing a command that’s guaranteed to exist given the implementation (e.g., sh itself).
| #[test] | ||
| fn test_command_exists_injection() { | ||
| assert!(!super::command_exists("ls; echo injected")); | ||
| assert!(!super::command_exists("ls && echo injected")); | ||
| } |
There was a problem hiding this comment.
test_command_exists_injection only asserts the result is false. That can be a weak regression test because it would still pass even if the vulnerable code executed the injected portion but ultimately returned non-zero, and it could theoretically fail if an executable with that literal name exists in PATH. Consider asserting a side effect does not occur (e.g., try "sh; touch <tmpfile>" / "sh && touch <tmpfile>" and verify the tmpfile was not created) to prove no extra commands are executed.
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| fn command_exists(command: &str) -> bool { | ||
| std::process::Command::new("sh") | ||
| .arg("-c") | ||
| .arg(format!("command -v {command} >/dev/null 2>&1")) | ||
| .arg("command -v \"$1\" >/dev/null 2>&1") | ||
| .arg("--") | ||
| .arg(command) | ||
| .status() |
There was a problem hiding this comment.
command_exists still shells out to sh, so it will always return false on systems/environments where sh is not on PATH (even if the target command exists). It also adds process-spawn overhead for a simple PATH lookup. Consider implementing this without invoking a shell (e.g., iterate PATH with std::env::split_paths like rust/crates/runtime/src/sandbox.rs:280-283), which avoids both the sh dependency and the injection class entirely.
| assert!(!super::command_exists("ls; echo injected")); | ||
| assert!(!super::command_exists("ls && echo injected")); |
There was a problem hiding this comment.
The injection regression test uses ls; echo injected / ls && echo injected. In a hostile or unusual environment it’s possible (though unlikely) to have an executable literally named ls; echo injected, which would make this test fail even though the injection fix is correct. To make the test robust, use a definitely-nonexistent command name as the prefix (e.g., reuse nonexistentcommandthatshouldneverexist123 and append ; echo injected / && echo injected).
| assert!(!super::command_exists("ls; echo injected")); | |
| assert!(!super::command_exists("ls && echo injected")); | |
| assert!(!super::command_exists( | |
| "nonexistentcommandthatshouldneverexist123; echo injected" | |
| )); | |
| assert!(!super::command_exists( | |
| "nonexistentcommandthatshouldneverexist123 && echo injected" | |
| )); |
|
Codex Review: Didn't find any major issues. 🚀 ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@gemini-code-assist, what are the steps and line numbers to make this fix? |
|
The fix for the command injection vulnerability in fn command_exists(command: &str) -> bool {
std::process::Command::new("sh")
.arg("-c")
.arg("command -v \"$1\" >/dev/null 2>&1")
.arg("--")
.arg(command)
.status()
.map(|status| status.success())
.unwrap_or(false)
}This change affects lines 5152–5157 in |
🎯 What: Fixed a command injection vulnerability in
⚠️ Risk: An attacker could potentially execute arbitrary shell commands by crafting malicious input (e.g.,
rust/crates/tools/src/lib.rswhere user input was being formatted directly into ash -cstring.ls; echo injected).🛡️ Solution: Updated the execution to use positional arguments for
sh -c(sh -c 'command -v "$1" >/dev/null 2>&1' -- <command>), which safely passes the argument to the shell instead of concatenating it.PR created automatically by Jules for task 17023235982071512804 started by @badMade