@@ -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,10 +69,9 @@ if (validator.isEmail(req.body.username)) {
69
69
} else {
70
70
return res . status ( 401 ) . send ( )
71
71
} ;
72
- */
73
72
74
73
// Fixed code: validator.escape() is used to sanitize the input parameters (username and password) before using them in the database query.
75
-
74
+ /*
76
75
exports.loginHandler = function (req, res, next) {
77
76
// Validate if the username is in email format
78
77
if (validator.isEmail(req.body.username)) {
@@ -100,7 +99,7 @@ exports.loginHandler = function (req, res, next) {
100
99
return res.status(401).send("Unauthorized");
101
100
}
102
101
};
103
-
102
+ */
104
103
105
104
function adminLoginSuccess ( redirectPage , session , username , res ) {
106
105
session . loggedIn = 1
@@ -239,40 +238,48 @@ exports.create = function (req, res, next) {
239
238
} ;
240
239
241
240
// Insert new vulnerable code:
241
+ /*
242
+ exports.destroy = function (req, res, next) {
243
+ Todo.findById(req.params.id, function (err, todo) {
242
244
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
- }
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
+
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
+ });
254
267
});
255
- } else {
256
- return res . status ( 401 ) . send ( )
257
- }
258
268
};
259
269
270
+ exports.update = function (req, res, next) {
271
+ Todo.findById(req.params.id, function (err, todo) {
260
272
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
- }
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
+
278
+ res.redirect('/');
279
+ });
271
280
});
272
- } else {
273
- return res . status ( 401 ) . send ( )
274
281
};
275
-
282
+ */
276
283
277
284
// ** express turns the cookie key to lowercase **
278
285
exports . current_user = function ( req , res , next ) {
0 commit comments