Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(plugins): validate the message:sending hook envelope

Yudhi Armyndharisca61faf90b01
diff --git a/CHANGELOG.md b/CHANGELOG.md
index be5dafc07..c7b1549c2 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
+- A `message:sending` plugin handler that returns a payload without a usable `input` no longer turns every outbound send on that session into a 500. The gate read `.input` off the hook chain's reply unchecked, so a malformed envelope handed `undefined` to each caller — or threw inside the gate for a `null` — with nothing in the error naming a plugin. The send is now refused with a message that names the hook, and refused rather than sent: this is a moderation chokepoint, and a handler whose reply cannot be read may have been redacting something. A chain that replies with nothing at all is unchanged and keeps the caller's own input.
+
- The `openwa_sessions_restricted` gauge now follows a restriction that lapses on its own. It was republished only when a restriction was recorded or cleared, and an expiry is neither — so after a reachout timelock passed its stated end, the gauge kept reporting the pre-expiry count while every read path already reported the session as unrestricted, leaving an alert on `> 0` firing indefinitely and disagreeing with `GET /api/sessions`.
- Both compose files now forward the inbound-media knobs — `MEDIA_DOWNLOAD_ENABLED`, `MEDIA_DOWNLOAD_MAX_BYTES`, `MEDIA_DOWNLOAD_TIMEOUT_MS` and `INBOUND_MEDIA_CONCURRENCY`. None of the four reached the container, so an operator running the bundled stack who set `MEDIA_DOWNLOAD_ENABLED=false` in `.env` got no error and no log line while the gateway kept decrypting every inbound media blob and base64-inlining it into every message row at up to 50 MiB apiece. The cap and the timeout were equally unreachable. A gate now binds all four, in both files.
diff --git a/src/core/hooks/sending-gate.spec.ts b/src/core/hooks/sending-gate.spec.ts
new file mode 100644
index 000000000..471b8293c
--- /dev/null
+++ b/src/core/hooks/sending-gate.spec.ts
@@ -0,0 +1,47 @@
+import { BadRequestException } from '@nestjs/common';
+import { applySendingGate } from './sending-gate';
+import { HookManager } from './hook-manager.service';
+
+/**
+ * The gate returned `(hookData as { input: T }).input` with no check that the hook chain's reply
+ * still carried an envelope. A `message:sending` handler returning any other truthy object made the
+ * gate hand `undefined` to the caller, which then dereferenced it — one plugin authoring mistake
+ * turned EVERY outbound send on that session into an unhandled TypeError and a 500 whose text named
+ * no plugin. A `data: null` made the gate itself throw. Nothing covered this file at all.
+ */
+describe('applySendingGate', () => {
+ const dto = { chatId: '[email protected]', text: 'hi' };
+ const gate = (result: unknown): Promise<typeof dto> =>
+ applySendingGate(
+ { execute: jest.fn().mockResolvedValue(result) } as unknown as HookManager,
+ 's1',
+ 'text',
+ dto,
+ 'MessageService',
+ );
+
+ it('returns the plugin-modified input when the envelope is intact', async () => {
+ const rewritten = { chatId: '[email protected]', text: 'redacted' };
+ await expect(gate({ continue: true, data: { input: rewritten } })).resolves.toEqual(rewritten);
+ });
+
+ it('returns the original input when the chain replies with no data at all', async () => {
+ await expect(gate({ continue: true })).resolves.toEqual(dto);
+ });
+
+ it('still refuses a send the plugin vetoed', async () => {
+ await expect(gate({ continue: false, data: { input: dto } })).rejects.toThrow(BadRequestException);
+ });
+
+ // Fail CLOSED, and say why. This is a moderation chokepoint: a handler whose reply cannot be read
+ // may have been trying to redact something, so proceeding with the original would be a bypass.
+ it.each([
+ ['a truthy object with no input', { continue: true, data: { notInput: 1 } }],
+ ['a null data', { continue: true, data: null }],
+ ['a primitive data', { continue: true, data: 'ok' }],
+ ['an input that is not an object', { continue: true, data: { input: 'text' } }],
+ ])('refuses the send when the hook returns %s', async (_label, result) => {
+ await expect(gate(result)).rejects.toThrow(BadRequestException);
+ await expect(gate(result)).rejects.toThrow(/message:sending/);
+ });
+});
diff --git a/src/core/hooks/sending-gate.ts b/src/core/hooks/sending-gate.ts
index 2a48c2fbd..718859acf 100644
--- a/src/core/hooks/sending-gate.ts
+++ b/src/core/hooks/sending-gate.ts
@@ -41,6 +41,30 @@ export async function applySendingGate<T extends object>(
if (!shouldContinue) {
throw new BadRequestException('Message sending blocked by plugin');
}
+ // A chain that replies with nothing changed nothing: keep the caller's own input. This is the
+ // ordinary no-plugin path and must not be treated as a malformed reply.
+ if (hookData === undefined) {
+ return input;
+ }
+ // Anything else must still be an envelope. Reading `.input` off it unchecked handed `undefined`
+ // (or threw, for a null) to every caller, so one plugin authoring mistake turned every outbound
+ // send on the session into an unhandled TypeError and a 500 that named no plugin.
+ //
+ // Fails CLOSED, deliberately: this is a moderation chokepoint, and a handler whose reply cannot be
+ // read may have been redacting something — proceeding with the original input would turn a plugin
+ // bug into a moderation bypass. Same status as a veto, because to the caller the outcome is the
+ // same (a plugin stopped this send); the message names the hook so the operator can find it.
+ const envelope = hookData as { input?: unknown } | null;
+ if (
+ typeof envelope !== 'object' ||
+ envelope === null ||
+ typeof envelope.input !== 'object' ||
+ envelope.input === null
+ ) {
+ throw new BadRequestException(
+ 'A message:sending handler returned a payload without a usable `input`; the send was refused rather than sent unmoderated',
+ );
+ }
// Use the potentially plugin-modified input.
- return (hookData as { input: T }).input;
+ return envelope.input as T;
}