The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Added several abuse protection measures
Steven Teyf97d02694f0d
diff --git a/components/app/modals/add-edit-link-modal/index.tsx b/components/app/modals/add-edit-link-modal/index.tsxindex c76be8f0b5..e5e944faa0 100644--- a/components/app/modals/add-edit-link-modal/index.tsx+++ b/components/app/modals/add-edit-link-modal/index.tsx@@ -42,6 +42,7 @@ function AddEditLinkModal({const { project: { domain } = {} } = useProject();const [keyExistsError, setKeyExistsError] = useState(false);+ const [urlError, setUrlError] = useState(false);const [generatingSlug, setGeneratingSlug] = useState(false);const [saving, setSaving] = useState(false);@@ -191,8 +192,12 @@ function AddEditLinkModal({: `/api/links${getQueryString(router)}`,);setShowAddEditLinkModal(false);- } else {+ } else if (res.status === 403) {setKeyExistsError(true);+ } else if (res.status === 400) {+ setUrlError(true);+ } else {+ alert("Something went wrong");}});}}@@ -269,21 +274,38 @@ function AddEditLinkModal({>Destination URL</label>- <div className="mt-1 flex rounded-md shadow-sm">+ <div className="relative mt-1 flex rounded-md shadow-sm"><inputname="url"id="url"type="url"required- className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"placeholder="https://github.com/steven-tey/dub"value={url}onChange={(e) => {setData({ ...data, url: e.target.value });}}+ className={`${+ urlError+ ? "border-red-300 pr-10 text-red-900 placeholder-red-300 focus:border-red-500 focus:ring-red-500"+ : "border-gray-300 text-gray-900 placeholder-gray-300 focus:border-gray-500 focus:ring-gray-500"+ } block w-full rounded-md focus:outline-none sm:text-sm`}aria-invalid="true"/>+ {urlError && (+ <div className="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3">+ <AlertCircleFill+ className="h-5 w-5 text-red-500"+ aria-hidden="true"+ />+ </div>+ )}</div>+ {urlError && (+ <p className="mt-2 text-sm text-red-600" id="key-error">+ Invalid url.+ </p>+ )}</div></div>diff --git a/lib/api/links.ts b/lib/api/links.tsindex 89a38d237c..663fd7a019 100644--- a/lib/api/links.ts+++ b/lib/api/links.ts@@ -304,7 +304,11 @@ export async function deleteLink(domain: string, key: string) {]);}-export async function archiveLink(domain: string, key: string, archived = true) {+export async function archiveLink(+ domain: string,+ key: string,+ archived = true,+) {return await prisma.link.update({where: {domain_key: {diff --git a/lib/constants/blacklisted-domains.ts b/lib/constants/blacklisted-domains.tsnew file mode 100644index 0000000000..514b67961d--- /dev/null+++ b/lib/constants/blacklisted-domains.ts@@ -0,0 +1,8 @@+const BLACKLISTED_DOMAINS = new Set([+ "lopescarvalho.adv.br",+ "piracynchill.in",+ "tgnewstv.it",+ "secure.oldschool.com-os.cz",+]);++export default BLACKLISTED_DOMAINS;diff --git a/lib/constants/blacklisted-emails.ts b/lib/constants/blacklisted-emails.tsnew file mode 100644index 0000000000..7ab9bf8971--- /dev/null+++ b/lib/constants/blacklisted-emails.ts@@ -0,0 +1,3 @@+const BLACKLISTED_EMAILS = new Set(["hi2.in", "gmx.com"]);++export default BLACKLISTED_EMAILS;diff --git a/lib/constants/cctlds.ts b/lib/constants/cctlds.tsnew file mode 100644index 0000000000..44ac32c872--- /dev/null+++ b/lib/constants/cctlds.ts@@ -0,0 +1,276 @@+const ccTLDs = new Set([+ "af",+ "ax",+ "al",+ "dz",+ "as",+ "ad",+ "ao",+ "ai",+ "aq",+ "ag",+ "ar",+ "am",+ "aw",+ "ac",+ "au",+ "at",+ "az",+ "bs",+ "bh",+ "bd",+ "bb",+ "eus",+ "by",+ "be",+ "bz",+ "bj",+ "bm",+ "bt",+ "bo",+ "bq",+ "an",+ "nl",+ "ba",+ "bw",+ "bv",+ "br",+ "io",+ "vg",+ "bn",+ "bg",+ "bf",+ "mm",+ "bi",+ "kh",+ "cm",+ "ca",+ "cv",+ "cat",+ "ky",+ "cf",+ "td",+ "cl",+ "cn",+ "cx",+ "cc",+ "co",+ "km",+ "cd",+ "cg",+ "ck",+ "cr",+ "ci",+ "hr",+ "cu",+ "cw",+ "cy",+ "cz",+ "dk",+ "dj",+ "dm",+ "do",+ "tl",+ "tp",+ "ec",+ "eg",+ "sv",+ "gq",+ "er",+ "ee",+ "et",+ "eu",+ "fk",+ "fo",+ "fm",+ "fj",+ "fi",+ "fr",+ "gf",+ "pf",+ "tf",+ "ga",+ "gal",+ "gm",+ "ps",+ "ge",+ "de",+ "gh",+ "gi",+ "gr",+ "gl",+ "gd",+ "gp",+ "gu",+ "gt",+ "gg",+ "gn",+ "gw",+ "gy",+ "ht",+ "hm",+ "hn",+ "hk",+ "hu",+ "is",+ "in",+ "id",+ "ir",+ "iq",+ "ie",+ "im",+ "il",+ "it",+ "jm",+ "jp",+ "je",+ "jo",+ "kz",+ "ke",+ "ki",+ "kw",+ "kg",+ "la",+ "lv",+ "lb",+ "ls",+ "lr",+ "ly",+ "li",+ "lt",+ "lu",+ "mo",+ "mk",+ "mg",+ "mw",+ "my",+ "mv",+ "ml",+ "mt",+ "mh",+ "mq",+ "mr",+ "mu",+ "yt",+ "mx",+ "md",+ "mc",+ "mn",+ "me",+ "ms",+ "ma",+ "mz",+ "mm",+ "na",+ "nr",+ "np",+ "nl",+ "nc",+ "nz",+ "ni",+ "ne",+ "ng",+ "nu",+ "nf",+ "nc",+ "tr",+ "kp",+ "mp",+ "no",+ "om",+ "pk",+ "pw",+ "ps",+ "pa",+ "pg",+ "py",+ "pe",+ "ph",+ "pn",+ "pl",+ "pt",+ "pr",+ "qa",+ "ro",+ "ru",+ "rw",+ "re",+ "bq",+ "an",+ "bl",+ "gp",+ "fr",+ "sh",+ "kn",+ "lc",+ "mf",+ "gp",+ "fr",+ "pm",+ "vc",+ "ws",+ "sm",+ "st",+ "sa",+ "sn",+ "rs",+ "sc",+ "sl",+ "sg",+ "bq",+ "an",+ "nl",+ "sx",+ "an",+ "sk",+ "si",+ "sb",+ "so",+ "so",+ "za",+ "gs",+ "kr",+ "ss",+ "es",+ "lk",+ "sd",+ "sr",+ "sj",+ "sz",+ "se",+ "ch",+ "sy",+ "tw",+ "tj",+ "tz",+ "th",+ "tg",+ "tk",+ "to",+ "tt",+ "tn",+ "tr",+ "tm",+ "tc",+ "tv",+ "ug",+ "ua",+ "ae",+ "uk",+ "us",+ "vi",+ "uy",+ "uz",+ "vu",+ "va",+ "ve",+ "vn",+ "wf",+ "eh",+ "ma",+ "ye",+ "zm",+ "zw",+]);++export default ccTLDs;diff --git a/lib/constants.ts b/lib/constants/countries.tssimilarity index 57%rename from lib/constants.tsrename to lib/constants/countries.tsindex a83181d141..d8b7f4faa8 100644--- a/lib/constants.ts+++ b/lib/constants/countries.ts@@ -1,65 +1,4 @@-export const LOCALHOST_GEO_DATA = {- city: "San Francisco",- region: "CA",- country: "US",- latitude: "37.7695",- longitude: "-122.385",-};--export const INTERVALS = ["1h", "24h", "7d", "30d"];--export const FRAMER_MOTION_LIST_ITEM_VARIANTS = {- hidden: { scale: 0.8, opacity: 0 },- show: { scale: 1, opacity: 1, transition: { type: "spring" } },-};--export const HOME_HOSTNAMES = new Set([- // comment for better diffs- "dub.sh",- "preview.dub.sh",- "localhost:3000",-]);--export const RESERVED_KEYS = new Set([- "api",- "va",- "static",- "stats",- "proxy",- "auth",- "pricing",- "about",- "placeholder",- "blog",- "careers",- "features",- "contact",- "terms",- "privacy",- "admin",- "help",- "new",- "info",- "demo",-]);--export const DEFAULT_REDIRECTS = {- home: "https://dub.sh",- signin: "https://app.dub.sh/login",- login: "https://app.dub.sh/login",- register: "https://app.dub.sh/register",- signup: "https://app.dub.sh/register",- app: "https://app.dub.sh",- dashboard: "https://app.dub.sh",- links: "https://app.dub.sh/links",- settings: "https://app.dub.sh/settings",-};--export const FREE_PLAN_PROJECT_LIMIT = 5;--export const FAVICON_FOLDER = "/_static/favicons";--export const COUNTRIES: { [key: string]: string } = {+const COUNTRIES: { [key: string]: string } = {AF: "Afghanistan",AL: "Albania",DZ: "Algeria",@@ -312,291 +251,4 @@ export const COUNTRIES: { [key: string]: string } = {XK: "Kosovo",};-export const ccTLDs = new Set([- "af",- "ax",- "al",- "dz",- "as",- "ad",- "ao",- "ai",- "aq",- "ag",- "ar",- "am",- "aw",- "ac",- "au",- "at",- "az",- "bs",- "bh",- "bd",- "bb",- "eus",- "by",- "be",- "bz",- "bj",- "bm",- "bt",- "bo",- "bq",- "an",- "nl",- "ba",- "bw",- "bv",- "br",- "io",- "vg",- "bn",- "bg",- "bf",- "mm",- "bi",- "kh",- "cm",- "ca",- "cv",- "cat",- "ky",- "cf",- "td",- "cl",- "cn",- "cx",- "cc",- "co",- "km",- "cd",- "cg",- "ck",- "cr",- "ci",- "hr",- "cu",- "cw",- "cy",- "cz",- "dk",- "dj",- "dm",- "do",- "tl",- "tp",- "ec",- "eg",- "sv",- "gq",- "er",- "ee",- "et",- "eu",- "fk",- "fo",- "fm",- "fj",- "fi",- "fr",- "gf",- "pf",- "tf",- "ga",- "gal",- "gm",- "ps",- "ge",- "de",- "gh",- "gi",- "gr",- "gl",- "gd",- "gp",- "gu",- "gt",- "gg",- "gn",- "gw",- "gy",- "ht",- "hm",- "hn",- "hk",- "hu",- "is",- "in",- "id",- "ir",- "iq",- "ie",- "im",- "il",- "it",- "jm",- "jp",- "je",- "jo",- "kz",- "ke",- "ki",- "kw",- "kg",- "la",- "lv",- "lb",- "ls",- "lr",- "ly",- "li",- "lt",- "lu",- "mo",- "mk",- "mg",- "mw",- "my",- "mv",- "ml",- "mt",- "mh",- "mq",- "mr",- "mu",- "yt",- "mx",- "md",- "mc",- "mn",- "me",- "ms",- "ma",- "mz",- "mm",- "na",- "nr",- "np",- "nl",- "nc",- "nz",- "ni",- "ne",- "ng",- "nu",- "nf",- "nc",- "tr",- "kp",- "mp",- "no",- "om",- "pk",- "pw",- "ps",- "pa",- "pg",- "py",- "pe",- "ph",- "pn",- "pl",- "pt",- "pr",- "qa",- "ro",- "ru",- "rw",- "re",- "bq",- "an",- "bl",- "gp",- "fr",- "sh",- "kn",- "lc",- "mf",- "gp",- "fr",- "pm",- "vc",- "ws",- "sm",- "st",- "sa",- "sn",- "rs",- "sc",- "sl",- "sg",- "bq",- "an",- "nl",- "sx",- "an",- "sk",- "si",- "sb",- "so",- "so",- "za",- "gs",- "kr",- "ss",- "es",- "lk",- "sd",- "sr",- "sj",- "sz",- "se",- "ch",- "sy",- "tw",- "tj",- "tz",- "th",- "tg",- "tk",- "to",- "tt",- "tn",- "tr",- "tm",- "tc",- "tv",- "ug",- "ua",- "ae",- "uk",- "us",- "vi",- "uy",- "uz",- "vu",- "va",- "ve",- "vn",- "wf",- "eh",- "ma",- "ye",- "zm",- "zw",-]);--export const secondLevelDomains = new Set([- "com",- "co",- "net",- "org",- "edu",- "gov",- "in",-]);--export const SPECIAL_APEX_DOMAINS = {- "youtu.be": "youtube.com",-};+export default COUNTRIES;diff --git a/lib/constants/default-redirects.ts b/lib/constants/default-redirects.tsnew file mode 100644index 0000000000..6c822695a6--- /dev/null+++ b/lib/constants/default-redirects.ts@@ -0,0 +1,13 @@+const DEFAULT_REDIRECTS = {+ home: "https://dub.sh",+ signin: "https://app.dub.sh/login",+ login: "https://app.dub.sh/login",+ register: "https://app.dub.sh/register",+ signup: "https://app.dub.sh/register",+ app: "https://app.dub.sh",+ dashboard: "https://app.dub.sh",+ links: "https://app.dub.sh/links",+ settings: "https://app.dub.sh/settings",+};++export default DEFAULT_REDIRECTS;diff --git a/lib/constants/index.ts b/lib/constants/index.tsnew file mode 100644index 0000000000..5d41610ac6--- /dev/null+++ b/lib/constants/index.ts@@ -0,0 +1,46 @@+export const LOCALHOST_GEO_DATA = {+ city: "San Francisco",+ region: "CA",+ country: "US",+ latitude: "37.7695",+ longitude: "-122.385",+};++export const INTERVALS = ["1h", "24h", "7d", "30d"];++export const FRAMER_MOTION_LIST_ITEM_VARIANTS = {+ hidden: { scale: 0.8, opacity: 0 },+ show: { scale: 1, opacity: 1, transition: { type: "spring" } },+};++export const HOME_HOSTNAMES = new Set([+ // comment for better diffs+ "dub.sh",+ "preview.dub.sh",+ "localhost:3000",+]);++export { default as RESERVED_KEYS } from "./reserved-keys";+export { default as DEFAULT_REDIRECTS } from "./default-redirects";++export const FREE_PLAN_PROJECT_LIMIT = 5;+export const FAVICON_FOLDER = "/_static/favicons";+export { default as COUNTRIES } from "./countries";+export { default as ccTLDs } from "./cctlds";++export const SECOND_LEVEL_DOMAINS = new Set([+ "com",+ "co",+ "net",+ "org",+ "edu",+ "gov",+ "in",+]);++export const SPECIAL_APEX_DOMAINS = {+ "youtu.be": "youtube.com",+};++export { default as BLACKLISTED_DOMAINS } from "./blacklisted-domains";+export { default as BLACKLISTED_EMAILS } from "./blacklisted-emails";diff --git a/lib/constants/reserved-keys.ts b/lib/constants/reserved-keys.tsnew file mode 100644index 0000000000..084ea4c322--- /dev/null+++ b/lib/constants/reserved-keys.ts@@ -0,0 +1,24 @@+const RESERVED_KEYS = new Set([+ "api",+ "va",+ "static",+ "stats",+ "proxy",+ "auth",+ "pricing",+ "about",+ "placeholder",+ "blog",+ "careers",+ "features",+ "contact",+ "terms",+ "privacy",+ "admin",+ "help",+ "new",+ "info",+ "demo",+]);++export default RESERVED_KEYS;diff --git a/lib/utils.ts b/lib/utils.tsindex 9e9bc3756f..0bd723c013 100644--- a/lib/utils.ts+++ b/lib/utils.ts@@ -1,7 +1,11 @@import { NextRouter } from "next/router";import ms from "ms";import { customAlphabet } from "nanoid";-import { SPECIAL_APEX_DOMAINS, ccTLDs, secondLevelDomains } from "./constants";+import {+ SPECIAL_APEX_DOMAINS,+ ccTLDs,+ SECOND_LEVEL_DOMAINS,+} from "./constants";export const nanoid = customAlphabet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",@@ -209,7 +213,7 @@ export const getApexDomain = (url: string) => {if (parts.length > 2) {// if this is a second-level TLD (e.g. co.uk, .com.ua, .org.tt), we need to return the last 3 partsif (- secondLevelDomains.has(parts[parts.length - 2]) &&+ SECOND_LEVEL_DOMAINS.has(parts[parts.length - 2]) &&ccTLDs.has(parts[parts.length - 1])) {return parts.slice(-3).join(".");@@ -221,6 +225,16 @@ export const getApexDomain = (url: string) => {return domain;};+export const getDomainWithoutWWW = (url: string) => {+ let hostname;+ try {+ hostname = new URL(url).hostname;+ } catch (e) {+ return "";+ }+ return hostname.replace(/^www\./, "");+};+export const getParamsFromURL = (url: string) => {if (!url) return {};try {diff --git a/pages/api/auth/[...nextauth].tsx b/pages/api/auth/[...nextauth].tsxindex f875116e94..cc18b10300 100644--- a/pages/api/auth/[...nextauth].tsx+++ b/pages/api/auth/[...nextauth].tsx@@ -5,6 +5,7 @@ import WelcomeEmail from "emails/WelcomeEmail";import NextAuth, { type NextAuthOptions } from "next-auth";import EmailProvider from "next-auth/providers/email";import prisma from "@/lib/prisma";+import { BLACKLISTED_EMAILS } from "@/lib/constants";const VERCEL_DEPLOYMENT = !!process.env.VERCEL_URL;@@ -36,6 +37,12 @@ export const authOptions: NextAuthOptions = {},},callbacks: {+ signIn: async ({ user }) => {+ if (BLACKLISTED_EMAILS.has(user.email.split("@")[1])) {+ return false;+ }+ return true;+ },session: async ({ session, token }) => {session.user = {// @ts-ignorediff --git a/pages/api/edge/links/index.ts b/pages/api/edge/links/index.tsindex 8c8b948fbc..1e681d6eac 100644--- a/pages/api/edge/links/index.ts+++ b/pages/api/edge/links/index.ts@@ -1,5 +1,7 @@import type { NextRequest } from "next/server";import { setRandomKey } from "@/lib/upstash";+import { BLACKLISTED_DOMAINS } from "@/lib/constants";+import { getDomainWithoutWWW } from "@/lib/utils";export const config = {runtime: "experimental-edge",@@ -11,6 +13,9 @@ export default async function handler(req: NextRequest) {if (!url) {return new Response(`Missing url`, { status: 400 });}+ if (BLACKLISTED_DOMAINS.has(getDomainWithoutWWW(url))) {+ return new Response(`Invalid url`, { status: 400 });+ }const { response, key } = await setRandomKey(url);if (response === "OK") {// if key was successfully addeddiff --git a/pages/api/links/index.ts b/pages/api/links/index.tsindex 6ba921e1e5..a2c98bc79f 100644--- a/pages/api/links/index.ts+++ b/pages/api/links/index.ts@@ -1,6 +1,8 @@import { NextApiRequest, NextApiResponse } from "next";import { addLink, getLinksForProject } from "@/lib/api/links";import { Session, withUserAuth } from "@/lib/auth";+import { BLACKLISTED_DOMAINS } from "@/lib/constants";+import { getDomainWithoutWWW } from "@/lib/utils";// This is a special route for retrieving and creating custom dub.sh links.@@ -26,6 +28,9 @@ export default withUserAuth(if (!key || !url) {return res.status(400).json({ error: "Missing key or url" });}+ if (BLACKLISTED_DOMAINS.has(getDomainWithoutWWW(url))) {+ return res.status(400).json({ error: "Invalid url" });+ }const response = await addLink({...req.body,domain: "dub.sh",@@ -33,7 +38,7 @@ export default withUserAuth(});if (response === null) {- return res.status(400).json({ error: "Key already exists" });+ return res.status(403).json({ error: "Key already exists" });}return res.status(200).json(response);} else {diff --git a/pages/app/login.tsx b/pages/app/login.tsxindex e3e98b5602..3196c7af52 100644--- a/pages/app/login.tsx+++ b/pages/app/login.tsx@@ -47,6 +47,7 @@ export default function Login() {}).then((res) => {setSignInClicked(false);if (res?.ok && !res?.error) {+ setEmail("");setButtonText("Email sent - check your inbox!");} else {setButtonText("Error sending email - try again?");@@ -71,6 +72,7 @@ export default function Login() {placeholder="[email protected]"autoComplete="email"required+ value={email}onChange={(e) => {setNoSuchAccount(false);setEmail(e.target.value);diff --git a/pages/app/register.tsx b/pages/app/register.tsxindex 69a27ccb7f..ec5eeb452b 100644--- a/pages/app/register.tsx+++ b/pages/app/register.tsx@@ -45,6 +45,7 @@ export default function Login() {}).then((res) => {setSignInClicked(false);if (res?.ok && !res?.error) {+ setEmail("");setButtonText("Email sent - check your inbox!");} else {setButtonText("Error sending email - try again?");@@ -69,6 +70,7 @@ export default function Login() {placeholder="[email protected]"autoComplete="email"required+ value={email}onChange={(e) => {setAccountExists(false);setEmail(e.target.value);