-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcontributions.js
80 lines (65 loc) · 2.42 KB
/
contributions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const ContributionsDAO = require("../data/contributions-dao").ContributionsDAO;
const {
environmentalScripts
} = require("../../config/config");
/* The ContributionsHandler must be constructed with a connected db */
function ContributionsHandler(db) {
"use strict";
const contributionsDAO = new ContributionsDAO(db);
this.displayContributions = (req, res, next) => {
const {
userId
} = req.session;
contributionsDAO.getByUserId(userId, (error, contrib) => {
if (error) return next(error);
contrib.userId = userId; //set for nav menu items
return res.render("contributions", {
...contrib,
environmentalScripts
});
});
};
this.handleContributionsUpdate = (req, res, next) => {
/*jslint evil: true */
// Insecure use of eval() to parse inputs
const preTax = eval(req.body.preTax);
const afterTax = eval(req.body.afterTax);
const roth = eval(req.body.roth);
/*
//Fix for A1 -1 SSJS Injection attacks - uses alternate method to eval
const preTax = parseInt(req.body.preTax);
const afterTax = parseInt(req.body.afterTax);
const roth = parseInt(req.body.roth);
*/
const {
userId
} = req.session;
//validate contributions
const validations = [isNaN(preTax), isNaN(afterTax), isNaN(roth), preTax < 0, afterTax < 0, roth < 0]
const isInvalid = validations.some(validation => validation)
if (isInvalid) {
return res.render("contributions", {
updateError: "Invalid contribution percentages",
userId,
environmentalScripts
});
}
// Prevent more than 30% contributions
if (preTax + afterTax + roth > 30) {
return res.render("contributions", {
updateError: "Contribution percentages cannot exceed 30 %",
userId,
environmentalScripts
});
}
contributionsDAO.update(userId, preTax, afterTax, roth, (err, contributions) => {
if (err) return next(err);
contributions.updateSuccess = true;
return res.render("contributions", {
...contributions,
environmentalScripts
});
});
};
}
module.exports = ContributionsHandler;