Skip to content

ssh -t exec: quote the command operand so -c shells get the whole command - #889

Open
曹梦轩 (caomengxuan666) wants to merge 3 commits into
PowerShell:latestw_allfrom
caomengxuan666:fix/pty-exec-command-quoting
Open

曹梦轩 (caomengxuan666) wants to merge 3 commits into
PowerShell:latestw_allfrom
caomengxuan666:fix/pty-exec-command-quoting

Conversation

@caomengxuan666

Copy link
Copy Markdown

Problem

In do_exec_windows, the pty+exec path serializes the child command line by plain concatenation:

strcpy_s(pty_cmd, len, shell);                       // "\"<shell path>\""
strcat_s(pty_cmd, len, " ");
strcat_s(pty_cmd, len, shell_command_option_local);  // "-c"
strcat_s(pty_cmd, len, " ");
strcat_s(pty_cmd, len, command);                     // unquoted

The resulting command line is "C:\shell.exe" -c sleep 30. Any shell whose -c takes a single operand (GNU semantics — bash, and bash-compatible shells) then parses sleep as the command and 30 as $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 why ssh host 'sleep 30' works while ssh -t host 'sleep 30' doesn't. cmd/powershell were never affected because /c/-c consume the rest of the line verbatim — so this only surfaces with non-stock shells.

Repro (with DefaultShell = a bash-style shell, DefaultShellCommandOption = -c)

ssh -tt host 'whoami'          → works (single word)
ssh -tt host 'sleep 30'        → "sleep: missing operand"   (30 lost)
ssh -tt host 'printf ok123'    → "printf: usage: ..."       (operand lost)
ssh -tt host 'echo x > f.txt'  → bare echo; redirect dropped
ssh -tt host 'cmd /c echo x'   → an interactive cmd prompt  (args dropped)

Verified live against OpenSSH_for_Windows_9.5p1 on 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

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
Copilot AI balanced review requested due to automatic review settings October 24, 2026 09:46
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI 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.

🟡 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

Comment thread contrib/win32/win32compat/w32-doexec.c Resolved
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
@caomengxuan666

Copy link
Copy Markdown
Author

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.

@tgauth
Tess Gauthier (tgauth) requested a balanced review from Copilot November 2, 2026 20:25
@tgauth

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

Copilot AI 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.

🟡 Changes recommended

The PTY command path ignores escaping opt-outs for some shells and falls back to unquoted command text on allocation failure, potentially changing the command executed.

2 open issues
1 resolved since last review

Review effort: Balanced

Comment thread contrib/win32/win32compat/w32-doexec.c Outdated
Comment on lines +381 to +382
if (shell_type != SH_CMD && shell_type != SH_PS &&
shell_type != SH_SHELLHOST &&

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@/tmp/reply1.md

Comment thread contrib/win32/win32compat/w32-doexec.c Outdated
Comment on lines +383 to +384
(operand = quote_argv_operand(command)) != NULL)
operand_sp = operand;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@/tmp/reply2.md

…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.
@caomengxuan666

Copy link
Copy Markdown
Author

Status: pushed 6e08287 to address the two open review points (SH_OTHER now honors DefaultShellEscapeArguments=0; quoting-allocation failure fails the exec with an ENOMEM diagnostic instead of silently falling back to the unquoted command). CI was fully green on 082f35d; this commit is review-driven robustness hardening only, no behavior change for the default configuration. Re-running the pipeline.

@caomengxuan666

Copy link
Copy Markdown
Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Commenter does not have sufficient privileges for PR 889 in repo PowerShell/openssh-portable

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.

ssh -t exec path concatenates the command unquoted — GNU-style -c shells lose all but the first word

3 participants