Skip to content

Commit 6f4ec53

Browse files
committed
Merge branch 'GHSA-vcv2-q258-wrg7' into develop
2 parents 61d38ee + 5680a5d commit 6f4ec53

File tree

4 files changed

+416
-9
lines changed

4 files changed

+416
-9
lines changed

docs/aoa/actions.rst

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,39 @@ reached:
3333
3434
[fs]
3535
warning=70
36-
warning_action=echo "{{time}} {{mnt_point}} {{used}}/{{size}}" > /tmp/fs.alert
36+
warning_action=python /path/to/fs-warning.py {{mnt_point}} {{used}} {{size}}
3737
38-
A last example would be to create a log file containing the total user disk
38+
.. note::
39+
40+
For security reasons, Mustache-rendered values are sanitized: the
41+
characters ``&&``, ``|``, ``>`` and ``>>`` are replaced by spaces
42+
before execution. This prevents command injection through
43+
user-controllable data such as process names, container names or
44+
mount points.
45+
46+
As a consequence, **shell operators (pipes, redirections, command
47+
chaining) cannot be used directly in action command lines**. If your
48+
action requires pipes, redirections or chained commands, write a
49+
shell script and call it from the action instead.
50+
51+
For example, to create a log file containing the total user disk
3952
space usage for a device and notify by email each time a space trigger
40-
critical is reached:
53+
critical is reached, create a shell script ``/etc/glances/actions.d/fs-critical.sh``:
54+
55+
.. code-block:: bash
56+
57+
#!/bin/bash
58+
# Usage: fs-critical.sh <time> <device_name> <percent>
59+
echo "$1 $2 $3" > /tmp/fs.alert
60+
python /etc/glances/actions.d/fs-critical.py
61+
62+
Then reference it in the configuration file:
4163

4264
.. code-block:: ini
4365
4466
[fs]
4567
critical=90
46-
critical_action_repeat=echo "{{time}} {{device_name}} {{percent}}" > /tmp/fs.alert && python /etc/glances/actions.d/fs-critical.py
47-
48-
.. note::
49-
Use && as separator for multiple commands
68+
critical_action_repeat=/etc/glances/actions.d/fs-critical.sh {{time}} {{device_name}} {{percent}}
5069
5170
Within ``/etc/glances/actions.d/fs-critical.py``:
5271

docs/aoa/containers.rst

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ under the ``[containers]`` section:
4545
containername_cpu_careful=10
4646
containername_cpu_warning=20
4747
containername_cpu_critical=30
48-
containername_cpu_critical_action=echo {{Image}} {{Id}} {{cpu}} > /tmp/container_{{name}}.alert
48+
containername_cpu_critical_action=/etc/glances/actions.d/container-alert.sh {{Image}} {{Id}} {{cpu}} {{name}}
4949
# By default, Glances only display running containers
5050
# Set the following key to True to display all containers
5151
all=False
@@ -54,6 +54,21 @@ under the ``[containers]`` section:
5454
5555
You can use all the variables ({{foo}}) available in the containers plugin.
5656

57+
.. note::
58+
59+
Shell operators (``&&``, ``|``, ``>``, ``>>``) are **not allowed**
60+
directly in action command lines. If your action requires pipes or
61+
redirections, write a shell script and call it from the action.
62+
For example, create ``/etc/glances/actions.d/container-alert.sh``:
63+
64+
.. code-block:: bash
65+
66+
#!/bin/bash
67+
# Usage: container-alert.sh <image> <id> <cpu> <name>
68+
echo "$1 $2 $3" > "/tmp/container_$4.alert"
69+
70+
See :ref:`actions` for details.
71+
5772
Filtering (for hide or show) is based on regular expression. Please be sure that your regular
5873
expression works as expected. You can use an online tool like `regex101`_ in
5974
order to test your regular expression.

glances/actions.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,31 @@
2020
else:
2121
chevron_tag = True
2222

23+
# Characters that secure_popen interprets as shell operators.
24+
# Mustache-rendered values must not contain these to prevent command injection.
25+
_SHELL_OPERATORS = ('&&', '|', '>>', '>')
26+
27+
28+
def _sanitize_mustache_dict(mustache_dict):
29+
"""Return a copy of mustache_dict with shell operators replaced by spaces.
30+
31+
This prevents command injection when user-controllable data (process names,
32+
container names, mount points, etc.) is rendered into action command lines
33+
via Mustache templates.
34+
"""
35+
if not mustache_dict:
36+
return mustache_dict
37+
38+
safe = {}
39+
for k, v in mustache_dict.items():
40+
if isinstance(v, str):
41+
for op in _SHELL_OPERATORS:
42+
v = v.replace(op, ' ')
43+
safe[k] = v
44+
else:
45+
safe[k] = v
46+
return safe
47+
2348

2449
class GlancesActions:
2550
"""This class manage action if an alert is reached."""
@@ -75,7 +100,9 @@ def run(self, stat_name, criticality, commands, repeat, mustache_dict=None):
75100
for cmd in commands:
76101
# Replace {{arg}} by the dict one (Thk to {Mustache})
77102
if chevron_tag:
78-
cmd_full = chevron.render(cmd, mustache_dict)
103+
# Sanitize mustache values to prevent shell operator injection
104+
safe_dict = _sanitize_mustache_dict(mustache_dict)
105+
cmd_full = chevron.render(cmd, safe_dict)
79106
else:
80107
cmd_full = cmd
81108
# Execute the action

0 commit comments

Comments
 (0)