Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): default a generic fetched media type per kind

Yudhi Armyndharisa7895e58266d
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index 99ee20152..7dbe6cb5c 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -142,15 +142,22 @@ async function toWebpSticker(data: Buffer, mimetype: string): Promise<Buffer> {
}
}
+/** Fetched Content-Types that say nothing about the bytes (a missing header reads as ''). */
+const GENERIC_FETCHED_TYPES = new Set(['', 'application/octet-stream', 'binary/octet-stream']);
+
/**
* Resolve a MediaInput's data (Buffer | base64 string | http(s) URL) to bytes + mimetype.
*
* `sessionProxyUrl` is this session's egress proxy, which the URL fetch leaves through (#1626). It
* is required, not optional, so a new call site cannot fetch direct on a proxied session by omission.
+ *
+ * `fallbackType` is the kind's default (image/jpeg, video/mp4, audio/mpeg), used for a URL whose type
+ * neither the caller nor the host names. A document send passes none and keeps what it was given.
*/
export async function resolveMediaBuffer(
media: MediaInput,
sessionProxyUrl: string | undefined,
+ fallbackType?: string,
): Promise<{ data: Buffer; mimetype: string }> {
if (Buffer.isBuffer(media.data)) {
return { data: media.data, mimetype: media.mimetype };
@@ -158,11 +165,14 @@ export async function resolveMediaBuffer(
if (/^https?:\/\//i.test(media.data)) {
const fetched = await loadRemoteMediaBuffer(media.data, sessionProxyUrl);
// A generic placeholder mimetype (buildMediaInput's 'application/octet-stream' default when the
- // caller supplied none) carries no real signal — defer to the fetched response content-type,
- // which was sniffed from the actual bytes. This fixes URL-based sends where the caller has no
- // mimetype to pass through the conversation-send facade (e.g. chatwoot-adapter outbound relay).
+ // caller supplied none) carries no real signal, so the fetched Content-Type decides. That header
+ // is taken as the host sent it, not sniffed from the bytes, so a missing or generic one is no
+ // better than the placeholder and the kind's default stands in. This serves URL-based sends where
+ // the caller has no mimetype to pass through the conversation-send facade (e.g. chatwoot-adapter
+ // outbound relay).
const callerMimetype = media.mimetype && media.mimetype !== 'application/octet-stream' ? media.mimetype : null;
- return { data: fetched.data, mimetype: callerMimetype ?? fetched.mimetype };
+ const unknown = fallbackType !== undefined && GENERIC_FETCHED_TYPES.has(fetched.mimetype.toLowerCase());
+ return { data: fetched.data, mimetype: callerMimetype ?? (unknown ? fallbackType : fetched.mimetype) };
}
return { data: Buffer.from(media.data, 'base64'), mimetype: media.mimetype };
}
@@ -333,7 +343,7 @@ export class BaileysMessaging {
async sendImageMessage(chatId: string, media: MediaInput): Promise<MessageResult> {
this.host.ensureReady();
- const { data, mimetype } = await resolveMediaBuffer(media, this.host.sessionProxyUrl());
+ const { data, mimetype } = await resolveMediaBuffer(media, this.host.sessionProxyUrl(), 'image/jpeg');
return this.sendContent(
chatId,
{
@@ -348,7 +358,7 @@ export class BaileysMessaging {
async sendVideoMessage(chatId: string, media: MediaInput): Promise<MessageResult> {
this.host.ensureReady();
- const { data, mimetype } = await resolveMediaBuffer(media, this.host.sessionProxyUrl());
+ const { data, mimetype } = await resolveMediaBuffer(media, this.host.sessionProxyUrl(), 'video/mp4');
return this.sendContent(
chatId,
{
@@ -363,7 +373,7 @@ export class BaileysMessaging {
async sendAudioMessage(chatId: string, media: MediaInput): Promise<MessageResult> {
this.host.ensureReady();
- const { data, mimetype } = await resolveMediaBuffer(media, this.host.sessionProxyUrl());
+ const { data, mimetype } = await resolveMediaBuffer(media, this.host.sessionProxyUrl(), 'audio/mpeg');
return this.sendContent(
chatId,
// Audio carries no caption, so a mention here tags the recipient through contextInfo without
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index c557ddfc4..0f5e16ae3 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -3776,6 +3776,52 @@ describe('BaileysAdapter media sends', () => {
expect(loadRemoteMediaBuffer).toHaveBeenCalledWith('https://cdn.example/v.mp4', 'socks5://proxy.invalid:1080');
});
+ // The placeholder a send without a declared type carries; the fetched Content-Type is then the only
+ // signal, and a generic or missing one says nothing about what the bytes are.
+ const PLACEHOLDER = 'application/octet-stream';
+ const sendByKind = {
+ image: (a: BaileysAdapter, url: string) =>
+ a.sendImageMessage('[email protected]', { mimetype: PLACEHOLDER, data: url }),
+ video: (a: BaileysAdapter, url: string) =>
+ a.sendVideoMessage('[email protected]', { mimetype: PLACEHOLDER, data: url }),
+ audio: (a: BaileysAdapter, url: string) =>
+ a.sendAudioMessage('[email protected]', { mimetype: PLACEHOLDER, data: url }),
+ };
+ const sentMimetype = (): unknown =>
+ (fakeSock.sendMessage.mock.calls[0] as [string, { mimetype?: string }])[1].mimetype;
+
+ it.each([
+ ['image', 'image/jpeg', ''],
+ ['image', 'image/jpeg', 'application/octet-stream'],
+ ['video', 'video/mp4', 'binary/octet-stream'],
+ ['video', 'video/mp4', 'Application/Octet-Stream'],
+ ['audio', 'audio/mpeg', ''],
+ ['audio', 'audio/mpeg', 'binary/octet-stream'],
+ ] as const)('labels an undeclared %s URL as %s when the host answers %j', async (kind, fallback, fetchedType) => {
+ (loadRemoteMediaBuffer as jest.Mock).mockResolvedValue({ data: Buffer.from([1]), mimetype: fetchedType });
+ const adapter = await ready();
+ await sendByKind[kind](adapter, 'https://cdn.example/m');
+ expect(sentMimetype()).toBe(fallback);
+ });
+
+ it('keeps a specific fetched type for an undeclared media URL', async () => {
+ (loadRemoteMediaBuffer as jest.Mock).mockResolvedValue({ data: Buffer.from([1]), mimetype: 'image/png' });
+ const adapter = await ready();
+ await sendByKind.image(adapter, 'https://cdn.example/m');
+ expect(sentMimetype()).toBe('image/png');
+ });
+
+ it('leaves a document with the type it was fetched with', async () => {
+ (loadRemoteMediaBuffer as jest.Mock).mockResolvedValue({ data: Buffer.from([1]), mimetype: PLACEHOLDER });
+ const adapter = await ready();
+ await adapter.sendDocumentMessage('[email protected]', {
+ mimetype: PLACEHOLDER,
+ data: 'https://cdn.example/m',
+ filename: 'm.bin',
+ });
+ expect(sentMimetype()).toBe(PLACEHOLDER);
+ });
+
it('uses the caller-declared mimetype over the fetched content-type for a URL', async () => {
(loadRemoteMediaBuffer as jest.Mock).mockResolvedValue({
data: Buffer.from([1]),