From 0d27fc467b2bf32ef9da0d06052e3c6d1c6f7972 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Mon, 29 Nov 2021 23:57:07 +0530 Subject: [PATCH] feat: add sanity check for chat modifications --- src/Utils/chat-utils.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Utils/chat-utils.ts b/src/Utils/chat-utils.ts index febe0cf..908f96b 100644 --- a/src/Utils/chat-utils.ts +++ b/src/Utils/chat-utils.ts @@ -420,9 +420,19 @@ export const chatModificationToAppPatch = ( lastMessages: Pick[] ) => { const OP = proto.SyncdMutation.SyncdMutationSyncdOperation - const messageRange: proto.ISyncActionMessageRange = { - lastMessageTimestamp: lastMessages[lastMessages.length-1]?.messageTimestamp, - messages: lastMessages + const getMessageRange = () => { + if(!lastMessages?.length) { + throw new Boom('Expected last message to be not from me', { statusCode: 400 }) + } + const lastMsg = lastMessages[lastMessages.length-1] + if(lastMsg.key.fromMe) { + throw new Boom('Expected last message in array to be not from me', { statusCode: 400 }) + } + const messageRange: proto.ISyncActionMessageRange = { + lastMessageTimestamp: lastMsg?.messageTimestamp, + messages: lastMessages + } + return messageRange } let patch: WAPatchCreate if('mute' in mod) { @@ -443,7 +453,7 @@ export const chatModificationToAppPatch = ( syncAction: { archiveChatAction: { archived: !!mod.archive, - messageRange + messageRange: getMessageRange() } }, index: ['archive', jid], @@ -456,7 +466,7 @@ export const chatModificationToAppPatch = ( syncAction: { markChatAsReadAction: { read: mod.markRead, - messageRange + messageRange: getMessageRange() } }, index: ['markChatAsRead', jid],