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.

moved reserved keys to edge config

Steven Tey2903d8f336ca
diff --git a/components/app/modals/add-edit-link-modal/index.tsx b/components/app/modals/add-edit-link-modal/index.tsx
index f0f4798d62..027f79ccd8 100644
--- a/components/app/modals/add-edit-link-modal/index.tsx
+++ b/components/app/modals/add-edit-link-modal/index.tsx
@@ -59,7 +59,7 @@ function AddEditLinkModal({
const [keyExistsError, setKeyExistsError] = useState(false);
const [urlError, setUrlError] = useState(false);
- const [generatingSlug, setGeneratingSlug] = useState(false);
+ const [generatingKey, setGeneratingKey] = useState(false);
const [saving, setSaving] = useState(false);
const [data, setData] = useState<LinkProps>(
@@ -88,8 +88,8 @@ function AddEditLinkModal({
}
}, [debouncedKey]);
- const generateRandomSlug = useCallback(async () => {
- setGeneratingSlug(true);
+ const generateRandomKey = useCallback(async () => {
+ setGeneratingKey(true);
const res = await fetch(
domain
? `/api/projects/${slug}/domains/${domain}/links/random`
@@ -97,7 +97,7 @@ function AddEditLinkModal({
);
const key = await res.json();
setData((prev) => ({ ...prev, key }));
- setGeneratingSlug(false);
+ setGeneratingKey(false);
}, []);
const [generatingMetatags, setGeneratingMetatags] = useState(
@@ -376,16 +376,16 @@ function AddEditLinkModal({
) : (
<button
className="flex items-center space-x-2 text-sm text-gray-500 transition-all duration-75 hover:text-black active:scale-95"
- onClick={generateRandomSlug}
- disabled={generatingSlug}
+ onClick={generateRandomKey}
+ disabled={generatingKey}
type="button"
>
- {generatingSlug ? (
+ {generatingKey ? (
<LoadingCircle />
) : (
<Random className="h-3 w-3" />
)}
- <p>{generatingSlug ? "Generating" : "Randomize"}</p>
+ <p>{generatingKey ? "Generating" : "Randomize"}</p>
</button>
)}
</div>
diff --git a/lib/api/links.ts b/lib/api/links.ts
index de102627ea..9a6d37e5df 100644
--- a/lib/api/links.ts
+++ b/lib/api/links.ts
@@ -1,9 +1,9 @@
import cloudinary from "cloudinary";
-import { DEFAULT_REDIRECTS, RESERVED_KEYS } from "@/lib/constants";
+import { DEFAULT_REDIRECTS } from "@/lib/constants";
import prisma from "@/lib/prisma";
import { LinkProps } from "@/lib/types";
import { redis } from "@/lib/upstash";
-import { getParamsFromURL, nanoid, truncate } from "@/lib/utils";
+import { getParamsFromURL, isReservedKey, nanoid, truncate } from "@/lib/utils";
const getFiltersFromStatus = (status: string) => {
if (status === "all" || status === "none") {
@@ -110,7 +110,7 @@ export async function getRandomKey(domain: string): Promise<string> {
export async function checkIfKeyExists(domain: string, key: string) {
if (
domain === "dub.sh" &&
- (RESERVED_KEYS.has(key) || DEFAULT_REDIRECTS[key])
+ ((await isReservedKey(key)) || DEFAULT_REDIRECTS[key])
) {
return true; // reserved keys for dub.sh
}
diff --git a/lib/constants/index.ts b/lib/constants/index.ts
index 6d711effc8..686d199de6 100644
--- a/lib/constants/index.ts
+++ b/lib/constants/index.ts
@@ -60,29 +60,6 @@ export const HOME_HOSTNAMES = new Set([
"localhost:3000",
]);
-export const RESERVED_KEYS = new Set([
- "api",
- "va",
- "static",
- "stats",
- "proxy",
- "auth",
- "pricing",
- "about",
- "metatags",
- "blog",
- "careers",
- "docs",
- "features",
- "contact",
- "terms",
- "privacy",
- "admin",
- "help",
- "new",
- "info",
- "demo",
-]);
export const DEFAULT_REDIRECTS = {
home: "https://dub.sh",
signin: "https://app.dub.sh/login",
diff --git a/lib/utils.ts b/lib/utils.ts
index fcd3fd051b..a29fdcac96 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -325,33 +325,25 @@ export const edgeConfig = createClient(
`https://edge-config.vercel.com/ecfg_eh6zdvznm70adch6q0mqxshrt4ny?token=64aef40c-ea06-4aeb-b528-b94d924ec05a`,
);
-export const getBlackListedDomains = async () => {
+export const isBlacklistedDomain = async (domain: string) => {
+ let blacklistedDomains;
try {
- const domains = await edgeConfig.get("domains");
- return domains || [];
+ blacklistedDomains = await edgeConfig.get("domains");
} catch (e) {
- return [];
+ blacklistedDomains = [];
}
-};
-
-export const isBlacklistedDomain = async (domain: string) => {
- const blacklistedDomains = await getBlackListedDomains();
return new RegExp(blacklistedDomains.join("|")).test(
getDomainWithoutWWW(domain),
);
};
-export const getBlacklistedKeys = async () => {
+export const isBlacklistedKey = async (key: string) => {
+ let blacklistedKeys;
try {
- const domains = await edgeConfig.get("keys");
- return domains || [];
+ blacklistedKeys = await edgeConfig.get("keys");
} catch (e) {
- return [];
+ blacklistedKeys = [];
}
-};
-
-export const isBlacklistedKey = async (key: string) => {
- const blacklistedKeys = await getBlacklistedKeys();
return new RegExp(blacklistedKeys.join("|"), "i").test(key);
};
@@ -367,3 +359,13 @@ export const getBlackListedEmails = async () => {
return new Set();
}
};
+
+export const isReservedKey = async (key: string) => {
+ let reservedKey;
+ try {
+ reservedKey = await edgeConfig.get("reserved");
+ } catch (e) {
+ reservedKey = [];
+ }
+ return new Set(reservedKey).has(key);
+};
diff --git a/middleware.ts b/middleware.ts
index 458e42bfac..200c8f5263 100644
--- a/middleware.ts
+++ b/middleware.ts
@@ -1,9 +1,5 @@
import { NextFetchEvent, NextRequest, NextResponse } from "next/server";
-import {
- DEFAULT_REDIRECTS,
- HOME_HOSTNAMES,
- RESERVED_KEYS,
-} from "@/lib/constants";
+import { DEFAULT_REDIRECTS } from "@/lib/constants";
import {
AppMiddleware,
ApiMiddleware,
@@ -11,6 +7,7 @@ import {
RootMiddleware,
} from "@/lib/middleware";
import { parse } from "@/lib/middleware/utils";
+import { isReservedKey } from "./lib/utils";
export const config = {
matcher: [
@@ -60,7 +57,7 @@ export default async function middleware(req: NextRequest, ev: NextFetchEvent) {
if (DEFAULT_REDIRECTS[key]) {
return NextResponse.redirect(DEFAULT_REDIRECTS[key]);
}
- if (RESERVED_KEYS.has(key)) {
+ if (await isReservedKey(key)) {
return NextResponse.next();
}
}
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index 652abd6f4f..d7329f299e 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -35,10 +35,6 @@ export default withUserAuth(
if (!key || !url) {
return res.status(400).json({ error: "Missing key or url" });
}
- const { hostname, pathname } = new URL(url);
- if (hostname === "dub.sh" && pathname === `/${key}`) {
- return res.status(400).json({ error: "Invalid url" });
- }
const keyBlacklisted = await isBlacklistedKey(key);
if (keyBlacklisted) {
return res.status(400).json({ error: "Invalid key" });
diff --git a/pages/api/projects/[slug]/exists.ts b/pages/api/projects/[slug]/exists.ts
index b2ff8066cf..15ced0a7fc 100644
--- a/pages/api/projects/[slug]/exists.ts
+++ b/pages/api/projects/[slug]/exists.ts
@@ -1,7 +1,8 @@
import { NextApiRequest, NextApiResponse } from "next";
import { withUserAuth } from "@/lib/auth";
-import { DEFAULT_REDIRECTS, RESERVED_KEYS } from "@/lib/constants";
+import { DEFAULT_REDIRECTS } from "@/lib/constants";
import prisma from "@/lib/prisma";
+import { isReservedKey } from "@/lib/utils";
export default withUserAuth(
async (req: NextApiRequest, res: NextApiResponse) => {
@@ -9,7 +10,7 @@ export default withUserAuth(
// GET /api/projects/[slug]/exists – check if a project exists
if (req.method === "GET") {
- if (RESERVED_KEYS.has(slug) || DEFAULT_REDIRECTS[slug]) {
+ if ((await isReservedKey(slug)) || DEFAULT_REDIRECTS[slug]) {
return res.status(200).json(1);
}
const project = await prisma.project.findUnique({
diff --git a/pages/api/projects/index.ts b/pages/api/projects/index.ts
index ebe8a17e75..807644b424 100644
--- a/pages/api/projects/index.ts
+++ b/pages/api/projects/index.ts
@@ -1,9 +1,9 @@
import { NextApiRequest, NextApiResponse } from "next";
import { Session, withUserAuth } from "@/lib/auth";
-import { DEFAULT_REDIRECTS, RESERVED_KEYS } from "@/lib/constants";
-import { addDomain, removeDomain } from "@/lib/domains";
+import { DEFAULT_REDIRECTS } from "@/lib/constants";
+import { addDomain } from "@/lib/domains";
import prisma from "@/lib/prisma";
-import { validDomainRegex } from "@/lib/utils";
+import { isReservedKey, validDomainRegex } from "@/lib/utils";
export default withUserAuth(
async (req: NextApiRequest, res: NextApiResponse, session: Session) => {
@@ -31,7 +31,7 @@ export default withUserAuth(
let slugError = null;
if (slug.includes(" ") || slug.includes(".")) {
slugError = "Slug cannot contain spaces or periods";
- } else if (RESERVED_KEYS.has(slug) || DEFAULT_REDIRECTS[slug]) {
+ } else if ((await isReservedKey(slug)) || DEFAULT_REDIRECTS[slug]) {
slugError = "Cannot use reserved slugs";
}
const validDomain =