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.

added rate limits

Steven Teyff93f0a0cae8
diff --git a/pages/api/edge/links/[key]/stats/[endpoint].ts b/pages/api/edge/links/[key]/stats/[endpoint].ts
index 7afbffca8b..4e4df1306d 100644
--- a/pages/api/edge/links/[key]/stats/[endpoint].ts
+++ b/pages/api/edge/links/[key]/stats/[endpoint].ts
@@ -2,6 +2,9 @@ import type { NextRequest } from "next/server";
import { getStats } from "#/lib/stats";
import { getLinkViaEdge } from "#/lib/planetscale";
import { isHomeHostname } from "#/lib/utils";
+import { ipAddress } from "@vercel/edge";
+import { LOCALHOST_IP } from "#/lib/constants";
+import { ratelimit } from "#/lib/upstash";
export const config = {
runtime: "edge",
@@ -15,6 +18,12 @@ export default async function handler(req: NextRequest) {
let domain = req.nextUrl.hostname;
if (isHomeHostname(domain)) domain = "dub.sh";
+ const ip = ipAddress(req) || LOCALHOST_IP;
+ const { success } = await ratelimit(10, "10 s").limit(ip);
+ if (!success) {
+ return new Response("Don't DDoS me pls 🥺", { status: 429 });
+ }
+
let data;
// if the link is NOT dub.sh/github (demo link)
if (!(domain === "dub.sh" && key === "github")) {