Skip to content

Media sent through the API can never be downloaded back, and the request hangs forever #221

Description

@AndreGMarra

Version: CodeChat 1.3.6 · Baileys 7.0.0-rc.9, also reproduced on rc13 and rc14

What happens

POST /chat/mediaData/{instance}?binary=true never completes for messages the instance itself sent (keyFromMe: true). The client receives a partial body and then the connection just hangs until it times out on its own — no error, no EOF.

Two separate problems are stacked here:

1. Decryption fails

Error: error:1C800064:Provider routines::bad decrypt
    at Decipheriv.final (node:internal/crypto/cipher:193:29)
    at Transform.final [as _final] (.../@whiskeysockets/baileys/lib/Utils/messages-media.js:486:31)
  reason: 'bad decrypt',
  code: 'ERR_OSSL_BAD_DECRYPT'

2. The error handler crashes instead of closing the response

ERROR [uncaughtException]
  Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
      at ServerResponse.json (.../express/lib/response.js:252:15)
      at Transform.<anonymous> (.../dist/src/whatsapp/routers/chat.router.js:151:79)

Streaming has already begun when decryption fails, so res.json(...) throws and the response is never terminated.

The media itself is fine

Downloading the encrypted file directly from the url in the message payload returns it complete and intact:

HTTP=200 bytes=77402 time=0.14s

That matches exactly what is expected: fileLength 77381 → 77392 (padded to the 16-byte AES block) + 10 byte MAC = 77402. So this is neither a network nor a CDN problem.

Consistent with that, the bytes that do reach the client stop at 77376 — precisely the largest 16-byte boundary below 77381, i.e. everything decrypts except the final block.

Incoming media (keyFromMe: false) on the same instance downloads and decrypts without any issue.

Reproduce

  1. Send an image through POST /message/sendMedia/{instance}
  2. Wait for the messages.upsert webhook and take the numeric id
  3. POST /chat/mediaData/{instance}?binary=true with that message's keyId
  4. The request hangs; the log shows both errors above

Suggested fixes

  • Terminate the response when it has already started. Guarding on res.headersSent and destroying/ending the stream instead of calling res.json() would at least turn this into a fast failure rather than a hang, and would stop the uncaught exception. This alone is worth fixing on its own, since any error mid-stream currently leaves the connection open.
  • Investigate why the stored mediaKey for self-sent messages fails to decrypt, while the same instance decrypts received media correctly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions