-
Notifications
You must be signed in to change notification settings - Fork 13
feat(auth): add centralized request validation and input sanitization for authentication endpoints #61
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
base: main
Are you sure you want to change the base?
feat(auth): add centralized request validation and input sanitization for authentication endpoints #61
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,72 @@ | ||||||||||||||||||||||||
| import { Request, Response, NextFunction } from 'express'; | ||||||||||||||||||||||||
| import { body, validationResult } from 'express-validator'; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Register Validation | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| export const registerValidation = [ | ||||||||||||||||||||||||
| body('name') | ||||||||||||||||||||||||
| .trim() | ||||||||||||||||||||||||
| .notEmpty() | ||||||||||||||||||||||||
| .withMessage('Name is required') | ||||||||||||||||||||||||
| .isLength({ min: 2, max: 50 }) | ||||||||||||||||||||||||
| .withMessage('Name must be between 2 and 50 characters'), | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| body('email') | ||||||||||||||||||||||||
| .trim() | ||||||||||||||||||||||||
| .notEmpty() | ||||||||||||||||||||||||
| .withMessage('Email is required') | ||||||||||||||||||||||||
| .isEmail() | ||||||||||||||||||||||||
| .withMessage('Please provide a valid email address') | ||||||||||||||||||||||||
| .normalizeEmail(), | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| body('password') | ||||||||||||||||||||||||
| .trim() | ||||||||||||||||||||||||
| .notEmpty() | ||||||||||||||||||||||||
| .withMessage('Password is required') | ||||||||||||||||||||||||
| .isLength({ min: 8 }) | ||||||||||||||||||||||||
| .withMessage('Password must be at least 8 characters long'), | ||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Login Validation | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| export const loginValidation = [ | ||||||||||||||||||||||||
| body('email') | ||||||||||||||||||||||||
| .trim() | ||||||||||||||||||||||||
| .notEmpty() | ||||||||||||||||||||||||
| .withMessage('Email is required') | ||||||||||||||||||||||||
| .isEmail() | ||||||||||||||||||||||||
| .withMessage('Please provide a valid email address') | ||||||||||||||||||||||||
| .normalizeEmail(), | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| body('password') | ||||||||||||||||||||||||
| .trim() | ||||||||||||||||||||||||
| .notEmpty() | ||||||||||||||||||||||||
| .withMessage('Password is required'), | ||||||||||||||||||||||||
|
Comment on lines
+43
to
+46
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Enforce the password length floor on login too. Line 43-46 only reject empty passwords, so short values still reach Suggested fix export const loginValidation = [
body('email')
.trim()
.notEmpty()
.withMessage('Email is required')
.isEmail()
.withMessage('Please provide a valid email address')
.normalizeEmail(),
body('password')
.trim()
.notEmpty()
- .withMessage('Password is required'),
+ .withMessage('Password is required')
+ .bail()
+ .isLength({ min: 8 })
+ .withMessage('Password must be at least 8 characters long'),
];📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||
| * Validation Error Handler | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
| export const validateRequest = ( | ||||||||||||||||||||||||
| req: Request, | ||||||||||||||||||||||||
| res: Response, | ||||||||||||||||||||||||
| next: NextFunction | ||||||||||||||||||||||||
| ): void => { | ||||||||||||||||||||||||
| const errors = validationResult(req); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (!errors.isEmpty()) { | ||||||||||||||||||||||||
| res.status(400).json({ | ||||||||||||||||||||||||
| success: false, | ||||||||||||||||||||||||
| errors: errors.array().map(error => ({ | ||||||||||||||||||||||||
| field: error.type === 'field' ? error.path : undefined, | ||||||||||||||||||||||||
| message: error.msg, | ||||||||||||||||||||||||
| })), | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| next(); | ||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Stop each validation chain after the first failure.
Without
.bail(), whitespace-onlyname,email, orpasswordvalues can produce multiple errors for the same field (requiredplusinvalid/too short). That makes the centralized error payload less consistent than intended.Suggested fix
export const registerValidation = [ body('name') .trim() .notEmpty() .withMessage('Name is required') + .bail() .isLength({ min: 2, max: 50 }) .withMessage('Name must be between 2 and 50 characters'), body('email') .trim() .notEmpty() .withMessage('Email is required') + .bail() .isEmail() .withMessage('Please provide a valid email address') .normalizeEmail(), body('password') .trim() .notEmpty() .withMessage('Password is required') + .bail() .isLength({ min: 8 }) .withMessage('Password must be at least 8 characters long'), ]; export const loginValidation = [ body('email') .trim() .notEmpty() .withMessage('Email is required') + .bail() .isEmail() .withMessage('Please provide a valid email address') .normalizeEmail(), body('password') .trim() .notEmpty() .withMessage('Password is required') ];Also applies to: 35-46
🤖 Prompt for AI Agents