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.

Updates to ratelimit & cron implementation

Steven Tey4ea65b2659e3
diff --git a/apps/web/app/api/cron/domains/route.ts b/apps/web/app/api/cron/domains/route.ts
index aafb52cf16..b5558525a9 100644
--- a/apps/web/app/api/cron/domains/route.ts
+++ b/apps/web/app/api/cron/domains/route.ts
@@ -3,7 +3,7 @@ import {
getDomainResponse,
verifyDomain,
} from "@/lib/api/domains";
-import { receiver } from "@/lib/cron";
+import { receiver, verifySignature } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { log } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -17,16 +17,10 @@ import { handleDomainUpdates } from "./utils";
**/
// Runs every 3 hours (0 */3 * * *)
-export async function POST(req: Request) {
- const body = await req.json();
- if (process.env.VERCEL === "1") {
- const isValid = await receiver.verify({
- signature: req.headers.get("Upstash-Signature") || "",
- body: JSON.stringify(body),
- });
- if (!isValid) {
- return new Response("Unauthorized", { status: 401 });
- }
+export async function GET(req: Request) {
+ const validSignature = await verifySignature(req);
+ if (!validSignature) {
+ return new Response("Unauthorized", { status: 401 });
}
try {
diff --git a/apps/web/app/api/cron/usage/route.ts b/apps/web/app/api/cron/usage/route.ts
index e8d3c4bea4..8701fae5a4 100644
--- a/apps/web/app/api/cron/usage/route.ts
+++ b/apps/web/app/api/cron/usage/route.ts
@@ -1,4 +1,4 @@
-import { receiver } from "@/lib/cron";
+import { receiver, verifySignature } from "@/lib/cron";
import { log } from "@dub/utils";
import { NextResponse } from "next/server";
import { updateUsage } from "./utils";
@@ -6,16 +6,10 @@ import { updateUsage } from "./utils";
// Cron to update the usage stats of each project.
// Runs once every day at 7AM PST (0 14 * * *)
-export async function POST(req: Request) {
- const body = await req.json();
- if (process.env.VERCEL === "1") {
- const isValid = await receiver.verify({
- signature: req.headers.get("Upstash-Signature") || "",
- body: JSON.stringify(body),
- });
- if (!isValid) {
- return new Response("Unauthorized", { status: 401 });
- }
+export async function GET(req: Request) {
+ const validSignature = await verifySignature(req);
+ if (!validSignature) {
+ return new Response("Unauthorized", { status: 401 });
}
try {
diff --git a/apps/web/lib/cron.ts b/apps/web/lib/cron.ts
index b7b9447cd0..2a62836994 100644
--- a/apps/web/lib/cron.ts
+++ b/apps/web/lib/cron.ts
@@ -16,3 +16,20 @@ export const receiver = new Receiver({
export const qstash = new Client({
token: process.env.QSTASH_TOKEN || "",
});
+
+export const verifySignature = async (req: Request) => {
+ const authHeader = req.headers.get("authorization");
+
+ // if we're on Vercel and:
+ // 1. there's no CRON_SECRET env var
+ // 2. request doesn't have the correct auth header,
+ // then return false
+ if (
+ process.env.VERCEL === "1" &&
+ (!process.env.CRON_SECRET ||
+ authHeader !== `Bearer ${process.env.CRON_SECRET}`)
+ ) {
+ return false;
+ }
+ return true;
+};
diff --git a/apps/web/lib/upstash.ts b/apps/web/lib/upstash.ts
index 0d721dbf46..253eb885a1 100644
--- a/apps/web/lib/upstash.ts
+++ b/apps/web/lib/upstash.ts
@@ -18,24 +18,20 @@ export const ratelimit = (
| `${number} h`
| `${number} d` = "10 s",
) => {
- return process.env.UPSTASH_REDIS_REST_URL &&
- process.env.UPSTASH_REDIS_REST_TOKEN
- ? new Ratelimit({
- redis: Redis.fromEnv(),
- limiter: Ratelimit.slidingWindow(requests, seconds),
- analytics: true,
- })
- : // if Redis is not configured, return a dummy ratelimiter
- // with the function limit() that always returns true
- {
- limit: () => ({
- success: true,
- limit: 10,
- remaining: 10,
- reset: 0,
- retryAfter: 0,
- }),
- };
+ return new Ratelimit({
+ redis: new Redis({
+ url:
+ process.env.RATELIMIT_UPSTASH_REDIS_REST_URL ||
+ process.env.UPSTASH_REDIS_REST_URL ||
+ "",
+ token:
+ process.env.RATELIMIT_UPSTASH_REDIS_REST_TOKEN ||
+ process.env.UPSTASH_REDIS_REST_TOKEN ||
+ "",
+ }),
+ limiter: Ratelimit.slidingWindow(requests, seconds),
+ analytics: true,
+ });
};
// only for dub.sh public demo
diff --git a/apps/web/vercel.json b/apps/web/vercel.json
new file mode 100644
index 0000000000..c2e560207c
--- /dev/null
+++ b/apps/web/vercel.json
@@ -0,0 +1,12 @@
+{
+ "crons": [
+ {
+ "path": "/api/cron/domains",
+ "schedule": "0 */3 * * *"
+ },
+ {
+ "path": "/api/cron/domains",
+ "schedule": "0 14 * * *"
+ }
+ ]
+}