From 87c1a045033369868d7612b353093183cd679576 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Thu, 28 Apr 2022 15:49:02 +0530 Subject: [PATCH] feat: add more sanity checks to chat patch --- src/Utils/chat-utils.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/Utils/chat-utils.ts b/src/Utils/chat-utils.ts index 08112ee..f6aaf71 100644 --- a/src/Utils/chat-utils.ts +++ b/src/Utils/chat-utils.ts @@ -2,7 +2,7 @@ import { Boom } from '@hapi/boom' import type { Logger } from 'pino' import { proto } from '../../WAProto' import { AuthenticationCreds, BaileysEventMap, Chat, ChatModification, ChatMutation, Contact, LastMessageList, LTHashState, WAPatchCreate, WAPatchName } from '../Types' -import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, jidNormalizedUser } from '../WABinary' +import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, isJidGroup, jidNormalizedUser } from '../WABinary' import { aesDecrypt, aesEncrypt, hkdf, hmacSign } from './crypto' import { toNumber } from './generics' import { LT_HASH_ANTI_TAMPERING } from './lt-hash' @@ -452,6 +452,18 @@ export const chatModificationToAppPatch = ( lastMessageTimestamp: lastMsg?.messageTimestamp, messages: lastMessages?.length ? lastMessages.map( m => { + if(!m.key?.id || !m.key?.remoteJid) { + throw new Boom('Incomplete key', { statusCode: 400, data: m }) + } + + if(isJidGroup(m.key.remoteJid) && !m.key.fromMe && !m.key.participant) { + throw new Boom('Expected not from me message to have participant', { statusCode: 400, data: m }) + } + + if(!m.messageTimestamp || !toNumber(m.messageTimestamp)) { + throw new Boom('Missing timestamp in last message list', { statusCode: 400, data: m }) + } + if(m.key.participant) { m.key = { ...m.key } m.key.participant = jidNormalizedUser(m.key.participant)