Skip to content

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.

fix: remember agent IP alerts across panel restarts

Juan Denis1a6335e7cf66
diff --git a/backend/app/services/anomaly_detection_service.py b/backend/app/services/anomaly_detection_service.py
index 0bba3df2..6245f0b9 100644
--- a/backend/app/services/anomaly_detection_service.py
+++ b/backend/app/services/anomaly_detection_service.py
@@ -184,7 +184,14 @@ class AnomalyDetectionService:
"""
with self._lock:
known = self._known_ips[server_id]
- is_new = ip_address not in known
+ if ip_address in known:
+ return False
+ # Resolved/acknowledged alerts still prove this address is known.
+ # Read before populating the cache so a DB failure remains retryable.
+ existing = SecurityAlert.query.filter_by(
+ server_id=server_id, source_ip=ip_address, alert_type='new_ip'
+ ).first()
+ is_new = existing is None
known.add(ip_address)
if is_new and self.THRESHOLDS['new_ip_alert']:
diff --git a/backend/tests/test_new_ip_history.py b/backend/tests/test_new_ip_history.py
new file mode 100644
index 00000000..2a8d0e9f
--- /dev/null
+++ b/backend/tests/test_new_ip_history.py
@@ -0,0 +1,30 @@
+from collections import defaultdict
+import threading
+
+import pytest
+from flask import Flask
+
+from app import db
+from app.models.security_alert import SecurityAlert
+from app.services.anomaly_detection_service import AnomalyDetectionService
+
+
[email protected]('status', ['open', 'acknowledged', 'resolved'])
+def test_known_ip_survives_memory_reset(status):
+ app = Flask(__name__)
+ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
+ db.init_app(app)
+ with app.app_context():
+ db.create_all()
+ db.session.add(SecurityAlert(alert_type='new_ip', source_ip='192.0.2.1', status=status))
+ db.session.commit()
+ # Avoid starting another singleton/background thread for this test.
+ detector = object.__new__(AnomalyDetectionService)
+ detector._lock = threading.Lock()
+ detector._known_ips = defaultdict(set)
+ assert detector.check_new_ip(None, '192.0.2.1') is False
+ assert detector.check_new_ip(None, '192.0.2.2') is True
+ assert detector.check_new_ip(None, '192.0.2.2') is False
+ assert SecurityAlert.query.filter_by(alert_type='new_ip').count() == 2
+ db.session.remove()
+ db.drop_all()