From ff9b84c561b17a084a7bc1656ea5b662f37c0edf Mon Sep 17 00:00:00 2001 From: Renato Alcara Date: Mon, 16 Mar 2026 22:59:28 -0300 Subject: [PATCH 1/4] fix: unify PN and LID session reuse for 1:1 sends (#293) fix: unify PN and LID session reuse for 1:1 sends (#293) --- src/Socket/messages-send.ts | 89 +++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index 992aa1f8..4ddae005 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -417,6 +417,39 @@ export const makeMessagesSocket = (config: SocketConfig) => { ) } + const resolveSessionJid = async (jid: string) => { + if (isLidUser(jid) || isHostedLidUser(jid)) { + return jid + } + + if (isPnUser(jid) || isHostedPnUser(jid)) { + return await signalRepository.lidMapping.getLIDForPN(jid) || jid + } + + return jid + } + + const canonicalizeSessionRecipients = async (recipientJids: string[]) => { + const uniqueRecipients = [...new Set(recipientJids)] + const canonicalRecipients: string[] = [] + + for (const jid of uniqueRecipients) { + const canonicalJid = await resolveSessionJid(jid) + if (!canonicalRecipients.includes(canonicalJid)) { + canonicalRecipients.push(canonicalJid) + } + } + + if (canonicalRecipients.length !== uniqueRecipients.length || canonicalRecipients.some((jid, index) => jid !== uniqueRecipients[index])) { + logger.debug( + { before: uniqueRecipients, after: canonicalRecipients }, + '[SESSION] Canonicalized recipient addressing for session reuse' + ) + } + + return canonicalRecipients + } + const assertSessions = async (jids: string[], force?: boolean) => { let didFetchNewSession = false const uniqueJids = [...new Set(jids)] // Deduplicate JIDs @@ -426,22 +459,32 @@ export const makeMessagesSocket = (config: SocketConfig) => { // Check peerSessionsCache and validate sessions using libsignal loadSession for (const jid of uniqueJids) { - const signalId = signalRepository.jidToSignalProtocolAddress(jid) - const cachedSession = peerSessionsCache.get(signalId) + const canonicalJid = await resolveSessionJid(jid) + const signalIds = [ + signalRepository.jidToSignalProtocolAddress(jid), + signalRepository.jidToSignalProtocolAddress(canonicalJid) + ] + const cachedSession = signalIds + .map(signalId => peerSessionsCache.get(signalId)) + .find(session => session !== undefined) + if (cachedSession !== undefined) { if (cachedSession && !force) { continue // Session exists in cache } } else { - const sessionValidation = await signalRepository.validateSession(jid) + const sessionValidation = await signalRepository.validateSession(canonicalJid) const hasSession = sessionValidation.exists - peerSessionsCache.set(signalId, hasSession) + for (const signalId of signalIds) { + peerSessionsCache.set(signalId, hasSession) + } + if (hasSession && !force) { continue } } - jidsRequiringFetch.push(jid) + jidsRequiringFetch.push(canonicalJid) } if (jidsRequiringFetch.length) { @@ -682,33 +725,6 @@ export const makeMessagesSocket = (config: SocketConfig) => { return false } - const canonicalizeCarouselRecipients = async (recipientJids: string[]) => { - const uniqueRecipients = [...new Set(recipientJids)] - const pnRecipients = uniqueRecipients.filter(jid => isPnUser(jid) || isHostedPnUser(jid)) - const lidMappings = pnRecipients.length > 0 - ? await signalRepository.lidMapping.getLIDsForPNs(pnRecipients) - : [] - - const lidByPn = new Map((lidMappings || []).map(({ pn, lid }) => [pn, lid])) - const canonicalRecipients: string[] = [] - - for (const jid of uniqueRecipients) { - const canonicalJid = lidByPn.get(jid) || jid - if (!canonicalRecipients.includes(canonicalJid)) { - canonicalRecipients.push(canonicalJid) - } - } - - if (canonicalRecipients.length !== uniqueRecipients.length || canonicalRecipients.some((jid, index) => jid !== uniqueRecipients[index])) { - logger.debug( - { before: uniqueRecipients, after: canonicalRecipients }, - '[CAROUSEL] Canonicalized recipient addressing for session reuse' - ) - } - - return canonicalRecipients - } - const relayMessage = async ( jid: string, message: proto.IMessage, @@ -995,13 +1011,8 @@ export const makeMessagesSocket = (config: SocketConfig) => { allRecipients.push(jid) } - const isCarouselFanout = isCarouselMessage(message) - const effectiveMeRecipients = isCarouselFanout - ? await canonicalizeCarouselRecipients(meRecipients) - : meRecipients - const effectiveOtherRecipients = isCarouselFanout - ? await canonicalizeCarouselRecipients(otherRecipients) - : otherRecipients + const effectiveMeRecipients = await canonicalizeSessionRecipients(meRecipients) + const effectiveOtherRecipients = await canonicalizeSessionRecipients(otherRecipients) const effectiveAllRecipients = [...effectiveMeRecipients, ...effectiveOtherRecipients] await assertSessions(effectiveAllRecipients) From e152aee740fccec093542cee974a7697829d3d6c Mon Sep 17 00:00:00 2001 From: Renato Alcara Date: Mon, 16 Mar 2026 23:22:05 -0300 Subject: [PATCH 2/4] Fix/pn lid session reuse (#294) * fix: unify PN and LID session reuse for 1:1 sends * fix: limit PN/LID reuse changes to session cache --- src/Socket/messages-send.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index 4ddae005..d78025d9 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -1011,6 +1011,8 @@ export const makeMessagesSocket = (config: SocketConfig) => { allRecipients.push(jid) } + + await assertSessions(allRecipients) const effectiveMeRecipients = await canonicalizeSessionRecipients(meRecipients) const effectiveOtherRecipients = await canonicalizeSessionRecipients(otherRecipients) const effectiveAllRecipients = [...effectiveMeRecipients, ...effectiveOtherRecipients] @@ -1022,14 +1024,14 @@ export const makeMessagesSocket = (config: SocketConfig) => { { nodes: otherNodes, shouldIncludeDeviceIdentity: s2 } ] = await Promise.all([ // For own devices: use DSM if available (1:1 chats only) - createParticipantNodes(effectiveMeRecipients, meMsg || message, extraAttrs), - createParticipantNodes(effectiveOtherRecipients, message, extraAttrs, meMsg) + createParticipantNodes(meRecipients, meMsg || message, extraAttrs), + createParticipantNodes(otherRecipients, message, extraAttrs, meMsg) ]) participants.push(...meNodes) participants.push(...otherNodes) - if (effectiveMeRecipients.length > 0 || effectiveOtherRecipients.length > 0) { - extraAttrs['phash'] = generateParticipantHashV2([...effectiveMeRecipients, ...effectiveOtherRecipients]) + if (meRecipients.length > 0 || otherRecipients.length > 0) { + extraAttrs['phash'] = generateParticipantHashV2([...meRecipients, ...otherRecipients]) } shouldIncludeDeviceIdentity = shouldIncludeDeviceIdentity || s1 || s2 From dbc6dd6aed72a025fd0940cdbd7965f5d503205e Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 17 Mar 2026 00:46:29 -0300 Subject: [PATCH 3/4] chore: update proto/version to v2.3000.1035302375 (#295) Co-authored-by: rsalcara --- WAProto/WAProto.proto | 2 +- src/Defaults/baileys-version.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/WAProto/WAProto.proto b/WAProto/WAProto.proto index 2ee95777..c9116099 100644 --- a/WAProto/WAProto.proto +++ b/WAProto/WAProto.proto @@ -1,7 +1,7 @@ syntax = "proto3"; package proto; -/// WhatsApp Version: 2.3000.1035216863 +/// WhatsApp Version: 2.3000.1035302375 message ADVDeviceIdentity { optional uint32 rawId = 1; diff --git a/src/Defaults/baileys-version.json b/src/Defaults/baileys-version.json index 2e57a23a..238eecb9 100644 --- a/src/Defaults/baileys-version.json +++ b/src/Defaults/baileys-version.json @@ -1 +1 @@ -{"version":[2,3000,1035223526]} +{"version": [2, 3000, 1035302375]} From 11eed3aedceaca01de56e9d2be9f4eed84f65205 Mon Sep 17 00:00:00 2001 From: Renato Alcara Date: Tue, 17 Mar 2026 06:16:35 -0300 Subject: [PATCH 4/4] chore: update WhatsApp Web version to v2.3000.1035317910 (#296) Co-authored-by: github-actions[bot] --- src/Defaults/baileys-version.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Defaults/baileys-version.json b/src/Defaults/baileys-version.json index 238eecb9..a20fa7ea 100644 --- a/src/Defaults/baileys-version.json +++ b/src/Defaults/baileys-version.json @@ -1 +1 @@ -{"version": [2, 3000, 1035302375]} +{"version":[2,3000,1035317910]}