-
Notifications
You must be signed in to change notification settings - Fork 34
Fix/otp rate limit security #89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
24211a05q8-hue
wants to merge
9
commits into
kunalverma2512:main
Choose a base branch
from
24211a05q8-hue:fix/otp-rate-limit-security
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c323d1d
fix: add OTP brute-force protection and rate limiting
24211a05q8-hue b84b299
fix: add GitHub OAuth environment validation
24211a05q8-hue 401e28f
fix: add OTP expiration TTL
24211a05q8-hue 1474f1a
fix: make OTP creation atomic
24211a05q8-hue 265d552
fix: strengthen OTP security and GitHub auth handling
24211a05q8-hue 728adc3
fix: resolve OTP TTL and auth security review issues
24211a05q8-hue 939fc97
fix: resolve OTP TTL lockout issue
24211a05q8-hue d8049c5
fix: address auth security review feedback
24211a05q8-hue 5b8a484
fix: add OTP verification rate limiting and security improvements
24211a05q8-hue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,39 @@ | ||
|
|
||
| import dotenv from "dotenv"; | ||
|
|
||
| dotenv.config(); | ||
| // FORCE load .env from correct path | ||
| dotenv.config({ path: "./.env" }); | ||
|
|
||
| const requiredEnvVars = [ | ||
| "PORT", | ||
| "MONGO_URI", | ||
| "JWT_SECRET", | ||
| "JWT_EXPIRES_IN", | ||
| "GEMINI_API_KEY", | ||
| "CLIENT_URL", | ||
| "NODE_ENV", | ||
| "SMTP_HOST", | ||
| "SMTP_PORT", | ||
| "SMTP_USER", | ||
| "SMTP_PASS" | ||
| "SMTP_PASS", | ||
| "GEMINI_API_KEY", | ||
| "GITHUB_CLIENT_ID", | ||
| "GITHUB_CLIENT_SECRET", | ||
| "GITHUB_CALLBACK_URL" | ||
| ]; | ||
|
|
||
| const missingEnvVars = requiredEnvVars.filter(envVar => !process.env[envVar]); | ||
| const missingEnvVars = requiredEnvVars.filter( | ||
| (key) => !process.env[key] | ||
| ); | ||
|
|
||
| console.log("DEBUG ENV:", { | ||
| PORT: process.env.PORT, | ||
| MONGO_URI: process.env.MONGO_URI ? "OK" : "MISSING" | ||
| }); | ||
|
|
||
| if (missingEnvVars.length > 0) { | ||
| throw new Error(`Missing required environment variables: ${missingEnvVars.join(", ")}`); | ||
| throw new Error( | ||
| `Missing required environment variables: ${missingEnvVars.join(", ")}` | ||
| ); | ||
| } | ||
|
|
||
| export const env = { | ||
| PORT: process.env.PORT, | ||
| MONGO_URI: process.env.MONGO_URI, | ||
| JWT_SECRET: process.env.JWT_SECRET, | ||
| JWT_EXPIRES_IN: process.env.JWT_EXPIRES_IN, | ||
| GEMINI_API_KEY: process.env.GEMINI_API_KEY, | ||
| CLIENT_URL: process.env.CLIENT_URL, | ||
| NODE_ENV: process.env.NODE_ENV, | ||
| SMTP_HOST: process.env.SMTP_HOST, | ||
| SMTP_PORT: parseInt(process.env.SMTP_PORT, 10), | ||
| SMTP_USER: process.env.SMTP_USER, | ||
| SMTP_PASS: process.env.SMTP_PASS | ||
| }; | ||
|
|
||
| export default env; | ||
| export default process.env; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import rateLimit from "express-rate-limit"; | ||
|
|
||
| export const otpLimiter = rateLimit({ | ||
| windowMs: 15 * 60 * 1000, // 15 minutes | ||
|
|
||
| max: 10, | ||
|
|
||
| message: { | ||
| success: false, | ||
| message: "Too many OTP requests. Please try again later.", | ||
| }, | ||
|
|
||
| standardHeaders: true, | ||
| legacyHeaders: false, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,48 @@ | ||
|
|
||
| import mongoose from "mongoose"; | ||
|
|
||
| const otpSchema = new mongoose.Schema({ | ||
| email: { type: String, required: true, lowercase: true, index: true }, | ||
| otp: { type: String, required: true }, // stored hashed | ||
| purpose: { type: String, enum: ["signup", "forgot-password"], required: true }, | ||
| createdAt: { type: Date, default: Date.now, expires: 600 } // TTL 10 minutes | ||
| email: { | ||
| type: String, | ||
| required: true, | ||
| lowercase: true, | ||
| index: true, | ||
| }, | ||
|
|
||
| otp: { | ||
| type: String, | ||
| required: true, | ||
| }, | ||
|
|
||
| purpose: { | ||
| type: String, | ||
| enum: ["signup", "forgot-password"], | ||
| required: true, | ||
| }, | ||
|
|
||
| failedAttempts: { | ||
| type: Number, | ||
| default: 0, | ||
| }, | ||
|
|
||
| lockUntil: { | ||
| type: Date, | ||
| default: null, | ||
| }, | ||
|
|
||
| createdAt: { | ||
| type: Date, | ||
| default: Date.now, | ||
| expires: 1200, | ||
| }, | ||
| }); | ||
|
|
||
| otpSchema.index( | ||
| { email: 1, purpose: 1 }, | ||
| { unique: true } | ||
| ); | ||
|
|
||
| export default mongoose.model("Otp", otpSchema); | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep OTP expiry aligned with the email copy.
expires: 1200keeps OTPs around for 20 minutes, but both OTP email templates still tell users the code expires in 10 minutes. If 20 minutes is intentional to cover the 15-minute lockout window, the templates need to be updated from the same source of truth.🤖 Prompt for AI Agents