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 rate limits

Steven Tey13c7affdb595
diff --git a/apps/web/app/api/analytics/edge/[endpoint]/route.ts b/apps/web/app/api/analytics/edge/[endpoint]/route.ts
index 681f48e9d0..bb8d151f37 100644
--- a/apps/web/app/api/analytics/edge/[endpoint]/route.ts
+++ b/apps/web/app/api/analytics/edge/[endpoint]/route.ts
@@ -5,7 +5,6 @@ import {
handleAndReturnErrorResponse,
} from "@/lib/api/errors";
import { getIdentityHash } from "@/lib/edge";
-import { isBlacklistedReferrer } from "@/lib/edge-config";
import { getDomainOrLink, getProjectViaEdge } from "@/lib/planetscale";
import { ratelimit } from "@/lib/upstash";
import {
@@ -37,16 +36,10 @@ export const GET = async (
if (demoLink) {
// Rate limit in production
if (process.env.NODE_ENV !== "development") {
- if (await isBlacklistedReferrer(req.headers.get("referer"))) {
- throw new DubApiError({
- code: "rate_limit_exceeded",
- message: "Don't DDoS me pls 🥺",
- });
- }
const identity_hash = await getIdentityHash(req);
const { success } = await ratelimit(
15,
- endpoint === "clicks" ? "10 s" : "1 h",
+ endpoint === "clicks" ? "10 s" : "1 m",
).limit(`demo-analytics:${demoLink.id}:${identity_hash}:${endpoint}`);
if (!success) {
diff --git a/apps/web/lib/middleware/link.ts b/apps/web/lib/middleware/link.ts
index 978af31864..bbd246ae13 100644
--- a/apps/web/lib/middleware/link.ts
+++ b/apps/web/lib/middleware/link.ts
@@ -1,7 +1,12 @@
import { detectBot, getFinalUrl, parse } from "@/lib/middleware/utils";
import { recordClick } from "@/lib/tinybird";
-import { formatRedisLink, ratelimit, redis } from "@/lib/upstash";
-import { DUB_HEADERS, LEGAL_PROJECT_ID, LOCALHOST_GEO_DATA } from "@dub/utils";
+import { formatRedisLink, redis } from "@/lib/upstash";
+import {
+ DUB_DEMO_LINKS,
+ DUB_HEADERS,
+ LEGAL_PROJECT_ID,
+ LOCALHOST_GEO_DATA,
+} from "@dub/utils";
import {
NextFetchEvent,
NextRequest,
@@ -11,7 +16,6 @@ import {
import { isBlacklistedReferrer } from "../edge-config";
import { getLinkViaEdge } from "../planetscale";
import { RedisLinkProps } from "../types";
-import { getIdentityHash } from "../edge";
export default async function LinkMiddleware(
req: NextRequest,
@@ -26,22 +30,17 @@ export default async function LinkMiddleware(
// links on Dub are case insensitive by default
key = key.toLowerCase();
+ const demoLink = DUB_DEMO_LINKS.find(
+ (l) => l.domain === domain && l.key === key,
+ );
+
+ // if it's a demo link, block bad referrers in production
if (
process.env.NODE_ENV !== "development" &&
- domain === "dub.sh" &&
- key === "try"
+ demoLink &&
+ (await isBlacklistedReferrer(req.headers.get("referer")))
) {
- if (await isBlacklistedReferrer(req.headers.get("referer"))) {
- return new Response("Don't DDoS me pls 🥺", { status: 429 });
- }
- const identity_hash = await getIdentityHash(req);
- const { success } = await ratelimit(10, "1 d").limit(
- `demo-click:${identity_hash}`,
- );
-
- if (!success) {
- return new Response("Don't DDoS me pls 🥺", { status: 429 });
- }
+ return new Response("Don't DDoS me pls 🥺", { status: 429 });
}
const inspectMode = key.endsWith("+");