Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(webhook): update only the carried fields and 404 on a deleted row
Yudhi Armyndharis1c005413c912
diff --git a/src/modules/webhook/webhook.service.spec.ts b/src/modules/webhook/webhook.service.spec.tsindex d77be1d81..0a692d4f2 100644--- a/src/modules/webhook/webhook.service.spec.ts+++ b/src/modules/webhook/webhook.service.spec.ts@@ -377,7 +377,10 @@ describe('WebhookService', () => {it('should update only provided fields', async () => {const webhook = createMockWebhook();(repository.findOne as jest.Mock).mockResolvedValue(webhook);- (repository.save as jest.Mock).mockImplementation(w => Promise.resolve(w));+ (repository.update as jest.Mock).mockImplementation((_where, patch: Partial<Webhook>) => {+ Object.assign(webhook, patch);+ return Promise.resolve({ affected: 1 });+ });const result = await service.update('sess-1', 'wh-uuid-1', { url: 'https://new-url.com/hook' });@@ -385,6 +388,29 @@ describe('WebhookService', () => {expect(result.events).toEqual(['message.received']); // unchanged});+ it('writes only the fields the request carries, scoped to the row as it exists now', async () => {+ const webhook = createMockWebhook();+ (repository.findOne as jest.Mock).mockResolvedValue(webhook);+ (repository.update as jest.Mock).mockResolvedValue({ affected: 1 });++ await service.update('sess-1', 'wh-uuid-1', { active: false, secret: '' });++ expect(repository.update).toHaveBeenCalledWith(+ { id: 'wh-uuid-1', sessionId: 'sess-1' },+ { active: false, secret: null },+ );+ expect(repository.save).not.toHaveBeenCalled();+ });++ it('answers 404 instead of re-creating a webhook deleted while the update was in flight', async () => {+ const webhook = createMockWebhook();+ (repository.findOne as jest.Mock).mockResolvedValue(webhook);+ (repository.update as jest.Mock).mockResolvedValue({ affected: 0 });++ await expect(service.update('sess-1', 'wh-uuid-1', { active: false })).rejects.toThrow(NotFoundException);+ expect(repository.save).not.toHaveBeenCalled();+ });+it('rejects a URL carrying userinfo on update and leaves the stored URL unchanged', async () => {const webhook = createMockWebhook();(repository.findOne as jest.Mock).mockResolvedValue(webhook);@@ -393,7 +419,7 @@ describe('WebhookService', () => {service.update('sess-1', 'wh-uuid-1', { url: 'https://user:[email protected]/hook' }),).rejects.toMatchObject({ status: 400 });expect(webhook.url).toBe('https://example.com/webhook');- expect(repository.save).not.toHaveBeenCalled();+ expect(repository.update).not.toHaveBeenCalled();});describe('with the SSRF guard on', () => {@@ -407,15 +433,14 @@ describe('WebhookService', () => {it('saves an edit that re-sends an unchanged URL the guard would now refuse', async () => {const webhook = createMockWebhook({ url: 'https://169.254.169.254/hook' });(repository.findOne as jest.Mock).mockResolvedValue(webhook);- (repository.save as jest.Mock).mockImplementation(w => Promise.resolve(w));+ (repository.update as jest.Mock).mockResolvedValue({ affected: 1 });- const result = await service.update('sess-1', 'wh-uuid-1', {+ await service.update('sess-1', 'wh-uuid-1', {url: 'https://169.254.169.254/hook',active: false,});- expect(result.active).toBe(false);- expect(repository.save).toHaveBeenCalledWith(expect.objectContaining({ active: false }));+ expect(repository.update).toHaveBeenCalledWith({ id: 'wh-uuid-1', sessionId: 'sess-1' }, { active: false });});it('still refuses a changed URL the guard blocks', async () => {@@ -426,7 +451,7 @@ describe('WebhookService', () => {service.update('sess-1', 'wh-uuid-1', { url: 'https://169.254.169.254/hook', active: false }),).rejects.toMatchObject({ status: 400 });expect(webhook.url).toBe('https://example.com/webhook');- expect(repository.save).not.toHaveBeenCalled();+ expect(repository.update).not.toHaveBeenCalled();});});});diff --git a/src/modules/webhook/webhook.service.ts b/src/modules/webhook/webhook.service.tsindex 81d52ed91..c0c582580 100644--- a/src/modules/webhook/webhook.service.ts+++ b/src/modules/webhook/webhook.service.ts@@ -220,24 +220,32 @@ export class WebhookService implements OnModuleInit, OnModuleDestroy {async update(sessionId: string, id: string, dto: UpdateWebhookDto): Promise<Webhook> {const webhook = await this.findOne(sessionId, id);+ const patch: Partial<Pick<Webhook, 'url' | 'events' | 'secret' | 'headers' | 'filters' | 'active' | 'retryCount'>> =+ {};// An unchanged URL is not re-validated: every edit re-sends it, so a webhook whose host stopped// resolving or became SSRF-blocked could not otherwise be deactivated or re-filtered. Delivery// still checks the URL on every send.if (dto.url !== undefined && dto.url !== webhook.url) {await this.validateWebhookUrl(dto.url);- webhook.url = dto.url;+ patch.url = dto.url;}- if (dto.events !== undefined) webhook.events = dto.events;+ if (dto.events !== undefined) patch.events = dto.events;// Normalize empty string to null (parity with create) — an empty secret means "no HMAC",// not a stored blank that silently disables signing while looking configured.- if (dto.secret !== undefined) webhook.secret = dto.secret || null;- if (dto.headers !== undefined) webhook.headers = dto.headers;- if (dto.filters !== undefined) webhook.filters = dto.filters;- if (dto.active !== undefined) webhook.active = dto.active;- if (dto.retryCount !== undefined) webhook.retryCount = dto.retryCount;+ if (dto.secret !== undefined) patch.secret = dto.secret || null;+ if (dto.headers !== undefined) patch.headers = dto.headers;+ if (dto.filters !== undefined) patch.filters = dto.filters;+ if (dto.active !== undefined) patch.active = dto.active;+ if (dto.retryCount !== undefined) patch.retryCount = dto.retryCount;+ if (Object.keys(patch).length === 0) return webhook;- return this.webhookRepository.save(webhook);+ // A conditional UPDATE of the carried fields, not save() of the entity read above: save() would+ // re-insert a row a concurrent DELETE removed during the URL check, and write back stale values+ // of every column this request did not touch.+ const result = await this.webhookRepository.update({ id, sessionId }, patch);+ if (!result.affected) throw new NotFoundException(`Webhook with id '${id}' not found`);+ return this.findOne(sessionId, id);}async delete(sessionId: string, id: string): Promise<void> {