Skip to content

Commit 7492748

Browse files
authored
Merge pull request #100 from 100NikhilBro/enhancement/sanitize-html-57
Enhancement: Security Improvement Suggestion for HTML inputs #57
2 parents a8ea7a4 + 609b49f commit 7492748

File tree

4 files changed

+215
-6
lines changed

4 files changed

+215
-6
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const sanitizeHtml = require('sanitize-html');
2+
3+
const sanitize = (obj) => {
4+
if (obj && typeof obj === 'object') {
5+
for (const k of Object.keys(obj)) {
6+
if (typeof obj[k] === 'string') {
7+
obj[k] = sanitizeHtml(obj[k], {
8+
allowedTags: [],
9+
allowedAttributes: {},
10+
});
11+
} else if (obj[k] !== null && typeof obj[k] === 'object') {
12+
sanitize(obj[k]);
13+
}
14+
}
15+
}
16+
};
17+
18+
exports.sanitizeMiddleware = () => {
19+
return (req, res, next) => {
20+
if (req.body) {
21+
sanitize(req.body);
22+
}
23+
24+
if (req.params) {
25+
sanitize(req.params);
26+
}
27+
28+
if (req.query) {
29+
sanitize(req.query);
30+
}
31+
32+
next();
33+
};
34+
};

backend/package-lock.json

Lines changed: 171 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"mongoose": "^8.18.1",
2727
"multer": "^2.0.2",
2828
"node-cron": "^4.2.1",
29-
"papaparse": "^5.5.3"
29+
"papaparse": "^5.5.3",
30+
"sanitize-html": "^2.17.0"
3031
},
3132
"devDependencies": {
3233
"jest": "^29.7.0",

backend/server.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ const axios = require('axios');
77
const cron = require('node-cron');
88
require('./cron');
99

10+
// import the sanitizeMiddleware
11+
const { sanitizeMiddleware } = require("./middleware/sanitizeMiddleware")
12+
1013
// Load environment variables
1114
dotenv.config();
1215

@@ -21,7 +24,7 @@ const allowedOrigins = [
2124
];
2225

2326
app.use(cors({
24-
origin: function (origin, callback) {
27+
origin: function(origin, callback) {
2528
if (!origin || allowedOrigins.includes(origin)) {
2629
callback(null, true);
2730
} else {
@@ -32,6 +35,9 @@ app.use(cors({
3235
}));
3336
app.use(express.json());
3437

38+
// sanitizeMiddleware
39+
app.use(sanitizeMiddleware());
40+
3541
// Routes
3642
app.use('/api/auth', require('./routes/authRoutes'));
3743
app.use('/api/transactions', require('./routes/transactionRoutes'));
@@ -51,7 +57,7 @@ const PORT = process.env.PORT || 5000;
5157

5258
const server = app.listen(PORT, () => console.log(`Server started on port ${PORT}`));
5359

54-
cron.schedule("*/10 * * * *", async () => {
60+
cron.schedule("*/10 * * * *", async() => {
5561
const keepAliveUrl = process.env.KEEP_ALIVE_URL;
5662
if (!keepAliveUrl) {
5763
console.error(

0 commit comments

Comments
 (0)