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.

rate limit

Kiran K88559f3c2ed1
diff --git a/apps/web/lib/actions/create-user-account.ts b/apps/web/lib/actions/create-user-account.ts
index ebcfd2cef1..d32c7998fa 100644
--- a/apps/web/lib/actions/create-user-account.ts
+++ b/apps/web/lib/actions/create-user-account.ts
@@ -1,10 +1,11 @@
"use server";
import { prisma } from "@/lib/prisma";
-import { redis } from "@/lib/upstash";
+import { ratelimit, redis } from "@/lib/upstash";
import { sendEmail } from "emails";
import VerifyEmail from "emails/verify-email";
import { flattenValidationErrors } from "next-safe-action";
+import { getIP } from "../api/utils";
import { generateOTP } from "../auth";
import { EMAIL_OTP_EXPIRY_IN } from "../auth/constants";
import { hashPassword } from "../auth/password";
@@ -19,9 +20,15 @@ export const createUserAccountAction = actionClient
flattenValidationErrors(ve).fieldErrors,
})
.use(throwIfAuthenticated)
- .action(async ({ parsedInput }) => {
+ .action(async ({ parsedInput, ctx }) => {
const { email, password } = parsedInput;
+ const { success } = await ratelimit(1, "1 m").limit(`signup:${getIP()}`);
+
+ if (!success) {
+ throw new Error("Too many requests. Please try again later.");
+ }
+
// Check user with email exists
const user = await prisma.user.findUnique({
where: {
diff --git a/apps/web/lib/actions/verify-email.ts b/apps/web/lib/actions/verify-email.ts
index 255a2fac77..faa17732f7 100644
--- a/apps/web/lib/actions/verify-email.ts
+++ b/apps/web/lib/actions/verify-email.ts
@@ -1,7 +1,9 @@
"use server";
import { prisma } from "@/lib/prisma";
+import { ratelimit } from "@/lib/upstash";
import { flattenValidationErrors } from "next-safe-action";
+import { getIP } from "../api/utils";
import { verifyEmailSchema } from "../zod/schemas/auth";
import { throwIfAuthenticated } from "./middlewares/throw-if-authenticated";
import { actionClient } from "./safe-action";
@@ -16,6 +18,14 @@ export const verifyEmailAction = actionClient
.action(async ({ parsedInput }) => {
const { email, code } = parsedInput;
+ const { success } = await ratelimit(3, "1 m").limit(
+ `verifyEmail:${getIP()}`,
+ );
+
+ if (!success) {
+ throw new Error("Too many requests. Please try again later.");
+ }
+
const verificationToken = await prisma.verificationToken.findUnique({
where: {
identifier: email,
diff --git a/apps/web/lib/api/utils.ts b/apps/web/lib/api/utils.ts
index be70554646..caca93bd44 100644
--- a/apps/web/lib/api/utils.ts
+++ b/apps/web/lib/api/utils.ts
@@ -1,11 +1,10 @@
import { ipAddress } from "@vercel/edge";
import { getToken } from "next-auth/jwt";
+import { headers } from "next/headers";
import { NextRequest } from "next/server";
import { ratelimit } from "../upstash";
import { DubApiError } from "./errors";
-// TODO:
-// Move this to a proper place
export const parseRequestBody = async (req: Request) => {
try {
return await req.json();
@@ -40,3 +39,14 @@ export const ratelimitOrThrow = async (
}
}
};
+
+export const getIP = () => {
+ const FALLBACK_IP_ADDRESS = "0.0.0.0";
+ const forwardedFor = headers().get("x-forwarded-for");
+
+ if (forwardedFor) {
+ return forwardedFor.split(",")[0] ?? FALLBACK_IP_ADDRESS;
+ }
+
+ return headers().get("x-real-ip") ?? FALLBACK_IP_ADDRESS;
+};