Skip to content

🔒 Fix command injection vulnerability in command_exists#74

Open
badMade wants to merge 2 commits into
mainfrom
jules-17023235982071512804-0a17bec3
Open

🔒 Fix command injection vulnerability in command_exists#74
badMade wants to merge 2 commits into
mainfrom
jules-17023235982071512804-0a17bec3

Conversation

@badMade
Copy link
Copy Markdown
Owner

@badMade badMade commented Apr 30, 2026

🎯 What: Fixed a command injection vulnerability in rust/crates/tools/src/lib.rs where user input was being formatted directly into a sh -c string.
⚠️ Risk: An attacker could potentially execute arbitrary shell commands by crafting malicious input (e.g., 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

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 30, 2026 01:59
@google-labs-jules
Copy link
Copy Markdown

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@gemini-code-assist
Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_exists to 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.

Comment on lines 5151 to +5156
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)
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +5357 to +5360
#[test]
fn test_command_exists_valid() {
assert!(super::command_exists("ls") || super::command_exists("dir"));
}
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copilot uses AI. Check for mistakes.
Comment on lines +5367 to +5371
#[test]
fn test_command_exists_injection() {
assert!(!super::command_exists("ls; echo injected"));
assert!(!super::command_exists("ls && echo injected"));
}
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
@badMade badMade marked this pull request as ready for review April 30, 2026 04:16
Copilot AI review requested due to automatic review settings April 30, 2026 04:16
@gemini-code-assist
Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 5151 to 5157
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()
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +5371 to +5372
assert!(!super::command_exists("ls; echo injected"));
assert!(!super::command_exists("ls && echo injected"));
Copy link

Copilot AI Apr 30, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
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"
));

Copilot uses AI. Check for mistakes.
@badMade
Copy link
Copy Markdown
Owner Author

badMade commented Apr 30, 2026

@claude code review
@codex code review

@chatgpt-codex-connector
Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@badMade
Copy link
Copy Markdown
Owner Author

badMade commented May 6, 2026

@gemini-code-assist, what are the steps and line numbers to make this fix?

@gemini-code-assist
Copy link
Copy Markdown

The fix for the command injection vulnerability in rust/crates/tools/src/lib.rs involves changing how the shell command is constructed to avoid direct string interpolation. You can apply this change by replacing the existing command_exists function implementation with the following:

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 rust/crates/tools/src/lib.rs. By using positional arguments (-- and the command variable), you ensure that the shell treats the input strictly as an argument to command -v rather than as part of the shell command string itself, effectively neutralizing potential injection attempts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants