Skip to content

fix: escape quotes in working_directory to prevent shell command injection (fixes #479) - #481

Open
Wbhhh996 wants to merge 1 commit into
mediar-ai:mainfrom
Wbhhh996:fix/working-directory-quote-injection
Open

Wbhhh996 wants to merge 1 commit into
mediar-ai:mainfrom
Wbhhh996:fix/working-directory-quote-injection

Conversation

@Wbhhh996

Copy link
Copy Markdown

Summary

Fixes #479 — working_directory allows shell command injection in run_command.

The working_directory argument was interpolated into a single-quoted shell fragment (cd '<cwd>' && <cmd>) without escaping quote characters. A caller-controlled path containing a single quote could close the cd argument early and append arbitrary shell commands.

Changes

  • Added escape_cwd_single_quotes() helper in terminator-rs (shared by both call sites)
  • Unix shells (bash/sh/zsh): single quotes escaped as '\'' (close-quote, escaped-quote, reopen)
  • PowerShell (powershell/pwsh): single quotes doubled ('')
  • cmd.exe: double quotes stripped from the path (legal Windows paths cannot contain ", so this only affects malicious input and neutralizes it)
  • Applied the fix in both call sites: Desktop::run() (crates/terminator/src/lib.rs) and the MCP run_command handler (crates/terminator-mcp-agent/src/server.rs)

Verification

  • cargo check -p terminator-rs ✅
  • cargo check -p terminator-mcp-agent ✅
  • cargo test -p terminator-rs --lib escape_cwd_tests ✅ 2/2 passed
    • unix quote escaping (/tmp/a'b → /tmp/a'\''b)
    • powershell quote doubling (C:\a'b → C:\a''b)

Notes

  • Behavior for paths without quote characters is unchanged.
  • The cmd.exe branch already wrapped the path in double quotes; stripping embedded " preserves that for all legal Windows paths.

… injection

Fixes mediar-ai#479

working_directory was interpolated into a single-quoted shell fragment
(cd '<cwd>' && <cmd>) without escaping quote characters, allowing a
caller-controlled path to close the quote and append arbitrary shell
commands.

- Unix shells: escape single quotes as '\'' (close, escaped, reopen)
- PowerShell: double single quotes ('')
- cmd.exe: strip double quotes (legal Windows paths cannot contain them)

Added unit tests for the new escape_cwd_single_quotes helper.
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.

[Bug] working_directory allows shell command injection in run_command

1 participant