feat(core): allow custom request headers on notification channels

Operators can attach Authorization and other headers to a webhook
channel so authenticated providers work without a separate proxy.

Fixes #451
This commit is contained in:
DrSmoothl committed 2026-09-14 19:34:29 +08:00
1 parent 9ac39f480a
commit 60c9669abd
6 files changed
+166 -6

No files matched your search

@@ -137,6 +137,36 @@ describe('normalizeNotificationsConfig — total normalize', () => {
expect(channels[0].enabled).toBe(true);
expect(channels[0].name).toBe('c1');
expect(channels[0].bodyTemplate).toBe(DEFAULT_BODY_TEMPLATE);
expect(channels[0].headers).toBeUndefined();
});
it('keeps valid request headers and drops unsafe or empty ones', () => {
const { channels } = normalizeNotificationsConfig({
channels: [{
id: 'feishu',
url: 'https://open.feishu.cn/hook',
headers: {
Authorization: 'Bearer secret',
'X-Custom': 'ok',
'Bad Name': 'x',
'': 'x',
Inject: 'line\r\nX-Evil: 1',
Empty: ' ',
NotString: 1,
} as Record<string, unknown>,
}],
});
expect(channels[0].headers).toEqual({
Authorization: 'Bearer secret',
'X-Custom': 'ok',
});
});
it('omits an empty headers object after normalize', () => {
const { channels } = normalizeNotificationsConfig({
channels: [{ id: 'c1', url: 'https://a.com', headers: { 'Bad Name': 'x' } }],
});
expect(channels[0].headers).toBeUndefined();
});
});
@@ -57,6 +57,23 @@ describe('NotificationManager — dispatch + history', () => {
expect(posts).toEqual([{ url: 'https://hook.example/c1', body: 'offline:123:123' }]);
});
it('forwards channel request headers to the post collaborator', async () => {
const posts: { url: string; body: string; headers?: Record<string, string> }[] = [];
const { mgr } = setup({
loadConfig: () => ({
version: 1,
debounceSeconds: 30,
channels: [ch({ headers: { Authorization: 'Bearer secret' } })],
}),
post: async (url, body, headers) => {
posts.push({ url, body, headers });
return { ok: true, status: 200 };
},
});
await mgr.notify('123', 'offline');
expect(posts[0]?.headers).toEqual({ Authorization: 'Bearer secret' });
});
it('uses the nickname captured on an online/offline edge', async () => {
const { posts, mgr } = setup();
mgr.handleOnline('123', 'Alice'); // caches nickname; cold online → no post