The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add rate limiting
Steven Teyc9b389d3f38f
diff --git a/apps/web/app/api/links/route.ts b/apps/web/app/api/links/route.tsindex bec5a14f6c..65f25dd53a 100644--- a/apps/web/app/api/links/route.ts+++ b/apps/web/app/api/links/route.ts@@ -1,8 +1,10 @@import { addLink, getLinksForProject, processLink } from "@/lib/api/links";import { withAuth } from "@/lib/auth";+import { ratelimit } from "@/lib/upstash";import {DUB_PROJECT_ID,GOOGLE_FAVICON_URL,+ LOCALHOST_IP,getApexDomain,log,} from "@dub/utils";@@ -47,6 +49,18 @@ export const POST = withAuth(return new Response("Missing or invalid body.", { status: 400, headers });}+ if (!session) {+ const ip = req.headers.get("x-forwarded-for") || LOCALHOST_IP;+ const { success } = await ratelimit(10, "1 d").limit(ip);++ if (!success) {+ return new Response(+ "Rate limited – you can only create up to 10 links per day without an account.",+ { status: 429 },+ );+ }+ }+const { link, error, status } = await processLink({payload: body,project,@@ -92,5 +106,6 @@ export const POST = withAuth(},{needNotExceededUsage: true,+ allowAnonymous: true,},);diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.tsindex 0e9d697839..04b1836e4a 100644--- a/apps/web/lib/api/links.ts+++ b/apps/web/lib/api/links.ts@@ -242,7 +242,7 @@ export async function processLink({}} else {// if it's not a custom project, do some filtering- if (key.includes("/")) {+ if (key?.includes("/")) {return {link: payload,error:diff --git a/apps/web/lib/auth/index.ts b/apps/web/lib/auth/index.tsindex 8f3f8abd35..26bb7cd9e7 100644--- a/apps/web/lib/auth/index.ts+++ b/apps/web/lib/auth/index.ts@@ -67,10 +67,12 @@ export const withAuth =requiredPlan = ["free", "pro", "enterprise"], // if the action needs a specific planrequiredRole = ["owner", "member"],needNotExceededUsage, // if the action needs the user to not have exceeded their usage+ allowAnonymous, // special case for /api/links (POST /api/links) – allow no session}: {requiredPlan?: Array<PlanProps>;requiredRole?: Array<"owner" | "member">;needNotExceededUsage?: boolean;+ allowAnonymous?: boolean;} = {},) =>async (@@ -175,6 +177,17 @@ export const withAuth =} else {session = await getSession();if (!session?.user.id) {+ // for demo links, we allow anonymous link creation+ if (allowAnonymous) {+ // @ts-expect-error+ return handler({+ req,+ params: params || {},+ searchParams,+ headers,+ });+ }+return new Response("Unauthorized: Login required.", {status: 401,headers,diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsxindex 664f7f508d..c61e2473b7 100644--- a/apps/web/ui/links/link-card.tsx+++ b/apps/web/ui/links/link-card.tsx@@ -323,7 +323,7 @@ export default function LinkCard({<div className="w-full p-4"><Avatar user={user} className="h-10 w-10" /><p className="mt-2 text-sm font-semibold text-gray-700">- {user?.name || user?.email}+ {user?.name || user?.email || "Anonymous User"}</p><p className="mt-1 text-xs text-gray-500">Created{" "}diff --git a/apps/web/ui/projects/project-card.tsx b/apps/web/ui/projects/project-card.tsxindex 1bb5f9f14b..d9d2c4926c 100644--- a/apps/web/ui/projects/project-card.tsx+++ b/apps/web/ui/projects/project-card.tsx@@ -81,13 +81,13 @@ export default function ProjectCard({<PlanBadge plan={plan} /></div><div className="flex items-center space-x-4">- <div className="flex items-center space-x-2 text-gray-500">+ <div className="flex items-center space-x-1 text-gray-500"><Globe className="h-4 w-4" /><h2 className="whitespace-nowrap text-sm">{nFormatter(domains.length)} domain{domains.length > 1 && "s"}</h2></div>- <div className="flex items-center space-x-2 text-gray-500">+ <div className="flex items-center space-x-1 text-gray-500"><Link2 className="h-4 w-4" />{count || count === 0 ? (<NumberTooltip value={count} unit="links">@@ -99,7 +99,7 @@ export default function ProjectCard({<div className="h-4 w-8 animate-pulse rounded-md bg-gray-200" />)}</div>- <div className="flex items-center space-x-2 text-gray-500">+ <div className="flex items-center space-x-1 text-gray-500"><BarChart2 className="h-4 w-4" /><NumberTooltip value={usage}><h2 className="whitespace-nowrap text-sm">