Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
set cooldown to 2 mins
  • Loading branch information
NiallJoeMaher committed Oct 12, 2024
commit 9192b733df2feeae145ad016801d6d8b811d6c68
2 changes: 1 addition & 1 deletion app/(app)/settings/_client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const Settings = ({ profile }: { profile: User }) => {
setLoading(false);
toast.success("Verification link sent to your email.");
setSendForVerification(true);
setCooldown(60); // Set a 60-second cooldown
setCooldown(120); // Set a 2 minute cooldown
setEmailError(""); // Clear any existing error
},
},
Expand Down
8 changes: 3 additions & 5 deletions server/api/router/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,18 +152,16 @@ export const profileRouter = createTRPCRouter({
if (existingUser) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Email already in use",
message: "Unable to process the request",
});
}
Comment on lines +148 to +157
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Prevent Timing Attacks in User Existence Checks

When checking if the new email is already in use, the error message reveals whether an email is registered in the system. This can be a security concern, as it might allow attackers to validate email addresses. Consider returning a generic error message instead.

[security]

Apply this diff to return a generic error:

   if (existingUser) {
     throw new TRPCError({
       code: "BAD_REQUEST",
-      message: "Email already in use",
+      message: "Unable to process the request",
     });
   }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Check if the new email is already in use
const existingUser = await ctx.db.query.user.findFirst({
where: eq(user.email, newEmail),
});
if (ifEmailExists) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Email already exists",
});
}
if (existingUser) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Email already in use",
});
}
// Check if the new email is already in use
const existingUser = await ctx.db.query.user.findFirst({
where: eq(user.email, newEmail),
});
if (existingUser) {
throw new TRPCError({
code: "BAD_REQUEST",
message: "Unable to process the request",
});
}


// Rate limiting: Check for recent requests
const twoMinutesAgo = new Date(Date.now() - 2 * 60 * 1000);
const recentRequest = await ctx.db.query.emailChangeRequest.findFirst({
where: and(
eq(emailChangeRequest.userId, userId),
gte(
emailChangeRequest.createdAt,
new Date(Date.now() - 5 * 60 * 1000),
), // 5 minutes
gte(emailChangeRequest.createdAt, twoMinutesAgo), // 2 minutes
),
});

Expand Down