Skip to content

windows: fix cmd.exe quoting bugs in shell_exec + execute_code - #307

Merged
FMXExpress merged 1 commit into
mainfrom
claude/windows-shell-quoting
Jun 18, 2026
Merged

FMXExpress merged 1 commit into
mainfrom
claude/windows-shell-quoting

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

Two layered Windows quoting bugs surfaced from real prediction logs. Both reproduce on every Windows build that hits a command with internal quotes, which is essentially every model call that runs Python / Node / any -c-style one-liner.

BUG 1 -- execute_code over-quoted the executable name.

PasClaw.Tools.ExecuteCode.pas:467-472 unconditionally wrapped
every argv element in "..." before joining for cmd /C, so the
command line ended up:

  "powershell" "-NoProfile" "-ExecutionPolicy" "Bypass" \
                            "-File" "C:\Users\...\exec.ps1"

cmd.exe's command lookup looks for an executable literally named
"powershell" (with the surrounding quotes interpreted as part
of the name in a cmd quirk), doesn't find one, and fails with

  '"powershell"' is not recognized as an internal or external
  command, operable program or batch file.

Fix: new ArgvNeedsQuoting helper that returns True only for
args containing whitespace or shell metacharacters
(space / tab / quote / & / | / < / > / ^ / ( ) / * ? ; / $ / ). Argv[0] -- always a bareword like bash/pwsh/powershell`
on every current code path -- passes through unquoted; the
script path under $PASCLAW_HOME/tmp/ still gets quoted when the
operator profile has a space in it.

BUG 2 -- shell_exec mangled internal quotes via MSVCRT escaping.

PasClaw.Platform.pas (both RunOneShot and RunOneShotWithEnv on
the Windows branch) did:

  P.Executable := 'cmd.exe';
  P.Parameters.Add('/C');
  P.Parameters.Add(Cmd);

FPC's TProcess can't pass an "array of params" to Windows --
CreateProcess takes a single lpCommandLine string -- so it
builds that string from Parameters by applying its own
MSVCRT-style escape pass: embedded " becomes \".

cmd.exe does NOT recognise \". Its escape for an embedded
quote is "" (doubled) or ^" (caret-escape). When the model
sent python -c "with open(...)", FPC turned that into
python -c \"with open(...)\", cmd's parser split at the wrong
boundary, and Python received a truncated -c argument:

  File "<string>", line 1
      "with
      ^
  SyntaxError: unterminated string literal

Fix: bypass FPC's escape pass by setting TProcess.CommandLine
directly. cmd.exe's cmd /C "<stuff>" outer-quote-strip rule
then handles the common case cleanly --

  P.CommandLine := 'cmd.exe /C "' + Cmd + '"';

-> cmd /C "python -c "with open(...)""
^ cmd strips first/last
-> python -c "with open(...)" "

Documented limitation: if Cmd contains an unquoted shell
operator (&&, |, >) AND no internal quotes, cmd's strip rule
may misapply. The model rarely emits such commands and the
FPC.Parameters path's mangling was a guaranteed bug for every
quoted command, so this is a net improvement. Block comments
on both code paths spell out the trade-off so a future cleanup
doesn't reintroduce the Parameters call.

Tests:

src/tests/execute_code_tests.pas grows TestArgvNeedsQuoting
with 14 assertions:
* bareword executables (bash / powershell / pwsh) unquoted
* bareword flags (-File / -NoProfile / Bypass) unquoted
* plain Windows paths (no spaces) unquoted
* plain POSIX paths unquoted
* Windows path WITH space quoted
* POSIX path with space quoted
* empty string quoted (so the slot isn't lost)
* each metacharacter triggers quoting (&&, |, ", $, `)

All existing suites still pass: test-execute-code,
test-shell-backend, test-shell-filters, test-run-timeout.

Sandbox here is FPC/linux; the Windows fixes are verified by
inspection and unit-tests against the helper. Real validation
on a Windows box is the next step.

Two layered Windows quoting bugs surfaced from real prediction
logs.  Both reproduce on every Windows build that hits a command
with internal quotes, which is essentially every model call that
runs Python / Node / any -c-style one-liner.

BUG 1 -- execute_code over-quoted the executable name.

  PasClaw.Tools.ExecuteCode.pas:467-472 unconditionally wrapped
  every argv element in `"..."` before joining for cmd /C, so the
  command line ended up:

      "powershell" "-NoProfile" "-ExecutionPolicy" "Bypass" \
                                "-File" "C:\Users\...\exec.ps1"

  cmd.exe's command lookup looks for an executable literally named
  `"powershell"` (with the surrounding quotes interpreted as part
  of the name in a cmd quirk), doesn't find one, and fails with

      '"powershell"' is not recognized as an internal or external
      command, operable program or batch file.

  Fix: new ArgvNeedsQuoting helper that returns True only for
  args containing whitespace or shell metacharacters
  (space / tab / quote / & / | / < / > / ^ / ( ) / * ? ; / $ / `).
  Argv[0] -- always a bareword like `bash` / `pwsh` / `powershell`
  on every current code path -- passes through unquoted; the
  script path under $PASCLAW_HOME/tmp/ still gets quoted when the
  operator profile has a space in it.

BUG 2 -- shell_exec mangled internal quotes via MSVCRT escaping.

  PasClaw.Platform.pas (both RunOneShot and RunOneShotWithEnv on
  the Windows branch) did:

      P.Executable := 'cmd.exe';
      P.Parameters.Add('/C');
      P.Parameters.Add(Cmd);

  FPC's TProcess can't pass an "array of params" to Windows --
  CreateProcess takes a single lpCommandLine string -- so it
  builds that string from Parameters by applying its own
  MSVCRT-style escape pass: embedded `"` becomes `\"`.

  cmd.exe does NOT recognise `\"`. Its escape for an embedded
  quote is `""` (doubled) or `^"` (caret-escape). When the model
  sent `python -c "with open(...)"`, FPC turned that into
  `python -c \"with open(...)\"`, cmd's parser split at the wrong
  boundary, and Python received a truncated `-c` argument:

      File "<string>", line 1
          "with
          ^
      SyntaxError: unterminated string literal

  Fix: bypass FPC's escape pass by setting TProcess.CommandLine
  directly.  cmd.exe's `cmd /C "<stuff>"` outer-quote-strip rule
  then handles the common case cleanly --

      P.CommandLine := 'cmd.exe /C "' + Cmd + '"';

  ->  cmd /C "python -c "with open(...)""
                                          ^ cmd strips first/last
      ->  python -c "with open(...)"      "

  Documented limitation: if Cmd contains an unquoted shell
  operator (&&, |, >) AND no internal quotes, cmd's strip rule
  may misapply.  The model rarely emits such commands and the
  FPC.Parameters path's mangling was a guaranteed bug for every
  quoted command, so this is a net improvement.  Block comments
  on both code paths spell out the trade-off so a future cleanup
  doesn't reintroduce the Parameters call.

Tests:

  src/tests/execute_code_tests.pas grows TestArgvNeedsQuoting
  with 14 assertions:
    * bareword executables (bash / powershell / pwsh) unquoted
    * bareword flags (-File / -NoProfile / Bypass) unquoted
    * plain Windows paths (no spaces) unquoted
    * plain POSIX paths unquoted
    * Windows path WITH space quoted
    * POSIX path with space quoted
    * empty string quoted (so the slot isn't lost)
    * each metacharacter triggers quoting (&&, |, ", $, `)

  All existing suites still pass: test-execute-code,
  test-shell-backend, test-shell-filters, test-run-timeout.

  Sandbox here is FPC/linux; the Windows fixes are verified by
  inspection and unit-tests against the helper.  Real validation
  on a Windows box is the next step.
@FMXExpress
FMXExpress merged commit 2aff30c into main Jun 18, 2026
khongten001 pushed a commit to khongten001/PasClaw that referenced this pull request Jul 13, 2026
The README + docs/changelog.md last entry was 2026-06-13 (FMXExpress#244). Five
substantial PRs (FMXExpress#314 FMXExpress#315 FMXExpress#316 FMXExpress#317 FMXExpress#318) and ten more feature PRs
between them and that entry shipped without changelog entries. This
brings both files current through today.

Twenty new entries (newest first, matching the existing style):

  2026-06-21
    FMXExpress#318  Relay pull-worker provider (queue + provider + /v1/relay/* HTTP)

  2026-06-20
    FMXExpress#317  pasclaw plan + build PLAN.md pickup + build --goal + cog mode
    FMXExpress#316  Cloudflare AI Gateway provider catalog rows
    FMXExpress#315  MCP progressive disclosure + tool_search
    FMXExpress#314  Stock defaults adopt lean-edit shape; fs_grep split-gate

  2026-06-18
    FMXExpress#312  Web UI workspace.zip export + import
    FMXExpress#310  Model-callable cron tool
    FMXExpress#309  FTS5 snippet window widened 24 -> 60
    FMXExpress#307  Windows cmd.exe quoting fix

  2026-06-17
    FMXExpress#306  cog-build honours profile=security sandbox
    FMXExpress#304  cog-build default profile = max-build
    FMXExpress#303  cog-build list[Path] outputs + Secret inputs + -q

  2026-06-16
    FMXExpress#302  cog-build optional workspace_in
    FMXExpress#300  pasclaw build cmd + cog-build/ predictor (parent feature)
    FMXExpress#299  Checkpoints zpaq backend + /redo
    FMXExpress#298  AGENTS.md + pasclaw init + /init
    FMXExpress#297  tool_choice force-a-specific-tool form
    FMXExpress#296  Delphi ExtraEnv injection fix
    FMXExpress#295  KB native PDF ingest

  2026-06-15
    FMXExpress#292  pasclaw profile diff + bench

Skipped per the changelog's stated convention ("Bug fixes and review
follow-ups are in the full git log"):

  FMXExpress#293  cmd/profile dcc64 fix
  FMXExpress#294  skills_manage gemini empty-enum fix
  FMXExpress#301  cmd.build dcc64 fix
  FMXExpress#311  cron dcc64 fix

Files updated identically (the docs/changelog.md note at the top says
"This file mirrors the changelog section at the top of the root
README.md"):

  M README.md           20 entries + 20 link refs added
  M docs/changelog.md   same

The link-ref block at the bottom of each file gained 20 [#NNN] entries
keeping the existing one-per-line / sorted-numerically format.
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