Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(web): add Slack request signature verification

Richie McIlroybdf7fc2d2c00
diff --git a/apps/web/__tests__/unit/slack-signature.test.ts b/apps/web/__tests__/unit/slack-signature.test.ts
new file mode 100644
index 000000000..6f2580437
--- /dev/null
+++ b/apps/web/__tests__/unit/slack-signature.test.ts
@@ -0,0 +1,64 @@
+import { describe, expect, it } from "vitest";
+import {
+ createSlackSignature,
+ verifySlackSignature,
+} from "@/lib/slack/signature";
+
+describe("Slack request signatures", () => {
+ const signingSecret = "test-signing-secret";
+ const timestamp = "1720000000";
+ const now = Number(timestamp) * 1000;
+ const body = '{"type":"event_callback"}';
+
+ it("accepts a current signature over the exact raw body", () => {
+ const signature = createSlackSignature({
+ body,
+ timestamp,
+ signingSecret,
+ });
+ expect(
+ verifySlackSignature({
+ body,
+ timestamp,
+ signature,
+ signingSecret,
+ now,
+ }),
+ ).toBe(true);
+ });
+
+ it("rejects tampering, missing headers, and stale requests", () => {
+ const signature = createSlackSignature({
+ body,
+ timestamp,
+ signingSecret,
+ });
+ expect(
+ verifySlackSignature({
+ body: `${body} `,
+ timestamp,
+ signature,
+ signingSecret,
+ now,
+ }),
+ ).toBe(false);
+ expect(
+ verifySlackSignature({
+ body,
+ timestamp: undefined,
+ signature,
+ signingSecret,
+ now,
+ }),
+ ).toBe(false);
+ expect(
+ verifySlackSignature({
+ body,
+ timestamp,
+ signature,
+ signingSecret,
+ now: now + 301_000,
+ }),
+ ).toBe(false);
+ });
+});
diff --git a/apps/web/lib/slack/signature.ts b/apps/web/lib/slack/signature.ts
new file mode 100644
index 000000000..cd65a5bce
--- /dev/null
+++ b/apps/web/lib/slack/signature.ts
@@ -0,0 +1,46 @@
+import { createHmac, timingSafeEqual } from "node:crypto";
+
+const SIGNATURE_VERSION = "v0";
+const DEFAULT_TOLERANCE_SECONDS = 60 * 5;
+
+export const createSlackSignature = ({
+ body,
+ timestamp,
+ signingSecret,
+}: {
+ body: string;
+ timestamp: string;
+ signingSecret: string;
+}) =>
+ `${SIGNATURE_VERSION}=${createHmac("sha256", signingSecret)
+ .update(`${SIGNATURE_VERSION}:${timestamp}:${body}`)
+ .digest("hex")}`;
+
+export const verifySlackSignature = ({
+ body,
+ timestamp,
+ signature,
+ signingSecret,
+ now = Date.now(),
+ toleranceSeconds = DEFAULT_TOLERANCE_SECONDS,
+}: {
+ body: string;
+ timestamp: string | undefined;
+ signature: string | undefined;
+ signingSecret: string;
+ now?: number;
+ toleranceSeconds?: number;
+}) => {
+ if (!timestamp || !signature || !/^\d+$/.test(timestamp)) return false;
+ const requestTime = Number(timestamp);
+ if (!Number.isSafeInteger(requestTime)) return false;
+ if (Math.abs(Math.floor(now / 1000) - requestTime) > toleranceSeconds) {
+ return false;
+ }
+
+ const expected = createSlackSignature({ body, timestamp, signingSecret });
+ const expectedBuffer = Buffer.from(expected);
+ const signatureBuffer = Buffer.from(signature);
+ if (expectedBuffer.length !== signatureBuffer.length) return false;
+ return timingSafeEqual(expectedBuffer, signatureBuffer);
+};