From 0103932996c2cb891d7fa1d0388852872b08ef6c Mon Sep 17 00:00:00 2001 From: Tommaso Barbugli Date: Tue, 24 Nov 2026 17:32:02 +0100 Subject: [PATCH] fix(webhook): reject empty secret or signature in HMAC verification --- .../integration/webhook-signature.test.ts | 37 +++++++++++++++++++ src/StreamClient.ts | 14 +------ src/utils/webhook.ts | 3 ++ 3 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 __tests__/integration/webhook-signature.test.ts diff --git a/__tests__/integration/webhook-signature.test.ts b/__tests__/integration/webhook-signature.test.ts new file mode 100644 index 0000000..6b9e3b5 --- /dev/null +++ b/__tests__/integration/webhook-signature.test.ts @@ -0,0 +1,37 @@ +import crypto from 'crypto'; +import { describe, expect, it } from 'vitest'; +import { StreamClient } from '../../src/StreamClient'; +import { verifySignature } from '../../src/utils/webhook'; + +describe('webhook signature empty secret & signature guards', () => { + const payload = JSON.stringify({ type: 'message.new' }); + + it('rejects an empty secret even with a matching HMAC', () => { + const forged = crypto + .createHmac('sha256', Buffer.from('', 'utf8')) + .update(payload) + .digest('hex'); + + expect(verifySignature(payload, forged, '')).toBe(false); + }); + + it('rejects an empty signature', () => { + expect(verifySignature(payload, '', 'real-secret')).toBe(false); + }); + + it('rejects an empty signature on StreamClient.verifyWebhook', () => { + const client = new StreamClient('test-key', 'real-secret'); + + expect(client.verifyWebhook(payload, '')).toBe(false); + }); + + it('accepts a valid signature on StreamClient.verifyWebhook', () => { + const client = new StreamClient('test-key', 'real-secret'); + const valid = crypto + .createHmac('sha256', Buffer.from('real-secret', 'utf8')) + .update(payload) + .digest('hex'); + + expect(client.verifyWebhook(payload, valid)).toBe(true); + }); +}); diff --git a/src/StreamClient.ts b/src/StreamClient.ts index f38ac43..4d47a27 100644 --- a/src/StreamClient.ts +++ b/src/StreamClient.ts @@ -4,10 +4,10 @@ import { parseSns as parseSnsHelper, parseSqs as parseSqsHelper, verifyAndParseWebhook as verifyAndParseWebhookHelper, + verifySignature, } from './utils/webhook'; import { CommonApi } from './gen/common/CommonApi'; import { StreamVideoClient } from './StreamVideoClient'; -import crypto from 'crypto'; import { StreamChatClient } from './StreamChatClient'; import { CallTokenPayload, UserTokenPayload } from './types'; import { @@ -244,17 +244,7 @@ export class StreamClient extends CommonApi { }; verifyWebhook = (requestBody: string | Buffer, xSignature: string) => { - const key = Buffer.from(this.secret, 'utf8'); - const hash = crypto - .createHmac('sha256', key) - .update(requestBody) - .digest('hex'); - - try { - return crypto.timingSafeEqual(Buffer.from(hash), Buffer.from(xSignature)); - } catch { - return false; - } + return verifySignature(requestBody, xSignature, this.secret); }; /** diff --git a/src/utils/webhook.ts b/src/utils/webhook.ts index 7757951..6a5a9e8 100644 --- a/src/utils/webhook.ts +++ b/src/utils/webhook.ts @@ -155,6 +155,9 @@ export function verifySignature( signature: string, secret: string, ): boolean { + if (!secret || !signature) { + return false; + } const key = Buffer.from(secret, 'utf8'); const hash = crypto.createHmac('sha256', key).update(body).digest('hex'); try {