Skip to content

Commit

Permalink
conditionally set bodyType
Browse files Browse the repository at this point in the history
  • Loading branch information
steven-tey committed Jan 12, 2025
1 parent c1f8c91 commit 18d2f21
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion apps/web/app/api/webhooks/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const searchParamsSchema = z.object({
export const POST = async (req: Request) => {
const rawBody = await req.text();

await verifyQstashSignature(req, rawBody);
await verifyQstashSignature(req, rawBody, "text");

const { url, status, body, sourceBody, sourceMessageId } =
webhookCallbackSchema.parse(JSON.parse(rawBody));
Expand Down
10 changes: 7 additions & 3 deletions apps/web/lib/cron/verify-qstash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ const receiver = new Receiver({
nextSigningKey: process.env.QSTASH_NEXT_SIGNING_KEY || "",
});

export const verifyQstashSignature = async (req: Request, body?: any) => {
body = body || (await req.text());
export const verifyQstashSignature = async (
req: Request,
body: any,
bodyType: "json" | "text" = "json",
) => {
body = body || (bodyType === "json" ? await req.json() : await req.text());

const isValid = await receiver.verify({
signature: req.headers.get("Upstash-Signature") || "",
body,
body: bodyType === "json" ? JSON.stringify(body) : body,
});

if (!isValid) {
Expand Down

0 comments on commit 18d2f21

Please sign in to comment.