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.

security code improvements

Pedro Ladeiraba5a394e7d9b
diff --git a/apps/web/app/(ee)/partners.dub.co/(dashboard)/profile/profile-details-form.tsx b/apps/web/app/(ee)/partners.dub.co/(dashboard)/profile/profile-details-form.tsx
index af92fd9361..9dd379a3d4 100644
--- a/apps/web/app/(ee)/partners.dub.co/(dashboard)/profile/profile-details-form.tsx
+++ b/apps/web/app/(ee)/partners.dub.co/(dashboard)/profile/profile-details-form.tsx
@@ -285,7 +285,7 @@ function BasicInfoForm({
const result = await executeAsync({
...data,
username: data.username || undefined,
- image: imageChanged ? data.image : null,
+ ...(imageChanged ? { image: data.image } : {}),
syncIdentity,
});
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.ts
index 5247f63898..708133f137 100644
--- a/apps/web/app/api/user/route.ts
+++ b/apps/web/app/api/user/route.ts
@@ -13,8 +13,8 @@ import { storage } from "@/lib/storage";
import { uploadedImageSchema } from "@/lib/zod/schemas/images";
import {
APP_DOMAIN,
- APP_HOSTNAMES,
PARTNERS_DOMAIN,
+ PARTNERS_HOSTNAMES,
R2_URL,
nanoid,
trim,
@@ -76,7 +76,8 @@ export const PATCH = withSession(async ({ req, session }) => {
await updateUserSchema.parseAsync(await req.json());
const hostName = req.headers.get("host") || "";
- const isPartnersDomain = !APP_HOSTNAMES.has(hostName);
+ const isPartnersDomain = PARTNERS_HOSTNAMES.has(hostName);
+ const emailChangeHost = isPartnersDomain ? PARTNERS_DOMAIN : APP_DOMAIN;
const partnerId = session.user.defaultPartnerId;
const shouldSyncIdentity =
syncIdentity === true && isPartnersDomain && !!partnerId;
@@ -167,7 +168,7 @@ export const PATCH = withSession(async ({ req, session }) => {
email: session.user.email,
newEmail: email,
identifier: session.user.id,
- hostName: APP_HOSTNAMES.has(hostName) ? APP_DOMAIN : PARTNERS_DOMAIN,
+ hostName: emailChangeHost,
});
}
}
@@ -178,7 +179,7 @@ export const PATCH = withSession(async ({ req, session }) => {
},
data: {
...(name && { name }),
- ...(image && { image }),
+ ...(image !== undefined && { image: image ?? null }),
...(source && { source }),
...(defaultWorkspace && { defaultWorkspace }),
},
@@ -187,8 +188,8 @@ export const PATCH = withSession(async ({ req, session }) => {
if (shouldSyncIdentity && partnerId) {
await syncNameAndImageToPartner({
partnerId,
- ...(name && { name }),
- ...(image && { image }),
+ ...(name !== undefined && name && { name }),
+ ...(image !== undefined && { image }),
});
}
diff --git a/apps/web/app/app.dub.co/(auth)/auth/confirm-email-change/[token]/page.tsx b/apps/web/app/app.dub.co/(auth)/auth/confirm-email-change/[token]/page.tsx
index 71064e4ce9..63353fe27a 100644
--- a/apps/web/app/app.dub.co/(auth)/auth/confirm-email-change/[token]/page.tsx
+++ b/apps/web/app/app.dub.co/(auth)/auth/confirm-email-change/[token]/page.tsx
@@ -1,4 +1,5 @@
import { getSession, hashToken } from "@/lib/auth";
+import { hasPermission } from "@/lib/auth/partner-users/partner-user-permissions";
import { prisma } from "@/lib/prisma";
import { redis } from "@/lib/upstash";
import EmptyState from "@/ui/shared/empty-state";
@@ -75,11 +76,41 @@ const VerifyEmailChange = async ({ params, searchParams }: PageProps) => {
redirect(`/login?next=/auth/confirm-email-change/${token}`);
}
- const { id: userId, defaultPartnerId: partnerId } = session.user;
+ const { id: userId } = session.user;
+ const tokenIdentifier = tokenFound.identifier;
- const identifier = tokenFound.identifier.startsWith("pn_")
- ? partnerId
- : userId;
+ if (tokenIdentifier.startsWith("pn_")) {
+ const partnerUser = await prisma.partnerUser.findUnique({
+ where: {
+ userId_partnerId: {
+ userId,
+ partnerId: tokenIdentifier,
+ },
+ },
+ select: { role: true },
+ });
+
+ if (
+ !partnerUser ||
+ !hasPermission(partnerUser.role, "partner_profile.update")
+ ) {
+ return (
+ <EmptyState
+ icon={InputPassword}
+ title="Invalid Token"
+ description="This token is invalid. Please request a new one."
+ />
+ );
+ }
+ } else if (tokenIdentifier !== userId) {
+ return (
+ <EmptyState
+ icon={InputPassword}
+ title="Invalid Token"
+ description="This token is invalid. Please request a new one."
+ />
+ );
+ }
const data = await redis.get<{
email: string;
@@ -88,7 +119,7 @@ const VerifyEmailChange = async ({ params, searchParams }: PageProps) => {
syncIdentity?: boolean;
partnerId?: string;
redirectTo?: "/profile" | "/account/settings";
- }>(`email-change-request:user:${identifier}`);
+ }>(`email-change-request:user:${tokenIdentifier}`);
if (!data) {
return (
@@ -120,10 +151,13 @@ const VerifyEmailChange = async ({ params, searchParams }: PageProps) => {
partnerId: syncedPartnerId,
},
},
- select: { userId: true },
+ select: { role: true },
});
- if (!partnerUser) {
+ if (
+ !partnerUser ||
+ !hasPermission(partnerUser.role, "partner_profile.update")
+ ) {
return (
<EmptyState
icon={InputPassword}
@@ -155,19 +189,9 @@ const VerifyEmailChange = async ({ params, searchParams }: PageProps) => {
// Update the partner profile email
else if (data.isPartnerProfile) {
- if (!partnerId) {
- return (
- <EmptyState
- icon={InputPassword}
- title="No Partner Profile Found"
- description="We couldn’t find a partner profile for your account. Please make sure you’re logged in with the correct account at https://partners.dub.co"
- />
- );
- }
-
await prisma.partner.update({
where: {
- id: partnerId,
+ id: tokenIdentifier,
},
data: {
email: data.newEmail,
@@ -198,6 +222,7 @@ const VerifyEmailChange = async ({ params, searchParams }: PageProps) => {
oldEmail: data.email,
newEmail: data.newEmail,
isPartnerProfile: !!data.isPartnerProfile,
+ syncIdentity: !!data.syncIdentity,
}),
}),
]),
diff --git a/apps/web/lib/actions/partners/update-partner-profile.ts b/apps/web/lib/actions/partners/update-partner-profile.ts
index 9b7e9299eb..d2fdfab384 100644
--- a/apps/web/lib/actions/partners/update-partner-profile.ts
+++ b/apps/web/lib/actions/partners/update-partner-profile.ts
@@ -93,17 +93,20 @@ export const updatePartnerProfileAction = authPartnerActionClient
input: parsedInput,
});
- let imageUrl: string | null = null;
+ let imageUrl: string | null | undefined = undefined;
let needsEmailVerification = false;
const emailChanged = newEmail !== undefined && partner.email !== newEmail;
- // Upload the new image
- if (image) {
- const uploaded = await storage.upload({
- key: `partners/${partner.id}/image_${nanoid(7)}`,
- body: image,
- });
- imageUrl = uploaded.url;
+ if (image !== undefined) {
+ if (image) {
+ const uploaded = await storage.upload({
+ key: `partners/${partner.id}/image_${nanoid(7)}`,
+ body: image,
+ });
+ imageUrl = uploaded.url;
+ } else {
+ imageUrl = null;
+ }
}
if (username && username !== partner.username) {
@@ -149,7 +152,7 @@ export const updatePartnerProfileAction = authPartnerActionClient
data: {
name,
description,
- ...(imageUrl && { image: imageUrl }),
+ ...(imageUrl !== undefined && { image: imageUrl }),
username,
profileType,
companyName,
@@ -186,6 +189,14 @@ export const updatePartnerProfileAction = authPartnerActionClient
// If the email is being changed, we need to verify the new email address
if (emailChanged) {
if (syncIdentity) {
+ if (!user.email) {
+ throw new DubApiError({
+ code: "bad_request",
+ message:
+ "Your login account does not have an email address on file.",
+ });
+ }
+
await assertEmailAvailableForIdentitySync({
newEmail,
userId: user.id,
@@ -193,7 +204,7 @@ export const updatePartnerProfileAction = authPartnerActionClient
});
await requestSyncedEmailChange({
- currentEmail: user.email!,
+ currentEmail: user.email,
newEmail,
userId: user.id,
partnerId: partner.id,
@@ -201,6 +212,14 @@ export const updatePartnerProfileAction = authPartnerActionClient
redirectTo: "/profile",
});
} else {
+ if (!partner.email) {
+ throw new DubApiError({
+ code: "bad_request",
+ message:
+ "Your partner profile does not have an email address on file.",
+ });
+ }
+
const partnerWithEmail = await prisma.partner.findUnique({
where: {
email: newEmail,
@@ -214,7 +233,7 @@ export const updatePartnerProfileAction = authPartnerActionClient
}
await confirmEmailChange({
- email: partner.email!,
+ email: partner.email,
newEmail,
identifier: partner.id,
isPartnerProfile: true,
@@ -228,8 +247,8 @@ export const updatePartnerProfileAction = authPartnerActionClient
if (syncIdentity) {
await syncNameAndImageToUser({
userId: user.id,
- ...(name && { name }),
- ...(imageUrl && { image: imageUrl }),
+ ...(name !== undefined && name && { name }),
+ ...(imageUrl !== undefined && { image: imageUrl }),
});
}
diff --git a/apps/web/lib/partners/sync-partner-identity.ts b/apps/web/lib/partners/sync-partner-identity.ts
index 2d80ca5c20..037456c8f1 100644
--- a/apps/web/lib/partners/sync-partner-identity.ts
+++ b/apps/web/lib/partners/sync-partner-identity.ts
@@ -78,7 +78,10 @@ export async function syncNameAndImageToPartner({
name?: string;
image?: string | null;
}) {
- if (!name && !image) {
+ const hasNameUpdate = name !== undefined;
+ const hasImageUpdate = image !== undefined;
+
+ if (!hasNameUpdate && !hasImageUpdate) {
return;
}
@@ -94,8 +97,8 @@ export async function syncNameAndImageToPartner({
await prisma.partner.update({
where: { id: partnerId },
data: {
- ...(name && { name }),
- ...(partnerImage && { image: partnerImage }),
+ ...(hasNameUpdate && name && { name }),
+ ...(hasImageUpdate && { image: partnerImage ?? null }),
},
});
}
@@ -109,7 +112,10 @@ export async function syncNameAndImageToUser({
name?: string;
image?: string | null;
}) {
- if (!name && !image) {
+ const hasNameUpdate = name !== undefined;
+ const hasImageUpdate = image !== undefined;
+
+ if (!hasNameUpdate && !hasImageUpdate) {
return;
}
@@ -122,8 +128,8 @@ export async function syncNameAndImageToUser({
await prisma.user.update({
where: { id: userId },
data: {
- ...(name && { name }),
- ...(userImage && { image: userImage }),
+ ...(hasNameUpdate && name && { name }),
+ ...(hasImageUpdate && { image: userImage ?? null }),
},
});
}
diff --git a/packages/email/src/templates/email-updated.tsx b/packages/email/src/templates/email-updated.tsx
index 79e2df9842..6578315d0e 100644
--- a/packages/email/src/templates/email-updated.tsx
+++ b/packages/email/src/templates/email-updated.tsx
@@ -18,11 +18,19 @@ export default function EmailUpdated({
oldEmail = "[email protected]",
newEmail = "[email protected]",
isPartnerProfile = false,
+ syncIdentity = false,
}: {
oldEmail: string;
newEmail: string;
isPartnerProfile?: boolean;
+ syncIdentity?: boolean;
}) {
+ const profileLabel = syncIdentity
+ ? "login account and partner profile"
+ : isPartnerProfile
+ ? "partner profile"
+ : "account";
+
return (
<Html>
<Head />
@@ -37,10 +45,8 @@ export default function EmailUpdated({
Your email address has been changed
</Heading>
<Text className="mx-auto text-sm leading-6">
- The e-mail address for your Dub{" "}
- {isPartnerProfile ? "partner profile" : "account"} has been
- changed from <strong>{oldEmail}</strong> to{" "}
- <strong>{newEmail}</strong>.
+ The e-mail address for your Dub {profileLabel} has been changed
+ from <strong>{oldEmail}</strong> to <strong>{newEmail}</strong>.
</Text>
<Text className="text-sm leading-6 text-black">
If you did not make this change, please contact our support team