WhiskeySockets / Baileys

Lightweight full-featured typescript/javascript WhatsApp Web API
https://baileys.whiskeysockets.io/
MIT License
3.97k stars 1.35k forks source link

[BUG] Bad decrypt while downloading media document #681

Open matheusb-comp opened 7 months ago

matheusb-comp commented 7 months ago

Describe the bug Calling downloadMediaMessage on a message with an audio file sent as a "document" causes a decryption error. The same situation described on #420.

The returned stream throws an error event during the execution of the pipe call on downloadEncryptedContent.

Error: error:1C800064:Provider routines::bad decrypt
    at Decipheriv.final (node:internal/crypto/cipher:193:29)
    at Transform.final [as _final] (/app/node_modules/@whiskeysockets/baileys/lib/Utils/messages-media.js:498:31)
    at callFinal (node:internal/streams/writable:698:12)
    at prefinish (node:internal/streams/writable:710:7)
    at finishMaybe (node:internal/streams/writable:720:5)
    at Writable.end (node:internal/streams/writable:634:5)
    at IncomingMessage.onend (node:internal/streams/readable:705:10)
    at Object.onceWrapper (node:events:627:28)
    at IncomingMessage.emit (node:events:525:35)
    at IncomingMessage.emit (node:domain:552:15) {
  library: 'Provider routines',
  reason: 'bad decrypt',
  code: 'ERR_OSSL_BAD_DECRYPT'
}

From what I could understand, the wrong "literal string" is chosen by Baileys for the HKDF function. In the reveng docs, it seems that the text WhatsApp Audio Keys should be used, and not WhatsApp Document Keys.

However, the mediaType sent to the downloadContentFromMessage method is based on the message object key. So, if it is a "documentMessage", the type will be document, even if the "documentMessage" contains an "audio file", leading to the error.

To Reproduce

  1. Send an audio file to the phone connected to Baileys as a document: image

  2. On Baileys, call downloadMediaMessage

    
    // Received message example, it's an "audio/ogg" MIME, but in a "documentMessage"
    // {
    //   "key": {"id": "ABCD", "remoteJid": "123@s.whatsapp.net"},
    //   "messageTimestamp": 1710167093,
    //   "message": {
    //     "documentMessage": {
    //       "url": "https://mmg.whatsapp.net...",
    //       "mimetype": "audio/ogg",
    //       "mediaKey": "xxxx"
    //     },
    //     "messageContextInfo": {}
    //   }
    // }

import { downloadMediaMessage } from '@whiskeysockets/baileys' await downloadMediaMessage(m, 'stream', {}, { logger, reuploadRequest: sock.updateMediaMessage })



**Environment**

Baileys version: `6.6.0`
ariyanaid commented 4 months ago

npm i crypto

abe-om commented 1 day ago

same here