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.

Reject trailing newline in Postfix install hostname

Juan Denis4eecee8bf0d8
diff --git a/backend/app/services/postfix_service.py b/backend/app/services/postfix_service.py
index 60a14ea0..c5fa4961 100644
--- a/backend/app/services/postfix_service.py
+++ b/backend/app/services/postfix_service.py
@@ -133,7 +133,7 @@ mailbox_size_limit = 0
def install(cls, hostname: str = None) -> Dict:
"""Install Postfix."""
try:
- if hostname and not re.match(r'^[a-zA-Z0-9.-]+$', hostname):
+ if hostname and not re.fullmatch(r'[a-zA-Z0-9.-]+', hostname):
return {'success': False, 'error': 'Invalid hostname format'}
# Pre-seed debconf to avoid interactive prompts. The values are
diff --git a/backend/tests/test_postfix_install_hostname.py b/backend/tests/test_postfix_install_hostname.py
new file mode 100644
index 00000000..ee47cfd0
--- /dev/null
+++ b/backend/tests/test_postfix_install_hostname.py
@@ -0,0 +1,52 @@
+"""Postfix install must never let `hostname` reach a shell (GHSA-mc93-rc3x-fpgq).
+
+The original install concatenated hostname into a `bash -c` string run as
+root. The fix validates the hostname and feeds debconf over stdin; these
+tests pin both halves so the shell form cannot come back.
+"""
+import types
+
+import pytest
+
+from app.services import postfix_service as pf_module
+from app.services.postfix_service import PostfixService
+
+
+def calls(monkeypatch):
+ recorded = []
+
+ def fake_run(cmd, **kwargs):
+ recorded.append((cmd, kwargs))
+ return types.SimpleNamespace(returncode=0, stdout='', stderr='')
+
+ monkeypatch.setattr(pf_module, 'run_privileged', fake_run)
+ monkeypatch.setattr(pf_module.PackageManager, 'detect', staticmethod(lambda: 'apt'))
+ monkeypatch.setattr(pf_module.PackageManager, 'install',
+ staticmethod(lambda *a, **k: types.SimpleNamespace(returncode=1, stdout='', stderr='stop')))
+ return recorded
+
+
[email protected]('hostname', [
+ '"; id > /tmp/serverkit-rce-poc; echo "',
+ 'mail.example.com; touch /tmp/pwned',
+ 'mail.example.com\n',
+ 'mail.example.com\npostfix postfix/main_mailer_type select No configuration',
+ '$(id)',
+ '`id`',
+ 'mail example.com',
+])
+def test_malicious_hostname_is_rejected_before_any_command(calls, hostname):
+ result = PostfixService.install(hostname=hostname)
+
+ assert result == {'success': False, 'error': 'Invalid hostname format'}
+ assert calls == []
+
+
+def test_valid_hostname_goes_to_debconf_via_stdin_not_a_shell(calls):
+ PostfixService.install(hostname='mail.example.com')
+
+ cmd, kwargs = calls[0]
+ assert cmd == ['debconf-set-selections']
+ assert 'postfix postfix/mailname string mail.example.com\n' in kwargs['input']
+ assert not any(c[0][0] in ('bash', 'sh') for c in calls)