Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/68780.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Guard against non-iterable `Target` in cached job entries when filtering by `search_target` in `salt-run jobs.list_jobs`.
9 changes: 9 additions & 0 deletions salt/runners/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,15 @@ def list_jobs(
targets = ret[item]["Target"]
if isinstance(targets, str):
targets = [targets]
elif hasattr(targets, "__iter__"):
targets = list(targets)
else:
log.warning(
"Job %s has a non-iterable Target value %r; skipping",
item,
targets,
)
Comment on lines +337 to +344

@bdrx312 bdrx312 Jul 10, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe just expect type can be converted in the nominal case:

                try:
                    targets = list(targets)
                except TypeError:
                    log.warning(
                        "Job %s has a non-iterable Target value %r; skipping",
                        item,
                        targets,
                    )
                    targets = []

targets = []
for target in targets:
for key in salt.utils.args.split_input(search_target):
if fnmatch.fnmatch(target, key):
Expand Down
84 changes: 84 additions & 0 deletions tests/pytests/unit/runners/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,87 @@ def __init__(self, *args, **kwargs):
assert jobs.list_jobs(search_target="node-1-2.com") == returns["node-1-2.com"]

assert jobs.list_jobs(search_target="non-existant") == returns["non-existant"]


def test_list_jobs_with_non_iterable_target():
"""
test jobs.list_jobs does not crash when Target is not iterable (e.g. int)

Regression test for https://github.com/saltstack/salt/issues/68780
"""
mock_jobs_cache = {
"20160524035503086853": {
"Arguments": [],
"Function": "test.ping",
"StartTime": "2016, May 24 03:55:03.086853",
"Target": 3,
"Target-type": "glob",
"User": "root",
},
"20160524035524895387": {
"Arguments": [],
"Function": "test.ping",
"StartTime": "2016, May 24 03:55:24.895387",
"Target": "node-1-1.com",
"Target-type": "glob",
"User": "sudo_ubuntu",
},
}

def return_mock_jobs():
return mock_jobs_cache

class MockMasterMinion:

returners = {"local_cache.get_jids": return_mock_jobs}

def __init__(self, *args, **kwargs):
pass

with patch.object(salt.minion, "MasterMinion", MockMasterMinion):
# Should not raise TypeError; the non-iterable target job is skipped
result = jobs.list_jobs(search_target="node-1-1.com")
assert "20160524035524895387" in result
assert "20160524035503086853" not in result


def test_list_jobs_with_set_target():
"""
test jobs.list_jobs handles Target stored as a set (any non-str iterable)

Regression test for https://github.com/saltstack/salt/issues/68780
"""
mock_jobs_cache = {
"20160524035503086853": {
"Arguments": [],
"Function": "test.ping",
"StartTime": "2016, May 24 03:55:03.086853",
"Target": {"node-1-1.com", "node-1-2.com"},
"Target-type": "list",
"User": "root",
},
"20160524035524895387": {
"Arguments": [],
"Function": "test.ping",
"StartTime": "2016, May 24 03:55:24.895387",
"Target": "node-2-1.com",
"Target-type": "glob",
"User": "sudo_ubuntu",
},
}

def return_mock_jobs():
return mock_jobs_cache

class MockMasterMinion:

returners = {"local_cache.get_jids": return_mock_jobs}

def __init__(self, *args, **kwargs):
pass

with patch.object(salt.minion, "MasterMinion", MockMasterMinion):
# Should not raise TypeError; set target should be iterated correctly
result = jobs.list_jobs(search_target="node-1-1.com")
assert "20160524035503086853" in result
assert "20160524035524895387" not in result