mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
ID Validation + Full Stub Types
This commit is contained in:
@@ -34,15 +34,14 @@ export enum MessageType {
|
||||
document = 'documentMessage',
|
||||
audio = 'audioMessage',
|
||||
}
|
||||
/**
|
||||
* Tells us what kind of message it is
|
||||
*/
|
||||
export const MessageStubTypes = {
|
||||
20: 'addedToGroup',
|
||||
32: 'leftGroup',
|
||||
39: 'createdGroup',
|
||||
}
|
||||
export const WAMessageType = function () {
|
||||
const types = proto.WebMessageInfo.WEB_MESSAGE_INFO_STUBTYPE
|
||||
const dict: Record<number, string> = {}
|
||||
Object.keys(types).forEach(element => dict[ types[element] ] = element)
|
||||
return dict
|
||||
}()
|
||||
export const HKDFInfoKeys = (function () {
|
||||
|
||||
const dict: Record<string, string> = {}
|
||||
dict[MessageType.image] = 'WhatsApp Image Keys'
|
||||
dict[MessageType.video] = 'WhatsApp Audio Keys'
|
||||
@@ -65,10 +64,12 @@ export interface MessageOptions {
|
||||
caption?: string
|
||||
thumbnail?: string
|
||||
mimetype?: Mimetype
|
||||
validateID?: boolean
|
||||
}
|
||||
export interface MessageStatusUpdate {
|
||||
from: string
|
||||
to: string
|
||||
/** Which participant caused the update (only for groups) */
|
||||
participant?: string
|
||||
timestamp: Date
|
||||
/** Message IDs read/delivered */
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
} from './Constants'
|
||||
import { generateMessageID, sha256, hmacSign, aesEncrypWithIV, randomBytes } from '../WAConnection/Utils'
|
||||
import { WAMessageContent, WAMetric, WAFlag } from '../WAConnection/Constants'
|
||||
import { generateThumbnail, getMediaKeys } from './Utils'
|
||||
import { validateJIDForSending, generateThumbnail, getMediaKeys } from './Utils'
|
||||
|
||||
export default class WhatsAppWebMessages extends WhatsAppWebBase {
|
||||
/**
|
||||
@@ -48,6 +48,9 @@ export default class WhatsAppWebMessages extends WhatsAppWebBase {
|
||||
type: MessageType,
|
||||
options: MessageOptions = {},
|
||||
) {
|
||||
if (options.validateID === true || !('validateID' in options)) {
|
||||
validateJIDForSending (id)
|
||||
}
|
||||
let m: any = {}
|
||||
switch (type) {
|
||||
case MessageType.text:
|
||||
|
||||
@@ -3,7 +3,7 @@ import { MessageType, MessageOptions, Mimetype, Presence } from './Constants'
|
||||
import * as fs from 'fs'
|
||||
import * as assert from 'assert'
|
||||
|
||||
import { decodeMediaMessage } from './Utils'
|
||||
import { decodeMediaMessage, validateJIDForSending } from './Utils'
|
||||
import { promiseTimeout } from '../WAConnection/Utils'
|
||||
|
||||
require ('dotenv').config () // dotenv to load test jid
|
||||
@@ -61,6 +61,18 @@ WAClientTest('Messages', (client) => {
|
||||
assert.strictEqual(message.message.imageMessage.contextInfo.stanzaId, messages[0].key.id)
|
||||
})
|
||||
})
|
||||
describe('Validate WhatsApp IDs', () => {
|
||||
it ('should correctly validate', () => {
|
||||
assert.doesNotThrow (() => validateJIDForSending ('12345@s.whatsapp.net'))
|
||||
assert.doesNotThrow (() => validateJIDForSending ('919999999999@s.whatsapp.net'))
|
||||
assert.doesNotThrow (() => validateJIDForSending ('10203040506@s.whatsapp.net'))
|
||||
assert.doesNotThrow (() => validateJIDForSending ('12345-3478@g.us'))
|
||||
assert.doesNotThrow (() => validateJIDForSending ('1234567890-34712121238@g.us'))
|
||||
assert.throws (() => validateJIDForSending ('123454677@c.us'))
|
||||
assert.throws (() => validateJIDForSending ('+123454677@s.whatsapp.net'))
|
||||
assert.throws (() => validateJIDForSending ('+12345-3478@g.us'))
|
||||
})
|
||||
})
|
||||
WAClientTest('Presence', (client) => {
|
||||
it('should update presence', async () => {
|
||||
const presences = Object.values(Presence)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { MessageType, HKDFInfoKeys, MessageOptions, MessageStubTypes } from './Constants'
|
||||
import { MessageType, HKDFInfoKeys, MessageOptions, WAMessageType } from './Constants'
|
||||
import sharp from 'sharp'
|
||||
import * as fs from 'fs'
|
||||
import fetch from 'node-fetch'
|
||||
@@ -8,18 +8,28 @@ import { proto } from '../../WAMessage/WAMessage'
|
||||
import { randomBytes } from 'crypto'
|
||||
import { exec } from 'child_process'
|
||||
|
||||
export function validateJIDForSending (jid: string) {
|
||||
const regexp = /^[0-9]{1,20}(-[0-9]{1,20}@g.us|@s.whatsapp.net)$/
|
||||
if (!regexp.test (jid)) {
|
||||
throw new Error (
|
||||
`Invalid WhatsApp id: ${jid}
|
||||
1. Please ensure you suffix '@s.whatsapp.net' for individual numbers & '@g.us' for groups
|
||||
2. Please do not put any alphabets or special characters like a '+' in the number. A '-' symbol in groups is fine`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** Type of notification */
|
||||
export function getNotificationType(message: WAMessage) {
|
||||
export function getNotificationType(message: WAMessage): [string, string] {
|
||||
if (message.message) {
|
||||
return ['message', Object.keys(message.message)[0]]
|
||||
} else if (message.messageStubType) {
|
||||
return [MessageStubTypes[message.messageStubType], null]
|
||||
return [WAMessageType[message.messageStubType], null]
|
||||
} else {
|
||||
return ['unknown', null]
|
||||
}
|
||||
}
|
||||
/** generates all the keys required to encrypt/decrypt & sign a media message */
|
||||
|
||||
export function getMediaKeys(buffer, mediaType: MessageType) {
|
||||
// expand using HKDF to 112 bytes, also pass in the relevant app info
|
||||
const expandedMediaKey = hkdf(buffer, 112, HKDFInfoKeys[mediaType])
|
||||
|
||||
Reference in New Issue
Block a user