Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Fix Qstash bypass implementation (#4358)

Steven Teyda6443aa1a93
diff --git a/apps/web/lib/cron/index.ts b/apps/web/lib/cron/index.ts
index c7f8930d5d..e85b0ddf01 100644
--- a/apps/web/lib/cron/index.ts
+++ b/apps/web/lib/cron/index.ts
@@ -3,10 +3,17 @@ import { Client } from "@upstash/qstash";
export const qstash = new Client({
baseUrl: process.env.QSTASH_URL || "https://qstash-us-east-1.upstash.io",
token: process.env.QSTASH_TOKEN || "",
- headers: {
- "x-vercel-protection-bypass":
- process.env.VERCEL_AUTOMATION_BYPASS_SECRET || "",
- },
+ ...(process.env.VERCEL_ENV === "preview" && {
+ headers: {
+ "x-vercel-protection-bypass":
+ process.env.VERCEL_AUTOMATION_BYPASS_SECRET || "",
+ },
+ }),
+});
+
+export const qstashWithoutBypass = new Client({
+ baseUrl: process.env.QSTASH_URL || "https://qstash-us-east-1.upstash.io",
+ token: process.env.QSTASH_TOKEN || "",
});
// Default batch size for cron jobs that process records in batches
diff --git a/apps/web/lib/cron/qstash-workflow.ts b/apps/web/lib/cron/qstash-workflow.ts
index a301907da2..62a31e427d 100644
--- a/apps/web/lib/cron/qstash-workflow.ts
+++ b/apps/web/lib/cron/qstash-workflow.ts
@@ -6,10 +6,12 @@ import { Client } from "@upstash/workflow";
const client = new Client({
baseUrl: process.env.QSTASH_URL || "https://qstash-us-east-1.upstash.io",
token: process.env.QSTASH_TOKEN || "",
- headers: {
- "x-vercel-protection-bypass":
- process.env.VERCEL_AUTOMATION_BYPASS_SECRET || "",
- },
+ ...(process.env.VERCEL_ENV === "preview" && {
+ headers: {
+ "x-vercel-protection-bypass":
+ process.env.VERCEL_AUTOMATION_BYPASS_SECRET || "",
+ },
+ }),
});
type WorkflowType = "partner-approved" | "create-partner-commission";
diff --git a/apps/web/lib/webhook/qstash.ts b/apps/web/lib/webhook/qstash.ts
index e10de21b0e..c37ab55cdf 100644
--- a/apps/web/lib/webhook/qstash.ts
+++ b/apps/web/lib/webhook/qstash.ts
@@ -1,4 +1,4 @@
-import { qstash } from "@/lib/cron";
+import { qstashWithoutBypass } from "@/lib/cron";
import { webhookPayloadSchema } from "@/lib/webhook/schemas";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { Webhook, WebhookReceiver } from "@prisma/client";
@@ -92,13 +92,25 @@ const publishWebhookEventToQStash = async ({
// TODO:
// Add deduplicationId to the webhook
- const response = await qstash.publishJSON({
+ // here, we use qstashWithoutBypass to avoid passing the
+ // Vercel automation bypass secret to arbitrary third party webhook receivers
+ const response = await qstashWithoutBypass.publishJSON({
url: webhook.url,
body: finalPayload,
headers: {
"Dub-Signature": signature,
"Upstash-Hide-Headers": "true",
+ // for Vercel preview (e2e tests), we need to pass the
+ // Vercel automation bypass secret to the callback URL
+ // see: https://upstash.com/docs/qstash/features/callbacks#configuring-callbacks
+ ...(process.env.VERCEL_ENV === "preview" && {
+ "Upstash-Callback-Forward-x-vercel-protection-bypass":
+ process.env.VERCEL_AUTOMATION_BYPASS_SECRET || "",
+ "Upstash-Failure-Callback-Forward-x-vercel-protection-bypass":
+ process.env.VERCEL_AUTOMATION_BYPASS_SECRET || "",
+ }),
+
// Integration specific headers
...(receiver === "segment" && {
"Upstash-Forward-Authorization": createSegmentBasicAuthHeader(
@@ -108,6 +120,8 @@ const publishWebhookEventToQStash = async ({
},
callback: callbackUrl.href,
failureCallback: failureCallbackUrl.href,
+
+ // for E2E tests, add a 5s delay
...(process.env.NODE_ENV === "test" && { delay: 5 }),
});