fix(cli): shell-quote positional argv so shell metacharacters survive the internal re-parse#220
Open
anonx3247 wants to merge 1 commit into
Conversation
When invoked as `srt -- cmd arg1 arg2 ...`, the CLI built the command
string by joining argv with a single space and then handed the result
to `spawn(cmd, { shell: true })`. Bash then re-parsed that string,
which corrupted any argument containing shell metacharacters
(parentheses, single/double quotes, backticks, `$`, `|`, `;`, etc.).
In practice this broke any caller that forwarded arbitrary user text
as a positional argument — for example, `srt -- claude -p "note (a)
with 'quotes'"` would fail with a bash syntax error rather than
running claude with that prompt.
The fix routes argv through `shellquote.quote`, which is the same
library already used elsewhere in this codebase to produce shell-safe
command strings. Each argv element is individually quoted so bash
re-parses it back to the original value.
The `-c` flag retains its current "no escaping" semantics by design —
that mode is for callers who want shell interpretation.
Regression tests cover parentheses, single quotes, backticks, literal
`$VAR`, and a combined realistic prompt argument.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
srt -- cmd arg1 arg2 ...silently mangles (or outright fails on) any argument containing shell metacharacters. The CLI builds the command string withcommandArgs.join(' ')(src/cli.ts line 151 on main) and then hands the result tospawn(cmd, { shell: true }), so bash re-parses it. Every unquoted paren, quote, backtick,$,|,;, or glob in an argv element gets reinterpreted as shell syntax.This PR routes argv through
shellquote.quote— the same library already used acrosssrc/sandbox/*-sandbox-utils.tsto produce shell-safe command strings — so each argv element round-trips through the bash re-parse as a single literal argument.Reproduction (before the fix)
After the fix, all three behave as argv-semantics callers expect: the positional argument arrives at the child process byte-for-byte.
Design notes
-c <command>flag is unchanged and still does no escaping, by design — that mode is the escape hatch for callers who want bash interpretation (pipes, substitution, expansion).env,xargs -0,nice,nohup, etc.).shell-quoteis already adependenciesentry in package.json — no new dep added.Tests
Five regression tests added to
test/cli.test.tsunder the existingdefault mode (positional arguments)describe block, covering:$VAR(must not expand)All new tests assert the argument round-trips through
printf '%s' <arg>unchanged.Test plan
bun test test/cli.test.ts— all 20 tests pass (15 existing + 5 new)npm run typecheck— cleannpm run lint:check— clean