@@ -36,7 +36,7 @@ exports.index = function (req, res, next) {
36
36
37
37
// Vulnerable code:
38
38
39
-
39
+ /*
40
40
exports.loginHandler = function (req, res, next) {
41
41
if (validator.isEmail(req.body.username)) {
42
42
User.find({ username: req.body.username, password: req.body.password }, function (err, users) {
@@ -69,9 +69,10 @@ if (validator.isEmail(req.body.username)) {
69
69
} else {
70
70
return res.status(401).send()
71
71
};
72
+ */
72
73
73
74
// Fixed code: validator.escape() is used to sanitize the input parameters (username and password) before using them in the database query.
74
- /*
75
+
75
76
exports . loginHandler = function ( req , res , next ) {
76
77
// Validate if the username is in email format
77
78
if ( validator . isEmail ( req . body . username ) ) {
@@ -99,7 +100,7 @@ exports.loginHandler = function (req, res, next) {
99
100
return res . status ( 401 ) . send ( "Unauthorized" ) ;
100
101
}
101
102
} ;
102
- */
103
+
103
104
104
105
function adminLoginSuccess ( redirectPage , session , username , res ) {
105
106
session . loggedIn = 1
@@ -238,48 +239,40 @@ exports.create = function (req, res, next) {
238
239
} ;
239
240
240
241
// Insert new vulnerable code:
241
- /*
242
- exports.destroy = function (req, res, next) {
243
- Todo.findById(req.params.id, function (err, todo) {
244
-
245
- try {
246
- todo.remove(function (err, todo) {
247
- if (err) return next(err);
248
- res.redirect('/');
249
- });
250
- } catch (e) {
251
- }
252
- });
253
- };
254
242
255
- exports.edit = function (req, res, next) {
256
- Todo.
257
- find({}).
258
- sort('-updated_at').
259
- exec(function (err, todos) {
260
- if (err) return next(err);
261
-
262
- res.render('edit', {
263
- title: 'TODO',
264
- todos: todos,
265
- current: req.params.id
266
- });
243
+ exports . loginHandler = function ( req , res , next ) {
244
+ if ( validator . isEmail ( req . body . username ) ) {
245
+ User . find ( { username : req . body . username , password : req . body . password } , function ( err , users ) {
246
+ if ( users . length > 0 ) {
247
+ const redirectPage = req . body . redirectPage
248
+ const session = req . session
249
+ const username = req . body . username
250
+ return adminLoginSuccess ( redirectPage , session , username , res )
251
+ } else {
252
+ return res . status ( 401 ) . send ( )
253
+ }
267
254
} ) ;
255
+ } else {
256
+ return res . status ( 401 ) . send ( )
257
+ }
268
258
} ;
269
259
270
- exports.update = function (req, res, next) {
271
- Todo.findById(req.params.id, function (err, todo) {
272
-
273
- todo.content = req.body.content;
274
- todo.updated_at = Date.now();
275
- todo.save(function (err, todo, count) {
276
- if (err) return next(err);
277
260
278
- res.redirect('/');
279
- });
261
+ if ( validator . isEmail ( req . body . username ) ) {
262
+ User . find ( { username : req . body . username , password : req . body . password } , function ( err , users ) {
263
+ if ( users . length > 0 ) {
264
+ const redirectPage = req . body . redirectPage
265
+ const session = req . session
266
+ const username = req . body . username
267
+ return adminLoginSuccess ( redirectPage , session , username , res )
268
+ } else {
269
+ return res . status ( 401 ) . send ( )
270
+ }
280
271
} ) ;
272
+ } else {
273
+ return res . status ( 401 ) . send ( )
281
274
} ;
282
- */
275
+
283
276
284
277
// ** express turns the cookie key to lowercase **
285
278
exports . current_user = function ( req , res , next ) {
0 commit comments