From 62f91cdeac6e2c0a43fbd391bdf52e72dab77870 Mon Sep 17 00:00:00 2001 From: Warren Lee <5959690+wrn14897@users.noreply.github.com> Date: Mon, 23 Nov 2026 18:28:24 +0800 Subject: [PATCH] fix(api): send a ClickStack User-Agent on generic alert webhooks The undici fetch adopted for DNS-pinned webhook delivery defaults to User-Agent: undici, which some receiver WAFs block with a 403. Send ClickStack-Alerts/ unless the webhook sets its own User-Agent, matched case-insensitively. Refs HDX-6170 --- .changeset/webhook-default-user-agent.md | 10 +++++ .../transports/__tests__/generic.test.ts | 40 +++++++++++++++++++ .../tasks/checkAlerts/transports/generic.ts | 11 ++++- packages/api/src/tasks/util.ts | 5 +++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 .changeset/webhook-default-user-agent.md diff --git a/.changeset/webhook-default-user-agent.md b/.changeset/webhook-default-user-agent.md new file mode 100644 index 0000000000..331b7c1ad0 --- /dev/null +++ b/.changeset/webhook-default-user-agent.md @@ -0,0 +1,10 @@ +--- +'@hyperdx/api': patch +--- + +fix: send a ClickStack User-Agent on generic alert webhooks + +Generic and incident.io alert webhooks sent `User-Agent: undici`, which some +receivers' firewalls block with a 403. They now send +`ClickStack-Alerts/`. A User-Agent set in the webhook's headers still +takes precedence. diff --git a/packages/api/src/tasks/checkAlerts/transports/__tests__/generic.test.ts b/packages/api/src/tasks/checkAlerts/transports/__tests__/generic.test.ts index a634d479c8..f66373294c 100644 --- a/packages/api/src/tasks/checkAlerts/transports/__tests__/generic.test.ts +++ b/packages/api/src/tasks/checkAlerts/transports/__tests__/generic.test.ts @@ -13,6 +13,7 @@ import { handleSendGenericWebhook, } from '@/tasks/checkAlerts/transports/generic'; import type { Message } from '@/tasks/checkAlerts/transports/types'; +import { WEBHOOK_USER_AGENT } from '@/tasks/util'; import { WebhookUrlValidationError } from '@/utils/validators'; const message: Message = { @@ -311,6 +312,45 @@ describe('the default incident.io body', () => { }); }); +describe('the User-Agent header', () => { + const originalFetch = global.fetch; + afterEach(() => { + global.fetch = originalFetch; + }); + + const sentHeaders = async (headers?: Record) => { + const fetchMock: any = jest.fn(async () => new Response('ok')); + global.fetch = fetchMock; + const headerChannel: any = { + type: 'webhook', + channel: { + ...webhook, + url: 'https://example.test/hook', + headers: headers && { toJSON: () => headers }, + }, + }; + await handleSendGenericWebhook(headerChannel, message); + return fetchMock.mock.calls[0][1].headers; + }; + + it('defaults to ClickStack-Alerts instead of undici', async () => { + expect((await sentHeaders())['User-Agent']).toBe(WEBHOOK_USER_AGENT); + expect(WEBHOOK_USER_AGENT.startsWith('ClickStack-Alerts')).toBe(true); + }); + + it('is replaced by a user-set User-Agent', async () => { + expect( + (await sentHeaders({ 'User-Agent': 'custom/1.0' }))['User-Agent'], + ).toBe('custom/1.0'); + }); + + it('is replaced by a user-set user-agent of any case', async () => { + const headers = await sentHeaders({ 'user-agent': 'custom/1.0' }); + expect(headers['user-agent']).toBe('custom/1.0'); + expect(headers).not.toHaveProperty('User-Agent'); + }); +}); + describe('a webhook saved without a body', () => { const originalFetch = global.fetch; afterEach(() => { diff --git a/packages/api/src/tasks/checkAlerts/transports/generic.ts b/packages/api/src/tasks/checkAlerts/transports/generic.ts index b4b2f04769..f898cbb36e 100644 --- a/packages/api/src/tasks/checkAlerts/transports/generic.ts +++ b/packages/api/src/tasks/checkAlerts/transports/generic.ts @@ -19,7 +19,7 @@ import type { Message, WebhookChannel, } from '@/tasks/checkAlerts/transports/types'; -import { escapeJsonString } from '@/tasks/util'; +import { escapeJsonString, WEBHOOK_USER_AGENT } from '@/tasks/util'; import { getCounter, getHistogram } from '@/utils/instrumentation'; import logger from '@/utils/logger'; import { withRetry } from '@/utils/retry'; @@ -208,9 +208,16 @@ const sendGenericWebhook = async ( // TODO: handle real webhook security and signage after v0 // X-HyperDX-Signature FROM PRIVATE SHA-256 HMAC, time based nonces, caching functionality etc + const userHeaders = toStringHeaderRecord(webhook.headers?.toJSON()); + // Without this undici sends `User-Agent: undici`, which some receiver WAFs + // block. Checked case-insensitively so a user's `user-agent` isn't sent twice. + const hasUserAgent = Object.keys(userHeaders).some( + k => k.toLowerCase() === 'user-agent', + ); const headers: Record = { 'Content-Type': 'application/json', // default, will be overwritten if user has set otherwise - ...toStringHeaderRecord(webhook.headers?.toJSON()), + ...(hasUserAgent ? {} : { 'User-Agent': WEBHOOK_USER_AGENT }), + ...userHeaders, // Stable per-alert key for receivers that honour Idempotency-Key; delivery is at-least-once. 'Idempotency-Key': objectHash({ eventId: message.eventId, diff --git a/packages/api/src/tasks/util.ts b/packages/api/src/tasks/util.ts index 5f199d3e9a..e9c1119118 100644 --- a/packages/api/src/tasks/util.ts +++ b/packages/api/src/tasks/util.ts @@ -1,7 +1,12 @@ import { set } from 'lodash'; +import { CODE_VERSION } from '@/config'; import logger from '@/utils/logger'; +export const WEBHOOK_USER_AGENT = CODE_VERSION + ? `ClickStack-Alerts/${CODE_VERSION}` + : 'ClickStack-Alerts'; + // transfer keys of attributes with dot into nested object // ex: { 'a.b': 'c', 'd.e.f': 'g' } -> { a: { b: 'c' }, d: { e: { f: 'g' } } } export const unflattenObject = (