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.

Validate .dub.link subdomains (#3809)

Pedro Ladeiraa9da2bb024ca
diff --git a/apps/web/app/api/domains/[domain]/validate/route.ts b/apps/web/app/api/domains/[domain]/validate/route.ts
index 533806e8b4..98a4e578a6 100644
--- a/apps/web/app/api/domains/[domain]/validate/route.ts
+++ b/apps/web/app/api/domains/[domain]/validate/route.ts
@@ -1,5 +1,6 @@
import { isValidDomain } from "@/lib/api/domains/is-valid-domain";
import { domainExists } from "@/lib/api/domains/utils";
+import { validateDubLinkSubdomain } from "@/lib/api/domains/validate-dub-link-subdomain";
import { withSession } from "@/lib/auth";
import dns from "dns/promises";
import { NextResponse } from "next/server";
@@ -13,6 +14,13 @@ export const GET = withSession(async ({ params }) => {
status: "invalid",
});
}
+ const dubLinkError = validateDubLinkSubdomain(domain);
+ if (dubLinkError) {
+ return NextResponse.json({
+ status: "invalid",
+ message: dubLinkError.error,
+ });
+ }
const exists = await domainExists(domain);
if (exists) {
return NextResponse.json({
diff --git a/apps/web/lib/api/domains/utils.ts b/apps/web/lib/api/domains/utils.ts
index 24fbe426b6..7a2bf30e5f 100644
--- a/apps/web/lib/api/domains/utils.ts
+++ b/apps/web/lib/api/domains/utils.ts
@@ -1,6 +1,7 @@
import { prisma } from "@dub/prisma";
import { DubApiError } from "../errors";
import { isValidDomain } from "./is-valid-domain";
+import { validateDubLinkSubdomain } from "./validate-dub-link-subdomain";
export const validateDomain = async (
domain: string,
@@ -11,6 +12,10 @@ export const validateDomain = async (
if (!isValidDomain(domain)) {
return { error: "Invalid domain", code: "unprocessable_entity" };
}
+ const dubLinkError = validateDubLinkSubdomain(domain);
+ if (dubLinkError) {
+ return dubLinkError;
+ }
const exists = await domainExists(domain);
if (exists) {
return { error: "Domain is already in use.", code: "conflict" };
diff --git a/apps/web/lib/api/domains/validate-dub-link-subdomain.ts b/apps/web/lib/api/domains/validate-dub-link-subdomain.ts
new file mode 100644
index 0000000000..e35ef56a80
--- /dev/null
+++ b/apps/web/lib/api/domains/validate-dub-link-subdomain.ts
@@ -0,0 +1,52 @@
+import { ErrorCode } from "@/lib/api/error-codes";
+import { DEFAULT_REDIRECTS, RESERVED_SLUGS, validSlugRegex } from "@dub/utils";
+import * as z from "zod/v4";
+
+const DUB_LINK_SUFFIX = ".dub.link";
+
+type ApiErrorCode = z.infer<typeof ErrorCode>;
+
+export function validateDubLinkSubdomain(
+ slug: string,
+): { error: string; code: ApiErrorCode } | null {
+ const lower = slug.trim().toLowerCase();
+
+ if (!lower.endsWith(DUB_LINK_SUFFIX)) {
+ return null;
+ }
+
+ const label = lower.slice(0, -DUB_LINK_SUFFIX.length);
+
+ if (!label || label.includes(".")) {
+ return {
+ error: "Invalid .dub.link subdomain format.",
+ code: "unprocessable_entity",
+ };
+ }
+
+ if (label.length < 4) {
+ return {
+ error: "Subdomain must be at least 4 characters.",
+ code: "unprocessable_entity",
+ };
+ }
+
+ if (!validSlugRegex.test(label)) {
+ return {
+ error: "Invalid subdomain format.",
+ code: "unprocessable_entity",
+ };
+ }
+
+ if (
+ RESERVED_SLUGS.includes(label) ||
+ DEFAULT_REDIRECTS[label as keyof typeof DEFAULT_REDIRECTS]
+ ) {
+ return {
+ error: "This subdomain is reserved.",
+ code: "unprocessable_entity",
+ };
+ }
+
+ return null;
+}
diff --git a/apps/web/ui/domains/add-edit-domain-form.tsx b/apps/web/ui/domains/add-edit-domain-form.tsx
index 9e72867e17..65b0968a77 100644
--- a/apps/web/ui/domains/add-edit-domain-form.tsx
+++ b/apps/web/ui/domains/add-edit-domain-form.tsx
@@ -59,7 +59,14 @@ const formatJson = (string: string) => {
type FormData = z.infer<typeof createDomainBodySchemaExtended>;
-type DomainStatus = "checking" | "conflict" | "has site" | "available" | "idle";
+type DomainStatus =
+ | "checking"
+ | "conflict"
+ | "has site"
+ | "available"
+ | "idle"
+ | "invalid"
+ | "error";
const STATUS_CONFIG: Record<
DomainStatus,
@@ -83,6 +90,14 @@ const STATUS_CONFIG: Record<
icon: AlertCircleFill,
className: "bg-red-100 text-red-600",
},
+ invalid: {
+ icon: AlertCircleFill,
+ className: "bg-red-100 text-red-600",
+ },
+ error: {
+ icon: AlertCircleFill,
+ className: "bg-red-100 text-red-600",
+ },
"has site": {
suffix:
"is currently pointing to an existing website. Only proceed if you're sure you want to use this domain for short links on Dub.",
@@ -118,6 +133,9 @@ export function AddEditDomainForm({
const [domainStatus, setDomainStatus] = useState<DomainStatus>(
props ? "available" : "idle",
);
+ const [domainValidateMessage, setDomainValidateMessage] = useState<
+ string | null
+ >(null);
const [showOptionStates, setShowOptionStates] = useState<
Record<string, boolean>
>({});
@@ -194,11 +212,49 @@ export function AddEditDomainForm({
const debouncedValidateDomain = useDebouncedCallback(
async (value: string) => {
if (!isValidDomain(value)) return;
+ setDomainValidateMessage(null);
setDomainStatus("checking");
- fetch(`/api/domains/${value}/validate`).then(async (res) => {
- const data = await res.json();
- setDomainStatus(data.status);
- });
+
+ const validationFailedMessage =
+ "Could not check domain availability. Please try again.";
+
+ fetch(`/api/domains/${encodeURIComponent(value)}/validate`)
+ .then(async (res) => {
+ if (!res.ok) {
+ setDomainStatus("error");
+ setDomainValidateMessage(validationFailedMessage);
+ return;
+ }
+
+ let data: { status?: unknown; message?: unknown };
+ try {
+ data = await res.json();
+ } catch {
+ setDomainStatus("error");
+ setDomainValidateMessage(validationFailedMessage);
+ return;
+ }
+
+ const status = data.status;
+ if (
+ status === "invalid" ||
+ status === "conflict" ||
+ status === "has site" ||
+ status === "available"
+ ) {
+ setDomainStatus(status);
+ setDomainValidateMessage(
+ typeof data.message === "string" ? data.message : null,
+ );
+ } else {
+ setDomainStatus("error");
+ setDomainValidateMessage(validationFailedMessage);
+ }
+ })
+ .catch(() => {
+ setDomainStatus("error");
+ setDomainValidateMessage(validationFailedMessage);
+ });
},
500,
);
@@ -365,6 +421,7 @@ export function AddEditDomainForm({
shouldDirty: true,
});
setDomainStatus("idle");
+ setDomainValidateMessage(null);
if (prefix) {
debouncedValidateDomain(fullDomain);
}
@@ -383,6 +440,7 @@ export function AddEditDomainForm({
{...register("slug", {
onChange: (e) => {
setDomainStatus("idle");
+ setDomainValidateMessage(null);
debouncedValidateDomain(e.target.value);
},
})}
@@ -400,19 +458,27 @@ export function AddEditDomainForm({
<div className="flex items-center justify-between gap-4 p-2 text-sm">
<p>
{domainStatus !== "idle" ? (
- <>
- {currentStatusProps.prefix || "The domain"}{" "}
- {currentStatusProps.useStrong ? (
- <strong className="font-semibold underline underline-offset-2">
- {domain}
- </strong>
- ) : (
- <span className="font-semibold underline underline-offset-2">
- {domain}
- </span>
- )}{" "}
- {currentStatusProps.suffix}
- </>
+ domainStatus === "invalid" ||
+ domainStatus === "error" ? (
+ domainValidateMessage ??
+ (domainStatus === "error"
+ ? "Could not check domain availability. Please try again."
+ : "This domain is not valid.")
+ ) : (
+ <>
+ {currentStatusProps.prefix || "The domain"}{" "}
+ {currentStatusProps.useStrong ? (
+ <strong className="font-semibold underline underline-offset-2">
+ {domain}
+ </strong>
+ ) : (
+ <span className="font-semibold underline underline-offset-2">
+ {domain}
+ </span>
+ )}{" "}
+ {currentStatusProps.suffix}
+ </>
+ )
) : (
currentStatusProps.message
)}