@checkstack/notification-discord-backend 0.1.63 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,41 @@
1
1
  # @checkstack/notification-discord-backend
2
2
 
3
+ ## 0.2.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [bd41130]
8
+ - Updated dependencies [bd41130]
9
+ - Updated dependencies [bd41130]
10
+ - @checkstack/backend-api@0.32.0
11
+ - @checkstack/notification-backend@1.8.0
12
+
13
+ ## 0.2.0
14
+
15
+ ### Minor Changes
16
+
17
+ - 43e4484: Harden the Discord, Slack, Gotify, and Backstage channels against SSRF. Each
18
+ POSTs to a configured arbitrary host (Discord/Slack incoming webhook, Gotify
19
+ server URL, Backstage base URL); these now run the shared `validateWebhookUrl`
20
+ pre-flight and send with `redirect: "error"` so a receiver cannot
21
+ `302`-redirect the request at a blocked host past the pre-flight. The pre-flight
22
+ blocks only the classic exfiltration / pivot targets (loopback, `0.0.0.0/8`,
23
+ cloud-metadata, link-local, IPv6 ULA) and ALLOWS internal RFC1918 hosts, so
24
+ self-hosted internal receivers keep working. Same defense-in-depth already
25
+ applied to the Webhook channel. (Pushover, Telegram, Teams, and Webex POST to
26
+ hard-coded vendor hosts and are unaffected.)
27
+
28
+ ### Patch Changes
29
+
30
+ - Updated dependencies [43e4484]
31
+ - Updated dependencies [43e4484]
32
+ - Updated dependencies [43e4484]
33
+ - Updated dependencies [43e4484]
34
+ - Updated dependencies [43e4484]
35
+ - Updated dependencies [43e4484]
36
+ - @checkstack/backend-api@0.31.1
37
+ - @checkstack/notification-backend@1.7.0
38
+
3
39
  ## 0.1.63
4
40
 
5
41
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@checkstack/notification-discord-backend",
3
- "version": "0.1.63",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "checkstack": {
@@ -18,9 +18,9 @@
18
18
  "pack": "bunx @checkstack/scripts plugin-pack"
19
19
  },
20
20
  "dependencies": {
21
- "@checkstack/backend-api": "0.31.0",
21
+ "@checkstack/backend-api": "0.32.0",
22
22
  "@checkstack/common": "0.22.0",
23
- "@checkstack/notification-backend": "1.6.7",
23
+ "@checkstack/notification-backend": "1.8.0",
24
24
  "zod": "^4.2.1"
25
25
  },
26
26
  "devDependencies": {
package/src/index.test.ts CHANGED
@@ -1,10 +1,44 @@
1
- import { describe, it, expect, spyOn } from "bun:test";
1
+ import { describe, it, expect, spyOn, mock } from "bun:test";
2
+ import type { Logger } from "@checkstack/backend-api";
3
+ import type { NotificationSendContext } from "@checkstack/backend-api";
2
4
  import {
3
5
  discordConfigSchemaV1,
4
6
  discordUserConfigSchema,
5
7
  buildDiscordEmbed,
8
+ discordStrategy,
6
9
  } from "./index";
7
10
 
11
+ function makeLogger(): Logger {
12
+ return {
13
+ info: mock(() => {}),
14
+ error: mock(() => {}),
15
+ warn: mock(() => {}),
16
+ debug: mock(() => {}),
17
+ };
18
+ }
19
+
20
+ function makeContext(
21
+ webhookUrl: string,
22
+ ): NotificationSendContext<
23
+ Record<string, never>,
24
+ { webhookUrl: string }
25
+ > {
26
+ return {
27
+ user: { userId: "u1" },
28
+ contact: webhookUrl,
29
+ notification: {
30
+ title: "Alert",
31
+ body: "body",
32
+ importance: "info",
33
+ type: "test",
34
+ },
35
+ strategyConfig: {},
36
+ userConfig: { webhookUrl },
37
+ layoutConfig: undefined,
38
+ logger: makeLogger(),
39
+ };
40
+ }
41
+
8
42
  /**
9
43
  * Unit tests for the Discord Notification Strategy.
10
44
  *
@@ -184,4 +218,54 @@ describe("Discord Notification Strategy", () => {
184
218
  }
185
219
  });
186
220
  });
221
+
222
+ // ─────────────────────────────────────────────────────────────────────────
223
+ // SSRF hardening (user-supplied webhook URL)
224
+ // ─────────────────────────────────────────────────────────────────────────
225
+
226
+ describe("SSRF hardening", () => {
227
+ it("rejects a webhook URL that resolves to a blocked host before dispatch", async () => {
228
+ const fetchSpy = spyOn(globalThis, "fetch");
229
+ try {
230
+ const result = await discordStrategy.send(
231
+ makeContext("http://169.254.169.254/latest/meta-data"),
232
+ );
233
+ expect(result.success).toBe(false);
234
+ // The SSRF pre-flight must fail closed: no request is ever dispatched.
235
+ expect(fetchSpy).not.toHaveBeenCalled();
236
+ } finally {
237
+ fetchSpy.mockRestore();
238
+ }
239
+ });
240
+
241
+ it("refuses redirects so a 302 to a blocked host is not followed", async () => {
242
+ let targetHit = false;
243
+ const fetchSpy = spyOn(globalThis, "fetch").mockImplementation((async (
244
+ _url: RequestInfo | URL,
245
+ init?: RequestInit,
246
+ ) => {
247
+ // Emulate real fetch semantics under `redirect: "error"`: a 3xx rejects
248
+ // rather than being followed. (End-to-end proof with a real server lives
249
+ // in notification-backend's post-json.test.ts.)
250
+ if (init?.redirect === "error") {
251
+ throw new TypeError("unexpected redirect");
252
+ }
253
+ targetHit = true;
254
+ return new Response(null, { status: 200 });
255
+ }) as unknown as typeof fetch);
256
+ try {
257
+ // A public IP literal passes the pre-flight (no DNS, not in a denied
258
+ // range), so the request is dispatched with redirect refusal.
259
+ const result = await discordStrategy.send(
260
+ makeContext("https://93.184.216.34/hook"),
261
+ );
262
+ expect(result.success).toBe(false);
263
+ expect(targetHit).toBe(false);
264
+ const init = fetchSpy.mock.calls[0]?.[1] as RequestInit | undefined;
265
+ expect(init?.redirect).toBe("error");
266
+ } finally {
267
+ fetchSpy.mockRestore();
268
+ }
269
+ });
270
+ });
187
271
  });
package/src/index.ts CHANGED
@@ -12,6 +12,7 @@ import {
12
12
  import {
13
13
  notificationStrategyExtensionPoint,
14
14
  postJson,
15
+ validateWebhookUrl,
15
16
  IMPORTANCE_EMOJI,
16
17
  renderSubjectsAsMarkdown,
17
18
  } from "@checkstack/notification-backend";
@@ -174,6 +175,17 @@ const discordStrategy: NotificationStrategy<DiscordConfig, DiscordUserConfig> =
174
175
  };
175
176
  }
176
177
 
178
+ // SSRF guard: the webhook URL is user-supplied. Reject it up front if it
179
+ // resolves to an internal/reserved address, and refuse redirects below so
180
+ // a receiver cannot 3xx us at an internal host past this pre-flight.
181
+ const validation = await validateWebhookUrl({ url: userConfig.webhookUrl });
182
+ if (!validation.ok) {
183
+ logger.warn(
184
+ `Blocked Discord delivery to ${userConfig.webhookUrl}: ${validation.error}`,
185
+ );
186
+ return { success: false, error: validation.error };
187
+ }
188
+
177
189
  // Build the embed
178
190
  const embed = buildDiscordEmbed({
179
191
  title: notification.title,
@@ -187,6 +199,7 @@ const discordStrategy: NotificationStrategy<DiscordConfig, DiscordUserConfig> =
187
199
  const result = await postJson({
188
200
  url: userConfig.webhookUrl,
189
201
  body: { embeds: [embed] },
202
+ redirect: "error",
190
203
  serviceName: "Discord",
191
204
  logger,
192
205
  });
@@ -219,6 +232,11 @@ export default createBackendPlugin({
219
232
  * public API surface.
220
233
  * @internal
221
234
  */
222
- export { discordConfigSchemaV1, discordUserConfigSchema, buildDiscordEmbed };
235
+ export {
236
+ discordConfigSchemaV1,
237
+ discordUserConfigSchema,
238
+ buildDiscordEmbed,
239
+ discordStrategy,
240
+ };
223
241
  /** @internal */
224
242
  export type { DiscordConfig, DiscordUserConfig, DiscordEmbedOptions };