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
refactor(security): malware-scan enqueue raises typed errors
Juan Denis0b1fbb0d6d97
diff --git a/backend/app/api/security.py b/backend/app/api/security.pyindex e0083267..32782cca 100644--- a/backend/app/api/security.py+++ b/backend/app/api/security.py@@ -121,10 +121,7 @@ def scan_directory():@admin_requireddef scan_app(app_id):"""Enqueue a job-backed malware scan (YARA + ClamAV) of an app's docroot."""- try:- job = MalwareScanService.enqueue_scan(app_id=app_id)- except ValueError as e:- return jsonify({'error': str(e)}), 404 if 'not found' in str(e) else 400+ job = MalwareScanService.enqueue_scan(app_id=app_id)return jsonify({'job_id': job.id, 'kind': job.kind,'path': (job.get_payload() or {}).get('path')}), 202@@ -136,10 +133,7 @@ def scan_path_job():data = request.get_json()if not data or not data.get('path'):return jsonify({'error': 'Path required'}), 400- try:- job = MalwareScanService.enqueue_scan(path=data['path'])- except ValueError as e:- return jsonify({'error': str(e)}), 400+ job = MalwareScanService.enqueue_scan(path=data['path'])return jsonify({'job_id': job.id, 'kind': job.kind, 'path': data['path']}), 202diff --git a/backend/app/services/malware_scan_service.py b/backend/app/services/malware_scan_service.pyindex 9d0629e2..e0a3ee24 100644--- a/backend/app/services/malware_scan_service.py+++ b/backend/app/services/malware_scan_service.py@@ -28,6 +28,7 @@ from datetime import datetimefrom typing import Dict, Optionalfrom app import paths+from app.exceptions import NotFoundError, ValidationErrorfrom app.utils.system import is_command_availablefrom app.services.security_service import SecurityServicefrom app.services.yara_scan_service import YaraScanService@@ -48,11 +49,11 @@ class MalwareScanService:@classmethoddef enqueue_scan(cls, path: Optional[str] = None, app_id: Optional[int] = None):"""Enqueue a malware scan job for a path or an application docroot.- Returns the Job row. Raises ``ValueError`` on bad input."""+ Returns the Job row. Raises typed application errors on bad input."""from app.jobs.service import JobServiceif not path and not app_id:- raise ValueError('Either path or app_id is required')+ raise ValidationError('Either path or app_id is required')if app_id is not None:# Resolve now so a bad app id fails the request, not the job.path = cls.resolve_app_path(app_id)@@ -75,7 +76,7 @@ class MalwareScanService:# deleted app must fail to resolve rather than scan somebody's path.app_row = Application.query_active().filter_by(id=app_id).first()if not app_row:- raise ValueError(f'Application {app_id} not found')+ raise NotFoundError(f'Application {app_id} not found')candidates = []if app_row.root_path:@@ -87,7 +88,7 @@ class MalwareScanService:if candidate and os.path.isdir(candidate):return candidate- raise ValueError(+ raise NotFoundError(f'No docroot found on disk for application {app_row.name or app_id} 'f'(checked: {", ".join(candidates) or "none"})')diff --git a/backend/tests/STATUS_SNIFFING_CEILING b/backend/tests/STATUS_SNIFFING_CEILINGindex ec635144..45a4fb75 100644--- a/backend/tests/STATUS_SNIFFING_CEILING+++ b/backend/tests/STATUS_SNIFFING_CEILING@@ -1 +1 @@-9+8diff --git a/backend/tests/test_malware_scan_job.py b/backend/tests/test_malware_scan_job.pyindex 806f2608..ba4d60d4 100644--- a/backend/tests/test_malware_scan_job.py+++ b/backend/tests/test_malware_scan_job.py@@ -10,6 +10,8 @@ from unittest.mock import patchimport pytest+from app.exceptions import NotFoundError+from app.jobs.models import Jobfrom app.jobs.service import JobService, GROUP_SLUG, QUEUE_SLUGfrom app.jobs.consumer import JobConsumer@@ -85,14 +87,14 @@ class TestAppDocrootResolution:assert MalwareScanService.resolve_app_path(application.id) == str(docroot)def test_unknown_app_raises(self, app):- with pytest.raises(ValueError, match='not found'):+ with pytest.raises(NotFoundError, match='not found'):MalwareScanService.resolve_app_path(999999)def test_missing_docroot_raises(self, app, tmp_path):row = _make_app_row('ghost', str(tmp_path / 'gone'))with patch('app.services.malware_scan_service.paths.APPS_DIR',str(tmp_path / 'no-apps')):- with pytest.raises(ValueError, match='No docroot'):+ with pytest.raises(NotFoundError, match='No docroot'):MalwareScanService.resolve_app_path(row.id)