Skip to content

[BUG] cmd.run module behaves incorrectly on Windows #68096

Closed

Description

@xsmile

Description

The cmdmod module makes assumptions about the shell type and the content of the command, modyfing them on a best-effort basis to ensure it can run successfully.

These assumptions override user parameters and in multiple cases lead to unwanted results or break command execution. cmdmod should leave the parameters as is.

Setup

  • VM (KVM)
  • onedir packaging via EXE installer

Case 1

Running a command via cmd.exe. A relative instead of an absolute path to cmd is used.

PS C:\Program Files\Salt Project\Salt> .\salt-call --local cmd.run 'echo 1' shell=cmd.exe
local:
    1

Executed command

cmd /c "echo 1"

Expected command:

C:\Windows\System32\cmd.exe /c "echo 1"

Case 2

Running a binary via a shell. The shell parameter is ignored. Instead, whoami.exe is determined to be a valid executable and started directly.

PS C:\Program Files\Salt Project\Salt> .\salt-call --local cmd.run 'whoami.exe' shell=cmd.exe
local:
    domain\administrator

Executed command:

whoami.exe

Expected command:

C:\Windows\System32\cmd.exe /c "whoami.exe"

Case 3

Running a command via PowerShell. pwsh.exe is used but the command is prepended with the call operator &.

PS C:\Program Files\Salt Project\Salt> .\salt-call --local cmd.run '(echo 1)' shell=pwsh.exe
[ERROR   ] Command '"C:\Program' failed with return code: 1
[ERROR   ] stdout: &: The term '1' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
[ERROR   ] retcode: 1
[ERROR   ] Command '(echo' failed with return code: 1
[ERROR   ] output: &: The term '1' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
local:
    ?[31;1m&: ?[31;1mThe term '1' is not recognized as a name of a cmdlet, function, script file, or executable program.?[0m
    ?[31;1m?[31;1mCheck the spelling of the name, or if a path was included, verify that the path is correct and try again.?[0m

Executed command:

"C:\Program Files\PowerShell\7\pwsh.exe" -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command "& (echo 1)"

Expected command:

"C:\Program Files\PowerShell\7\pwsh.exe" -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command "(echo 1)"

Case 4

Running a command via PowerShell containing characters assumed to be cmd-only. pwsh.exe is initially used to build the command string but is prepended by & and wrapped by cmd.exe in the end.

PS C:\Program Files\Salt Project\Salt> .\salt-call --local cmd.run 'echo 1 && echo 2' shell=pwsh.exe
local:
    Cannot process the command because of a missing parameter. A command must follow -Command.

    Usage: pwsh[.exe] [-Login] [[-File] <filePath> [args]]
                      [-Command { - | <script-block> [-args <arg-array>]
                                    | <string> [<CommandParameters>] } ]
                      [-CommandWithArgs <string> [<CommandParameters>]
                      [-ConfigurationName <string>] [-ConfigurationFile <filePath>]
                      [-CustomPipeName <string>] [-EncodedCommand <Base64EncodedCommand>]
                      [-ExecutionPolicy <ExecutionPolicy>] [-InputFormat {Text | XML}]
                      [-Interactive] [-MTA] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile]
                      [-NoProfileLoadTime] [-OutputFormat {Text | XML}]
                      [-SettingsFile <filePath>] [-SSHServerMode] [-STA]
                      [-Version] [-WindowStyle <style>]
                      [-WorkingDirectory <directoryPath>]

           pwsh[.exe] -h | -Help | -? | /?

    PowerShell Online Help https://aka.ms/powershell-docs

    All parameters are case-insensitive.
    1
    2""

Executed command:

cmd /c ""C:\Program Files\PowerShell\7\pwsh.EXE" -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command "& echo 1 && echo 2""

Expected command:

"C:\Program Files\PowerShell\7\pwsh.EXE" -NonInteractive -NoProfile -ExecutionPolicy Bypass -Command "echo 1 && echo 2"

Versions Report

salt --versions-report
Salt Version:
          Salt: 3006.12

Python Version:
        Python: 3.10.17 (heads/main:8bbacbe, Jun  9 2025, 20:41:47) [MSC v.1943 64 bit (AMD64)]

Dependency Versions:
          cffi: 1.14.6
      cherrypy: 18.6.1
  cryptography: 42.0.5
      dateutil: 2.8.1
     docker-py: Not Installed
         gitdb: 4.0.7
     gitpython: 3.1.41
        Jinja2: 3.1.6
       libgit2: Not Installed
  looseversion: 1.0.2
      M2Crypto: Not Installed
          Mako: Not Installed
       msgpack: 1.0.2
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     packaging: 22.0
     pycparser: 2.21
      pycrypto: Not Installed
  pycryptodome: 3.19.1
        pygit2: Not Installed
  python-gnupg: 0.4.8
        PyYAML: 6.0.1
         PyZMQ: 25.0.2
        relenv: 0.19.3
         smmap: 4.0.0
       timelib: 0.2.4
       Tornado: 4.5.3
           ZMQ: 4.3.4

System Versions:
          dist:
        locale: utf-8
       machine: AMD64
       release: 2022Server
        system: Windows
       version: 2022Server 10.0.20348 SP0 Multiprocessor Free

Similar issues

#68095
#67190

Activity

  1. added
    bug
    on Jun 19, 2025
  2. welcome commented on Jun 19, 2025

    @welcome

    Hi there! Welcome to the Salt Community! Thank you for making your first contribution. We have a lengthy process for issues and PRs. Someone from the Core Team will follow up as soon as possible. In the meantime, here’s some information that may help as you continue your Salt journey.
    Please be sure to review our Code of Conduct. Also, check out some of our community resources including:

    There are lots of ways to get involved in our community. Every month, there are around a dozen opportunities to meet with other contributors and the Salt Core team and collaborate in real time. The best way to keep track is by subscribing to the Salt Community Events Calendar.
    If you have additional questions, email us at [email protected]. We’re glad you’ve joined our community and look forward to doing awesome things with you!

  3. self-assigned this
    on Jun 19, 2025
  4. added this to the Sulfur v3006.13 milestone on Jun 19, 2025
  5. added theissue type on Jun 19, 2025
  6. twangboy commented on Jun 25, 2025

    @twangboy
    Contributor

    Case 3:
    This has been fixed by #68056
    Case 4:
    I think this is also fixed by #68056
    It shows the same error that the example shows

    At line:1 char:8
    + echo 1 && echo 2
    +        ~~
    The token '&&' is not a valid statement separator in this version.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : InvalidEndOfLine
    

    Need to do some more work to get it to honor shell when it is defined

  7. linked a pull request that will close this issuecmdmod enhancements for Windows #68156on Jul 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugneeds-triage

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions