fix(messages): handle encryption failures per recipient and fail when all fail (#2226)

This commit is contained in:
vini
2026-01-08 17:07:11 -03:00
committed by GitHub
parent c392d4ce6c
commit 432c26a07c
+19 -9
View File
@@ -541,14 +541,19 @@ export const makeMessagesSocket = (config: SocketConfig) => {
const encryptionPromises = (patchedMessages as any).map(
async ({ recipientJid: jid, message: patchedMessage }: any) => {
try {
if (!jid) return null
let msgToEncrypt = patchedMessage
if (dsmMessage) {
const { user: targetUser } = jidDecode(jid)!
const { user: ownPnUser } = jidDecode(meId)!
const ownLidUser = meLidUser
const isOwnUser = targetUser === ownPnUser || (ownLidUser && targetUser === ownLidUser)
const isExactSenderDevice = jid === meId || (meLid && jid === meLid)
if (isOwnUser && !isExactSenderDevice) {
msgToEncrypt = dsmMessage
logger.debug({ jid, targetUser }, 'Using DSM for own device')
@@ -557,11 +562,10 @@ export const makeMessagesSocket = (config: SocketConfig) => {
const bytes = encodeWAMessage(msgToEncrypt)
const mutexKey = jid
const node = await encryptionMutex.mutex(mutexKey, async () => {
const { type, ciphertext } = await signalRepository.encryptMessage({
jid,
data: bytes
})
const { type, ciphertext } = await signalRepository.encryptMessage({ jid, data: bytes })
if (type === 'pkmsg') {
shouldIncludeDeviceIdentity = true
}
@@ -572,21 +576,27 @@ export const makeMessagesSocket = (config: SocketConfig) => {
content: [
{
tag: 'enc',
attrs: {
v: '2',
type,
...(extraAttrs || {})
},
attrs: { v: '2', type, ...(extraAttrs || {}) },
content: ciphertext
}
]
}
})
return node
} catch (err) {
logger.error({ jid, err }, 'Failed to encrypt for recipient')
return null
}
}
)
const nodes = (await Promise.all(encryptionPromises)).filter(node => node !== null) as BinaryNode[]
if (recipientJids.length > 0 && nodes.length === 0) {
throw new Boom('All encryptions failed', { statusCode: 500 })
}
return { nodes, shouldIncludeDeviceIdentity }
}