mirror of
https://github.com/outline/outline.git
synced 2026-12-06 11:12:32 +03:00
* wip * fix: Address review feedback on Granola integration - Limit the size of Granola API responses - Load the team when authorizing notes into personal collections - Add the user's connection from granola.list to the integrations store * feat: Connect Granola from a dialog * fix: Accept Granola webhook test events * Copy pass * Copy pass
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import crypto from "node:crypto";
|
|
import type { Next } from "koa";
|
|
import type { Context } from "@server/types";
|
|
import { safeEqual } from "@server/utils/crypto";
|
|
|
|
export default function validateWebhook({
|
|
secretKey,
|
|
getSignatureFromHeader,
|
|
hmacSign = true,
|
|
}: {
|
|
secretKey: string | ((ctx: Context) => Promise<string | undefined>);
|
|
getSignatureFromHeader: (ctx: Context) => string | undefined;
|
|
hmacSign?: boolean;
|
|
}) {
|
|
return async function validateWebhookMiddleware(ctx: Context, next: Next) {
|
|
const { rawBody } = ctx.request;
|
|
const signatureFromHeader = getSignatureFromHeader(ctx);
|
|
|
|
if (!signatureFromHeader) {
|
|
ctx.status = 401;
|
|
ctx.body = "Missing signature header";
|
|
return;
|
|
}
|
|
|
|
const key =
|
|
typeof secretKey === "function" ? await secretKey(ctx) : secretKey;
|
|
|
|
if (!key) {
|
|
ctx.status = 401;
|
|
ctx.body = "Invalid signature";
|
|
return;
|
|
}
|
|
|
|
let computedSignature = key;
|
|
if (hmacSign) {
|
|
// The signature covers the exact bytes sent, not the parsed body.
|
|
if (rawBody === undefined) {
|
|
ctx.status = 401;
|
|
ctx.body = "Invalid signature";
|
|
return;
|
|
}
|
|
computedSignature = crypto
|
|
.createHmac("sha256", key)
|
|
.update(rawBody)
|
|
.digest("hex");
|
|
}
|
|
|
|
if (!safeEqual(computedSignature, signatureFromHeader)) {
|
|
ctx.status = 401;
|
|
ctx.body = "Invalid signature";
|
|
return;
|
|
}
|
|
|
|
return next();
|
|
};
|
|
}
|