Files
ServerKit/backend/tests/test_journal_priority.py
Juan Denis b35f6df97a Validate the journal priority parameter
The value went straight to a privileged journalctl -p. As its own argv
element it could not inject options, but anything other than levels
0-7 now gets a 400 instead of a journalctl error.
2026-10-10 20:54:45 -04:00

31 lines
1.1 KiB
Python

"""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'}
@pytest.fixture
def headers(app):
from app import db
return headers_for(make_user(db, 'jp_admin', 'admin'))
@pytest.mark.parametrize('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
@pytest.mark.parametrize('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