-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
38 lines (35 loc) · 1.02 KB
/
index.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
const sanitize = require("./modules/");
function middleware(
options = {},
whiteList = [],
only = ["body", "params", "headers", "query"],
dynamicRoutes = false
) {
return (req, res, next) => {
only.forEach((k) => {
if (req[k] && !whiteList.some((v) => {
if (dynamicRoutes && v.includes(':')) {
let check = false;
const vArr = v.replace(/^\/+/, '').replace(/\/$/, '').split('/');
const urlArr = req.url.replace(/^\/+/, '').replace(/\/$/, '').split('/');
if (vArr.length <= urlArr.length)
for (let i = 0; i < vArr.length; i++) {
if (vArr[i].includes(':'))
check = true;
else if (vArr[i] == urlArr[i]) check = true;
else { check = false; break; }
};
return check
}
else return req.url.trim().startsWith(v)
})) {
req[k] = sanitize.prepareSanitize(req[k], options);
}
});
next();
};
}
module.exports = {
clean: middleware,
sanitize,
};