From d169a6f755f94197e1373982ecf488b8c59322cc Mon Sep 17 00:00:00 2001 From: Renato Alcara Date: Sun, 26 Apr 2026 15:13:42 -0300 Subject: [PATCH] fix(inbound): handle malformed JIDs in postUpsertChatId fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses Copilot review on PR #392. `jidNormalizedUser()` returns an empty string when `jidDecode()` fails (e.g. a malformed JID missing the `@` separator). With the previous ternary `rawChatId ? jidNormalizedUser(rawChatId) : 'unknown'`, a truthy-but-malformed `rawChatId` produced `postUpsertChatId === ''`, so the keyed mutex ran with an empty-string key instead of the intended `'unknown'` fallback bucket — defeating the per-chat serialization and silently lumping malformed-key messages into a single anonymous queue. Compute the normalized value first then apply the fallback only when the result is empty. --- src/Socket/chats.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 85372b9f..31cc052f 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -1465,9 +1465,13 @@ export const makeChatsSocket = (config: SocketConfig) => { // Use getChatId + jidNormalizedUser so the mutex key matches the chat-id // scheme processMessage uses for chat updates (broadcasts target the // participant). Falling back to a constant bucket avoids fragmenting the - // queue with per-message ids when the key is malformed. + // queue when the key is malformed. + // Compute the normalized value first then apply the fallback: a malformed + // but truthy rawChatId (e.g. missing `@`) makes jidNormalizedUser return + // '', which would otherwise pass through as an empty mutex key. const rawChatId = getChatId(msg.key) - const postUpsertChatId = rawChatId ? jidNormalizedUser(rawChatId) : 'unknown' + const normalizedChatId = rawChatId ? jidNormalizedUser(rawChatId) : '' + const postUpsertChatId = normalizedChatId || 'unknown' // Wrap in `postUpsertMutex(chatId)` (a SEPARATE keyed mutex from the outer // `messageMutex` held by the inbound caller) so per-chat ordering of