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.

Validate the journal priority parameter

Juan Denisb35f6df97a64
diff --git a/backend/app/api/logs.py b/backend/app/api/logs.py
index f9625d44..c4c0f41f 100644
--- a/backend/app/api/logs.py
+++ b/backend/app/api/logs.py
@@ -2,6 +2,7 @@ from flask import Blueprint, request, jsonify
from app.middleware.rbac import admin_required
from app.services.log_service import LogService
from app.api._request import json_object
+from app.exceptions import ValidationError
logs_bp = Blueprint('logs', __name__)
@@ -62,7 +63,12 @@ def get_journal_logs():
unit = request.args.get('unit')
lines = request.args.get('lines', 100, type=int)
since = request.args.get('since')
- priority = request.args.get('priority')
+ priority = request.args.get('priority') or None
+ # journalctl levels 0 (emerg) .. 7 (debug); anything else is a client error,
+ # not something to hand to a privileged journalctl.
+ if priority is not None and priority not in {str(n) for n in range(8)}:
+ raise ValidationError('priority must be a journal level from 0 to 7',
+ code='validation.priority')
result = LogService.get_journalctl_logs(unit, lines, since, priority)
return jsonify(result), 200 if result['success'] else 400
diff --git a/backend/tests/test_journal_priority.py b/backend/tests/test_journal_priority.py
new file mode 100644
index 00000000..a35d328c
--- /dev/null
+++ b/backend/tests/test_journal_priority.py
@@ -0,0 +1,30 @@
+"""The journal endpoint only accepts journalctl levels 0-7 as priority."""
+from unittest.mock import patch
+
+import pytest
+
+from factories import headers_for, make_user
+
+OK = {'success': True, 'lines': [], 'source': 'journalctl'}
+
+
+def headers(app):
+ from app import db
+ return headers_for(make_user(db, 'jp_admin', 'admin'))
+
+
[email protected]('value', ['8', '-1', 'err', '--file=/etc/shadow'])
+def test_invalid_priority_is_400(client, headers, value):
+ with patch('app.api.logs.LogService.get_journalctl_logs', return_value=OK) as read:
+ resp = client.get(f'/api/v1/logs/journal?priority={value}', headers=headers)
+ assert resp.status_code == 400
+ assert read.call_count == 0
+
+
[email protected]('value,passed', [('3', '3'), ('', None)])
+def test_valid_or_absent_priority_reaches_journalctl(client, headers, value, passed):
+ with patch('app.api.logs.LogService.get_journalctl_logs', return_value=OK) as read:
+ resp = client.get(f'/api/v1/logs/journal?priority={value}', headers=headers)
+ assert resp.status_code == 200
+ assert read.call_args.args[3] == passed