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(security): parse restricted authorized keys
Juan Denis468a68ed961c
diff --git a/backend/app/services/security_service.py b/backend/app/services/security_service.pyindex 87df04a4..a1982cb9 100644--- a/backend/app/services/security_service.py+++ b/backend/app/services/security_service.py@@ -10,6 +10,7 @@ Handles security scanning and monitoring:import osimport json+import shleximport subprocessimport hashlibimport threading@@ -1027,6 +1028,37 @@ class SecurityService:# ==========================================SSH_DIR = '/root/.ssh'AUTHORIZED_KEYS = '/root/.ssh/authorized_keys'+ SSH_KEY_TYPES = frozenset({+ 'ssh-rsa',+ 'ssh-ed25519',+ 'ecdsa-sha2-nistp256',+ 'ecdsa-sha2-nistp384',+ 'ecdsa-sha2-nistp521',+ })++ @classmethod+ def _parse_ssh_public_key(cls, key_line: str) -> Optional[Dict[str, str]]:+ """Return key fields from an authorized_keys line.++ Existing entries may begin with restrictions such as ``from=`` or+ ``command=``. Splitting those lines at fixed indexes mistakes an+ option for the key type and can miss duplicate key data, especially+ when a quoted option contains spaces.+ """+ try:+ parts = shlex.split(key_line, comments=False, posix=True)+ except ValueError:+ return None++ for index, part in enumerate(parts[:-1]):+ if part not in cls.SSH_KEY_TYPES:+ continue+ return {+ 'type': part,+ 'data': parts[index + 1],+ 'comment': ' '.join(parts[index + 2:]),+ }+ return None@classmethoddef get_ssh_keys(cls, user: str = 'root') -> Dict:@@ -1053,11 +1085,11 @@ class SecurityService:continuekey_idx += 1- parts = line.split()- if len(parts) >= 2:- key_type = parts[0]- key_data = parts[1]- comment = ' '.join(parts[2:]) if len(parts) > 2 else ''+ parsed = cls._parse_ssh_public_key(line)+ if parsed:+ key_type = parsed['type']+ key_data = parsed['data']+ comment = parsed['comment']fingerprint = cls._get_key_fingerprint(line)@@ -1095,7 +1127,7 @@ class SecurityService:return {'success': False, 'error': 'Key cannot be empty'}parts = key.split()- if len(parts) < 2 or parts[0] not in ['ssh-rsa', 'ssh-ed25519', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384', 'ecdsa-sha2-nistp521']:+ if len(parts) < 2 or parts[0] not in cls.SSH_KEY_TYPES:return {'success': False, 'error': 'Invalid SSH key format'}if user == 'root':@@ -1116,8 +1148,8 @@ class SecurityService:# text happens to be contained in a longer line is not the# same key.for existing_line in existing.splitlines():- existing_parts = existing_line.split()- if len(existing_parts) >= 2 and existing_parts[1] == parts[1]:+ parsed = cls._parse_ssh_public_key(existing_line)+ if parsed and parsed['data'] == parts[1]:return {'success': False, 'error': 'Key already exists'}with open(auth_keys_path, 'a') as f:diff --git a/backend/tests/test_ssh_keys_roundtrip.py b/backend/tests/test_ssh_keys_roundtrip.pyindex 8e519122..a787e204 100644--- a/backend/tests/test_ssh_keys_roundtrip.py+++ b/backend/tests/test_ssh_keys_roundtrip.py@@ -107,3 +107,19 @@ def test_add_duplicate_matches_key_data_not_substring(auth_keys):# A different key must not be rejected just because its text shares a# prefix with an existing line.assert SecurityService.add_ssh_key(KEY_B)['success']+++def test_add_duplicate_finds_key_data_after_authorized_key_options(auth_keys):+ restricted = (+ 'from="10.0.0.0/8",command="echo backup only" '+ f'{KEY_A}'+ )+ _write(auth_keys, restricted)++ listed = SecurityService.get_ssh_keys()['keys']+ assert listed[0]['type'] == 'ssh-ed25519'+ assert listed[0]['comment'] == 'alice@laptop'++ duplicate = SecurityService.add_ssh_key(KEY_A + ' renamed@host')+ assert not duplicate['success']+ assert duplicate['error'] == 'Key already exists'