Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[BUG] cmdmod / cmd.run: incorrect handling of parameter list as cmd on Windows (breaks win_lgpo) #68095

@hurzhurz

Description

@hurzhurz

Description
cmd.run allows the command (first argument) to be ether a simple string or a list of strings.

Giving the CLI parameters as a list has the advantage, that proper quoting is handled automatically, escpacially if they contain spaces.
But this doesn't work properly on Windows since 3006.11 because the changes in #67919.
Inside prepend_cmd, the list always gets merged into a single string via a simple join:

cmd = salt.platform.win.prepend_cmd(cmd)

cmd = " ".join(cmd)

For example: salt xyz cmd.run '["cacls","C:\\Program Files"]'
It should effectively run cacls "C:\Program Files" and show the file permissions for the dir.
But it actually runs cacls C:\Program Files so the path is not recognized as one single parameter and it doesn't work.

This is actually also a problem for win_lgo because it runs secedit in a similar way here:

__salt__["cmd.run"](["secedit", "/export", "/cfg", f_exp])

The parameter f_exp contains a file path within the minion cache path, which can contain a space.
And as this doesn't get properly quoted, secedit gets incorrect parameters and doesn't write the file as wanted by win_lgpo.

# salt xyz lgpo.get
xyz:
    The minion function caused an exception: Traceback (most recent call last):
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\minion.py", line 2026, in _thread_return
        return_data = minion_instance._execute_job_function(
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\minion.py", line 1985, in _execute_job_function
        return_data = self.executors[fname](opts, data, func, args, kwargs)
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\loader\lazy.py", line 159, in __call__
        ret = self.loader.run(run_func, *args, **kwargs)
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\loader\lazy.py", line 1245, in run
        return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\loader\lazy.py", line 1260, in _run_as
        ret = _func_or_method(*args, **kwargs)
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\executors\direct_call.py", line 10, in execute
        return func(*args, **kwargs)
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\loader\lazy.py", line 159, in __call__
        ret = self.loader.run(run_func, *args, **kwargs)
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\loader\lazy.py", line 1245, in run
        return self._last_context.run(self._run_as, _func_or_method, *args, **kwargs)
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\loader\lazy.py", line 1260, in _run_as
        ret = _func_or_method(*args, **kwargs)
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\modules\win_lgpo.py", line 8876, in get
        class_vals[policy_name] = _get_policy_info_setting(_pol)
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\modules\win_lgpo.py", line 8953, in _get_policy_info_setting
        value = _get_secedit_value(option=policy_definition["Secedit"]["Option"])
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\modules\win_lgpo.py", line 5815, in _get_secedit_value
        secedit_data = _get_secedit_data()
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\modules\win_lgpo.py", line 5807, in _get_secedit_data
        __context__["lgpo.secedit_data"] = _load_secedit_data()
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\modules\win_lgpo.py", line 5782, in _load_secedit_data
        with salt.utils.files.fopen(f_exp, encoding="utf-16") as fp:
      File "C:\Program Files\Salt Project\Salt\Lib\site-packages\salt\utils\files.py", line 389, in fopen
        f_handle = open(  # pylint: disable=resource-leakage,unspecified-encoding
    FileNotFoundError: [Errno 2] No such file or directory: 'C:\\ProgramData\\Salt Project\\Salt\\var\\cache\\salt\\minion\\secedit-5ea8a8ab7d2643aaadba993760f690a3.txt'

Workaround:
Get cmdmod from 3006.10: https://raw.githubusercontent.com/saltstack/salt/refs/tags/v3006.10/salt/modules/cmdmod.py
Copy it to /srv/salt/_modules/ and do a saltutil.sync_modules

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: Not Installed
        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: 2019Server
        system: Windows
       version: 2019Server 10.0.17763 SP0 Multiprocessor Free

Metadata

Metadata

Assignees

Labels

bugbroken, incorrect, or confusing behaviorneeds-triage

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions