From c83eeec41f18d931fec0d3602542ee00d2735edf Mon Sep 17 00:00:00 2001 From: kc1awv Date: Sun, 4 Jan 2026 16:56:57 -0500 Subject: [PATCH] refactor nickname handling to preserve client-provided nicknames and improve validation --- rrcd/service.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/rrcd/service.py b/rrcd/service.py index cb437bd..b8d4a9c 100644 --- a/rrcd/service.py +++ b/rrcd/service.py @@ -3735,15 +3735,24 @@ class HubService: ) env[K_ROOM] = r - # Backwards-compatible extension: hub can attach the nickname learned - # from HELLO so clients can render a human-friendly name. - nick = sess.get("nick") - n = normalize_nick(nick, max_chars=self.config.nick_max_chars) - if n is not None: - env[K_NICK] = n + # Preserve the nickname from the incoming envelope if present. + # Fall back to session nickname (from HELLO) if client didn't provide one. + # This allows clients to update their nickname mid-session. + incoming_nick = env.get(K_NICK) + if incoming_nick is not None: + # Client provided a nickname in this message - validate and preserve it + n = normalize_nick(incoming_nick, max_chars=self.config.nick_max_chars) + if n is not None: + env[K_NICK] = n + else: + # Invalid nickname provided - remove it + env.pop(K_NICK, None) else: - # Prevent client-supplied spoofed nicknames. - env.pop(K_NICK, None) + # No nickname in message - use session nickname from HELLO if available + nick = sess.get("nick") + n = normalize_nick(nick, max_chars=self.config.nick_max_chars) + if n is not None: + env[K_NICK] = n payload = encode(env) for other in list(self.rooms.get(r, set())):