Files
romm/backend/handler/socket_handler.py
zurdi 57311dc99a fix: apply the security-audit and code-review passes
Security:
- The netplay socket server shared the main server's Redis channel while
  letting unauthenticated clients name their own rooms, so a netplay socket
  in room `user:N` or `admin` received that user's pushes and the admin log
  stream. Netplay now has a channel of its own.
- Notifying other users also requires users.write on the token, so an admin
  token scoped to me.write can only notify its owner.
- The create payload rejects NUL characters, NaN/Infinity in data and an
  empty recipients list instead of failing in the database; mark-read takes
  at most as many ids as an inbox holds.

Correctness and cleanup:
- The inbox is cleared whenever the signed-in user changes, so a failed
  fetch can't leave the previous user's notifications on screen.
- Enter no longer sends the admin form, where an unfinished title would go
  out to everyone.
- A notification without its own icon shows its level's, in the list and in
  a pushed toast alike, from one LEVEL_ICONS map.
- One write-only Redis manager per event loop instead of one per push.
- Tests for the inbox socket wiring; em-dashes and long docstrings trimmed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 12:47:11 +02:00

29 lines
986 B
Python

import socketio
from config import REDIS_URL
from utils import json_module
class SocketHandler:
def __init__(self, path: str, channel: str = "socketio") -> None:
self.socket_server = socketio.AsyncServer(
cors_allowed_origins="*",
async_mode="asgi",
json=json_module,
logger=False,
engineio_logger=False,
client_manager=socketio.AsyncRedisManager(REDIS_URL, channel=channel),
ping_timeout=60,
ping_interval=25,
max_http_buffer_size=1e6, # 1MB
cors_credentials=True,
)
self.socket_app = socketio.ASGIApp(self.socket_server, socketio_path=path)
socket_handler = SocketHandler(path="/ws/socket.io")
# Netplay clients are unauthenticated and name their own rooms, so they must not
# share a channel where `user:{id}` and `admin` rooms are addressed.
netplay_socket_handler = SocketHandler(path="/netplay/socket.io", channel="netplay")