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.

Fixed domain auth

Steven Tey56c14a0718f8
diff --git a/components/app/modals/add-edit-link-modal/index.tsx b/components/app/modals/add-edit-link-modal/index.tsx
index 74727fbd84..c12b5964a1 100644
--- a/components/app/modals/add-edit-link-modal/index.tsx
+++ b/components/app/modals/add-edit-link-modal/index.tsx
@@ -187,7 +187,7 @@ function AddEditLinkModal({
return {
method: "PUT",
url: `/api/links/${encodeURIComponent(props.key)}${
- slug ? `?slug=${slug}&domain=${domain}` : ""
+ slug ? `?slug=${slug}&domain=${props.domain}` : ""
}`,
};
} else {
diff --git a/lib/api/links.ts b/lib/api/links.ts
index e8206f740e..359f5a046f 100644
--- a/lib/api/links.ts
+++ b/lib/api/links.ts
@@ -274,7 +274,16 @@ export async function addLink(link: LinkProps) {
return response;
}
-export async function editLink(link: LinkProps, oldKey: string) {
+export async function editLink(
+ link: LinkProps,
+ {
+ oldDomain,
+ oldKey,
+ }: {
+ oldDomain: string;
+ oldKey: string;
+ },
+) {
let {
id,
domain,
@@ -294,9 +303,10 @@ export async function editLink(link: LinkProps, oldKey: string) {
const hasPassword = password && password.length > 0 ? true : false;
const exat = expiresAt ? new Date(expiresAt).getTime() : null;
const changedKey = key !== oldKey;
+ const changedDomain = domain !== oldDomain;
const uploadedImage = image && image.startsWith("data:image") ? true : false;
- if (changedKey) {
+ if (changedDomain || changedKey) {
const exists = await checkIfKeyExists(domain, key);
if (exists) return null;
}
@@ -344,14 +354,14 @@ export async function editLink(link: LinkProps, oldKey: string) {
exat ? { exat } : {},
),
// if key is changed: rename resource in Cloudinary, delete the old key in Redis and change the clicks key name
- ...(changedKey
+ ...(changedDomain || changedKey
? [
cloudinary.v2.uploader
- .destroy(`${domain}/${oldKey}`, {
+ .destroy(`${oldDomain}/${oldKey}`, {
invalidate: true,
})
.catch(() => {}),
- redis.del(`${domain}:${oldKey}`),
+ redis.del(`${oldDomain}:${oldKey}`),
]
: []),
]);
diff --git a/lib/auth.ts b/lib/auth.ts
index 259b84ea4b..8c833e28d6 100644
--- a/lib/auth.ts
+++ b/lib/auth.ts
@@ -268,6 +268,11 @@ const withLinksAuth =
slug,
},
include: {
+ domains: {
+ select: {
+ slug: true,
+ },
+ },
users: {
where: {
userId: session.user.id,
@@ -305,26 +310,13 @@ const withLinksAuth =
return res.status(403).end("Unauthorized: Need higher plan.");
}
- // if domain is defined
- if (domain) {
- // prevent domain from being query injected by
- // comparing the domain in the query params to the domain in the body
- if (req.body.domain && req.body.domain !== domain) {
- return res.status(403).end("Unauthorized: Invalid domain.");
- }
-
- // check if the domain is part of the project
- const domainProjectId = await prisma.domain.findUnique({
- where: {
- slug: domain,
- },
- select: {
- projectId: true,
- },
- });
- if (domainProjectId?.projectId !== project.id) {
- return res.status(403).end("Unauthorized: Invalid domain.");
- }
+ // prevent unauthorized access to domains
+ if (
+ (domain && !project.domains?.find((d) => d.slug === domain)) ||
+ (req.body.domain &&
+ !project.domains?.find((d) => d.slug === req.body.domain))
+ ) {
+ return res.status(403).end("Unauthorized: Invalid domain.");
}
}
diff --git a/lib/types.ts b/lib/types.ts
index 98f9466580..bb29e1e3c8 100644
--- a/lib/types.ts
+++ b/lib/types.ts
@@ -57,10 +57,9 @@ export interface ProjectProps {
stripeId?: string;
billingCycleStart?: number;
- // TO DELETE
- ownerUsageLimit?: number;
- ownerExceededUsage?: boolean;
-
+ domains?: {
+ slug: string;
+ }[];
users?: {
role: string;
}[];
diff --git a/pages/api/links/[key]/index.ts b/pages/api/links/[key]/index.ts
index 2c114cb033..34be3190fa 100644
--- a/pages/api/links/[key]/index.ts
+++ b/pages/api/links/[key]/index.ts
@@ -13,7 +13,10 @@ export const config = {
export default withLinksAuth(
async (req, res, session, project, domain, link) => {
- const { key: oldKey } = req.query as { key: string };
+ const { key: oldKey, domain: oldDomain } = req.query as {
+ key: string;
+ domain: string;
+ };
// GET /api/links/:key – get a link
if (req.method === "GET") {
@@ -44,10 +47,13 @@ export default withLinksAuth(
editLink(
{
...req.body,
- domain: domain || "dub.sh",
+ domain: req.body.domain || "dub.sh",
userId: session.user.id,
},
- oldKey,
+ {
+ oldDomain,
+ oldKey,
+ },
),
...(!project
? [fetch(`${GOOGLE_FAVICON_URL}${url}`).then((res) => !res.ok)]