The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
refactor hostname validation to support wildcard domains in AddHostnameForm
Kiran Kdb68cd09c4dc
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/analytics/allowed-hostnames.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/analytics/allowed-hostnames.tsxindex 004d82317d..cd368aeab7 100644--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/analytics/allowed-hostnames.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/analytics/allowed-hostnames.tsx@@ -52,16 +52,21 @@ const AddHostnameForm = () => {customPermissionDescription: "add hostnames",});+ const isValidHostname = (hostname: string) => {+ return (+ validDomainRegex.test(hostname) ||+ hostname === "localhost" ||+ hostname.startsWith("*.")+ );+ };+const addHostname = async () => {if (allowedHostnames?.includes(hostname)) {toast.error("Hostname already exists.");return;}- const isHostnameValid =- validDomainRegex.test(hostname) || hostname === "localhost";-- if (!isHostnameValid) {+ if (!isValidHostname(hostname)) {toast.error("Enter a valid domain.");return;}@@ -90,9 +95,6 @@ const AddHostnameForm = () => {setHostname("");};- const isHostnameValid =- validDomainRegex.test(hostname) || hostname === "localhost";-return (<formclassName="flex items-end gap-2"@@ -108,7 +110,7 @@ const AddHostnameForm = () => {value={hostname}onChange={(e) => setHostname(e.target.value)}autoComplete="off"- placeholder="example.com"+ placeholder="example.com or *.example.com"className={cn("block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",)}@@ -119,7 +121,7 @@ const AddHostnameForm = () => {text="Add Hostname"variant="primary"onClick={addHostname}- disabled={!isHostnameValid || hostname.length === 0}+ disabled={!isValidHostname(hostname) || hostname.length === 0}loading={processing}className="w-40"disabledTooltip={permissionsError || undefined}diff --git a/apps/web/lib/api/validate-allowed-hostnames.ts b/apps/web/lib/api/validate-allowed-hostnames.tsindex 9417d4e0f0..a64cb90ff7 100644--- a/apps/web/lib/api/validate-allowed-hostnames.ts+++ b/apps/web/lib/api/validate-allowed-hostnames.ts@@ -13,7 +13,10 @@ export const validateAllowedHostnames = (allowedHostnames = [...new Set(allowedHostnames)];const results = allowedHostnames.map(- (hostname) => validDomainRegex.test(hostname) || hostname === "localhost",+ (hostname) =>+ validDomainRegex.test(hostname) ||+ hostname === "localhost" ||+ hostname.startsWith("*."),);const invalidHostnames = results.filter((result) => !result);