Skip to content

fix(system.run): quote argv args containing spaces before shell join - #32

Closed
danedane wants to merge 2 commits into
openclaw:masterfrom
danedane:fix/argv-quoting-spaces
Closed

danedane wants to merge 2 commits into
openclaw:masterfrom
danedane:fix/argv-quoting-spaces

Conversation

@danedane

@danedane danedane commented Mar 10, 2026 •

Copy link
Copy Markdown
Contributor

Changes

  1. Shell-aware argv quoting in LocalCommandRunner.BuildProcessArgs

Problem: When system.run receives a command as an argv array, BuildProcessArgs joins args with spaces but doesn't quote args containing spaces. Multi-word arguments (e.g. -p "List all files") get split into separate tokens.

Fix: Added QuoteArgIfNeeded() with shell-aware escaping:

• cmd.exe: double quotes with backslash-escaped inner quotes
• PowerShell: single quotes with doubled inner quotes

PowerShell requires single quotes because double quotes in ProcessStartInfo.Arguments are stripped by the Windows CRT argv parser before PowerShell receives the -Command string.

  1. system.run.prepare pre-flight command

Problem: The gateway's nodes run action requires system.run.prepare to build an execution plan before the actual run. Without it, the only path is the invoke action which has a hardcoded 30s gateway timeout — too short for long-running tools like GitHub Copilot CLI.

Fix: Added system.run.prepare handler to SystemCapability that echoes back the execution plan (argv, cwd, rawCommand, agentId, sessionKey) without executing anything. Includes FormatExecCommand() helper that matches the gateway's argv formatting for consistency validation.

Testing

Verified on Windows node:

• ✅ Args with spaces preserved: Write-Output 'hello world' → single arg
• ✅ Copilot CLI with multi-word -p prompt runs successfully via nodes run
• ✅ system.run.prepare enables configurable timeout path (tested 120s+)
• ✅ Gateway rawCommand/command consistency validation passes no

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7ef0c5ccbd

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

return arg;

// Escape inner double quotes and wrap
return "\"" + arg.Replace("\"", "\\\"") + "\"";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Escape embedded quotes with PowerShell rules

QuoteArgIfNeeded currently escapes inner " before wrapping in double quotes, but BuildProcessArgs feeds the result to powershell.exe/pwsh -Command, where backslash is not a quote-escape character. Any argv element containing a literal " (for example JSON snippets or prompts with quotes) will be parsed incorrectly by PowerShell and can fail command execution or alter the argument value. This regression is introduced by the new quote-handling path and needs shell-correct escaping for PowerShell commands.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

@danedane
danedane force-pushed the fix/argv-quoting-spaces branch from 7ef0c5c to 914e815 Compare March 10, 2026 20:28
When system.run receives a command as an argv array, BuildProcessArgs
joins the args with spaces before passing to the shell. Args containing
spaces (e.g. a -p prompt like 'List all files') would be split into
separate tokens by the shell, causing tools like GitHub Copilot CLI
to report 'too many arguments'.

Added QuoteArgIfNeeded() that wraps args containing spaces, quotes,
or tabs in double quotes with proper escaping.
@danedane
danedane force-pushed the fix/argv-quoting-spaces branch from 914e815 to 3ab7eda Compare March 10, 2026 20:35
The gateway's nodes 'run' action requires system.run.prepare to build
an execution plan before the actual run. Without it, the only path is
the 'invoke' action which has a hardcoded 30s gateway timeout — too
short for long-running tools like Copilot CLI.

system.run.prepare echoes back the argv, cwd, rawCommand, agentId,
and sessionKey without executing anything, enabling the gateway to
use its configurable timeout path for the actual execution.
@danedane
danedane force-pushed the fix/argv-quoting-spaces branch from d5c8a58 to 4f8c387 Compare March 10, 2026 20:58
@github-actions github-actions Bot mentioned this pull request Mar 17, 2026
36 tasks
shanselman added a commit that referenced this pull request Mar 17, 2026
Address review findings from PRs #29 and #32:

- QuoteArgIfNeeded: check all shell metacharacters (& | ; < > etc.),
  not just whitespace/quotes — prevents command injection
- cmd.exe: use doubled-quote escaping (%"%") instead of backslash
  which cmd doesn't recognize
- FormatExecCommand: align metacharacter checks with QuoteArgIfNeeded
  so system.run.prepare shows the same command that actually executes
- StringBuilder: add lock around stdout/stderr event handlers and
  ToString() reads to prevent concurrent-access corruption

Co-authored-by: danedane <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: Copilot <[email protected]>
@shanselman

Copy link
Copy Markdown
Collaborator

Hey @danedane — great catch on the argv space-splitting bug and nice addition of system.run.prepare! Really appreciate the contribution.

This work has been cherry-picked into #38 along with the IPC hang fix from #29. During review we hardened the quoting (all shell metacharacters, fixed cmd.exe escaping to use doubled quotes), extracted a shared ShellQuoting helper class, and added 43 unit tests.

Thanks for kicking this off!

shanselman added a commit that referenced this pull request Mar 17, 2026
Merges the consolidated fix for system.run hang (#29), argv quoting (#32), and review hardening. Fixes #28.
@shanselman

Copy link
Copy Markdown
Collaborator

Closing — this work has been cherry-picked into #38 (now merged) with additional hardening: expanded metacharacter quoting, fixed cmd.exe escaping, shared ShellQuoting helper, and 43 new tests. Thank you @danedane for the great contribution! 🎉

@shanselman shanselman closed this Mar 17, 2026
@danedane

Copy link
Copy Markdown
Contributor Author

Thanks Scott. Have you found a way to keep the gateway open past 30s from WSL to Windows Node? I added this PR but maybe there's a simpler approach. openclaw/openclaw#42590

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