Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): media→channel on wwjs returns 501 not raw 500 (#673) (#676)

Yudhi Armyndharisa54b93d0f943
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e96cde616..1e3cb8543 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- **Sending media to a channel (`@newsletter`) on the whatsapp-web.js engine now fails fast with `501 Not Implemented` instead of a raw `500`.** whatsapp-web.js builds a channel media message and calls `msg.avParams()`, a WhatsApp-Web-page method removed in a recent WA Web build, so the send crashed with `TypeError: msg.avParams is not a function` (upstream wwebjs#201823, unresolved in the current 1.34.7 release). The engine adapter now guards all media sends — image/video/audio/document (which share one send path) **and sticker** (a separate path that funnels into the same crash for channels) — for a channel recipient and raises a typed `ChannelMediaNotSupportedError` (501) before reaching the broken upstream code. Text→channel still works; only media is affected. The Baileys engine is unaffected. Workaround for affected media-to-channel sends until the upstream is fixed: switch the session to the Baileys engine, or pin `WWEBJS_WEB_VERSION` to a cached WA Web build that predates the `avParams` removal. [#673]
+
- **`base64` media now takes precedence over `url` when both are provided on a media send.** A `send-image`/`send-video`/etc. request carrying both fields previously sent the URL — and because `@ValidateIf` skipped `@IsUrl` validation on `url` whenever `base64` was present, a stale `url` (e.g. an example default left in the body, or a client such as the n8n community node that sends both) was fetched unvalidated and could 404, silently shadowing the supplied base64 image. `buildMediaInput` now prefers `base64`, aligning the send path with the already-base64-first persisted message metadata. Both engine adapters (whatsapp-web.js and Baileys) already guard the remote fetch behind an `isHttpUrl` check, so the change covers both. [#670]
## [0.8.13] - 2026-07-09
diff --git a/src/common/errors/channel-media-not-supported.error.ts b/src/common/errors/channel-media-not-supported.error.ts
new file mode 100644
index 000000000..1ee9d3348
--- /dev/null
+++ b/src/common/errors/channel-media-not-supported.error.ts
@@ -0,0 +1,18 @@
+import { NotImplementedException } from '@nestjs/common';
+
+/**
+ * Thrown by the whatsapp-web.js engine adapter when a media send (image/video/audio/document/sticker)
+ * targets a channel (`<id>@newsletter`). whatsapp-web.js constructs the channel message and calls `msg.avParams()`
+ * (Utils.js), a WhatsApp-Web-page method removed in a recent WA Web build, so the send crashes with
+ * `TypeError: msg.avParams is not a function` (upstream wwebjs#201823, unresolved). Text→channel still
+ * works; only media is affected.
+ *
+ * Extends NestJS `NotImplementedException` so it maps to **HTTP 501** through the built-in exception
+ * handler — no custom global filter required. Mirrors how {@link ChatLabelsUnsupportedError} maps to 422
+ * and {@link ChannelNotFoundError} to 404.
+ */
+export class ChannelMediaNotSupportedError extends NotImplementedException {
+ constructor(message = 'Sending media to channels (@newsletter) is not supported by the whatsapp-web.js engine.') {
+ super(message);
+ }
+}
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
index c813ddfd1..5616f5217 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
@@ -18,6 +18,7 @@ import { UnprocessableEntityException } from '@nestjs/common';
import { EngineNotReadyError } from '../../common/errors/engine-not-ready.error';
import { EngineNotSupportedError } from '../../common/errors/engine-not-supported.error';
import { ChannelNotFoundError } from '../../common/errors/channel-not-found.error';
+import { ChannelMediaNotSupportedError } from '../../common/errors/channel-media-not-supported.error';
import { EngineStatus } from '../interfaces/whatsapp-engine.interface';
import { SsrfBlockedError } from '../../common/security/ssrf-guard';
import { fetch as undiciFetch } from 'undici';
@@ -538,6 +539,45 @@ describe('WhatsAppWebJsAdapter channel-JID guard (#554 — wwebjs Channel lacks
]);
});
});
+
+ describe('media sends (sendImageMessage/sendVideo/sendAudio/sendDocument/sendSticker)', () => {
+ // whatsapp-web.js crashes building a channel media message: `msg.avParams is not a function`
+ // (upstream wwebjs#201823). image/video/audio/document funnel through sendMediaMessage; sticker
+ // has its own path but hits the same channel crash. Guarding both fail-fasts as a typed 501
+ // instead of surfacing the raw TypeError as a 500 (#673).
+ it('rejects sendImageMessage on a newsletter JID with ChannelMediaNotSupportedError (→ 501)', async () => {
+ const sendMessage = jest.fn();
+ const err = await readyAdapter({ sendMessage })
+ .sendImageMessage(NEWSLETTER, { mimetype: 'image/jpeg', data: Buffer.from([1]).toString('base64') })
+ .catch((e: unknown) => e);
+ expect(err).toBeInstanceOf(ChannelMediaNotSupportedError);
+ // Pin the user-facing contract: this must surface as 501, not regress to a 500 (the raw
+ // upstream crash) or drift if the base class ever changes.
+ expect((err as ChannelMediaNotSupportedError).getStatus()).toBe(501);
+ expect(sendMessage).not.toHaveBeenCalled();
+ });
+
+ it('rejects sendStickerMessage on a newsletter JID too (parity — sticker has its own path but same crash)', async () => {
+ const sendMessage = jest.fn();
+ const err = await readyAdapter({ sendMessage })
+ .sendStickerMessage(NEWSLETTER, { mimetype: 'image/webp', data: Buffer.from([1]).toString('base64') })
+ .catch((e: unknown) => e);
+ expect(err).toBeInstanceOf(ChannelMediaNotSupportedError);
+ expect((err as ChannelMediaNotSupportedError).getStatus()).toBe(501);
+ expect(sendMessage).not.toHaveBeenCalled();
+ });
+
+ it('still sends media on a user JID', async () => {
+ const sendMessage = jest.fn().mockResolvedValue({ id: { _serialized: 'M1' }, timestamp: 1 });
+ await expect(
+ readyAdapter({ sendMessage }).sendImageMessage(USER, {
+ mimetype: 'image/jpeg',
+ data: Buffer.from([1]).toString('base64'),
+ }),
+ ).resolves.toMatchObject({ id: 'M1' });
+ expect(sendMessage).toHaveBeenCalledWith(USER, expect.anything(), expect.anything());
+ });
+ });
});
describe('WhatsAppWebJsAdapter chat labels (add/remove via read-modify-write, Business-only)', () => {
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.ts b/src/engine/adapters/whatsapp-web-js.adapter.ts
index 1625f818b..5299e4d78 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.ts
@@ -43,6 +43,7 @@ import { EngineNotReadyError } from '../../common/errors/engine-not-ready.error'
import { EngineNotSupportedError } from '../../common/errors/engine-not-supported.error';
import { MessageNotFoundError } from '../../common/errors/message-not-found.error';
import { ChannelNotFoundError } from '../../common/errors/channel-not-found.error';
+import { ChannelMediaNotSupportedError } from '../../common/errors/channel-media-not-supported.error';
import { loadRemoteMediaBuffer } from '../../common/media/load-remote-media';
import {
GroupChat,
@@ -972,6 +973,7 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEngin
extraOptions?: { sendAudioAsVoice?: boolean },
): Promise<MessageResult> {
this.ensureReady();
+ this.ensureNotChannelRecipient(chatId);
let messageMedia: MessageMedia;
@@ -1127,6 +1129,10 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEngin
async sendStickerMessage(chatId: string, media: MediaInput): Promise<MessageResult> {
this.ensureReady();
+ // Sticker has its own send path (sendMediaAsSticker), not the sendMediaMessage funnel, but it
+ // hits the same channel crash: for a channel wwjs drops the sticker form and runs processMediaData
+ // with sendToChannel, which still ends at msg.avParams() (Utils.js:518). Guard it too (#673).
+ this.ensureNotChannelRecipient(chatId);
let messageMedia: MessageMedia;
if (typeof media.data === 'string') {
@@ -1865,4 +1871,13 @@ export class WhatsAppWebJsAdapter extends EventEmitter implements IWhatsAppEngin
throw new EngineNotReadyError();
}
}
+
+ private ensureNotChannelRecipient(chatId: string): void {
+ // whatsapp-web.js crashes building a channel media message (`msg.avParams is not a function`,
+ // upstream wwebjs#201823 — WA Web removed Msg.avParams). Text→channel works; media does not.
+ // Fail fast with a typed 501 instead of surfacing the raw TypeError as a 500 (#673).
+ if (isChannelJid(chatId)) {
+ throw new ChannelMediaNotSupportedError();
+ }
+ }
}