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 for Metatags API

Steven Tey12d65b052cfb
diff --git a/lib/constants/index.ts b/lib/constants/index.ts
index 9803656420..889e525f38 100644
--- a/lib/constants/index.ts
+++ b/lib/constants/index.ts
@@ -32,7 +32,6 @@ export const HOME_HOSTNAMES = new Set([
"dub.sh",
"preview.dub.sh",
"localhost:3000",
- "c623-2600-1700-b5e4-b50-d083-66f5-64b2-e8bb.ngrok.io",
]);
export const RESERVED_KEYS = new Set([
diff --git a/lib/utils.ts b/lib/utils.ts
index fe24c4a6e6..88c2273a5c 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -304,7 +304,7 @@ export const log = async (message: string, type: "cron" | "links") => {
}),
});
} catch (e) {
- console.log(`Failed to log to Vercel Slack. Error: ${e}`);
+ console.log(`Failed to log to Dub Slack. Error: ${e}`);
}
};
diff --git a/pages/api/edge/metatags.ts b/pages/api/edge/metatags.ts
index 0b9f05efcb..b9dedbf450 100644
--- a/pages/api/edge/metatags.ts
+++ b/pages/api/edge/metatags.ts
@@ -4,6 +4,38 @@ import he from "he";
import { recordMetatags } from "@/lib/upstash";
import { getDomainWithoutWWW, isValidUrl } from "@/lib/utils";
import sanitize from "ultrahtml/transformers/sanitize";
+import { ratelimit } from "@/lib/upstash";
+
+const ALLOWLIST = new Set([
+ "dub.sh",
+ "app.dub.sh",
+ "localhost:3000",
+ "app.localhost:3000",
+]);
+
+export const config = {
+ runtime: "experimental-edge",
+};
+
+export default async function handler(req: NextRequest, ev: NextFetchEvent) {
+ if (req.method === "GET") {
+ let url = req.nextUrl.searchParams.get("url");
+ if (!isValidUrl(url)) {
+ return new Response("Invalid URL", { status: 400 });
+ }
+ const hostname = req.headers.get("host");
+ if (!ALLOWLIST.has(hostname)) {
+ const { success } = await ratelimit.limit("metatags");
+ if (!success) {
+ return new Response("Don't DDoS me pls 🥺", { status: 429 });
+ }
+ }
+ const metatags = await getMetaTags(url, ev);
+ return new Response(JSON.stringify(metatags), { status: 200 });
+ } else {
+ return new Response(`Method ${req.method} Not Allowed`, { status: 405 });
+ }
+}
const getHtml = async (url: string) => {
const controller = new AbortController();
@@ -97,20 +129,3 @@ export const getMetaTags = async (url: string, ev: NextFetchEvent) => {
image: getRelativeUrl(url, image),
};
};
-
-export default async function handler(req: NextRequest, ev: NextFetchEvent) {
- if (req.method === "GET") {
- let url = req.nextUrl.searchParams.get("url");
- if (!isValidUrl(url)) {
- return new Response("Invalid URL", { status: 400 });
- }
- const metatags = await getMetaTags(url, ev);
- return new Response(JSON.stringify(metatags), { status: 200 });
- } else {
- return new Response(`Method ${req.method} Not Allowed`, { status: 405 });
- }
-}
-
-export const config = {
- runtime: "experimental-edge",
-};