Skip to content
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

feat: added custom validation for loginId/userId when tried to auto l… #729

Merged
merged 6 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
10 changes: 5 additions & 5 deletions client/src/context/AuthContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createContext, useContext, useMemo, useReducer } from 'react';
import PropTypes from 'prop-types';

import authReducer, { initialState } from './reducers/authReducer';
import { validateUserID } from 'src/lib/utils';

const AuthContext = createContext({
...initialState,
Expand All @@ -22,11 +23,10 @@ const initializeAuthState = (defaultState) => {
if (!persistedState) {
return defaultState;
}

/**
* TODO: Validate loginId to be a valid uuid using the uuid
* library from npm
*/
if (!validateUserID(persistedState.loginId, persistedState.loginType)) {
Dun-sin marked this conversation as resolved.
Show resolved Hide resolved
// User is trying to hack app by manipulating localStorage
throw new Error('Invalid loginId! :(');
}
if (!persistedState.loginId && persistedState.isLoggedIn === true) {
// User is trying to hack app by manipulating localStorage
throw new Error('Gotcha! :D');
Expand Down
13 changes: 13 additions & 0 deletions client/src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,16 @@ export const decrypt = (encryptedText) => {

return originalText;
};

export const validateUserID = (userID, userType) => {
const userIDPattern = /^[a-z0-9]{12}$/;
const userHexIdPattern = /^[a-f0-9]{24}$/;

if (userType === 'email') {
// user id validation for hex pattern of email login
return userHexIdPattern.test(userID);
} else {
// user id validation for anonymous login
return userIDPattern.test(userID);
}
};
Dun-sin marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const storage = multer.memoryStorage();
const imageUpload = multer({ storage: storage });

const User = require('../models/UserModel');
const { emailValidator, generateObjectId } = require('../utils/helper');
const { emailValidator, generateObjectId, validateUserID } = require('../utils/helper');

const { isUserBlocked, blockUser } = require('../utils/lib.js');
const {
Expand Down Expand Up @@ -169,7 +169,7 @@ const blockUserHandler = async (req, res) => {
}
}

UserRouter.route('/login').post(emailValidator, loginUser);
UserRouter.route('/login').post(emailValidator, validateUserID, loginUser);
UserRouter.route('/profile').post(
imageUpload.single('profileImage'),
emailValidator,
Expand Down
17 changes: 17 additions & 0 deletions server/utils/helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const crypto = require('crypto');
const { NOT_ACCEPTABLE } = require('../httpStatusCodes');

// Defining separate email validation middleware
const validator = require('validator').default;
Expand All @@ -18,7 +19,23 @@ function generateObjectId() {
return crypto.randomBytes(12).toString('hex');
}

const validateUserID = (req, res, next) => {
const {id} = req.body;
const userIDPattern = /^[a-z0-9]{12}$/;

if(id !== undefined && (typeof id !== 'string' || !userIDPattern.test(id))){
//if id of type string is coming it'll be via the anonymous validation
return res.status(NOT_ACCEPTABLE).json({
message: 'Invalid login Id.'
});
}else{
// if id is not there or the id is valid move ahead
next();
}
}

Dun-sin marked this conversation as resolved.
Show resolved Hide resolved
module.exports = {
emailValidator,
generateObjectId,
validateUserID,
};
Loading