diff --git a/src/Socket/groups.ts b/src/Socket/groups.ts index 15573999..aa9e44ab 100644 --- a/src/Socket/groups.ts +++ b/src/Socket/groups.ts @@ -316,19 +316,19 @@ export const extractGroupMetadata = (result: BinaryNode) => { descId = descChild.attrs.id } - const groupId = group.attrs.id!.includes('@') ? group.attrs.id : jidEncode(group.attrs.id!, 'g.us') + const groupId = (group.attrs.id ?? '').includes('@') ? group.attrs.id : jidEncode(group.attrs.id ?? '', 'g.us') const eph = getBinaryNodeChild(group, 'ephemeral')?.attrs.expiration const memberAddMode = getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add' const metadata: GroupMetadata = { - id: groupId!, + id: groupId, notify: group.attrs.notify, addressingMode: group.attrs.addressing_mode === 'lid' ? WAMessageAddressingMode.LID : WAMessageAddressingMode.PN, - subject: group.attrs.subject!, + subject: group.attrs.subject ?? '', subjectOwner: group.attrs.s_o, subjectOwnerPn: group.attrs.s_o_pn, - subjectTime: +group.attrs.s_t!, + subjectTime: +group.attrs.s_t ?? '0', size: group.attrs.size ? +group.attrs.size : getBinaryNodeChildren(group, 'participant').length, - creation: +group.attrs.creation!, + creation: +group.attrs.creation ?? '0', owner: group.attrs.creator ? jidNormalizedUser(group.attrs.creator) : undefined, ownerPn: group.attrs.creator_pn ? jidNormalizedUser(group.attrs.creator_pn) : undefined, owner_country_code: group.attrs.creator_country_code, @@ -347,7 +347,7 @@ export const extractGroupMetadata = (result: BinaryNode) => { participants: getBinaryNodeChildren(group, 'participant').map(({ attrs }) => { // TODO: Store LID MAPPINGS return { - id: attrs.jid!, + id: attrs.jid ?? '', phoneNumber: isLidUser(attrs.jid) && isPnUser(attrs.phone_number) ? attrs.phone_number : undefined, lid: isPnUser(attrs.jid) && isLidUser(attrs.lid) ? attrs.lid : undefined, admin: (attrs.type || null) as GroupParticipant['admin'] diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index 6b2bcce1..85837eb3 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -156,16 +156,16 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { throw new Boom('Not authenticated') } - if (await placeholderResendCache.get(messageKey?.id!)) { + if (await placeholderResendCache.get(messageKey?.id)) { logger.debug({ messageKey }, 'already requested resend') return } else { - await placeholderResendCache.set(messageKey?.id!, true) + await placeholderResendCache.set(messageKey?.id, true) } await delay(2000) - if (!(await placeholderResendCache.get(messageKey?.id!))) { + if (!(await placeholderResendCache.get(messageKey?.id))) { logger.debug({ messageKey }, 'message received while resend requested') return 'RESOLVED' } @@ -180,9 +180,9 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { } setTimeout(async () => { - if (await placeholderResendCache.get(messageKey?.id!)) { + if (await placeholderResendCache.get(messageKey?.id)) { logger.debug({ messageKey }, 'PDO message without response after 8 seconds. Phone possibly offline') - await placeholderResendCache.del(messageKey?.id!) + await placeholderResendCache.del(messageKey?.id) } }, 8_000) @@ -233,7 +233,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { if (update.jid && update.user) { ev.emit('newsletter-participants.update', { id: update.jid, - author: node.attrs.from!, + author: node.attrs.from ?? '', user: update.user, new_role: 'ADMIN', action: 'promote' @@ -251,9 +251,10 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { // Handles newsletter notifications const handleNewsletterNotification = async (node: BinaryNode) => { - const from = node.attrs.from! - const child = getAllBinaryNodeChildren(node)[0]! - const author = node.attrs.participant! + const from = node.attrs.from ?? '' + const child = getAllBinaryNodeChildren(node)[0] + if (!child) return + const author = node.attrs.participant ?? '' logger.info({ from, child }, 'got newsletter notification') @@ -261,7 +262,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { case 'reaction': const reactionUpdate = { id: from, - server_id: child.attrs.message_id!, + server_id: child.attrs.message_id ?? '', reaction: { code: getBinaryNodeChildString(child, 'reaction'), count: 1 diff --git a/src/Utils/noise-handler.ts b/src/Utils/noise-handler.ts index f8a36c76..f51eebac 100644 --- a/src/Utils/noise-handler.ts +++ b/src/Utils/noise-handler.ts @@ -150,7 +150,7 @@ export const makeNoiseHandler = ({ while (true) { if (inBytes.length < 3) return - size = (inBytes[0]! << 16) | (inBytes[1]! << 8) | inBytes[2]! + size = (inBytes[0] << 16) | (inBytes[1] << 8) | inBytes[2] if (inBytes.length < size + 3) return @@ -180,13 +180,25 @@ export const makeNoiseHandler = ({ mixIntoKey, finishInit, processHandshake: ({ serverHello }: proto.HandshakeMessage, noiseKey: KeyPair) => { - authenticate(serverHello!.ephemeral!) - mixIntoKey(Curve.sharedKey(privateKey, serverHello!.ephemeral!)) + if (!serverHello?.ephemeral) { + throw new Boom('Missing server hello ephemeral', { statusCode: 500 }) + } - const decStaticContent = decrypt(serverHello!.static!) + if (!serverHello?.static) { + throw new Boom('Missing server hello static', { statusCode: 500 }) + } + + if (!serverHello?.payload) { + throw new Boom('Missing server hello payload', { statusCode: 500 }) + } + + authenticate(serverHello.ephemeral) + mixIntoKey(Curve.sharedKey(privateKey, serverHello.ephemeral)) + + const decStaticContent = decrypt(serverHello.static) mixIntoKey(Curve.sharedKey(privateKey, decStaticContent)) - const certDecoded = decrypt(serverHello!.payload!) + const certDecoded = decrypt(serverHello.payload) const { intermediate: certIntermediate, leaf } = proto.CertChain.decode(certDecoded) // leaf @@ -202,7 +214,11 @@ export const makeNoiseHandler = ({ const { issuerSerial } = details - const verify = Curve.verify(details.key!, leaf.details, leaf.signature) + if (!details.key) { + throw new Boom('Missing certificate key', { statusCode: 500 }) + } + + const verify = Curve.verify(details.key, leaf.details, leaf.signature) const verifyIntermediate = Curve.verify( WA_CERT_DETAILS.PUBLIC_KEY, @@ -223,7 +239,7 @@ export const makeNoiseHandler = ({ } const keyEnc = encrypt(noiseKey.public) - mixIntoKey(Curve.sharedKey(noiseKey.private, serverHello!.ephemeral!)) + mixIntoKey(Curve.sharedKey(noiseKey.private, serverHello.ephemeral)) return keyEnc },