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.

Disallow email changes for accounts younger than 1 hour.

Kiran K65f0325a5174
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.ts
index b546ad0605..9f8e183f31 100644
--- a/apps/web/app/api/user/route.ts
+++ b/apps/web/app/api/user/route.ts
@@ -168,6 +168,7 @@ export const PATCH = withSession(async ({ req, session }) => {
email: session.user.email,
newEmail: email,
identifier: session.user.id,
+ userId: session.user.id,
hostName: emailChangeHost,
});
}
diff --git a/apps/web/lib/actions/partners/update-partner-profile.ts b/apps/web/lib/actions/partners/update-partner-profile.ts
index 1029317953..7663bf1c9e 100644
--- a/apps/web/lib/actions/partners/update-partner-profile.ts
+++ b/apps/web/lib/actions/partners/update-partner-profile.ts
@@ -236,6 +236,7 @@ export const updatePartnerProfileAction = authPartnerActionClient
email: partner.email,
newEmail,
identifier: partner.id,
+ userId: user.id,
isPartnerProfile: true,
hostName: PARTNERS_DOMAIN,
});
diff --git a/apps/web/lib/auth/request-email-change.ts b/apps/web/lib/auth/request-email-change.ts
index cb34a443e1..119085d8cc 100644
--- a/apps/web/lib/auth/request-email-change.ts
+++ b/apps/web/lib/auth/request-email-change.ts
@@ -9,11 +9,14 @@ import { isEmailDomainBlocked } from "../email/is-email-domain-blocked";
import { isGenericEmail } from "../is-generic-email";
import { ratelimit, redis } from "../upstash";
+const EMAIL_CHANGE_MIN_ACCOUNT_AGE_MS = 60 * 60 * 1000; // 1 hour
+
// Send the OTP to confirm the email address change for existing users/partners
export const requestEmailChange = async ({
email,
newEmail,
identifier,
+ userId,
isPartnerProfile = false,
syncIdentity = false,
partnerId,
@@ -23,6 +26,7 @@ export const requestEmailChange = async ({
email: string;
newEmail: string;
identifier: string;
+ userId: string;
isPartnerProfile?: boolean; // If true, the email is being changed for a partner profile
syncIdentity?: boolean; // If true, update both user and partner email on confirm
partnerId?: string;
@@ -36,6 +40,33 @@ export const requestEmailChange = async ({
});
}
+ const user = await prisma.user.findUnique({
+ where: {
+ id: userId,
+ },
+ select: {
+ createdAt: true,
+ },
+ });
+
+ if (!user) {
+ throw new DubApiError({
+ code: "not_found",
+ message: "User not found.",
+ });
+ }
+
+ const isAccountTooNew =
+ Date.now() - user.createdAt.getTime() < EMAIL_CHANGE_MIN_ACCOUNT_AGE_MS;
+
+ if (isAccountTooNew) {
+ throw new DubApiError({
+ code: "forbidden",
+ message:
+ "This action is temporarily unavailable for your account. Please try again later or contact support at dub.co/support",
+ });
+ }
+
const { success } = await ratelimit(3, "1 d").limit(
`email-change-request:${identifier}`,
);
diff --git a/apps/web/lib/partners/sync-partner-identity.ts b/apps/web/lib/partners/sync-partner-identity.ts
index 7c1942419a..4467f93440 100644
--- a/apps/web/lib/partners/sync-partner-identity.ts
+++ b/apps/web/lib/partners/sync-partner-identity.ts
@@ -171,6 +171,7 @@ export async function requestSyncedEmailChange({
email: currentEmail,
newEmail,
identifier: userId,
+ userId,
hostName,
syncIdentity: true,
partnerId,