The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Improve security
Steven Tey3ce5f049f810
diff --git a/components/app/modals/delete-link-modal.tsx b/components/app/modals/delete-link-modal.tsxindex 987e12a1a9..4eb2e3e4ae 100644--- a/components/app/modals/delete-link-modal.tsx+++ b/components/app/modals/delete-link-modal.tsx@@ -45,7 +45,7 @@ function DeleteLinkModal({setShowModal={setShowDeleteLinkModal}><div className="inline-block w-full transform overflow-hidden bg-white align-middle shadow-xl transition-all sm:max-w-md sm:rounded-2xl sm:border sm:border-gray-200">- <div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 px-4 py-4 pt-8 sm:px-16">+ <div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 px-4 py-4 pt-8 text-center sm:px-16"><BlurImagesrc={`${GOOGLE_FAVICON_URL}${apexDomain}`}alt={apexDomain}@@ -54,7 +54,7 @@ function DeleteLinkModal({height={20}/><h3 className="text-lg font-medium">Delete {shortlink}</h3>- <p className="text-center text-sm text-gray-500">+ <p className="text-sm text-gray-500">Warning: Deleting this link will remove all of its stats. Thisaction cannot be undone.</p>diff --git a/components/app/projects/project-card.tsx b/components/app/projects/project-card.tsxindex 82cc2af694..f8bb994db9 100644--- a/components/app/projects/project-card.tsx+++ b/components/app/projects/project-card.tsx@@ -71,14 +71,14 @@ export default function ProjectCard({<div className="flex items-center space-x-4"><div className="flex items-center space-x-2 text-gray-500"><Globe className="h-4 w-4" />- <h2 className="text-sm">+ <h2 className="whitespace-nowrap text-sm">{nFormatter(domains.length)} domain{domains.length > 1 && "s"}</h2></div><div className="flex items-center space-x-2 text-gray-500"><Link2 className="h-4 w-4" />{count || count === 0 ? (- <h2 className="text-sm">+ <h2 className="whitespace-nowrap text-sm">{nFormatter(count)} link{count != 1 && "s"}</h2>) : (@@ -87,7 +87,7 @@ export default function ProjectCard({</div><div className="flex items-center space-x-2 text-gray-500"><BarChart2 className="h-4 w-4" />- <h2 className="text-sm">+ <h2 className="whitespace-nowrap text-sm">{nFormatter(usage)} click{usage != 1 && "s"}</h2></div>diff --git a/lib/api/domains.ts b/lib/api/domains.tsindex f2dfd26379..ce97561710 100644--- a/lib/api/domains.ts+++ b/lib/api/domains.ts@@ -1,7 +1,7 @@import prisma from "@/lib/prisma";import { redis } from "@/lib/upstash";import cloudinary from "cloudinary";-import { validDomainRegex } from "@/lib/utils";+import { getApexDomain, validDomainRegex } from "@/lib/utils";export const validateDomain = async (domain: string) => {if (!domain || typeof domain !== "string") {@@ -13,18 +13,35 @@ export const validateDomain = async (domain: string) => {if (!validDomain) {return "Invalid domain";}- const domainExists = await prisma.domain.findUnique({+ const exists = await domainExists(domain);+ if (exists) {+ return "Domain is already in use.";+ }+ return true;+};++export const domainExists = async (domain: string) => {+ const apexDomain = getApexDomain(`https://${domain}`);+ const response = await prisma.domain.findFirst({where: {- slug: domain,+ OR: [+ // if someone tries to add "sub.domain.com" but "domain.com" is already in use+ {+ slug: apexDomain,+ },+ // if someone tries to add "domain.com" but "sub.domain.com" is already in use+ {+ slug: {+ endsWith: `.${apexDomain}`,+ },+ },+ ],},select: {slug: true,},});- if (domainExists) {- return "Domain is already in use.";- }- return true;+ return response ? true : false;};interface CustomResponse extends Response {diff --git a/pages/api/projects/[slug]/domains/[domain]/exists.ts b/pages/api/projects/[slug]/domains/[domain]/exists.tsindex 1d031976e5..fec9a6f60f 100644--- a/pages/api/projects/[slug]/domains/[domain]/exists.ts+++ b/pages/api/projects/[slug]/domains/[domain]/exists.ts@@ -1,6 +1,6 @@import { NextApiRequest, NextApiResponse } from "next";import { withUserAuth } from "@/lib/auth";-import prisma from "@/lib/prisma";+import { domainExists } from "@/lib/api/domains";export default withUserAuth(async (req: NextApiRequest, res: NextApiResponse) => {@@ -8,15 +8,8 @@ export default withUserAuth(// GET /api/projects/[slug]/domains/[domain]/exists – check if a domain existsif (req.method === "GET") {- const project = await prisma.domain.findUnique({- where: {- slug: domain,- },- select: {- slug: true,- },- });- if (project) {+ const exists = await domainExists(domain);+ if (exists) {return res.status(200).json(1);} else {return res.status(200).json(0);diff --git a/pages/api/projects/[slug]/domains/[domain]/index.ts b/pages/api/projects/[slug]/domains/[domain]/index.tsindex b6fc998d6e..da696c6d86 100644--- a/pages/api/projects/[slug]/domains/[domain]/index.ts+++ b/pages/api/projects/[slug]/domains/[domain]/index.ts@@ -22,6 +22,17 @@ export default withProjectAuth(async (req, res, project) => {.json({ error: "Missing or misconfigured project slug or domain" });}+ // prevent unauthorized access to domains that don't belong to the project+ const domainBelongsToProject = await prisma.domain.findFirst({+ where: {+ slug: domain,+ projectId: project.id,+ },+ });+ if (!domainBelongsToProject) {+ return res.status(404).json({ error: "Domain not found" });+ }+// PUT /api/projects/[slug]/domains/[domain] edit a project's domainif (req.method === "PUT") {const { slug: newDomain, target, type, primary } = req.body;diff --git a/pages/api/projects/index.ts b/pages/api/projects/index.tsindex 9aa9346a0e..bc96dc23f7 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 } from "@/lib/constants";-import { addDomain, validateDomain } from "@/lib/api/domains";+import { addDomain, domainExists, validateDomain } from "@/lib/api/domains";import prisma from "@/lib/prisma";-import { isReservedKey } from "@/lib/utils";+import { getApexDomain, isReservedKey } from "@/lib/utils";export default withUserAuth(async (req: NextApiRequest, res: NextApiResponse, session: Session) => {@@ -53,14 +53,7 @@ export default withUserAuth(slug: true,},}),- prisma.domain.findUnique({- where: {- slug: domain,- },- select: {- slug: true,- },- }),+ domainExists(domain),]);if (slugExist || domainExist) {return res.status(422).json({