fix: validate LTHashState version to prevent app state crashes
fix: validate LTHashState version to prevent app state crashes
This commit is contained in:
+3
-1
@@ -40,6 +40,7 @@ import {
|
|||||||
extractSyncdPatches,
|
extractSyncdPatches,
|
||||||
generateProfilePicture,
|
generateProfilePicture,
|
||||||
getHistoryMsg,
|
getHistoryMsg,
|
||||||
|
ensureLTHashStateVersion,
|
||||||
newLTHashState,
|
newLTHashState,
|
||||||
processSyncAction
|
processSyncAction
|
||||||
} from '../Utils'
|
} from '../Utils'
|
||||||
@@ -595,6 +596,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
let state = result[name]
|
let state = result[name]
|
||||||
|
|
||||||
if (state) {
|
if (state) {
|
||||||
|
state = ensureLTHashStateVersion(state)
|
||||||
if (typeof initialVersionMap[name] === 'undefined') {
|
if (typeof initialVersionMap[name] === 'undefined') {
|
||||||
initialVersionMap[name] = state.version
|
initialVersionMap[name] = state.version
|
||||||
}
|
}
|
||||||
@@ -935,7 +937,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
await resyncAppState([name], false)
|
await resyncAppState([name], false)
|
||||||
|
|
||||||
const { [name]: currentSyncVersion } = await authState.keys.get('app-state-sync-version', [name])
|
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)
|
encodeResult = await encodeSyncdPatch(patchCreate, myAppStateKeyId, initial, getAppStateSyncKey)
|
||||||
const { patch, state } = encodeResult
|
const { patch, state } = encodeResult
|
||||||
|
|||||||
@@ -131,6 +131,13 @@ const generatePatchMac = (
|
|||||||
|
|
||||||
export const newLTHashState = (): LTHashState => ({ version: 0, hash: Buffer.alloc(128), indexValueMap: {} })
|
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 (
|
export const encodeSyncdPatch = async (
|
||||||
{ type, index, syncAction, apiVersion, operation }: WAPatchCreate,
|
{ type, index, syncAction, apiVersion, operation }: WAPatchCreate,
|
||||||
myAppStateKeyId: string,
|
myAppStateKeyId: string,
|
||||||
|
|||||||
Reference in New Issue
Block a user