Skip to content

Commit

Permalink
Make sure cmdmod._log_cmd handles tuples properly
Browse files Browse the repository at this point in the history
  • Loading branch information
dwoz authored and garethgreenaway committed Aug 7, 2021
1 parent 15ad87b commit e6dd6a4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/59793.fixed
@@ -0,0 +1 @@
Make sure cmdmod._log_cmd handles tuples properly
7 changes: 4 additions & 3 deletions salt/modules/cmdmod.py
Expand Up @@ -79,9 +79,10 @@ def __virtual__():


def _log_cmd(cmd):
if isinstance(cmd, str):
return cmd.split()[0].strip()
return cmd[0].strip()
if isinstance(cmd, (tuple, list)):
return cmd[0].strip()
else:
return str(cmd).split()[0].strip()


def _check_cb(cb_):
Expand Down
28 changes: 28 additions & 0 deletions tests/pytests/unit/modules/test_cmdmod.py
Expand Up @@ -664,3 +664,31 @@ def test_cve_2021_25284(caplog):
with caplog.at_level(logging.DEBUG, logger="salt.modules.cmdmod"):
cmdmod.run("testcmd -p ImAPassword", output_loglevel="error")
assert "ImAPassword" not in caplog.text


def test__log_cmd_str():
"_log_cmd function handles strings"
assert cmdmod._log_cmd("foo bar") == "foo"


def test__log_cmd_list():
"_log_cmd function handles lists"
assert cmdmod._log_cmd(["foo", "bar"]) == "foo"


def test_log_cmd_tuple():
"_log_cmd function handles tuples"
assert cmdmod._log_cmd(("foo", "bar")) == "foo"


def test_log_cmd_non_str_tuple_list():
"_log_cmd function casts objects to strings"

class cmd:
def __init__(self, cmd):
self.cmd = cmd

def __str__(self):
return self.cmd

assert cmdmod._log_cmd(cmd("foo bar")) == "foo"

0 comments on commit e6dd6a4

Please sign in to comment.