@contrast/assess 1.6.0 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/dataflow/propagation/index.js +3 -0
- package/lib/dataflow/propagation/install/JSON/index.js +33 -0
- package/lib/dataflow/propagation/install/JSON/stringify.js +290 -0
- package/lib/dataflow/propagation/install/buffer.js +79 -0
- package/lib/dataflow/propagation/install/contrast-methods/string.js +5 -1
- package/lib/dataflow/propagation/install/encode-uri-component.js +5 -2
- package/lib/dataflow/propagation/install/pug-runtime-escape.js +5 -2
- package/lib/dataflow/propagation/install/sequelize.js +310 -0
- package/lib/dataflow/propagation/install/sql-template-strings.js +5 -4
- package/lib/dataflow/propagation/install/string/match.js +2 -2
- package/lib/dataflow/propagation/install/string/replace.js +9 -4
- package/lib/dataflow/sinks/common.js +10 -1
- package/lib/dataflow/sinks/index.js +30 -1
- package/lib/dataflow/sinks/install/express/index.js +29 -0
- package/lib/dataflow/sinks/install/express/unvalidated-redirect.js +134 -0
- package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.js +96 -69
- package/lib/dataflow/sinks/install/http.js +20 -5
- package/lib/dataflow/sinks/install/koa/unvalidated-redirect.js +33 -9
- package/lib/dataflow/sinks/install/mongodb.js +297 -82
- package/lib/dataflow/sinks/install/mssql.js +9 -4
- package/lib/dataflow/sinks/install/mysql.js +20 -4
- package/lib/dataflow/sinks/install/postgres.js +25 -12
- package/lib/dataflow/sinks/install/sequelize.js +142 -0
- package/lib/dataflow/sinks/install/sqlite3.js +9 -4
- package/lib/dataflow/sources/handler.js +144 -26
- package/lib/dataflow/sources/index.js +6 -8
- package/lib/dataflow/sources/install/body-parser1.js +133 -0
- package/lib/dataflow/sources/install/cookie-parser1.js +101 -0
- package/lib/dataflow/sources/install/express/index.js +31 -0
- package/lib/dataflow/sources/install/express/params.js +81 -0
- package/lib/dataflow/sources/install/express/parsedUrl.js +87 -0
- package/lib/dataflow/sources/install/http.js +32 -18
- package/lib/dataflow/sources/install/querystring.js +75 -0
- package/lib/dataflow/tag-utils.js +68 -1
- package/package.json +3 -3
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2022 Contrast Security, Inc
|
|
3
|
+
* Contact: support@contrastsecurity.com
|
|
4
|
+
* License: Commercial
|
|
5
|
+
|
|
6
|
+
* NOTICE: This Software and the patented inventions embodied within may only be
|
|
7
|
+
* used as part of Contrast Security’s commercial offerings. Even though it is
|
|
8
|
+
* made available through public repositories, use of this Software is subject to
|
|
9
|
+
* the applicable End User Licensing Agreement found at
|
|
10
|
+
* https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
|
|
11
|
+
* between Contrast Security and the End User. The Software may not be reverse
|
|
12
|
+
* engineered, modified, repackaged, sold, redistributed or otherwise used in a
|
|
13
|
+
* way not consistent with the End User License Agreement.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
'use strict';
|
|
17
|
+
|
|
18
|
+
const {
|
|
19
|
+
isString,
|
|
20
|
+
DataflowTag: { SQL_ENCODED },
|
|
21
|
+
} = require('@contrast/common');
|
|
22
|
+
const { patchType, createModuleLabel } = require('../common');
|
|
23
|
+
|
|
24
|
+
module.exports = function (core) {
|
|
25
|
+
const {
|
|
26
|
+
scopes: { sources, instrumentation },
|
|
27
|
+
patcher,
|
|
28
|
+
depHooks,
|
|
29
|
+
assess: {
|
|
30
|
+
dataflow: {
|
|
31
|
+
tracker,
|
|
32
|
+
eventFactory: { createPropagationEvent },
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
} = core;
|
|
36
|
+
|
|
37
|
+
function getFormatPostions(str) {
|
|
38
|
+
const positions = [];
|
|
39
|
+
let index = -1;
|
|
40
|
+
|
|
41
|
+
while ((index = str.indexOf('?', index + 1)) !== -1) {
|
|
42
|
+
positions.push(index);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return positions;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function getFormatNamedParametersPositions(str) {
|
|
49
|
+
const regex = /:(\w+)(?=[\s,;)]|$)/g;
|
|
50
|
+
const matches = str.matchAll(regex);
|
|
51
|
+
|
|
52
|
+
return Array.from(matches, (match) => ({ [match[1]]: match.index }));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return (core.assess.dataflow.propagation.sequelizeInstrumentation = {
|
|
56
|
+
install() {
|
|
57
|
+
depHooks.resolve(
|
|
58
|
+
{ name: 'sequelize', file: 'lib/sql-string.js' },
|
|
59
|
+
(sqlString, version) => {
|
|
60
|
+
const origEscape = sqlString.escape;
|
|
61
|
+
|
|
62
|
+
patcher.patch(sqlString, 'escape', {
|
|
63
|
+
name: 'sequelize.escape',
|
|
64
|
+
patchType,
|
|
65
|
+
post(data) {
|
|
66
|
+
const { args, result, hooked, orig } = data;
|
|
67
|
+
|
|
68
|
+
if (
|
|
69
|
+
!result ||
|
|
70
|
+
!args[0] ||
|
|
71
|
+
!isString(args[0]) ||
|
|
72
|
+
!sources.getStore()?.assess ||
|
|
73
|
+
instrumentation.isLocked()
|
|
74
|
+
) return;
|
|
75
|
+
|
|
76
|
+
const argInfo = tracker.getData(args[0]);
|
|
77
|
+
|
|
78
|
+
if (!argInfo) return;
|
|
79
|
+
|
|
80
|
+
const resultInfo = tracker.getData(result);
|
|
81
|
+
if (!resultInfo) return;
|
|
82
|
+
const history = [argInfo];
|
|
83
|
+
const newTags = { ...resultInfo.tags };
|
|
84
|
+
|
|
85
|
+
newTags[SQL_ENCODED] = [0, result.length - 1];
|
|
86
|
+
|
|
87
|
+
const event = createPropagationEvent({
|
|
88
|
+
context: 'sequelize.escape',
|
|
89
|
+
name: 'sequelize/lib/sql-string.escape',
|
|
90
|
+
object: {
|
|
91
|
+
value: createModuleLabel('sequelize/lib/sql-string.escape', version),
|
|
92
|
+
tracked: false,
|
|
93
|
+
},
|
|
94
|
+
result: {
|
|
95
|
+
value: resultInfo.value,
|
|
96
|
+
tracked: true,
|
|
97
|
+
},
|
|
98
|
+
args: [{ value: argInfo.value, tracked: true }],
|
|
99
|
+
tags: newTags,
|
|
100
|
+
addedTags: [SQL_ENCODED],
|
|
101
|
+
source: 'P',
|
|
102
|
+
target: 'R',
|
|
103
|
+
history,
|
|
104
|
+
stacktraceOpts: {
|
|
105
|
+
constructorOpt: hooked,
|
|
106
|
+
prependFrames: [orig],
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
if (!event) return;
|
|
111
|
+
|
|
112
|
+
Object.assign(resultInfo, event);
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
patcher.patch(sqlString, 'format', {
|
|
117
|
+
name: 'Sequelize.Utils.format',
|
|
118
|
+
patchType,
|
|
119
|
+
post(data) {
|
|
120
|
+
const { args, result, hooked, orig } = data;
|
|
121
|
+
if (
|
|
122
|
+
!result ||
|
|
123
|
+
!args[0] ||
|
|
124
|
+
!isString(args[0]) ||
|
|
125
|
+
!sources.getStore()?.assess ||
|
|
126
|
+
instrumentation.isLocked()
|
|
127
|
+
)
|
|
128
|
+
return;
|
|
129
|
+
|
|
130
|
+
const resultInfo = tracker.getData(result);
|
|
131
|
+
if (!resultInfo) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const positions = getFormatPostions(data.args[0]);
|
|
136
|
+
const firstArgInfo = tracker.getData(data.args[0]);
|
|
137
|
+
|
|
138
|
+
if (!positions.length) {
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const replacements = [].concat(data.args[1]);
|
|
143
|
+
let replacementsTrakced = false;
|
|
144
|
+
const [, , timezone, dialect] = data.args;
|
|
145
|
+
const len = positions.length;
|
|
146
|
+
|
|
147
|
+
const history = new Set();
|
|
148
|
+
const newTags = { ...resultInfo.tags };
|
|
149
|
+
|
|
150
|
+
for (let i = 0; i < len; i++) {
|
|
151
|
+
const replacementInfo = tracker.getData(replacements[i]);
|
|
152
|
+
// we don't need to run in a no instrumentation scope here since
|
|
153
|
+
// patcher will do so automatically since we're in a post hook
|
|
154
|
+
const escapedVal = origEscape(
|
|
155
|
+
replacements[i],
|
|
156
|
+
timezone,
|
|
157
|
+
dialect,
|
|
158
|
+
true // true is required to get format function behavior.
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
if (replacementInfo) {
|
|
162
|
+
history.add(replacementInfo);
|
|
163
|
+
newTags[SQL_ENCODED] = newTags[SQL_ENCODED] || [];
|
|
164
|
+
replacementsTrakced = replacementsTrakced || true;
|
|
165
|
+
newTags[SQL_ENCODED].push(positions[i], positions[i] + escapedVal.length - 1);
|
|
166
|
+
}
|
|
167
|
+
//update the string replacement poisitions based on current val length - it's replacing a ? in the origional string
|
|
168
|
+
if (i < len - 1) {
|
|
169
|
+
for (let j = i + 1; j < len; j++) {
|
|
170
|
+
positions[j] = positions[j] + escapedVal.length - 1;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
const event = createPropagationEvent({
|
|
176
|
+
context: 'Sequelize.Utils.format',
|
|
177
|
+
name: 'sequelize/lib/sql-string.format',
|
|
178
|
+
object: {
|
|
179
|
+
value: createModuleLabel('sequelize/lib/sql-string.format', version),
|
|
180
|
+
tracked: false,
|
|
181
|
+
},
|
|
182
|
+
result: {
|
|
183
|
+
value: resultInfo.value,
|
|
184
|
+
tracked: true,
|
|
185
|
+
},
|
|
186
|
+
args: [
|
|
187
|
+
{ value: firstArgInfo ? firstArgInfo.value : args[0], tracked: !!firstArgInfo },
|
|
188
|
+
{ value: replacements, tracked: replacementsTrakced },
|
|
189
|
+
{ value: timezone, tracked: false },
|
|
190
|
+
{ value: dialect, tracked: false }
|
|
191
|
+
],
|
|
192
|
+
tags: newTags,
|
|
193
|
+
source: 'P1',
|
|
194
|
+
target: 'R',
|
|
195
|
+
addedTags: [SQL_ENCODED],
|
|
196
|
+
history: Array.from(history),
|
|
197
|
+
stacktraceOpts: {
|
|
198
|
+
constructorOpt: hooked,
|
|
199
|
+
prependFrames: [orig],
|
|
200
|
+
},
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
if (!event) return;
|
|
204
|
+
|
|
205
|
+
Object.assign(resultInfo, event);
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
patcher.patch(sqlString, 'formatNamedParameters', {
|
|
210
|
+
name: 'Sequelize.Utils.formatNamedParameters',
|
|
211
|
+
patchType,
|
|
212
|
+
post(data) {
|
|
213
|
+
const { args, result, hooked, orig } = data;
|
|
214
|
+
|
|
215
|
+
if (
|
|
216
|
+
!result ||
|
|
217
|
+
!args[0] ||
|
|
218
|
+
!isString(args[0]) ||
|
|
219
|
+
!sources.getStore()?.assess ||
|
|
220
|
+
instrumentation.isLocked()
|
|
221
|
+
)
|
|
222
|
+
return;
|
|
223
|
+
|
|
224
|
+
const resultInfo = tracker.getData(result);
|
|
225
|
+
if (!resultInfo) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const positions = getFormatNamedParametersPositions(data.args[0]);
|
|
230
|
+
const firstArgInfo = tracker.getData(data.args[0]);
|
|
231
|
+
|
|
232
|
+
if (!positions.length) {
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const replacements = Object.assign({}, data.args[1]);
|
|
237
|
+
let replacementsTrakced = false;
|
|
238
|
+
const [, , timezone, dialect] = data.args;
|
|
239
|
+
const len = positions.length;
|
|
240
|
+
|
|
241
|
+
const history = new Set();
|
|
242
|
+
const newTags = { ...resultInfo.tags };
|
|
243
|
+
|
|
244
|
+
for (let i = 0; i < len; i++) {
|
|
245
|
+
const replacementKey = Object.keys(positions[i])[0];
|
|
246
|
+
const replacementInfo = tracker.getData(replacements[replacementKey]);
|
|
247
|
+
const escapedVal = origEscape(
|
|
248
|
+
replacements[replacementKey],
|
|
249
|
+
timezone,
|
|
250
|
+
dialect,
|
|
251
|
+
true // true is required to get format function behavior.
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
if (replacementInfo) {
|
|
255
|
+
history.add(replacementInfo);
|
|
256
|
+
newTags[SQL_ENCODED] = newTags[SQL_ENCODED] || [];
|
|
257
|
+
replacementsTrakced = replacementsTrakced || true;
|
|
258
|
+
newTags[SQL_ENCODED].push(Object.values(positions[i])[0], Object.values(positions[i])[0] + escapedVal.length - 1);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
//update the string replacement poisitions based on current val length - it's replacing a :key with the string
|
|
262
|
+
if (i < len - 1) {
|
|
263
|
+
for (let j = i + 1; j < len; j++) {
|
|
264
|
+
const nextKey = Object.keys(positions[j])[0];
|
|
265
|
+
positions[j] = {
|
|
266
|
+
[nextKey]:
|
|
267
|
+
positions[j][nextKey] - (replacementKey.length + 1) + escapedVal.length // add 1 for the ':'
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const event = createPropagationEvent({
|
|
274
|
+
context: 'Sequelize.Utils.formatNamedParameters',
|
|
275
|
+
name: 'sequelize/lib/sql-string.formatNamedParameters',
|
|
276
|
+
object: {
|
|
277
|
+
value: createModuleLabel('sequelize/lib/sql-string.formatNamedParameters', version),
|
|
278
|
+
tracked: false,
|
|
279
|
+
},
|
|
280
|
+
result: {
|
|
281
|
+
value: resultInfo.value,
|
|
282
|
+
tracked: true,
|
|
283
|
+
},
|
|
284
|
+
args: [
|
|
285
|
+
{ value: firstArgInfo ? firstArgInfo.value : args[0], tracked: !!firstArgInfo },
|
|
286
|
+
{ value: replacements, tracked: replacementsTrakced },
|
|
287
|
+
{ value: timezone, tracked: false },
|
|
288
|
+
{ value: dialect, tracked: false }
|
|
289
|
+
],
|
|
290
|
+
tags: newTags,
|
|
291
|
+
source: 'P1',
|
|
292
|
+
target: 'R',
|
|
293
|
+
addedTags: [SQL_ENCODED],
|
|
294
|
+
history: Array.from(history),
|
|
295
|
+
stacktraceOpts: {
|
|
296
|
+
constructorOpt: hooked,
|
|
297
|
+
prependFrames: [orig],
|
|
298
|
+
},
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
if (!event) return;
|
|
302
|
+
|
|
303
|
+
Object.assign(resultInfo, event);
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
);
|
|
308
|
+
},
|
|
309
|
+
});
|
|
310
|
+
};
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
|
+
const {
|
|
19
|
+
DataflowTag: { SQL_ENCODED }
|
|
20
|
+
} = require('@contrast/common');
|
|
18
21
|
const { patchType, createModuleLabel } = require('../common');
|
|
19
22
|
|
|
20
23
|
module.exports = function(core) {
|
|
@@ -48,10 +51,8 @@ module.exports = function(core) {
|
|
|
48
51
|
const resultInfo = tracker.getData(resultValue);
|
|
49
52
|
if (!resultInfo) return;
|
|
50
53
|
const history = [argInfo];
|
|
51
|
-
// const newTags = createFullLengthCopyTags(argInfo.tags, resultValue.length);
|
|
52
54
|
const newTags = { ...resultInfo.tags };
|
|
53
|
-
|
|
54
|
-
newTags['sql-encoded'] = [0, resultValue.length - 1];
|
|
55
|
+
newTags[SQL_ENCODED] = [0, resultValue.length - 1];
|
|
55
56
|
|
|
56
57
|
const event = createPropagationEvent({
|
|
57
58
|
name: 'sql-template-strings.SQL',
|
|
@@ -65,7 +66,7 @@ module.exports = function(core) {
|
|
|
65
66
|
},
|
|
66
67
|
args: [{ value: argInfo.value, tracked: true }],
|
|
67
68
|
tags: newTags,
|
|
68
|
-
addedTags: [
|
|
69
|
+
addedTags: [SQL_ENCODED],
|
|
69
70
|
history,
|
|
70
71
|
stacktraceOpts: {
|
|
71
72
|
constructorOpt: hooked,
|
|
@@ -87,7 +87,7 @@ module.exports = function(core) {
|
|
|
87
87
|
const hasCaptureGroups = 'groups' in result;
|
|
88
88
|
for (let i = 0; i < result.length; i++) {
|
|
89
89
|
const res = result[i];
|
|
90
|
-
if (!res) continue;
|
|
90
|
+
if (!res || res === obj) continue;
|
|
91
91
|
const start = obj.indexOf(res, idx);
|
|
92
92
|
idx += hasCaptureGroups ? 0 : res.length;
|
|
93
93
|
const event = getPropagationEvent(data, res, objInfo, start);
|
|
@@ -101,7 +101,7 @@ module.exports = function(core) {
|
|
|
101
101
|
if (hasCaptureGroups && result.groups) {
|
|
102
102
|
Object.keys(result.groups).forEach((key) => {
|
|
103
103
|
const res = result.groups[key];
|
|
104
|
-
if (!res) return;
|
|
104
|
+
if (!res || res === obj) return;
|
|
105
105
|
const start = obj.indexOf(res);
|
|
106
106
|
const event = getPropagationEvent(data, res, objInfo, start);
|
|
107
107
|
if (event) {
|
|
@@ -42,7 +42,7 @@ module.exports = function(core) {
|
|
|
42
42
|
const namedGroups = hasNamedGroup ? lastEl : null;
|
|
43
43
|
return { match, captureGroups, matchIdx, str, namedGroups };
|
|
44
44
|
}
|
|
45
|
-
function replaceSpecialCharacters(replacement, { captureGroups, match, str, namedGroups }) {
|
|
45
|
+
function replaceSpecialCharacters(replacement, { captureGroups, match, str, namedGroups }, replacementType) {
|
|
46
46
|
const origReplace = patcher.unwrap(String.prototype.replace);
|
|
47
47
|
let ret = replacement;
|
|
48
48
|
[
|
|
@@ -74,7 +74,7 @@ module.exports = function(core) {
|
|
|
74
74
|
}
|
|
75
75
|
});
|
|
76
76
|
|
|
77
|
-
const numberedGroupMatches = replacement.match(/\$[1-9][0-9]|\$[1-9]/g);
|
|
77
|
+
const numberedGroupMatches = replacementType !== 'function' && replacement.match(/\$[1-9][0-9]|\$[1-9]/g);
|
|
78
78
|
if (numberedGroupMatches) {
|
|
79
79
|
numberedGroupMatches.forEach((numberedGroup) => {
|
|
80
80
|
const group = Number(numberedGroup.substring(1));
|
|
@@ -95,7 +95,7 @@ module.exports = function(core) {
|
|
|
95
95
|
data._replacement.call(global, ...replacerArgs) :
|
|
96
96
|
data._replacement;
|
|
97
97
|
|
|
98
|
-
replacement = replaceSpecialCharacters(String(replacement), parsedArgs);
|
|
98
|
+
replacement = replaceSpecialCharacters(String(replacement), parsedArgs, data._replacementType);
|
|
99
99
|
|
|
100
100
|
data._replacementInfo = tracker.getData(replacement);
|
|
101
101
|
if (data._replacement) {
|
|
@@ -152,9 +152,14 @@ module.exports = function(core) {
|
|
|
152
152
|
instrumentation.isLocked() ||
|
|
153
153
|
!data.result ||
|
|
154
154
|
// todo: can we reuse this optimization in other propagators? e.g those performing substring-like operations
|
|
155
|
-
!data._accumTags?.[UNTRUSTED]
|
|
155
|
+
!data._accumTags?.[UNTRUSTED] ||
|
|
156
|
+
!!tracker.getData(data.result)
|
|
156
157
|
) return;
|
|
157
158
|
|
|
159
|
+
if (data.obj === data.result) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
158
163
|
const { _replacementInfo, obj, args, result, hooked, orig } = data;
|
|
159
164
|
|
|
160
165
|
const event = createPropagationEvent({
|
|
@@ -16,5 +16,14 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
module.exports = {
|
|
19
|
-
patchType: 'assess-dataflow-sink'
|
|
19
|
+
patchType: 'assess-dataflow-sink',
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {string[]} safeTags array of sink's safe tags
|
|
23
|
+
* @param {object} strInfo tracked string info
|
|
24
|
+
* @returns {string[]} filtered list of safe tags found on tracked string data
|
|
25
|
+
*/
|
|
26
|
+
filterSafeTags(safeTags, strInfo) {
|
|
27
|
+
return safeTags.filter((t) => !!strInfo.tags?.[t]);
|
|
28
|
+
},
|
|
20
29
|
};
|
|
@@ -15,19 +15,33 @@
|
|
|
15
15
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
|
-
const {
|
|
18
|
+
const { AsyncLocalStorage } = require('async_hooks');
|
|
19
|
+
const { callChildComponentMethodsSync, Event, Rule } = require('@contrast/common');
|
|
19
20
|
const { isVulnerable } = require('../utils/is-vulnerable');
|
|
20
21
|
const { isSafeContentType } = require('../utils/is-safe-content-type');
|
|
21
22
|
|
|
22
23
|
module.exports = function (core) {
|
|
23
24
|
const {
|
|
25
|
+
logger,
|
|
24
26
|
messages,
|
|
25
27
|
scopes: { sources }
|
|
26
28
|
} = core;
|
|
27
29
|
|
|
30
|
+
const sinkScopes = {
|
|
31
|
+
[Rule.SQL_INJECTION]: new AsyncLocalStorage(),
|
|
32
|
+
[Rule.NOSQL_INJECTION_MONGO]: new AsyncLocalStorage()
|
|
33
|
+
};
|
|
28
34
|
const sinks = core.assess.dataflow.sinks = {
|
|
29
35
|
isVulnerable,
|
|
30
36
|
isSafeContentType,
|
|
37
|
+
reportSafePositive(data) {
|
|
38
|
+
const store = sources.getStore();
|
|
39
|
+
// these events need source correlation
|
|
40
|
+
messages.emit(Event.ASSESS_DATAFLOW_SAFE_POSITIVE, {
|
|
41
|
+
sourceInfo: store?.sourceInfo,
|
|
42
|
+
...data
|
|
43
|
+
});
|
|
44
|
+
},
|
|
31
45
|
reportFindings(data) {
|
|
32
46
|
const store = sources.getStore();
|
|
33
47
|
// these events need source correlation
|
|
@@ -36,6 +50,19 @@ module.exports = function (core) {
|
|
|
36
50
|
...data
|
|
37
51
|
});
|
|
38
52
|
},
|
|
53
|
+
runInActiveSink(rule, cb) {
|
|
54
|
+
if (sinkScopes[rule]) {
|
|
55
|
+
return sinkScopes[rule].run({ locked: true }, cb);
|
|
56
|
+
} else {
|
|
57
|
+
logger.error('Sink scope for %s rule does not exist', rule);
|
|
58
|
+
return cb();
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
isLocked(rule) {
|
|
62
|
+
if (!sinkScopes[rule]) return false;
|
|
63
|
+
|
|
64
|
+
return sinkScopes[rule].getStore()?.locked;
|
|
65
|
+
}
|
|
39
66
|
};
|
|
40
67
|
|
|
41
68
|
require('./install/fastify')(core);
|
|
@@ -47,8 +74,10 @@ module.exports = function (core) {
|
|
|
47
74
|
require('./install/mssql')(core);
|
|
48
75
|
require('./install/mysql')(core);
|
|
49
76
|
require('./install/postgres')(core);
|
|
77
|
+
require('./install/sequelize')(core);
|
|
50
78
|
require('./install/sqlite3')(core);
|
|
51
79
|
require('./install/marsdb')(core);
|
|
80
|
+
require('./install/express')(core);
|
|
52
81
|
|
|
53
82
|
sinks.install = function() {
|
|
54
83
|
callChildComponentMethodsSync(core.assess.dataflow.sinks, 'install');
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2022 Contrast Security, Inc
|
|
3
|
+
* Contact: support@contrastsecurity.com
|
|
4
|
+
* License: Commercial
|
|
5
|
+
|
|
6
|
+
* NOTICE: This Software and the patented inventions embodied within may only be
|
|
7
|
+
* used as part of Contrast Security’s commercial offerings. Even though it is
|
|
8
|
+
* made available through public repositories, use of this Software is subject to
|
|
9
|
+
* the applicable End User Licensing Agreement found at
|
|
10
|
+
* https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
|
|
11
|
+
* between Contrast Security and the End User. The Software may not be reverse
|
|
12
|
+
* engineered, modified, repackaged, sold, redistributed or otherwise used in a
|
|
13
|
+
* way not consistent with the End User License Agreement.
|
|
14
|
+
*/
|
|
15
|
+
'use strict';
|
|
16
|
+
|
|
17
|
+
const { callChildComponentMethodsSync } = require('@contrast/common');
|
|
18
|
+
|
|
19
|
+
module.exports = function(core) {
|
|
20
|
+
const express = core.assess.dataflow.sinks.express = {};
|
|
21
|
+
|
|
22
|
+
require('./unvalidated-redirect')(core);
|
|
23
|
+
|
|
24
|
+
express.install = function() {
|
|
25
|
+
callChildComponentMethodsSync(express, 'install');
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return express;
|
|
29
|
+
};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2022 Contrast Security, Inc
|
|
3
|
+
* Contact: support@contrastsecurity.com
|
|
4
|
+
* License: Commercial
|
|
5
|
+
|
|
6
|
+
* NOTICE: This Software and the patented inventions embodied within may only be
|
|
7
|
+
* used as part of Contrast Security’s commercial offerings. Even though it is
|
|
8
|
+
* made available through public repositories, use of this Software is subject to
|
|
9
|
+
* the applicable End User Licensing Agreement found at
|
|
10
|
+
* https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
|
|
11
|
+
* between Contrast Security and the End User. The Software may not be reverse
|
|
12
|
+
* engineered, modified, repackaged, sold, redistributed or otherwise used in a
|
|
13
|
+
* way not consistent with the End User License Agreement.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
'use strict';
|
|
17
|
+
|
|
18
|
+
const util = require('util');
|
|
19
|
+
const {
|
|
20
|
+
DataflowTag: {
|
|
21
|
+
UNTRUSTED,
|
|
22
|
+
CUSTOM_ENCODED,
|
|
23
|
+
CUSTOM_VALIDATED,
|
|
24
|
+
HTML_ENCODED,
|
|
25
|
+
LIMITED_CHARS,
|
|
26
|
+
URL_ENCODED,
|
|
27
|
+
},
|
|
28
|
+
isString
|
|
29
|
+
} = require('@contrast/common');
|
|
30
|
+
const { patchType, filterSafeTags } = require('../../common');
|
|
31
|
+
const { createSubsetTags } = require('../../../tag-utils');
|
|
32
|
+
|
|
33
|
+
const ruleId = 'unvalidated-redirect';
|
|
34
|
+
|
|
35
|
+
module.exports = function (core) {
|
|
36
|
+
const {
|
|
37
|
+
depHooks,
|
|
38
|
+
patcher,
|
|
39
|
+
config,
|
|
40
|
+
scopes: { sources },
|
|
41
|
+
assess: {
|
|
42
|
+
dataflow: {
|
|
43
|
+
tracker,
|
|
44
|
+
sinks: { isVulnerable, reportFindings, reportSafePositive },
|
|
45
|
+
eventFactory: { createSinkEvent },
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
} = core;
|
|
49
|
+
const unvalidatedRedirect =
|
|
50
|
+
(core.assess.dataflow.sinks.express.unvalidatedRedirect = {});
|
|
51
|
+
|
|
52
|
+
const inspect = patcher.unwrap(util.inspect);
|
|
53
|
+
|
|
54
|
+
const safeTags = [
|
|
55
|
+
CUSTOM_ENCODED,
|
|
56
|
+
CUSTOM_VALIDATED,
|
|
57
|
+
HTML_ENCODED,
|
|
58
|
+
LIMITED_CHARS,
|
|
59
|
+
URL_ENCODED,
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
unvalidatedRedirect.install = function () {
|
|
63
|
+
depHooks.resolve({ name: 'express', file: 'lib/response' }, (Response) => {
|
|
64
|
+
const name = 'Express.Response.location';
|
|
65
|
+
patcher.patch(Response, 'location', {
|
|
66
|
+
name: 'Express.Response.location',
|
|
67
|
+
patchType,
|
|
68
|
+
pre: (data) => {
|
|
69
|
+
const assessStore = sources.getStore()?.assess;
|
|
70
|
+
if (!assessStore) return;
|
|
71
|
+
|
|
72
|
+
let [url] = data.args;
|
|
73
|
+
if (url === 'back') {
|
|
74
|
+
url = data.obj.req.get('Referrer');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!url || !isString(url)) return;
|
|
78
|
+
|
|
79
|
+
const strInfo = tracker.getData(url);
|
|
80
|
+
if (!strInfo) return;
|
|
81
|
+
|
|
82
|
+
let urlPathTags = strInfo.tags;
|
|
83
|
+
if (url.indexOf('?') > -1) {
|
|
84
|
+
urlPathTags = createSubsetTags(strInfo.tags, 0, url.indexOf('?'));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (urlPathTags && isVulnerable(UNTRUSTED, safeTags, urlPathTags)) {
|
|
88
|
+
const event = createSinkEvent({
|
|
89
|
+
args: [{
|
|
90
|
+
tracked: true,
|
|
91
|
+
value: strInfo.value,
|
|
92
|
+
}],
|
|
93
|
+
context: `response.location(${inspect(strInfo.value)})`,
|
|
94
|
+
history: [strInfo],
|
|
95
|
+
name: 'Express.Response.location',
|
|
96
|
+
object: {
|
|
97
|
+
tracked: false,
|
|
98
|
+
value: 'Express.Response',
|
|
99
|
+
},
|
|
100
|
+
result: {
|
|
101
|
+
tracked: false,
|
|
102
|
+
value: undefined,
|
|
103
|
+
},
|
|
104
|
+
tags: urlPathTags,
|
|
105
|
+
source: 'P0',
|
|
106
|
+
stacktraceOpts: {
|
|
107
|
+
constructorOpt: data.hooked,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
if (event) {
|
|
112
|
+
reportFindings({
|
|
113
|
+
ruleId,
|
|
114
|
+
sinkEvent: event,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
} else if (config.assess.safe_positives.enable) {
|
|
118
|
+
reportSafePositive({
|
|
119
|
+
name,
|
|
120
|
+
ruleId,
|
|
121
|
+
safeTags: filterSafeTags(safeTags, strInfo),
|
|
122
|
+
strInfo: {
|
|
123
|
+
tags: strInfo.tags,
|
|
124
|
+
value: strInfo.value,
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
return unvalidatedRedirect;
|
|
134
|
+
};
|