From cbe5205dfd787c3de425eed3d3638b34a25637b3 Mon Sep 17 00:00:00 2001 From: Renato Alcara Date: Fri, 20 Feb 2026 01:19:04 -0300 Subject: [PATCH] fix: validate LTHashState version to prevent app state crashes fix: validate LTHashState version to prevent app state crashes --- src/Socket/chats.ts | 4 +++- src/Utils/chat-utils.ts | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 07a36845..8ae89566 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -40,6 +40,7 @@ import { extractSyncdPatches, generateProfilePicture, getHistoryMsg, + ensureLTHashStateVersion, newLTHashState, processSyncAction } from '../Utils' @@ -595,6 +596,7 @@ export const makeChatsSocket = (config: SocketConfig) => { let state = result[name] if (state) { + state = ensureLTHashStateVersion(state) if (typeof initialVersionMap[name] === 'undefined') { initialVersionMap[name] = state.version } @@ -935,7 +937,7 @@ export const makeChatsSocket = (config: SocketConfig) => { await resyncAppState([name], false) const { [name]: currentSyncVersion } = await authState.keys.get('app-state-sync-version', [name]) - initial = currentSyncVersion || newLTHashState() + initial = currentSyncVersion ? ensureLTHashStateVersion(currentSyncVersion) : newLTHashState() encodeResult = await encodeSyncdPatch(patchCreate, myAppStateKeyId, initial, getAppStateSyncKey) const { patch, state } = encodeResult diff --git a/src/Utils/chat-utils.ts b/src/Utils/chat-utils.ts index 5072dfe7..d131b684 100644 --- a/src/Utils/chat-utils.ts +++ b/src/Utils/chat-utils.ts @@ -131,6 +131,13 @@ const generatePatchMac = ( export const newLTHashState = (): LTHashState => ({ version: 0, hash: Buffer.alloc(128), indexValueMap: {} }) +export const ensureLTHashStateVersion = (state: LTHashState): LTHashState => { + if (typeof state.version !== 'number' || isNaN(state.version)) { + state.version = 0 + } + return state +} + export const encodeSyncdPatch = async ( { type, index, syncAction, apiVersion, operation }: WAPatchCreate, myAppStateKeyId: string,