Skip to content

fix(shellwrap): hand cmd.exe its command line so paths with spaces start - #1980

Closed
marcus-beauregard wants to merge 1 commit into
smart-mcp-proxy:mainfrom
marcus-beauregard:fix/windows-stdio-path-with-spaces
Closed

marcus-beauregard wants to merge 1 commit into
smart-mcp-proxy:mainfrom
marcus-beauregard:fix/windows-stdio-path-with-spaces

Conversation

@marcus-beauregard

Copy link
Copy Markdown

Fixes #1960.

What was wrong

WrapWithUserShell quotes a stdio command path for cmd.exe and returns the whole thing as one argv element (["/c", commandString]). When os/exec builds the Windows command line it escapes each embedded quote as ", and cmd.exe has no backslash escape, so a server installed under a path with a space was looked up under a name that began with a backslash-quote:

'"C:/Users/.../HTTP Toolkit/resources/httptoolkit-mcp.cmd"' is not recognized as an internal or external command

The fix

shellwrap.ApplyCommandLine sets SysProcAttr.CmdLine when the command being started is cmd.exe /c <commandString>, so CreateProcess uses the line verbatim instead of letting argv escaping mangle it.

  • internal/shellwrap/shellwrap.go: isCmdExeShell and cmdExeCommandLine, which emits <shell> /s /c "<commandString>". The extra outer quote pair is the one cmd.exe's /S rule strips, so the shell-quoted command inside survives unchanged — whether or not it starts with its own quote. The shell path is omitted when it contains a space (it cannot be quoted inside the line without breaking /S, and os/exec already passes the executable as lpApplicationName).
  • internal/shellwrap/cmdline_windows.go / cmdline_other.go: the Windows implementation and a no-op stub.
  • internal/upstream/core/process_windows.go: applied after winjob.MarkSuspended (that helper preserves other SysProcAttr fields, so CREATE_SUSPENDED and CmdLine coexist).
  • internal/upstream/core/connection_launcher.go: applied to the launcher command as well; it is a no-op for the docker/direct-exec branch, which has no /c.

Unix shells and Git Bash on Windows keep the argv path — their command string uses POSIX quoting, which argv escaping does not damage.

Tests

  • cmdline_test.go (any host): shell detection; the /S contract — stripping the first and last quote after /s /c must give back exactly the command string, checked for a spaced program path, a bare command and a shell path that itself has a space; a shape test that WrapWithUserShell still returns [/c, commandString] so the rewrite precondition cannot drift silently; and the no-op cases.
  • cmdline_windows_test.go (Windows): builds a .cmd under <temp>/Program Files/HTTP Toolkit/, wraps it with an argument, applies the command line and runs it, expecting the script's own output.
go build ./internal/shellwrap ./internal/upstream/core
go vet ./internal/shellwrap ./internal/upstream/core
GOOS=windows go vet ./internal/shellwrap ./internal/upstream/core   # compiles the Windows test
go test ./internal/shellwrap
go test ./internal/upstream/core

All pass. unit-tests.yml runs the Windows matrix only on pushes to main, so the Windows-only test compiles here (GOOS=windows go vet) and first executes there; the string-level contract it depends on is covered on Linux.

WrapWithUserShell quotes a stdio command path for cmd.exe, and os/exec then
escaped every embedded quote as \" while building the Windows command line.
cmd.exe has no backslash escape, so a server installed under "Program Files"
was looked up under a name that began with a quote and never started (smart-mcp-proxy#1960).

ApplyCommandLine sets SysProcAttr.CmdLine for cmd.exe /c invocations at both
spawn sites - the stdio process-group command func and the launcher - using
/s /c so cmd.exe strips exactly the outer pair of quotes and parses the
string WrapWithUserShell built. Unix shells and Git Bash keep the argv path.
The command-string builder and shell detection live in shellwrap so they are
testable on any host; the end-to-end test is Windows-only.
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@felipecrs

Copy link
Copy Markdown
Contributor

@marcus-beauregard have in mind I also proposed a fix for this issue at #1963.

@Dumbris

Dumbris commented Nov 24, 2026

Copy link
Copy Markdown
Member

Thanks @marcus-beauregard for picking up #1960 and for the tests here — the diagnosis is right.

Two PRs fix the same bug, and we are going with #1963: it was opened first and also covers the Docker fallback path in docker.go, which this PR leaves on the old behaviour. So I'm closing this one in its favour — nothing wrong with the approach.

Your tests are the part #1963 does not have: its tests only run on Windows, while yours run on Linux and macOS too. If you'd like to send them as a tests-only PR on top of main once #1963 has merged, we'd be glad to take it.

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]: Windows stdio upstreams fail when the command path contains a space

4 participants