Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(wwebjs): answer a delete of a contact's status with 403
Yudhi Armyndharis573b6f3742cc
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex 6861386c8..5d1f93fc3 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -4416,7 +4416,7 @@ Delete one of the session's own posted statuses.The service returns `void`; the controller returns a fixed success object. DELETE default status is `200`.-**Errors:** `401` missing/invalid API key · `403` key lacks `OPERATOR` role · `404` `Session {id} not found or not connected` · `409` conflict or engine not ready (retryable) · `503` the whatsapp-web.js page died mid-request, so the revoke did not complete+**Errors:** `401` missing/invalid API key · `403` key lacks `OPERATOR` role, or (whatsapp-web.js) the id is not one of the account's own statuses · `404` `Session {id} not found or not connected` · `409` conflict or engine not ready (retryable) · `503` the whatsapp-web.js page died mid-request, so the revoke did not completeSafe to retry: revoking an already-revoked status converges. The status POST routes deliberately doNOT answer `503` for the same failure, because whatsapp-web.js can throw after the request is on thediff --git a/src/engine/adapters/wwebjs-status-delete.spec.ts b/src/engine/adapters/wwebjs-status-delete.spec.tsnew file mode 100644index 000000000..aa0131cda--- /dev/null+++ b/src/engine/adapters/wwebjs-status-delete.spec.ts@@ -0,0 +1,49 @@+import type { Client } from 'whatsapp-web.js';+import { WwebjsStatus } from './wwebjs-status';+import { WwebjsLifecycle } from './wwebjs-lifecycle';+import { createLogger } from '../../common/services/logger.service';+import { type WwebjsEngineHost } from './wwebjs-host';+import { EngineRefusedError } from '../../common/errors/engine-refused.error';+import { EngineTransportError } from '../../common/errors/engine-transport.error';++/**+ * Client.revokeStatusMessage refuses a status that is not the account's own with a bare page-side+ * string (Client.js revokeStatusMessage), which Puppeteer rejects with as-is. GET /status lists+ * contacts' statuses, so their ids reach DELETE /status/:id, and the refusal answered 500.+ */+const REFUSAL = 'Invalid usage! Can only revoke the message its from own status broadcast';++function makeStatus(outcome: unknown): { status: WwebjsStatus; reportIfPageTransportError: jest.Mock } {+ const client = { revokeStatusMessage: jest.fn().mockRejectedValue(outcome) };+ const reportIfPageTransportError = jest.fn();+ const host = {+ ensureReady: jest.fn(),+ getClient: () => client as unknown as Client,+ isPageTransportError: (error: unknown) => WwebjsLifecycle.prototype.isPageTransportError.call({}, error),+ reportIfPageTransportError,+ logger: createLogger('wwebjs-status-delete.spec'),+ } as unknown as WwebjsEngineHost;+ return { status: new WwebjsStatus(host), reportIfPageTransportError };+}++describe('deleteStatus on whatsapp-web.js', () => {+ it("answers the library's refusal of a foreign status with a 403", async () => {+ await expect(makeStatus(REFUSAL).status.deleteStatus('false_status@[email protected]')).rejects.toThrow(+ EngineRefusedError,+ );+ });++ it('still answers a dead page with a 503, even as a bare string', async () => {+ const { status, reportIfPageTransportError } = makeStatus('Protocol error: Target closed');++ await expect(status.deleteStatus('x')).rejects.toThrow(EngineTransportError);+ expect(reportIfPageTransportError).toHaveBeenCalled();+ });++ it('propagates any other failure unchanged', async () => {+ const other = new Error('Invalid usage! as an Error is not the library refusal');++ await expect(makeStatus(other).status.deleteStatus('x')).rejects.toBe(other);+ await expect(makeStatus('something else').status.deleteStatus('x')).rejects.toBe('something else');+ });+});diff --git a/src/engine/adapters/wwebjs-status.ts b/src/engine/adapters/wwebjs-status.tsindex da79f7137..ff868d203 100644--- a/src/engine/adapters/wwebjs-status.ts+++ b/src/engine/adapters/wwebjs-status.ts@@ -8,6 +8,7 @@ import {StatusResult,} from '../interfaces/whatsapp-engine.interface';import { SerializedWid } from '../types/whatsapp-web-js.types';+import { EngineRefusedError } from '../../common/errors/engine-refused.error';import { toMessageMedia } from './wwebjs-messaging';import { type WwebjsEngineHost, withPage, reportPageDeath } from './wwebjs-host';@@ -204,7 +205,17 @@ export class WwebjsStatus {this.host.ensureReady();// Revokes the caller's own status post. revokeStatusMessage resolves the message by id and// throws if it isn't fromMe/isn't a status — the statusId returned by postText/Image/VideoStatus- // (msg.id._serialized) is the id it expects.- await this.withPage('deleteStatus', () => this.client().revokeStatusMessage(statusId));+ // (msg.id._serialized) is the id it expects. That refusal arrives as a bare page-side string,+ // so a contact's status id (which GET /status lists) was an opaque 500; it is a 403. An id that+ // resolves to no message returns silently, which keeps the documented idempotent 200. Caught+ // outside withPage so a dead page still answers 503 first.+ try {+ await this.withPage('deleteStatus', () => this.client().revokeStatusMessage(statusId));+ } catch (error) {+ if (typeof error === 'string' && error.startsWith('Invalid usage!')) {+ throw new EngineRefusedError("Only the account's own statuses can be deleted");+ }+ throw error;+ }}}