Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/webhook-default-user-agent.md
Original file line number Diff line number Diff line change
@@ -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/<version>`. A User-Agent set in the webhook's headers still
takes precedence.
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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<string, string>) => {
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(() => {
Expand Down
11 changes: 9 additions & 2 deletions packages/api/src/tasks/checkAlerts/transports/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<string, string> = {
'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,
Expand Down
5 changes: 5 additions & 0 deletions packages/api/src/tasks/util.ts
Original file line number Diff line number Diff line change
@@ -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 = (
Expand Down
Loading