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.

Add support for `.` in short links

Steven Teyc1ec2d88932b
diff --git a/apps/web/lib/api/links/utils/key-checks.ts b/apps/web/lib/api/links/utils/key-checks.ts
index aa66d2dc1e..d4c1c2db4a 100644
--- a/apps/web/lib/api/links/utils/key-checks.ts
+++ b/apps/web/lib/api/links/utils/key-checks.ts
@@ -7,6 +7,7 @@ import {
import { checkIfKeyExists } from "@/lib/planetscale";
import { WorkspaceProps } from "@/lib/types";
import { DEFAULT_REDIRECTS, isDubDomain } from "@dub/utils";
+import { RESERVED_PATHS } from "@dub/utils/src/constants/middleware";
export async function keyChecks({
domain,
@@ -25,6 +26,13 @@ export async function keyChecks({
};
}
+ if (RESERVED_PATHS.includes(key)) {
+ return {
+ error: `${key} is a reserved path and cannot be used as a short link.`,
+ code: "forbidden",
+ };
+ }
+
const link = await checkIfKeyExists(domain, key);
if (link) {
return {
@@ -34,7 +42,7 @@ export async function keyChecks({
}
if (isDubDomain(domain) && process.env.NEXT_PUBLIC_IS_DUB) {
- if (domain === "dub.sh") {
+ if (domain === "dub.sh" || domain === "dub.link") {
if (DEFAULT_REDIRECTS[key] || (await isReservedKey(key))) {
return {
error: "Duplicate key: This short link already exists.",
diff --git a/apps/web/lib/edge-config/is-reserved-key.ts b/apps/web/lib/edge-config/is-reserved-key.ts
index d1a0573f9e..1caf2315f5 100644
--- a/apps/web/lib/edge-config/is-reserved-key.ts
+++ b/apps/web/lib/edge-config/is-reserved-key.ts
@@ -1,5 +1,10 @@
import { get } from "@vercel/edge-config";
+/**
+ * Only for dub.sh / dub.link domains
+ * Check if a key is reserved – cannot be registered for a short link
+ */
+
export const isReservedKey = async (key: string) => {
if (!process.env.NEXT_PUBLIC_IS_DUB || !process.env.EDGE_CONFIG) {
return false;
diff --git a/apps/web/lib/edge-config/is-reserved-username.ts b/apps/web/lib/edge-config/is-reserved-username.ts
index 695bc48147..f8798a036d 100644
--- a/apps/web/lib/edge-config/is-reserved-username.ts
+++ b/apps/web/lib/edge-config/is-reserved-username.ts
@@ -1,5 +1,9 @@
import { get } from "@vercel/edge-config";
+/**
+ * Only for dub.sh / dub.link domains
+ * Check if a username is reserved – should only be available on Pro+
+ */
export const isReservedUsername = async (key: string) => {
if (!process.env.NEXT_PUBLIC_IS_DUB || !process.env.EDGE_CONFIG) {
return false;
diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts
index b3aea74f67..b2fdaa08b2 100644
--- a/apps/web/middleware.ts
+++ b/apps/web/middleware.ts
@@ -25,9 +25,9 @@ export const config = {
* 3. /_proxy/ (proxies for third-party services)
* 4. /_static (inside /public)
* 5. /_vercel (Vercel internals)
- * 6. Static files (e.g. /favicon.ico, /sitemap.xml, /robots.txt, etc.)
+ * 6. Metadata files: favicon.ico, sitemap.xml, robots.txt, manifest.webmanifest
*/
- "/((?!api/|_next/|_proxy/|_static|_vercel|[\\w-]+\\.\\w+).*)",
+ "/((?!api/|_next/|_proxy/|_static|_vercel|favicon.ico|sitemap.xml|robots.txt|manifest.webmanifest).*)",
],
};
diff --git a/apps/web/ui/links/short-link-input.tsx b/apps/web/ui/links/short-link-input.tsx
index 735b4c89c9..c24ccf977a 100644
--- a/apps/web/ui/links/short-link-input.tsx
+++ b/apps/web/ui/links/short-link-input.tsx
@@ -253,8 +253,8 @@ export const ShortLinkInput = forwardRef<HTMLInputElement, ShortLinkInputProps>(
type="text"
name="key"
id={inputId}
- // allow letters, numbers, '-', '_', '/' and emojis
- pattern="[\p{L}\p{N}\p{Pd}\/\p{Emoji}_]+"
+ // allow letters, numbers, '-', '_', '/', '.', and emojis
+ pattern="[\p{L}\p{N}\p{Pd}\/\p{Emoji}_.]+"
onInvalid={(e) => {
e.currentTarget.setCustomValidity(
"Only letters, numbers, '-', '_', '/', and emojis are allowed.",
diff --git a/packages/utils/src/constants/middleware.ts b/packages/utils/src/constants/middleware.ts
index 0bfc2fa998..2f19bec827 100644
--- a/packages/utils/src/constants/middleware.ts
+++ b/packages/utils/src/constants/middleware.ts
@@ -13,6 +13,13 @@ export const DEFAULT_REDIRECTS = {
discord: "https://twitter.com/dubdotco", // placeholder for now
};
+export const RESERVED_PATHS = [
+ "favicon.ico",
+ "sitemap.xml",
+ "robots.txt",
+ "manifest.webmanifest",
+];
+
export const DUB_HEADERS = {
"x-powered-by": "Dub.co - Link management for modern marketing teams",
};
diff --git a/packages/utils/src/functions/domains.ts b/packages/utils/src/functions/domains.ts
index 5f5615873c..a4d35e82f4 100644
--- a/packages/utils/src/functions/domains.ts
+++ b/packages/utils/src/functions/domains.ts
@@ -33,8 +33,9 @@ export const validDomainRegex = new RegExp(
/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}$/,
);
+// allow letters, numbers, '-', '_', '/', '.', and emojis
export const validKeyRegex = new RegExp(
- /^[0-9A-Za-z_\u0080-\uFFFF\/\-\p{Emoji}]*$/u,
+ /^[0-9A-Za-z_\u0080-\uFFFF\/\-\p{Emoji}.]+$/u,
);
export const validSlugRegex = new RegExp(/^[a-zA-Z0-9\-]+$/);