Security fix
This commit is contained in:
@@ -6,6 +6,7 @@ import sqlite3
|
||||
from contextlib import closing
|
||||
from flask_socketio import SocketIO
|
||||
from core.config import CACHE_FILE, DB_PATH, DMR_IDS_PATH, NXDN_IDS_PATH, logger, web_cfg
|
||||
from core.permissions import node_allowed
|
||||
|
||||
# --- WEBSOCKET SOCKETIO ---
|
||||
socketio = SocketIO(cors_allowed_origins=web_cfg.get('cors_origins', '*'))
|
||||
@@ -51,6 +52,35 @@ operating_modes = {}
|
||||
node_logs = {} # In-memory circular buffer for live logs
|
||||
connected_web_clients = set()
|
||||
active_logged_users = {}
|
||||
socket_auth = {} # sid -> {"username","role","allowed_nodes","ts"} (solo socket con sessione valida)
|
||||
auth_lookup = None # impostato da app.py: username -> dict auth aggiornato oppure None
|
||||
|
||||
AUTH_REFRESH_SEC = 120
|
||||
|
||||
def emit_to_authorized(event, data, cid=None):
|
||||
"""Invia l'evento solo ai socket autenticati (e, se cid e' dato, autorizzati su quel nodo)."""
|
||||
now = time.time()
|
||||
for sid, a in list(socket_auth.items()):
|
||||
try:
|
||||
# Rilegge periodicamente ruolo/nodi: utenti rimossi o declassati perdono l'accesso live
|
||||
if auth_lookup and now - a.get('ts', 0) > AUTH_REFRESH_SEC:
|
||||
fresh = auth_lookup(a['username'])
|
||||
if not fresh:
|
||||
socket_auth.pop(sid, None)
|
||||
continue
|
||||
a.update(fresh)
|
||||
a['ts'] = now
|
||||
if cid is None or node_allowed(a.get('role'), a.get('allowed_nodes'), cid):
|
||||
socketio.emit(event, data, to=sid)
|
||||
except Exception as e:
|
||||
logger.error(f"emit_to_authorized '{event}': {e}")
|
||||
|
||||
def emit_aprs(data):
|
||||
"""APRS live: broadcast come prima, oppure solo utenti loggati se web.require_login_for_read."""
|
||||
if web_cfg.get('require_login_for_read'):
|
||||
emit_to_authorized('aprs_msg', data)
|
||||
else:
|
||||
socketio.emit('aprs_msg', data)
|
||||
|
||||
# Caricamento iniziale cache telemetria
|
||||
if os.path.exists(CACHE_FILE):
|
||||
|
||||
Reference in New Issue
Block a user