Skip to content

fix(powershell): always use -EncodedCommand in Cmd() - #399

Merged
kke merged 1 commit into
mainfrom
fix/powershell-always-encoded
Jul 5, 2026
Merged

fix(powershell): always use -EncodedCommand in Cmd()#399
kke merged 1 commit into
mainfrom
fix/powershell-always-encoded

Conversation

@kke

@kke kke commented Jul 5, 2026

Copy link
Copy Markdown
Contributor

Fixes #398

Cmd() previously routed simple one-liners through the readable -Command "..." form and only base64-encoded when cmd.exe metacharacters were present. That guard set (" % ! ^ & | < >) omits every PowerShell metacharacter ($, backtick, ;, quotes, spaces), so the readable path is unsafe whenever the outer login shell is PowerShell -- e.g. OpenSSH with DefaultShell=PowerShell. There the outer shell parses and expands the argument (including the injected $ProgressPreference='SilentlyContinue'; prefix) before the inner powershell.exe runs, corrupting the command.

Cmd() is a pure string helper with no knowledge of the remote shell, so it cannot reliably decide when -Command is safe. Always emitting -EncodedCommand makes the payload opaque to both cmd.exe and PowerShell and correct regardless of the outer shell. The runner's debug log already decodes -EncodedCommand, so readability is preserved for logging.

Cmd() previously routed simple one-liners through the readable -Command
"..." form and only base64-encoded when cmd.exe metacharacters were present.
That guard set (" % ! ^ & | < >) omits every PowerShell metacharacter ($,
backtick, ;, quotes, spaces), so the readable path is unsafe whenever the
outer login shell is PowerShell -- e.g. OpenSSH with DefaultShell=PowerShell.
There the outer shell parses and expands the argument (including the injected
$ProgressPreference='SilentlyContinue'; prefix) before the inner
powershell.exe runs, corrupting the command (issue #398).

Cmd() is a pure string helper with no knowledge of the remote shell, so it
cannot reliably decide when -Command is safe. Always emitting -EncodedCommand
makes the payload opaque to both cmd.exe and PowerShell and correct
regardless of the outer shell. The runner's debug log already decodes
-EncodedCommand, so readability is preserved for logging.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
@kke kke added the bug Something isn't working label Jul 5, 2026
@kke
kke requested a review from Copilot July 5, 2026 21:14

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.

Pull request overview

This PR fixes an SSH/PowerShell interoperability bug by making powershell.Cmd() always emit -EncodedCommand, ensuring the PowerShell payload is not parsed/expanded by an outer PowerShell login shell (e.g., OpenSSH DefaultShell=PowerShell) before powershell.exe receives it.

Changes:

  • Updated powershell.Cmd() to always return a powershell.exe ... -E <base64> command line (removing the conditional -Command "..." path).
  • Revised unit tests to validate encoded output by decoding the -EncodedCommand payload and asserting on the decoded script contents.
  • Updated golden tests to pin the new encoded command strings.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
powershell/powershell.go Always uses -EncodedCommand and updates function documentation to explain why.
powershell/powershell_test.go Updates tests to expect encoded output and adds helpers to decode and assert decoded script contents.
powershell/golden_test.go Updates golden expectations to match the new encoded command strings.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@kke
kke merged commit a7ad5f5 into main Jul 5, 2026
14 checks passed
@kke
kke deleted the fix/powershell-always-encoded branch July 5, 2026 21:27
james-nesbitt pushed a commit to james-nesbitt/rig that referenced this pull request Aug 5, 2026
Cmd() previously routed simple one-liners through the readable -Command
"..." form and only base64-encoded when cmd.exe metacharacters were present.
That guard set (" % ! ^ & | < >) omits every PowerShell metacharacter ($,
backtick, ;, quotes, spaces), so the readable path is unsafe whenever the
outer login shell is PowerShell -- e.g. OpenSSH with DefaultShell=PowerShell.
There the outer shell parses and expands the argument (including the injected
$ProgressPreference='SilentlyContinue'; prefix) before the inner
powershell.exe runs, corrupting the command (issue k0sproject#398).

Cmd() is a pure string helper with no knowledge of the remote shell, so it
cannot reliably decide when -Command is safe. Always emitting -EncodedCommand
makes the payload opaque to both cmd.exe and PowerShell and correct
regardless of the outer shell. The runner's debug log already decodes
-EncodedCommand, so readability is preserved for logging.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

powershell.Cmd() still emits un-encoded -Command for simple one-liners → breaks on OpenSSH DefaultShell=PowerShell (residual of #346)

2 participants