Security fix
This commit is contained in:
@@ -71,7 +71,7 @@ def on_message(client, userdata, msg):
|
||||
state.node_logs[cid].append(log_entry)
|
||||
|
||||
# Inoltra in tempo reale via WebSocket
|
||||
state.socketio.emit(f"live_log_{cid}", log_entry)
|
||||
state.emit_to_authorized(f"live_log_{cid}", log_entry, cid=cid)
|
||||
|
||||
# B) Parsa le versioni
|
||||
if cid not in state.node_versions:
|
||||
@@ -134,7 +134,7 @@ def on_message(client, userdata, msg):
|
||||
|
||||
state.aprs_msgs.append(aprs_data)
|
||||
logger.info(f"📍 APRS (da log {svc_name}): {src_part} -> {dest_part}")
|
||||
state.socketio.emit('aprs_msg', aprs_data)
|
||||
state.emit_aprs(aprs_data)
|
||||
|
||||
# NOTA: Rimosso il return incondizionato qui, perché il log deve completare la sua corsa
|
||||
# e non bloccare eventuali altre logiche!
|
||||
@@ -399,7 +399,7 @@ def on_message(client, userdata, msg):
|
||||
from collections import deque
|
||||
state.aprs_msgs = deque(maxlen=100)
|
||||
state.aprs_msgs.append(data)
|
||||
state.socketio.emit('aprs_msg', data)
|
||||
state.emit_aprs(data)
|
||||
except Exception as e:
|
||||
logger.error(f"Errore parsing APRS: {e}")
|
||||
|
||||
@@ -527,6 +527,16 @@ def on_message(client, userdata, msg):
|
||||
mqtt_id = config.get('mqtt', {}).get('client_id', 'flask_backend_generic')
|
||||
mqtt_backend = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION2, mqtt_id)
|
||||
mqtt_backend.username_pw_set(config.get('mqtt', {}).get('user', ''), config.get('mqtt', {}).get('password', ''))
|
||||
# TLS opzionale (default: disattivato, comportamento invariato). In config.json:
|
||||
# "mqtt": { ..., "port": 8883, "tls": true, "ca_certs": "/etc/ssl/certs/ca-certificates.crt" }
|
||||
if config.get('mqtt', {}).get('tls'):
|
||||
import ssl
|
||||
try:
|
||||
mqtt_backend.tls_set(ca_certs=config.get('mqtt', {}).get('ca_certs') or None,
|
||||
cert_reqs=ssl.CERT_REQUIRED)
|
||||
logger.info("🔒 MQTT: TLS abilitato")
|
||||
except Exception as e:
|
||||
logger.error(f"MQTT TLS non configurabile: {e}")
|
||||
mqtt_backend.on_connect = on_connect
|
||||
mqtt_backend.on_disconnect = on_disconnect
|
||||
mqtt_backend.on_message = on_message
|
||||
|
||||
@@ -4,17 +4,10 @@ from contextlib import closing
|
||||
from pywebpush import webpush, WebPushException
|
||||
from core.config import config, logger
|
||||
from core.database import db_connect
|
||||
from core.permissions import node_allowed # noqa: F401 (re-export per compatibilita')
|
||||
|
||||
push_queue = queue.Queue(maxsize=200)
|
||||
|
||||
def node_allowed(role, allowed_nodes, cid):
|
||||
if role == 'admin':
|
||||
return True
|
||||
allowed = (allowed_nodes or '').strip().lower()
|
||||
if allowed == 'all':
|
||||
return True
|
||||
return cid in [x.strip().lower() for x in allowed.split(',') if x.strip()]
|
||||
|
||||
def broadcast_push_notification(title, body, cid=None):
|
||||
if not config.get('webpush'):
|
||||
return
|
||||
|
||||
@@ -45,17 +45,33 @@ def auto_update_ids():
|
||||
download_logic()
|
||||
prune_old_data()
|
||||
time.sleep(65)
|
||||
|
||||
# Controllo schedulazioni profili
|
||||
except Exception as e:
|
||||
logger.error(f"⚠️ Errore nel thread aggiornamento ID: {e}")
|
||||
time.sleep(15)
|
||||
|
||||
def schedule_loop():
|
||||
"""Thread dedicato alle schedulazioni: non e' piu' bloccato dai download degli ID."""
|
||||
while True:
|
||||
try:
|
||||
check_profile_schedules()
|
||||
except Exception as e:
|
||||
logger.error(f"⚠️ Errore nel thread scheduler: {e}")
|
||||
logger.error(f"⚠️ Errore nel thread schedulazioni: {e}")
|
||||
time.sleep(15)
|
||||
|
||||
from contextlib import closing
|
||||
from core.database import db_connect, audit
|
||||
|
||||
def apply_scheduled_profile(target_nodes, profile, action_type="SWITCH"):
|
||||
# Comandi che le API riservano agli admin: una schedulazione li puo' inviare solo se creata da un admin.
|
||||
PRIVILEGED_COMMANDS = {'REBOOT', 'RESET_HAT', 'UPDATE_AGENT'}
|
||||
|
||||
def _is_admin(conn, username):
|
||||
row = conn.execute("SELECT role FROM users WHERE username = ?", (username,)).fetchone()
|
||||
return bool(row) and row[0] == 'admin'
|
||||
|
||||
def apply_scheduled_profile(target_nodes, profile, action_type="SWITCH", allow_privileged=False):
|
||||
if str(profile).strip().upper() in PRIVILEGED_COMMANDS and not allow_privileged:
|
||||
logger.warning(f"🚫 Scheduler: comando privilegiato '{profile}' ignorato (schedulazione non creata da un admin)")
|
||||
return
|
||||
from services.mqtt_service import mqtt_publish
|
||||
from core.config import CLIENTS_PATH
|
||||
import json
|
||||
@@ -111,7 +127,7 @@ def check_profile_schedules():
|
||||
if status == 'SCHEDULED':
|
||||
if st_norm <= now_str < end_norm:
|
||||
logger.info(f"▶️ Avvio schedulazione ID {sid} - '{title}' -> Profilo {target_prof}")
|
||||
apply_scheduled_profile(nodes, target_prof, "START")
|
||||
apply_scheduled_profile(nodes, target_prof, "START", allow_privileged=_is_admin(conn, row['created_by']))
|
||||
conn.execute("UPDATE profile_schedules SET status = 'ACTIVE' WHERE id = ?", (sid,))
|
||||
conn.commit()
|
||||
audit("SCHEDULER", nodes, f"START_SCHED_{sid}_{target_prof}")
|
||||
@@ -125,7 +141,7 @@ def check_profile_schedules():
|
||||
if now_str >= end_norm:
|
||||
logger.info(f"⏹️ Fine schedulazione ID {sid} - '{title}' -> Ripristino {restore_prof}")
|
||||
if restore_prof and restore_prof.upper() != 'NONE':
|
||||
apply_scheduled_profile(nodes, restore_prof, "RESTORE")
|
||||
apply_scheduled_profile(nodes, restore_prof, "RESTORE", allow_privileged=_is_admin(conn, row['created_by']))
|
||||
conn.execute("UPDATE profile_schedules SET status = 'COMPLETED' WHERE id = ?", (sid,))
|
||||
conn.commit()
|
||||
audit("SCHEDULER", nodes, f"RESTORE_SCHED_{sid}_{restore_prof}")
|
||||
|
||||
Reference in New Issue
Block a user