Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(system): put a ceiling on every privileged subprocess call

Juan Denis54a9b2d391d0
diff --git a/backend/app/utils/system.py b/backend/app/utils/system.py
index 4baac2a5..023e44cf 100644
--- a/backend/app/utils/system.py
+++ b/backend/app/utils/system.py
@@ -57,6 +57,17 @@ def privileged_cmd(cmd: Union[List[str], str], *, user: Optional[str] = None) ->
return cmd
+# Ceiling for a privileged command that does not name its own. Generous enough
+# for the slow-but-legitimate work that runs through here (package installs,
+# image pulls), while guaranteeing no single call can wedge a worker forever.
+# Anything genuinely longer must say so explicitly with `timeout=`.
+DEFAULT_PRIVILEGED_TIMEOUT = 300
+
+# Read-only status probes (is this package installed, is this unit active).
+# They answer immediately or not at all, so they get a much tighter ceiling.
+PROBE_TIMEOUT = 30
+
+
def run_privileged(cmd: Union[List[str], str], *, user: Optional[str] = None, **kwargs) -> subprocess.CompletedProcess:
"""Run a command with sudo if the current process is not root.
@@ -64,12 +75,19 @@ def run_privileged(cmd: Union[List[str], str], *, user: Optional[str] = None, **
Pass *user* to run the command as a specific user (``sudo -u <user>``).
Defaults to ``capture_output=True, text=True`` but callers can override.
+ A default ``timeout`` is applied when the caller does not give one: with
+ output captured and no terminal, a command that never returns takes its
+ caller with it silently and forever. ``TimeoutExpired`` is a far better
+ outcome than a wedged request or a boot that never finishes — pass
+ ``timeout=None`` to opt out deliberately.
+
Returns the raw ``CompletedProcess`` so services keep their existing
error-handling patterns.
"""
cmd = privileged_cmd(cmd, user=user)
kwargs.setdefault('capture_output', True)
kwargs.setdefault('text', True)
+ kwargs.setdefault('timeout', DEFAULT_PRIVILEGED_TIMEOUT)
return subprocess.run(cmd, **kwargs)
@@ -163,7 +181,7 @@ class PackageManager:
try:
result = subprocess.run(
['dpkg', '-s', package],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=PROBE_TIMEOUT,
)
return (
result.returncode == 0
@@ -176,7 +194,7 @@ class PackageManager:
try:
result = subprocess.run(
['rpm', '-q', package],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=PROBE_TIMEOUT,
)
return result.returncode == 0
except FileNotFoundError:
@@ -244,7 +262,7 @@ class ServiceControl:
try:
result = subprocess.run(
['systemctl', 'is-active', service],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=PROBE_TIMEOUT,
)
return result.stdout.strip() == 'active'
except FileNotFoundError:
@@ -256,7 +274,7 @@ class ServiceControl:
try:
result = subprocess.run(
['systemctl', 'is-enabled', service],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=PROBE_TIMEOUT,
)
return result.stdout.strip() == 'enabled'
except FileNotFoundError:
diff --git a/backend/tests/test_utils_system.py b/backend/tests/test_utils_system.py
index c950c7b8..ee7d2e20 100644
--- a/backend/tests/test_utils_system.py
+++ b/backend/tests/test_utils_system.py
@@ -58,7 +58,7 @@ class TestRunPrivileged:
run_privileged(['systemctl', 'restart', 'nginx'])
mock_run.assert_called_once_with(
['sudo', '-n', 'systemctl', 'restart', 'nginx'],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=_module.DEFAULT_PRIVILEGED_TIMEOUT,
)
@patch('app.utils.system.subprocess.run')
@@ -68,7 +68,7 @@ class TestRunPrivileged:
run_privileged(['systemctl', 'restart', 'nginx'])
mock_run.assert_called_once_with(
['systemctl', 'restart', 'nginx'],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=_module.DEFAULT_PRIVILEGED_TIMEOUT,
)
@patch('app.utils.system.subprocess.run')
@@ -80,7 +80,7 @@ class TestRunPrivileged:
run_privileged(['sudo', 'systemctl', 'restart', 'nginx'])
mock_run.assert_called_once_with(
['sudo', 'systemctl', 'restart', 'nginx'],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=_module.DEFAULT_PRIVILEGED_TIMEOUT,
)
@patch('app.utils.system.subprocess.run')
@@ -101,6 +101,25 @@ class TestRunPrivileged:
assert kwargs['timeout'] == 120
assert kwargs['check'] is True
+ # Output is captured and no terminal is attached, so a command that never
+ # returns would hang its caller silently and forever. Every call gets a
+ # ceiling unless one is named explicitly.
+ @patch('app.utils.system.subprocess.run')
+ @patch('app.utils.system.os.geteuid', return_value=1000, create=True)
+ def test_default_timeout_applied(self, _euid, mock_run):
+ mock_run.return_value = subprocess.CompletedProcess([], 0)
+ run_privileged(['systemctl', 'restart', 'nginx'])
+ _, kwargs = mock_run.call_args
+ assert kwargs['timeout'] == _module.DEFAULT_PRIVILEGED_TIMEOUT
+
+ @patch('app.utils.system.subprocess.run')
+ @patch('app.utils.system.os.geteuid', return_value=1000, create=True)
+ def test_timeout_can_be_disabled_explicitly(self, _euid, mock_run):
+ mock_run.return_value = subprocess.CompletedProcess([], 0)
+ run_privileged(['tail', '-f', '/var/log/syslog'], timeout=None)
+ _, kwargs = mock_run.call_args
+ assert kwargs['timeout'] is None
+
@patch('app.utils.system.subprocess.run')
@patch('app.utils.system.os.geteuid', return_value=1000, create=True)
def test_caller_can_override_defaults(self, _euid, mock_run):
@@ -223,7 +242,7 @@ class TestPackageManager:
)
assert PackageManager.is_installed('nginx') is True
mock_run.assert_called_once_with(
- ['dpkg', '-s', 'nginx'], capture_output=True, text=True,
+ ['dpkg', '-s', 'nginx'], capture_output=True, text=True, timeout=_module.PROBE_TIMEOUT,
)
@patch('app.utils.system.subprocess.run')
@@ -297,7 +316,7 @@ class TestServiceControl:
ServiceControl.start('nginx')
mock_run.assert_called_once_with(
['sudo', '-n', 'systemctl', 'start', 'nginx'],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=_module.DEFAULT_PRIVILEGED_TIMEOUT,
)
@patch('app.utils.system.os.name', 'posix')
@@ -309,7 +328,7 @@ class TestServiceControl:
ServiceControl.stop('nginx')
mock_run.assert_called_once_with(
['sudo', '-n', 'systemctl', 'stop', 'nginx'],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=_module.DEFAULT_PRIVILEGED_TIMEOUT,
)
@patch('app.utils.system.os.name', 'posix')
@@ -321,7 +340,7 @@ class TestServiceControl:
ServiceControl.restart('nginx')
mock_run.assert_called_once_with(
['sudo', '-n', 'systemctl', 'restart', 'nginx'],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=_module.DEFAULT_PRIVILEGED_TIMEOUT,
)
@patch('app.utils.system.os.name', 'posix')
@@ -333,7 +352,7 @@ class TestServiceControl:
ServiceControl.reload('nginx')
mock_run.assert_called_once_with(
['sudo', '-n', 'systemctl', 'reload', 'nginx'],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=_module.DEFAULT_PRIVILEGED_TIMEOUT,
)
@patch('app.utils.system.os.name', 'posix')
@@ -345,7 +364,7 @@ class TestServiceControl:
ServiceControl.enable('nginx')
mock_run.assert_called_once_with(
['sudo', '-n', 'systemctl', 'enable', 'nginx'],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=_module.DEFAULT_PRIVILEGED_TIMEOUT,
)
@patch('app.utils.system.os.name', 'posix')
@@ -357,7 +376,7 @@ class TestServiceControl:
ServiceControl.disable('nginx')
mock_run.assert_called_once_with(
['sudo', '-n', 'systemctl', 'disable', 'nginx'],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=_module.DEFAULT_PRIVILEGED_TIMEOUT,
)
@patch('app.utils.system.os.name', 'posix')
@@ -369,7 +388,7 @@ class TestServiceControl:
ServiceControl.daemon_reload()
mock_run.assert_called_once_with(
['sudo', '-n', 'systemctl', 'daemon-reload'],
- capture_output=True, text=True,
+ capture_output=True, text=True, timeout=_module.DEFAULT_PRIVILEGED_TIMEOUT,
)
@patch('app.utils.system.subprocess.run')