From b0a973e24c878456e44748422ff07a4193cf8c69 Mon Sep 17 00:00:00 2001 From: Francisco Pessano <76450203+FranP-code@users.noreply.github.com> Date: Thu, 14 Aug 2025 16:50:03 +0000 Subject: [PATCH] fix: ensure proper type casting for cache and message handling --- src/Socket/chats.ts | 5 +++-- src/Socket/messages-recv.ts | 10 +++++----- src/Socket/messages-send.ts | 4 ++-- src/Utils/auth-utils.ts | 6 +++--- src/Utils/crypto.ts | 4 ++-- src/Utils/history.ts | 2 +- 6 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 856d8cb..692f5f4 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -5,6 +5,7 @@ import { DEFAULT_CACHE_TTLS, PROCESSABLE_HISTORY_TYPES } from '../Defaults' import { ALL_WA_PATCH_NAMES, BotListInfo, + CacheStore, ChatModification, ChatMutation, LTHashState, @@ -79,7 +80,7 @@ export const makeChatsSocket = (config: SocketConfig) => { }) if (!config.placeholderResendCache) { - config.placeholderResendCache = placeholderResendCache + config.placeholderResendCache = placeholderResendCache as CacheStore } /** helper function to fetch the given app state sync key */ @@ -974,7 +975,7 @@ export const makeChatsSocket = (config: SocketConfig) => { })(), processMessage(msg, { shouldProcessHistoryMsg, - placeholderResendCache, + placeholderResendCache: placeholderResendCache as CacheStore, ev, creds: authState.creds, keyStore: authState.keys, diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index 4115bde..6056f66 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -169,7 +169,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { const msgId = msgKey.id! const key = `${msgId}:${msgKey?.participant}` - let retryCount = msgRetryCache.get(key) || 0 + let retryCount = (msgRetryCache.get(key) as number) || 0 if (retryCount >= maxMsgRetryCount) { logger.debug({ retryCount, msgId }, 'reached retry limit, clearing') msgRetryCache.del(key) @@ -585,13 +585,13 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { const willSendMessageAgain = (id: string, participant: string) => { const key = `${id}:${participant}` - const retryCount = msgRetryCache.get(key) || 0 + const retryCount = (msgRetryCache.get(key) as number) || 0 return retryCount < maxMsgRetryCount } const updateSendMessageAgainCount = (id: string, participant: string) => { const key = `${id}:${participant}` - const newValue = (msgRetryCache.get(key) || 0) + 1 + const newValue = ((msgRetryCache.get(key) as number) || 0) + 1 msgRetryCache.set(key, newValue) } @@ -961,8 +961,8 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { // use existing call info to populate this event if (existingCall) { - call.isVideo = existingCall.isVideo - call.isGroup = existingCall.isGroup + call.isVideo = (existingCall as WACallEvent).isVideo + call.isGroup = (existingCall as WACallEvent).isGroup } // delete data once call has ended diff --git a/src/Socket/messages-send.ts b/src/Socket/messages-send.ts index f266955..65bdcbe 100644 --- a/src/Socket/messages-send.ts +++ b/src/Socket/messages-send.ts @@ -195,8 +195,8 @@ export const makeMessagesSocket = (config: SocketConfig) => { const user = jidDecode(jid)?.user jid = jidNormalizedUser(jid) if (useCache) { - const devices = userDevicesCache.get(user!) - if (devices) { + const devices = userDevicesCache.get(user!) as JidWithDevice[] | undefined + if (devices && Array.isArray(devices)) { deviceResults.push(...devices) logger.trace({ user }, 'using cache for devices') diff --git a/src/Utils/auth-utils.ts b/src/Utils/auth-utils.ts index 90b31f9..b089b5d 100644 --- a/src/Utils/auth-utils.ts +++ b/src/Utils/auth-utils.ts @@ -43,8 +43,8 @@ export function makeCacheableSignalKeyStore( const idsToFetch: string[] = [] for (const id of ids) { const item = cache.get(getUniqueId(type, id)) - if (typeof item !== 'undefined') { - data[id] = item + if (typeof item !== 'undefined' && item !== null) { + data[id] = item as SignalDataTypeMap[typeof type] } else { idsToFetch.push(id) } @@ -55,7 +55,7 @@ export function makeCacheableSignalKeyStore( const fetched = await store.get(type, idsToFetch) for (const id of idsToFetch) { const item = fetched[id] - if (item) { + if (item !== null && item !== undefined) { data[id] = item cache.set(getUniqueId(type, id), item) } diff --git a/src/Utils/crypto.ts b/src/Utils/crypto.ts index 4ae3dc9..d0d2510 100644 --- a/src/Utils/crypto.ts +++ b/src/Utils/crypto.ts @@ -136,7 +136,7 @@ export async function hkdf( const infoBytes = info.info ? new TextEncoder().encode(info.info) : new Uint8Array(0) // Import the input key material - const importedKey = await subtle.importKey('raw', inputKeyMaterial, { name: 'HKDF' }, false, ['deriveBits']) + const importedKey = await subtle.importKey('raw', inputKeyMaterial as BufferSource, { name: 'HKDF' }, false, ['deriveBits']) // Derive bits using HKDF const derivedBits = await subtle.deriveBits( @@ -167,7 +167,7 @@ export async function derivePairingCodeKey(pairingCode: string, salt: Buffer): P const derivedBits = await subtle.deriveBits( { name: 'PBKDF2', - salt: saltBuffer, + salt: saltBuffer as BufferSource, iterations: 2 << 16, hash: 'SHA-256' }, diff --git a/src/Utils/history.ts b/src/Utils/history.ts index eedbdff..1c65550 100644 --- a/src/Utils/history.ts +++ b/src/Utils/history.ts @@ -20,7 +20,7 @@ export const downloadHistory = async (msg: proto.Message.IHistorySyncNotificatio let buffer = Buffer.concat(bufferArray) // decompress buffer - buffer = await inflatePromise(buffer) + buffer = Buffer.from(await inflatePromise(buffer)) const syncData = proto.HistorySync.decode(buffer) return syncData