Merge local changes
This commit is contained in:
1 parent
ea29e6942c
commit
3d1fc8a638
12 files changed
+366
-15
No files matched your search
@@ -0,0 +1,77 @@
|
||||
"""Test that administrative API routes require authentication"""
|
||||
|
||||
import pytest
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
|
||||
ADMIN_ENDPOINTS = [
|
||||
("post", "/server/battlegroup/action"),
|
||||
("post", "/server/battlegroup/update"),
|
||||
("get", "/server/firewall"),
|
||||
("post", "/server/firewall/block"),
|
||||
("post", "/server/firewall/unblock"),
|
||||
("get", "/api/chat_logs"),
|
||||
("post", "/api/set_player_ip"),
|
||||
("post", "/api/detect_player_ips"),
|
||||
("post", "/api/ban_player"),
|
||||
("post", "/api/get_player_ban"),
|
||||
("post", "/api/get_player_history"),
|
||||
("post", "/api/unban_player"),
|
||||
("post", "/api/emergency_unban"),
|
||||
("post", "/api/kick_player"),
|
||||
("post", "/api/edit_vitals"),
|
||||
("post", "/api/maintenance/create_indexes"),
|
||||
("delete", "/api/vehicles/1"),
|
||||
("delete", "/api/buildings/1"),
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(tmp_path, monkeypatch):
|
||||
settings_path = tmp_path / "settings.yaml"
|
||||
settings_path.write_text(
|
||||
"""
|
||||
server:
|
||||
host: 127.0.0.1
|
||||
user: dune
|
||||
dashboard:
|
||||
host: 127.0.0.1
|
||||
port: 5050
|
||||
debug: false
|
||||
secret_key: test-secret
|
||||
database:
|
||||
host: 127.0.0.1
|
||||
port: 15433
|
||||
user: postgres
|
||||
password: postgres
|
||||
name: dune
|
||||
kubernetes:
|
||||
namespace: test
|
||||
auth:
|
||||
enabled: true
|
||||
username: admin
|
||||
password_hash: "$argon2id$v=19$m=65536,t=3,p=4$invalid$invalid"
|
||||
logging:
|
||||
file: logs/test.log
|
||||
""",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
monkeypatch.setattr("app.services.database.DatabaseService.ensure_tables", lambda self: None)
|
||||
monkeypatch.setattr("app.services.admin.AdminService.create_indexes", lambda self: (True, [], None))
|
||||
monkeypatch.setattr("app.services.updater.UpdateService.start_checker", lambda self: None)
|
||||
|
||||
from app.factory import create_app
|
||||
app, _ = create_app(str(settings_path))
|
||||
app.config.update(TESTING=True, WTF_CSRF_ENABLED=False)
|
||||
return app.test_client()
|
||||
|
||||
|
||||
@pytest.mark.parametrize(("method", "path"), ADMIN_ENDPOINTS)
|
||||
def test_admin_endpoints_require_login(client, method, path):
|
||||
"""All administrative endpoints should require authentication when auth is enabled."""
|
||||
response = getattr(client, method)(path)
|
||||
assert response.status_code in (302, 401, 403), f"{method.upper()} {path} returned {response.status_code}, expected 302/401/403"
|
||||
@@ -0,0 +1,43 @@
|
||||
"""Test command input validation"""
|
||||
|
||||
import pytest
|
||||
import re
|
||||
|
||||
K8S_NAME_RE = re.compile(r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$")
|
||||
|
||||
|
||||
def require_k8s_name(value, label):
|
||||
"""Validate that a value is a valid Kubernetes resource name."""
|
||||
value = str(value or "").strip()
|
||||
if not value or not K8S_NAME_RE.fullmatch(value):
|
||||
raise ValueError(f"Invalid {label}")
|
||||
return value
|
||||
|
||||
|
||||
class TestK8sNameValidation:
|
||||
def test_valid_k8s_names(self):
|
||||
"""Valid Kubernetes resource names should pass."""
|
||||
assert require_k8s_name('my-app', 'name') == 'my-app'
|
||||
assert require_k8s_name('nginx', 'name') == 'nginx'
|
||||
assert require_k8s_name('api-server-7d9f6', 'name') == 'api-server-7d9f6'
|
||||
assert require_k8s_name('a', 'name') == 'a'
|
||||
assert require_k8s_name('abc123', 'name') == 'abc123'
|
||||
|
||||
def test_invalid_k8s_names(self):
|
||||
"""Invalid Kubernetes resource names should raise ValueError."""
|
||||
with pytest.raises(ValueError):
|
||||
require_k8s_name('', 'name')
|
||||
with pytest.raises(ValueError):
|
||||
require_k8s_name(None, 'name')
|
||||
with pytest.raises(ValueError):
|
||||
require_k8s_name('MyApp', 'name') # uppercase
|
||||
with pytest.raises(ValueError):
|
||||
require_k8s_name('my_app', 'name') # underscore
|
||||
with pytest.raises(ValueError):
|
||||
require_k8s_name('my-app-', 'name') # ends with dash
|
||||
with pytest.raises(ValueError):
|
||||
require_k8s_name('-app', 'name') # starts with dash
|
||||
with pytest.raises(ValueError):
|
||||
require_k8s_name('my app', 'name') # space
|
||||
with pytest.raises(ValueError):
|
||||
require_k8s_name('my*app', 'name') # special char
|
||||
@@ -0,0 +1,56 @@
|
||||
"""Test filebrowser path validation"""
|
||||
|
||||
import pytest
|
||||
import sys
|
||||
import os
|
||||
import posixpath
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
|
||||
FILEBROWSER_BASE_PATH = '/srv'
|
||||
|
||||
|
||||
def _validate_fb_path(path):
|
||||
"""Validate filebrowser path is within allowed directory."""
|
||||
path_str = str(path or "").lstrip("/")
|
||||
if '..' in path_str or '\x00' in path_str:
|
||||
return False
|
||||
normalized = posixpath.normpath("/" + path_str)
|
||||
return normalized == FILEBROWSER_BASE_PATH or normalized.startswith(FILEBROWSER_BASE_PATH + "/")
|
||||
|
||||
|
||||
class TestFileBrowserPathValidation:
|
||||
def test_valid_root_path(self):
|
||||
"""Root /srv should be allowed."""
|
||||
assert _validate_fb_path('/srv') is True
|
||||
assert _validate_fb_path('srv') is True
|
||||
|
||||
def test_valid_subdirectory(self):
|
||||
"""Subdirectories of /srv should be allowed."""
|
||||
assert _validate_fb_path('/srv/config') is True
|
||||
assert _validate_fb_path('/srv/config/settings.ini') is True
|
||||
assert _validate_fb_path('/srv/nested/deep/path') is True
|
||||
|
||||
def test_invalid_false_prefix(self):
|
||||
"""Paths that start with /srv but are not under /srv should be rejected."""
|
||||
assert _validate_fb_path('/srv2') is False
|
||||
assert _validate_fb_path('/srv2/config') is False
|
||||
assert _validate_fb_path('/srvbackup') is False
|
||||
|
||||
def test_invalid_parent_traversal(self):
|
||||
"""Paths with .. should be rejected."""
|
||||
assert _validate_fb_path('/srv/../etc') is False
|
||||
assert _validate_fb_path('/srv/config/../../../etc') is False
|
||||
assert _validate_fb_path('../settings.yaml') is False
|
||||
|
||||
def test_invalid_absolute_path(self):
|
||||
"""Paths outside /srv should be rejected."""
|
||||
assert _validate_fb_path('/etc') is False
|
||||
assert _validate_fb_path('/home') is False
|
||||
assert _validate_fb_path('/') is False
|
||||
|
||||
def test_invalid_null_byte(self):
|
||||
"""Paths with null bytes should be rejected."""
|
||||
assert _validate_fb_path('/srv/config\x00') is False
|
||||
assert _validate_fb_path('/srv/../etc\x00') is False
|
||||
Reference in new issue
Block a user