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.
Python astracloudflarecontrol-panelcyberpaneldnsfablehosting-control-panelhosting-panelkimi-k3server-managementserverkitubuntu-web-panelwebserver
fix(logs): compare whole segments in path guard
Max Freedom Pollard50d64e9d7229
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4585addd..3f798b6a 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -27,6 +27,13 @@ release history; historical `agent-v*` tags are not panel releases.official template registry's CI holds every template to the installer'srules before a merge publishes it.+### Fixed++- Hold the log viewer to its allowed directories. A folder whose name only+ begins with an allowed root, such as `/opt-private` next to `/opt`, was+ treated as though it sat inside that root and could be read, searched and+ cleared through the Log Files tab.+## [1.11.4] - 2026-09-12Covers the 1.11.1 through 1.11.3 version bumps, which shipped only as devdiff --git a/backend/app/services/log_service.py b/backend/app/services/log_service.pyindex 94aa9968..3113ad04 100644--- a/backend/app/services/log_service.py+++ b/backend/app/services/log_service.py@@ -42,9 +42,11 @@ class LogService:# Resolve the absolute path to prevent traversal attacksreal_path = os.path.realpath(filepath)- # Check if the path starts with any allowed directory+ # Compare whole path segments, the way FileService.is_path_allowed+ # does. A bare startswith is a text test, not a containment test:+ # '/opt-private' would pass on the strength of the root '/opt'.for allowed_dir in cls.ALLOWED_LOG_DIRECTORIES:- if real_path.startswith(allowed_dir):+ if real_path == allowed_dir or real_path.startswith(allowed_dir + os.sep):return Truereturn Falsediff --git a/backend/tests/test_log_path_containment.py b/backend/tests/test_log_path_containment.pynew file mode 100644index 00000000..82a856ff--- /dev/null+++ b/backend/tests/test_log_path_containment.py@@ -0,0 +1,81 @@+"""LogService.is_path_allowed must compare whole path segments.++The guard resolved the requested path with realpath and then asked whether the+result merely *started with* one of ALLOWED_LOG_DIRECTORIES. That is a text+test, not a containment test: '/opt-private' starts with the allowed root+'/opt' without sitting inside it, so a sibling directory that only shares a+name prefix was accepted as an allowed log location.++FileService guards the same roots and already gets this right at+file_service.py:96-100, comparing `real_path == root or+real_path.startswith(root + os.sep)`. These tests hold LogService to the same+contract.++The sandbox cases monkeypatch the root list onto a tmp_path so they run+anywhere. The shipped-constant case uses /opt rather than /var/log, because+realpath('/var/log') is '/private/var/log' on a macOS dev box, which would+assert the developer's filesystem instead of the guard.+"""+import os++import pytest++from app.services.log_service import LogService+++def roots(tmp_path, monkeypatch):+ """One allowed root, plus a sibling whose name shares its prefix.++ tmp_path is resolved first so the fixture compares like with like: the+ guard realpaths its argument before matching, and /var is a symlink to+ /private/var on macOS.+ """+ base = os.path.realpath(str(tmp_path))+ allowed = os.path.join(base, 'logroot')+ sibling = os.path.join(base, 'logroot-backup')+ os.makedirs(allowed)+ os.makedirs(sibling)+ monkeypatch.setattr(LogService, 'ALLOWED_LOG_DIRECTORIES', [allowed])+ return {'allowed': allowed, 'sibling': sibling}+++# --------------------------------------------------------------------------- #+# A name prefix is not containment (the actual bug)+# --------------------------------------------------------------------------- #+def test_prefix_sibling_is_not_inside_the_root(roots):+ assert not LogService.is_path_allowed(roots['sibling'])+ assert not LogService.is_path_allowed(+ os.path.join(roots['sibling'], 'app.log'))+++def test_shipped_roots_reject_a_prefix_sibling():+ """The literal constants. /opt is a shipped root and resolves to itself on+ the platforms the panel installs on, so it can be asserted directly."""+ assert '/opt' in LogService.ALLOWED_LOG_DIRECTORIES+ assert not LogService.is_path_allowed('/opt-private')+ assert not LogService.is_path_allowed('/opt-private/creds.txt')+++def test_clear_log_refuses_a_prefix_sibling(roots):+ """The guard gates truncation as well as reading, through clear_log.++ The target deliberately does not exist, so the broken guard falls through+ to 'Log file not found' instead of running truncate against a real file.+ """+ result = LogService.clear_log(os.path.join(roots['sibling'], 'app.log'))++ assert result['success'] is False+ assert result['error'] == 'Access denied: path not in allowed directories'+++# --------------------------------------------------------------------------- #+# Everything genuinely inside a root still resolves+# --------------------------------------------------------------------------- #+def test_paths_inside_the_root_still_pass(roots):+ """Including one reached through '..', which realpath collapses before the+ comparison happens."""+ assert LogService.is_path_allowed(roots['allowed'])+ assert LogService.is_path_allowed(os.path.join(roots['allowed'], 'app.log'))+ assert LogService.is_path_allowed(+ os.path.join(roots['allowed'], 'nested', '..', 'app.log'))