-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
128 lines (112 loc) · 3.03 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
'use strict';
const TEST_REGEX = /^\$|\./;
const TEST_REGEX_WITHOUT_DOT = /^\$/;
const REPLACE_REGEX = /^\$|\./g;
function isPlainObject(obj) {
return typeof obj === 'object' && obj !== null;
}
function getTestRegex(allowDots) {
return allowDots ? TEST_REGEX_WITHOUT_DOT : TEST_REGEX;
}
function withEach(target, cb) {
(function act(obj) {
if (Array.isArray(obj)) {
obj.forEach(act);
} else if (isPlainObject(obj)) {
Object.keys(obj).forEach(function (key) {
const val = obj[key];
const resp = cb(obj, val, key);
if (resp.shouldRecurse) {
act(obj[resp.key || key]);
}
});
}
})(target);
}
function has(target, allowDots) {
const regex = getTestRegex(allowDots);
let hasProhibited = false;
withEach(target, function (obj, val, key) {
if (regex.test(key)) {
hasProhibited = true;
return { shouldRecurse: false };
} else {
return { shouldRecurse: true };
}
});
return hasProhibited;
}
function _sanitize(target, options) {
const regex = getTestRegex(options.allowDots);
let isSanitized = false;
let replaceWith = null;
const dryRun = Boolean(options.dryRun);
if (!regex.test(options.replaceWith) && options.replaceWith !== '.') {
replaceWith = options.replaceWith;
}
withEach(target, function (obj, val, key) {
let shouldRecurse = true;
if (regex.test(key)) {
isSanitized = true;
// if dryRun is enabled, do not modify the target
if (dryRun) {
return {
shouldRecurse: shouldRecurse,
key: key,
};
}
delete obj[key];
if (replaceWith) {
key = key.replace(REPLACE_REGEX, replaceWith);
// Avoid to set __proto__ and constructor.prototype
// https://portswigger.net/daily-swig/prototype-pollution-the-dangerous-and-underrated-vulnerability-impacting-javascript-applications
// https://snyk.io/vuln/SNYK-JS-LODASH-73638
if (
key !== '__proto__' &&
key !== 'constructor' &&
key !== 'prototype'
) {
obj[key] = val;
}
} else {
shouldRecurse = false;
}
}
return {
shouldRecurse: shouldRecurse,
key: key,
};
});
return {
isSanitized,
target,
};
}
function sanitize(target, options = {}) {
return _sanitize(target, options).target;
}
/**
* @param {{replaceWith?: string, onSanitize?: function, dryRun?: boolean}} options
* @returns {function}
*/
function middleware(options = {}) {
const hasOnSanitize = typeof options.onSanitize === 'function';
return function (req, res, next) {
['body', 'params', 'headers', 'query'].forEach(function (key) {
if (req[key]) {
const { target, isSanitized } = _sanitize(req[key], options);
req[key] = target;
if (isSanitized && hasOnSanitize) {
options.onSanitize({
req,
key,
});
}
}
});
next();
};
}
module.exports = middleware;
module.exports.sanitize = sanitize;
module.exports.has = has;