Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): check the chat before ownership on edit

Yudhi Armyndharisaffb96c24138
diff --git a/src/engine/adapters/baileys-messaging.ts b/src/engine/adapters/baileys-messaging.ts
index afe8b9685..d16bc69c6 100644
--- a/src/engine/adapters/baileys-messaging.ts
+++ b/src/engine/adapters/baileys-messaging.ts
@@ -557,6 +557,9 @@ export class BaileysMessaging {
async editMessage(chatId: string, messageId: string, body: string, mentions?: string[]): Promise<MessageResult> {
this.host.ensureReady();
const target = await this.requireStored(messageId);
+ // The chat check comes first so a message from another chat answers not-found whichever side
+ // sent it; checking fromMe first would tell an inbound message there from an own one.
+ this.assertStoredInChat(target, chatId, messageId);
// Only the account's own messages are editable: WhatsApp refuses the edit of an inbound message
// but the send would still resolve, dressing the refusal up as success (and the service layer
// would then "update" the stored body). Refuse first — mirrors the wwjs null-edit guard.
@@ -565,7 +568,6 @@ export class BaileysMessaging {
`the edit of message ${messageId} was rejected — only the account's own messages can be edited`,
);
}
- this.assertStoredInChat(target, chatId, messageId);
// An edit keeps the original message id, so it is neither re-persisted nor echoed as a new send.
// The destination is resolved like any other send: a lid-migrated contact rejects PN-addressed
// sends with ack error 463 (see toDeliverableJid).
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 42732b65c..d9959a02b 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -3914,6 +3914,16 @@ describe('BaileysAdapter store-backed ops', () => {
expect(fakeSock.sendMessage).not.toHaveBeenCalled();
});
+ it('editMessage answers not-found for an inbound message from another chat, like an own one', async () => {
+ // Refusing the inbound one would tell a caller fenced to one chat who sent a message elsewhere.
+ fakeStore.getMessage.mockResolvedValue({ ...stored, key: { ...stored.key, remoteJid: '[email protected]' } });
+ const adapter = await ready();
+ await expect(adapter.editMessage('[email protected]', 'TARGET', 'x')).rejects.toBeInstanceOf(
+ MessageNotFoundError,
+ );
+ expect(fakeSock.sendMessage).not.toHaveBeenCalled();
+ });
+
it('editMessage matches the chat across dialects (@c.us request vs @s.whatsapp.net stored key)', async () => {
fakeStore.getMessage.mockResolvedValue(ownStored);
fakeSock.sendMessage.mockResolvedValue(undefined);