Repository navigation
ssh -t exec: quote the command operand so -c shells get the whole command - #889
曹梦轩 (caomengxuan666) wants to merge 3 commits into
Conversation
The pty+exec branch of do_exec_windows concatenates the remote command
into the child command line without quoting, producing
"shell.exe" -c sleep 30. Shells whose -c takes a single operand
(GNU semantics: bash, and other bash-compatible shells) then run only
the first word; cmd/powershell are unaffected because their command
option consumes the rest of the line. The non-pty path already quotes
the operand ("%s %s \"%s\""); this brings the pty path to parity.
Repro (DefaultShell=bash-style shell, DefaultShellCommandOption=-c):
ssh -tt host 'sleep 30' -> sleep: missing operand
ssh -tt host 'whoami' -> works (single word)
Fixes #2478
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
🟡 Changes recommended
The added quoting can corrupt previously working commands, so shell-appropriate escaping is needed before approval.
1 open issue
What changed in this PR
This PR addresses command truncation in Windows PTY execution by quoting the command operand passed to shells whose -c option accepts a single argument.
Changes:
- Adds surrounding quotes to the PTY command operand.
- Expands the allocation by two characters and documents the intended behavior.
| File | Description |
|---|---|
contrib/win32/win32compat/w32-doexec.c |
Quotes the PTY command operand and adjusts its buffer allocation. |
Review effort: Balanced
The first cut quoted the command operand unconditionally, which would corrupt rest-of-line shells on trailing backslashes (-c "... C:\" decodes the closing quote as escaped) and still left embedded quotes unescaped for argv-parsing shells. Now the pty branch mirrors the non-pty dispatch: cmd/powershell/ ssh-shellhost keep the raw concatenated command (their option consumes the rest of the line), while bash-style -c shells get the operand via quote_argv_operand() — quotes plus CommandLineToArgvW escaping, so trailing backslashes and embedded quotes survive the parse. ssh -tt host 'sleep 30' -> sleep 30 ssh -tt host 'echo "a b"' -> a b (quoted correctly) ssh -tt host 'dir C:\' -> dir C:\ (trailing backslash intact) cmd /c ... / powershell -c ... -> unchanged raw command line
|
Addressed the review feedback — the second commit makes the quoting shell-type-aware: cmd/powershell/ssh-shellhost keep the raw rest-of-line command unchanged, while -c shells (bash-style) get the operand via a new quote_argv_operand() that does CommandLineToArgvW escaping. Trailing backslashes (dir C:) now survive because backslash runs double only before a quote or the closing quote, and embedded double-quotes are escaped rather than splitting. Added regression cases for both shapes to the commit message. |
|
/azp run |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
| if (shell_type != SH_CMD && shell_type != SH_PS && | ||
| shell_type != SH_SHELLHOST && |
| (operand = quote_argv_operand(command)) != NULL) | ||
| operand_sp = operand; |
…oting OOM - The pty path quoted SH_OTHER shells unconditionally, ignoring the DefaultShellEscapeArguments=0 opt-out that the non-pty dispatch honors; a shell like C:\Windows\SysWOW64\cmd.exe (classified as SH_OTHER) received literal backslashes before quotes in e.g. echo "a b". Quote bash-style shells always, and SH_OTHER only when arg_escape is set, mirroring the non-pty path's per-type dispatch. - A quote_argv_operand() allocation failure silently fell back to the unquoted command, reintroducing the first-word truncation this PR fixes; treat it as ENOMEM, log it, and fail the exec instead of launching a different command than the client requested.
|
Status: pushed 6e08287 to address the two open review points (SH_OTHER now honors |
|
/azp run |
|
Commenter does not have sufficient privileges for PR 889 in repo PowerShell/openssh-portable |

Problem
In
do_exec_windows, the pty+exec path serializes the child command line by plain concatenation:The resulting command line is
"C:\shell.exe" -c sleep 30. Any shell whose-ctakes a single operand (GNU semantics — bash, and bash-compatible shells) then parsessleepas the command and30as$0: the remote command silently truncates to its first word.The non-pty path right below already quotes the operand (
"%s %s \"%s\""), which is whyssh host 'sleep 30'works whilessh -t host 'sleep 30'doesn't.cmd/powershellwere never affected because/c/-cconsume the rest of the line verbatim — so this only surfaces with non-stock shells.Repro (with
DefaultShell= a bash-style shell,DefaultShellCommandOption=-c)Verified live against
OpenSSH_for_Windows_9.5p1on Windows 10 19045.Fix
Wrap the command operand in quotes in the pty branch — same quoting the non-pty path applies — and budget the two extra characters.
Known limitation (inherited from the non-pty path's identical quoting): a command containing literal
"chars is still not escaped for argv-style parsing; matching the non-pty behavior keeps this minimal — a fuller CommandLineToArgvW escaping pass could be a follow-up if desired.Fixes PowerShell/Win32-OpenSSH#2478