Skip to content

Open source CapCut alternative.

TypeScript

fix(site): authenticate Vercel cron routes with CRON_SECRET

David Lec3348291df19
diff --git a/site/.env.example b/site/.env.example
index 05b60f1b..a52f0024 100644
--- a/site/.env.example
+++ b/site/.env.example
@@ -2,6 +2,10 @@ DATABASE_URL="postgresql://postgres:[PASSWORD]@[PROJECT-REF].pooler.supabase.com
DIRECT_URL="postgresql://postgres:[PASSWORD]@db.[PROJECT-REF].supabase.co:5432/postgres"
BETTER_AUTH_SECRET="replace-with-a-long-random-secret"
+
+# Vercel sends this as a bearer token on cron invocations; the cron routes
+# (analytics run, cut-cloud gc) reject requests without it.
+CRON_SECRET="replace-with-a-long-random-secret"
GOOGLE_CLIENT_ID="replace-with-google-web-client-id"
GOOGLE_CLIENT_SECRET="replace-with-google-web-client-secret"
diff --git a/site/src/app/api/analytics/run/route.ts b/site/src/app/api/analytics/run/route.ts
index aeb19674..7176241a 100644
--- a/site/src/app/api/analytics/run/route.ts
+++ b/site/src/app/api/analytics/run/route.ts
@@ -1,6 +1,6 @@
import { NextRequest, NextResponse } from "next/server";
-import { notFoundResponse } from "@/lib/donkey-api-auth";
+import { isVercelCron, notFoundResponse } from "@/lib/donkey-api-auth";
import { enqueueJob } from "@/lib/jobs/queue";
export const dynamic = "force-dynamic";
@@ -8,10 +8,10 @@ export const dynamic = "force-dynamic";
// response; give it room.
export const maxDuration = 300;
-// The nightly analytics run. Vercel's cron carries x-vercel-cron, which the
-// platform strips from outside traffic; manual runs — the dashboard's Run
-// button and per-day {day, force} retriggers — go through POST /api/jobs.
+// The nightly analytics run. Vercel's cron authenticates with the CRON_SECRET
+// bearer token; manual runs — the dashboard's Run button and per-day
+// {day, force} retriggers — go through POST /api/jobs.
export const GET = async (request: NextRequest) => {
- if (request.headers.get("x-vercel-cron") !== "1") return notFoundResponse();
+ if (!isVercelCron(request)) return notFoundResponse();
return NextResponse.json(await enqueueJob("analytics-daily", {}, "vercel-cron"));
};
diff --git a/site/src/app/api/cut-cloud/[[...slug]]/route.ts b/site/src/app/api/cut-cloud/[[...slug]]/route.ts
index 173a2260..1ab2315d 100644
--- a/site/src/app/api/cut-cloud/[[...slug]]/route.ts
+++ b/site/src/app/api/cut-cloud/[[...slug]]/route.ts
@@ -1,5 +1,5 @@
import type { NextRequest } from "next/server";
-import { withDonkeyAuth } from "@/lib/donkey-api-auth";
+import { isVercelCron, withDonkeyAuth } from "@/lib/donkey-api-auth";
import { runGc } from "@/cut/server/cloud/gc";
import { cutCloudCatchAll } from "@/cut/server/cloud/routes";
@@ -11,12 +11,11 @@ export const runtime = "nodejs";
const handle = withDonkeyAuth((request) => cutCloudCatchAll(request));
-// The daily GC cron has no session; Vercel marks its requests with the
-// x-vercel-cron header, which the platform strips from outside traffic.
-// Everything else goes through auth (a superuser can also GET /gc directly).
+// The daily GC cron has no session; Vercel authenticates its invocations with
+// the CRON_SECRET bearer token. Everything else goes through auth (a superuser
+// can also GET /gc directly).
export const GET = (request: NextRequest) =>
- new URL(request.url).pathname === "/api/cut-cloud/gc" &&
- request.headers.get("x-vercel-cron") === "1"
+ new URL(request.url).pathname === "/api/cut-cloud/gc" && isVercelCron(request)
? runGc()
: handle(request);
export const POST = handle;
diff --git a/site/src/cut/server/cloud/routes.ts b/site/src/cut/server/cloud/routes.ts
index fa9d58c3..9cb4c350 100644
--- a/site/src/cut/server/cloud/routes.ts
+++ b/site/src/cut/server/cloud/routes.ts
@@ -95,7 +95,7 @@ const CUT_CLOUD_ROUTES: CloudRoute[] = [
{ method: "GET", path: "/api/cut-cloud/usage", handler: (_r, u) => usageApi.get(u) },
// GC also runs unauthenticated from the Vercel cron — see the catch-all
- // route, which checks the x-vercel-cron header before auth. Any other
+ // route, which checks the CRON_SECRET bearer token before auth. Any other
// non-superuser caller sees a plain 404.
{
method: "GET",
diff --git a/site/src/lib/donkey-api-auth.ts b/site/src/lib/donkey-api-auth.ts
index 3f0d18fd..0ea5274f 100644
--- a/site/src/lib/donkey-api-auth.ts
+++ b/site/src/lib/donkey-api-auth.ts
@@ -61,6 +61,13 @@ export function forbiddenResponse() {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
+// True when the request comes from Vercel's cron scheduler, which sends the
+// project's CRON_SECRET env var as a bearer token on every invocation.
+export function isVercelCron(request: Request): boolean {
+ const secret = process.env.CRON_SECRET;
+ return !!secret && request.headers.get("authorization") === `Bearer ${secret}`;
+}
+
export type DonkeyAuthHandler<
TReq extends DonkeyAuthenticatedRequest = DonkeyAuthenticatedRequest,
TArgs extends unknown[] = [],