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.

Merge branch 'main' into centralize-auth-rate-limits

Steven Teyc8b530de4e4d
diff --cc apps/web/lib/auth/request-email-change.ts
index c7fcc14e5d,119085d8cc..a0cffe92d1
--- a/apps/web/lib/auth/request-email-change.ts
+++ b/apps/web/lib/auth/request-email-change.ts
@@@ -7,10 -7,10 +7,12 @@@ import { hashToken } from "."
import { DubApiError } from "../api/errors";
import { isEmailDomainBlocked } from "../email/is-email-domain-blocked";
import { isGenericEmail } from "../is-generic-email";
-import { ratelimit, redis } from "../upstash";
+import { redis } from "../upstash";
+import { assertRateLimit } from "../upstash/assert-rate-limit";
+import { RATELIMIT_POLICIES } from "../upstash/ratelimit-policies";
+ 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,
@@@ -40,15 -40,44 +42,41 @@@
});
}
+ await assertRateLimit({
+ policy: RATELIMIT_POLICIES.emailChangeRequest,
+ identifier: userId,
+ });
+ 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}`,
- );
-
- if (!success) {
- throw new DubApiError({
- code: "rate_limit_exceeded",
- message:
- "You've requested too many email change requests. Please try again later.",
- });
- }
+ await assertRateLimit({
+ policy: RATELIMIT_POLICIES.emailChangeRequestTarget,
+ identifier: newEmail.toLowerCase(),
+ });
const isGenericEmailWithPlus = email.includes("+") && isGenericEmail(email);
const emailDomainBlocked = await isEmailDomainBlocked(newEmail);