Skip to content

Commit 26fdf54

Browse files
authored
Merge pull request #108 from SOURAV-ROY/sentinel/add-user-details-and-password-validation-9377877397732449616
🛡️ Sentinel: Add Joi input validation for auth update details and password endpoints
2 parents ea5019e + f7f362d commit 26fdf54

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,16 @@ app.use(hpp());
115115
app.use(cors());
116116

117117
// Enforce secret presence in production; provide safe fallback in dev/test
118-
if (process.env.NODE_ENV === "production" && (!process.env.SESSION_SECRET || !process.env.JWT_SECRET)) {
119-
throw new Error("FATAL SECURITY ERROR: SESSION_SECRET and JWT_SECRET must be defined in production mode.");
118+
if (
119+
process.env.NODE_ENV === "production" &&
120+
(!process.env.SESSION_SECRET || !process.env.JWT_SECRET)
121+
) {
122+
throw new Error(
123+
"FATAL SECURITY ERROR: SESSION_SECRET and JWT_SECRET must be defined in production mode.",
124+
);
120125
}
121-
const sessionSecret = process.env.SESSION_SECRET || "dev_session_secret_fallback_key_32_chars";
126+
const sessionSecret =
127+
process.env.SESSION_SECRET || "dev_session_secret_fallback_key_32_chars";
122128

123129
// Set up session middleware
124130
app.use(

routes/authRoute.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ const forgotPasswordLimiter = expressRateLimit({
2121
validate: { trustProxy: false },
2222
message: {
2323
success: false,
24-
error: "Too many password reset requests from this IP, please try again after 15 minutes",
24+
error:
25+
"Too many password reset requests from this IP, please try again after 15 minutes",
2526
},
2627
});
2728

@@ -35,6 +36,8 @@ const {
3536
loginSchema,
3637
resetPasswordSchema,
3738
forgotPasswordSchema,
39+
updateDetailsSchema,
40+
updatePasswordSchema,
3841
},
3942
} = require("../utils/validators");
4043

@@ -44,8 +47,18 @@ router.get("/logout", logout);
4447
router.get("/csrf-token", getCsrfToken);
4548
router.get("/me", protect, getMe);
4649

47-
router.put("/updatedetails", protect, updateDetails);
48-
router.put("/updatepassword", protect, updatePassword);
50+
router.put(
51+
"/updatedetails",
52+
protect,
53+
validate(updateDetailsSchema),
54+
updateDetails,
55+
);
56+
router.put(
57+
"/updatepassword",
58+
protect,
59+
validate(updatePasswordSchema),
60+
updatePassword,
61+
);
4962

5063
router.post(
5164
"/forgotpassword",

utils/validators/authValidator.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,21 @@ const forgotPasswordSchema = Joi.object({
2020
email: Joi.string().email().required(),
2121
}).unknown(true);
2222

23+
const updateDetailsSchema = Joi.object({
24+
name: Joi.string().optional(),
25+
email: Joi.string().email().optional(),
26+
}).unknown(true);
27+
28+
const updatePasswordSchema = Joi.object({
29+
currentPassword: Joi.string().required(),
30+
newPassword: Joi.string().min(6).required(),
31+
}).unknown(true);
32+
2333
module.exports = {
2434
registerSchema,
2535
loginSchema,
2636
resetPasswordSchema,
2737
forgotPasswordSchema,
38+
updateDetailsSchema,
39+
updatePasswordSchema,
2840
};

0 commit comments

Comments
 (0)