fix: harden Protobuf Deserialization and Refactor Utilities (#1820)

* refactor: reorganize browser utility functions and improve buffer handling

* fix: harden protobuf deserialization and clean up code

* refactor: simplify data handling in GroupCipher and SenderKey classes

* fix: Ensure consistent Buffer hydration and remove redundant code

* refactor: update decodeAndHydrate calls to use proto types for improved type safety
This commit is contained in:
João Lucas de Oliveira Lopes
2025-09-25 22:45:30 -03:00
committed by GitHub
parent 202c39f3fe
commit 3b46d43beb
23 changed files with 194 additions and 160 deletions
+13 -40
View File
@@ -1,49 +1,13 @@
import { Boom } from '@hapi/boom'
import axios, { type AxiosRequestConfig } from 'axios'
import { createHash, randomBytes } from 'crypto'
import { platform, release } from 'os'
import { proto } from '../../WAProto/index.js'
const baileysVersion = [2, 3000, 1023223821]
import type {
BaileysEventEmitter,
BaileysEventMap,
BrowsersMap,
ConnectionState,
WACallUpdateType,
WAVersion
} from '../Types'
import type { BaileysEventEmitter, BaileysEventMap, ConnectionState, WACallUpdateType, WAVersion } from '../Types'
import { DisconnectReason } from '../Types'
import { type BinaryNode, getAllBinaryNodeChildren, jidDecode } from '../WABinary'
import { sha256 } from './crypto'
const PLATFORM_MAP = {
aix: 'AIX',
darwin: 'Mac OS',
win32: 'Windows',
android: 'Android',
freebsd: 'FreeBSD',
openbsd: 'OpenBSD',
sunos: 'Solaris',
linux: undefined,
haiku: undefined,
cygwin: undefined,
netbsd: undefined
}
export const Browsers: BrowsersMap = {
ubuntu: browser => ['Ubuntu', browser, '22.04.4'],
macOS: browser => ['Mac OS', browser, '14.4.1'],
baileys: browser => ['Baileys', browser, '6.5.0'],
windows: browser => ['Windows', browser, '10.0.22631'],
/** The appropriate browser based on your OS & release */
appropriate: browser => [PLATFORM_MAP[platform()] || 'Ubuntu', browser, release()]
}
export const getPlatformId = (browser: string) => {
const platformType = proto.DeviceProps.PlatformType[browser.toUpperCase() as any]
return platformType ? platformType.toString() : '1' //chrome
}
export const BufferJSON = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
replacer: (k: any, value: any) => {
@@ -56,9 +20,18 @@ export const BufferJSON = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
reviver: (_: any, value: any) => {
if (typeof value === 'object' && !!value && (value.buffer === true || value.type === 'Buffer')) {
const val = value.data || value.value
return typeof val === 'string' ? Buffer.from(val, 'base64') : Buffer.from(val || [])
if (typeof value === 'object' && value !== null && value.type === 'Buffer' && typeof value.data === 'string') {
return Buffer.from(value.data, 'base64')
}
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
const keys = Object.keys(value)
if (keys.length > 0 && keys.every(k => !isNaN(parseInt(k, 10)))) {
const values = Object.values(value)
if (values.every(v => typeof v === 'number')) {
return Buffer.from(values)
}
}
}
return value