@contrast/assess 1.18.0 → 1.19.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/constants.js +26 -0
- package/lib/crypto-analysis/common.js +20 -0
- package/lib/crypto-analysis/index.js +44 -0
- package/lib/crypto-analysis/install/crypto.js +151 -0
- package/lib/crypto-analysis/install/math.js +99 -0
- package/lib/dataflow/propagation/install/JSON/parse.js +12 -11
- package/lib/dataflow/propagation/install/JSON/stringify.js +1 -1
- package/lib/dataflow/propagation/install/ejs/escape-xml.js +2 -2
- package/lib/dataflow/propagation/install/ejs/index.js +1 -0
- package/lib/dataflow/propagation/install/ejs/template.js +77 -0
- package/lib/dataflow/propagation/install/util-format.js +9 -3
- package/lib/dataflow/sinks/install/child-process.js +20 -14
- package/lib/dataflow/sinks/install/eval.js +16 -14
- package/lib/dataflow/sinks/install/express/unvalidated-redirect.js +14 -8
- package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.js +12 -5
- package/lib/dataflow/sinks/install/fs.js +7 -7
- package/lib/dataflow/sinks/install/function.js +8 -12
- package/lib/dataflow/sinks/install/http/request.js +16 -8
- package/lib/dataflow/sinks/install/http/server-response.js +11 -2
- package/lib/dataflow/sinks/install/koa/unvalidated-redirect.js +15 -8
- package/lib/dataflow/sinks/install/libxmljs.js +15 -10
- package/lib/dataflow/sinks/install/marsdb.js +13 -8
- package/lib/dataflow/sinks/install/mongodb.js +25 -15
- package/lib/dataflow/sinks/install/mssql.js +20 -9
- package/lib/dataflow/sinks/install/mysql.js +15 -8
- package/lib/dataflow/sinks/install/node-serialize.js +15 -17
- package/lib/dataflow/sinks/install/postgres.js +17 -4
- package/lib/dataflow/sinks/install/sequelize.js +16 -9
- package/lib/dataflow/sinks/install/sqlite3.js +20 -7
- package/lib/dataflow/sinks/install/vm.js +19 -17
- package/lib/dataflow/sources/install/http.js +14 -42
- package/lib/dataflow/sources/install/koa/index.js +1 -0
- package/lib/dataflow/sources/install/koa/koa-multer.js +102 -0
- package/lib/dataflow/sources/install/multer1.js +25 -51
- package/lib/dataflow/sources/install/querystring.js +1 -4
- package/lib/event-factory.js +47 -0
- package/lib/get-policy.js +68 -0
- package/lib/get-source-context.js +62 -0
- package/lib/index.d.ts +50 -0
- package/lib/index.js +20 -19
- package/lib/make-source-context.js +74 -0
- package/lib/response-scanning/handlers/index.js +55 -28
- package/lib/response-scanning/install/http.js +13 -7
- package/lib/rule-scopes.js +48 -0
- package/lib/session-configuration/handlers.js +4 -3
- package/lib/session-configuration/install/express-session.js +8 -2
- package/package.json +2 -2
package/lib/constants.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2023 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 InstrumentationType = {
|
|
19
|
+
SOURCE: 'source',
|
|
20
|
+
PROPAGATOR: 'propagator',
|
|
21
|
+
RULE: 'rule',
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
module.exports = {
|
|
25
|
+
InstrumentationType,
|
|
26
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2023 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
|
+
module.exports = {
|
|
19
|
+
PATCH_TYPE: 'assess-cryptographic-analysis',
|
|
20
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2023 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
|
+
Event,
|
|
20
|
+
callChildComponentMethodsSync,
|
|
21
|
+
} = require('@contrast/common');
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {{
|
|
25
|
+
* assess: import('@contrast/assess').Assess,
|
|
26
|
+
* messages: import('@contrast/common').Messages,
|
|
27
|
+
* }} core
|
|
28
|
+
*/
|
|
29
|
+
module.exports = function(core) {
|
|
30
|
+
const cryptoAnalysis = core.assess.cryptoAnalysis = {
|
|
31
|
+
installers: {},
|
|
32
|
+
install() {
|
|
33
|
+
callChildComponentMethodsSync(cryptoAnalysis.installers, 'install');
|
|
34
|
+
},
|
|
35
|
+
report(event) {
|
|
36
|
+
core.messages.emit(Event.ASSESS_CRYPTO_ANALYSIS_FINDING, event);
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
require('./install/crypto')(core);
|
|
41
|
+
require('./install/math')(core);
|
|
42
|
+
|
|
43
|
+
return cryptoAnalysis;
|
|
44
|
+
};
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2023 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 { inspect } = require('util');
|
|
19
|
+
const {
|
|
20
|
+
Rule,
|
|
21
|
+
isString,
|
|
22
|
+
toLowerCase,
|
|
23
|
+
} = require('@contrast/common');
|
|
24
|
+
const { InstrumentationType: { RULE } } = require('../../constants');
|
|
25
|
+
const { PATCH_TYPE: patchType } = require('../common');
|
|
26
|
+
|
|
27
|
+
const SAFE_HASH_ALGORITHMS = new Set([
|
|
28
|
+
'RSA-SHA1-2',
|
|
29
|
+
'RSA-SHA224',
|
|
30
|
+
'RSA-SHA256',
|
|
31
|
+
'RSA-SHA384',
|
|
32
|
+
'RSA-SHA512',
|
|
33
|
+
'sha224',
|
|
34
|
+
'sha224WithRSAEncryption',
|
|
35
|
+
'sha256',
|
|
36
|
+
'sha256WithRSAEncryption',
|
|
37
|
+
'sha384',
|
|
38
|
+
'sha384WithRSAEncryption',
|
|
39
|
+
'sha512'
|
|
40
|
+
]);
|
|
41
|
+
const SAFE_HASH_LIBS = ['etag', 'browserify', 'deps-sort'];
|
|
42
|
+
const SAFE_CIPHER_ALGORITHM_PREFIXES = ['des-ede', 'id-aes', 'aes', 'rsa'];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @param {{
|
|
46
|
+
* config: import('@contrast/config').Config,
|
|
47
|
+
* assess: import('@contrast/assess').Assess,
|
|
48
|
+
* }} core
|
|
49
|
+
*/
|
|
50
|
+
module.exports = function(core) {
|
|
51
|
+
const {
|
|
52
|
+
config,
|
|
53
|
+
depHooks,
|
|
54
|
+
logger,
|
|
55
|
+
patcher,
|
|
56
|
+
assess: {
|
|
57
|
+
eventFactory,
|
|
58
|
+
cryptoAnalysis,
|
|
59
|
+
getSourceContext,
|
|
60
|
+
},
|
|
61
|
+
} = core;
|
|
62
|
+
|
|
63
|
+
const installer = core.assess.cryptoAnalysis.installers.crypto = {
|
|
64
|
+
install() {
|
|
65
|
+
depHooks.resolve({ name: 'crypto' }, (_export) => {
|
|
66
|
+
patcher.patch(_export, 'createHash', {
|
|
67
|
+
name: 'crypto.createHash',
|
|
68
|
+
patchType,
|
|
69
|
+
post(data) {
|
|
70
|
+
const [alg] = data.args;
|
|
71
|
+
|
|
72
|
+
if (
|
|
73
|
+
!isString(alg) ||
|
|
74
|
+
!getSourceContext(RULE, Rule.CRYPTO_BAD_MAC) ||
|
|
75
|
+
SAFE_HASH_ALGORITHMS.has(alg)
|
|
76
|
+
) return;
|
|
77
|
+
|
|
78
|
+
const event = eventFactory.createCryptoAnalysisEvent({
|
|
79
|
+
args: [{ tracked: false, value: alg }],
|
|
80
|
+
context: `crypto.createHash(${inspect(alg)})`,
|
|
81
|
+
methodName: 'createHash',
|
|
82
|
+
moduleName: 'crypto',
|
|
83
|
+
name: 'crypto.createHash',
|
|
84
|
+
object: { tracked: false, value: 'crypto' },
|
|
85
|
+
result: { tracked: false, value: 'Hash' },
|
|
86
|
+
source: 'P0',
|
|
87
|
+
stacktraceOpts: {
|
|
88
|
+
constructorOpt: data.hooked,
|
|
89
|
+
},
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
if (!event) return;
|
|
93
|
+
|
|
94
|
+
// check stacktrace for trusted libraries
|
|
95
|
+
if (config.getEffectiveValue('assess.stacktraces') === 'NONE') {
|
|
96
|
+
const { stack } = new Error(event.stacktraceOpts);
|
|
97
|
+
for (const lib of SAFE_HASH_LIBS) {
|
|
98
|
+
if (stack.indexOf(lib) >= 0) return;
|
|
99
|
+
}
|
|
100
|
+
} else {
|
|
101
|
+
for (const { file } of event.stack) {
|
|
102
|
+
for (const lib of SAFE_HASH_LIBS) {
|
|
103
|
+
logger.trace(`skipping reporting for ${Rule.CRYPTO_BAD_MAC} - trusting ${lib}`);
|
|
104
|
+
if (file.indexOf(lib) >= 0) return;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
cryptoAnalysis.report({ finding: event, ruleId: Rule.CRYPTO_BAD_MAC });
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
for (const method of ['createCipher', 'createCipheriv']) {
|
|
114
|
+
patcher.patch(_export, method, {
|
|
115
|
+
name: `crypto.${method}`,
|
|
116
|
+
patchType,
|
|
117
|
+
post(data) {
|
|
118
|
+
const [alg] = data.args;
|
|
119
|
+
if (!isString(alg) || !getSourceContext(RULE, Rule.CRYPTO_BAD_CIPHERS)) return;
|
|
120
|
+
|
|
121
|
+
const algLower = toLowerCase(alg);
|
|
122
|
+
for (const prefix of SAFE_CIPHER_ALGORITHM_PREFIXES) {
|
|
123
|
+
if (algLower.indexOf(prefix) === 0) return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const event = eventFactory.createCryptoAnalysisEvent({
|
|
127
|
+
args: [{ tracked: false, value: alg }],
|
|
128
|
+
context: `crypto.${method}(${inspect(alg)})`,
|
|
129
|
+
methodName: method,
|
|
130
|
+
moduleName: 'crypto',
|
|
131
|
+
name: `crypto.${method}`,
|
|
132
|
+
object: { tracked: false, value: 'crypto' },
|
|
133
|
+
result: { tracked: false, value: 'Cipher' },
|
|
134
|
+
source: 'P0',
|
|
135
|
+
stacktraceOpts: {
|
|
136
|
+
constructorOpt: data.hooked,
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
if (!event) return;
|
|
141
|
+
|
|
142
|
+
cryptoAnalysis.report({ finding: event, ruleId: Rule.CRYPTO_BAD_CIPHERS });
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
return installer;
|
|
151
|
+
};
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2023 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 { Rule } = require('@contrast/common');
|
|
19
|
+
const { InstrumentationType } = require('../../constants');
|
|
20
|
+
const { PATCH_TYPE: patchType } = require('../common');
|
|
21
|
+
|
|
22
|
+
const { CRYPTO_WEAK_RANDOMNESS: ruleId } = Rule;
|
|
23
|
+
const { RULE } = InstrumentationType;
|
|
24
|
+
|
|
25
|
+
const SAFE_RANDOM_LIBS = [
|
|
26
|
+
'node_modules/node-uuid/',
|
|
27
|
+
'browserify'
|
|
28
|
+
];
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {{
|
|
32
|
+
* config: import('@contrast/config').Config,
|
|
33
|
+
* assess: import('@contrast/assess').Assess,
|
|
34
|
+
* }} core
|
|
35
|
+
*/
|
|
36
|
+
module.exports = function(core) {
|
|
37
|
+
const {
|
|
38
|
+
assess: {
|
|
39
|
+
eventFactory,
|
|
40
|
+
cryptoAnalysis,
|
|
41
|
+
getSourceContext,
|
|
42
|
+
},
|
|
43
|
+
config,
|
|
44
|
+
logger,
|
|
45
|
+
patcher,
|
|
46
|
+
} = core;
|
|
47
|
+
|
|
48
|
+
core.assess.cryptoAnalysis.installers.math = {
|
|
49
|
+
install() {
|
|
50
|
+
patcher.patch(Math, 'random', {
|
|
51
|
+
name: 'global.Math.random',
|
|
52
|
+
patchType,
|
|
53
|
+
post(data) {
|
|
54
|
+
if (!getSourceContext(RULE, ruleId)) return;
|
|
55
|
+
|
|
56
|
+
const event = eventFactory.createCryptoAnalysisEvent({
|
|
57
|
+
args: [],
|
|
58
|
+
context: 'Math.random()',
|
|
59
|
+
methodName: 'random',
|
|
60
|
+
moduleName: 'global.Math',
|
|
61
|
+
name: 'global.Math.random',
|
|
62
|
+
object: { tracked: false, value: 'global.Math' },
|
|
63
|
+
result: { tracked: false, value: `${data.result}` },
|
|
64
|
+
source: 'A',
|
|
65
|
+
stacktraceOpts: {
|
|
66
|
+
constructorOpt: data.hooked,
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
if (!event) return;
|
|
71
|
+
|
|
72
|
+
// check stacktrace for trusted libraries
|
|
73
|
+
if (config.getEffectiveValue('assess.stacktraces') === 'NONE') {
|
|
74
|
+
const { stack } = new Error(event.stacktraceOpts);
|
|
75
|
+
for (const lib of SAFE_RANDOM_LIBS) {
|
|
76
|
+
if (stack.indexOf(lib) >= 0) return;
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
for (const { file } of event.stack) {
|
|
80
|
+
for (const lib of SAFE_RANDOM_LIBS) {
|
|
81
|
+
if (file.indexOf(lib) >= 0) {
|
|
82
|
+
logger.trace(`skipping reporting for ${ruleId} - trusting ${lib}`);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
cryptoAnalysis.report({ finding: event, ruleId });
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
uninstall() {
|
|
94
|
+
Math.random = patcher.unwrap(Math.random);
|
|
95
|
+
},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return core.assess.cryptoAnalysis.installers.math;
|
|
99
|
+
};
|
|
@@ -59,8 +59,18 @@ module.exports = function (core) {
|
|
|
59
59
|
|
|
60
60
|
function createEvent(data, value, newTags, reviver, strInfo) {
|
|
61
61
|
const method = 'JSON.parse';
|
|
62
|
+
const eventArgs = [
|
|
63
|
+
{
|
|
64
|
+
value: data.args[0],
|
|
65
|
+
tracked: true,
|
|
66
|
+
},
|
|
67
|
+
reviver && {
|
|
68
|
+
value: reviver.toString(),
|
|
69
|
+
tracked: false,
|
|
70
|
+
}
|
|
71
|
+
].filter(Boolean);
|
|
62
72
|
return createPropagationEvent({
|
|
63
|
-
context: method
|
|
73
|
+
context: `${method}(${eventArgs.map((arg) => `'${arg.value}'`)})`,
|
|
64
74
|
name: method,
|
|
65
75
|
history: [strInfo],
|
|
66
76
|
moduleName: 'JSON',
|
|
@@ -69,16 +79,7 @@ module.exports = function (core) {
|
|
|
69
79
|
value: inspect(data.obj),
|
|
70
80
|
tracked: false,
|
|
71
81
|
},
|
|
72
|
-
args:
|
|
73
|
-
{
|
|
74
|
-
value: data.args[0],
|
|
75
|
-
tracked: true,
|
|
76
|
-
},
|
|
77
|
-
reviver && {
|
|
78
|
-
value: reviver.toString(),
|
|
79
|
-
tracked: false,
|
|
80
|
-
},
|
|
81
|
-
].filter(Boolean),
|
|
82
|
+
args: eventArgs,
|
|
82
83
|
result: {
|
|
83
84
|
value,
|
|
84
85
|
tracked: true,
|
|
@@ -21,7 +21,7 @@ const {
|
|
|
21
21
|
const {
|
|
22
22
|
createFullLengthCopyTags
|
|
23
23
|
} = require('../../../tag-utils');
|
|
24
|
-
const { patchType
|
|
24
|
+
const { patchType } = require('../../common');
|
|
25
25
|
|
|
26
26
|
module.exports = function(core) {
|
|
27
27
|
const {
|
|
@@ -61,7 +61,7 @@ module.exports = function(core) {
|
|
|
61
61
|
moduleName: 'ejs',
|
|
62
62
|
methodName: 'escapeXML',
|
|
63
63
|
object: {
|
|
64
|
-
value:
|
|
64
|
+
value: 'ejs.utils',
|
|
65
65
|
tracked: false
|
|
66
66
|
},
|
|
67
67
|
result: {
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2023 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 { EOL } = require('os');
|
|
19
|
+
const { join } = require('@contrast/common');
|
|
20
|
+
const { patchType } = require('../../common');
|
|
21
|
+
const { InstrumentationType: { PROPAGATOR } } = require('../../../../constants');
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {{
|
|
25
|
+
* assess: import('@contrast/assess').Assess,
|
|
26
|
+
* logger: import('@contrast/logger').Logger,
|
|
27
|
+
* rewriter: import('@contrast/rewriter').Rewriter,
|
|
28
|
+
* }} core
|
|
29
|
+
* @returns {import('@contrast/common').Installable}
|
|
30
|
+
*/
|
|
31
|
+
module.exports = function(core) {
|
|
32
|
+
const {
|
|
33
|
+
assess: { getSourceContext },
|
|
34
|
+
depHooks,
|
|
35
|
+
logger,
|
|
36
|
+
patcher,
|
|
37
|
+
rewriter,
|
|
38
|
+
} = core;
|
|
39
|
+
|
|
40
|
+
const REWRITE_OPTS = { inject: false, wrap: false };
|
|
41
|
+
const WRAPPER_PREFIX = join([
|
|
42
|
+
'function tempWrapper() {',
|
|
43
|
+
'function __append(s) { if (s !== undefined && s !== null) __output += s }'
|
|
44
|
+
], EOL);
|
|
45
|
+
const WRAPPER_SUFFIX = join([
|
|
46
|
+
EOL,
|
|
47
|
+
'return __output;',
|
|
48
|
+
'}',
|
|
49
|
+
], EOL);
|
|
50
|
+
|
|
51
|
+
return core.assess.dataflow.propagation.ejsInstrumentation.template = {
|
|
52
|
+
/**
|
|
53
|
+
* inject our own rewritten version of __append():
|
|
54
|
+
* function __append() { // unrewritten }
|
|
55
|
+
* function __append() { // rewritten }
|
|
56
|
+
*/
|
|
57
|
+
install() {
|
|
58
|
+
depHooks.resolve({ name: 'ejs', version: '>=2.6.2' }, (_export, version) => {
|
|
59
|
+
const name = 'ejs.Template.prototype.generateSource';
|
|
60
|
+
patcher.patch(_export.Template.prototype, 'generateSource', {
|
|
61
|
+
name,
|
|
62
|
+
patchType,
|
|
63
|
+
post(data) {
|
|
64
|
+
if (!getSourceContext(PROPAGATOR)) return;
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const { code } = rewriter.rewrite(`${WRAPPER_PREFIX}${data.obj.source}${WRAPPER_SUFFIX}`, REWRITE_OPTS);
|
|
68
|
+
data.obj.source = code.substring(code.indexOf('{') + 1, code.lastIndexOf('}'));
|
|
69
|
+
} catch (err) {
|
|
70
|
+
logger.error({ err, source: data.obj.source }, 'error occurred while rewriting ejs source');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
};
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
const { patchType } = require('../common');
|
|
19
|
-
const {
|
|
19
|
+
const { isString } = require('@contrast/common');
|
|
20
20
|
const { createAppendTags } = require('../../tag-utils');
|
|
21
21
|
|
|
22
22
|
module.exports = function(core) {
|
|
@@ -47,7 +47,13 @@ module.exports = function(core) {
|
|
|
47
47
|
const history = [];
|
|
48
48
|
const eventArgs = [];
|
|
49
49
|
const formatChars = args[0].includes('%') ? args[0].match(/[^%]+/g).map((x) => x[0]) : [];
|
|
50
|
-
let i =
|
|
50
|
+
let i = 0;
|
|
51
|
+
|
|
52
|
+
if (formatChars.length > 0) {
|
|
53
|
+
i = 1;
|
|
54
|
+
eventArgs.push({ value: args[0], tracked: false });
|
|
55
|
+
}
|
|
56
|
+
|
|
51
57
|
for (i; i < args.length; i++) {
|
|
52
58
|
let arg = args[i];
|
|
53
59
|
const formatChar = formatChars[i - 1];
|
|
@@ -80,7 +86,7 @@ module.exports = function(core) {
|
|
|
80
86
|
name,
|
|
81
87
|
moduleName: 'util',
|
|
82
88
|
methodName: 'format',
|
|
83
|
-
context: `util.format(${
|
|
89
|
+
context: `util.format(${eventArgs.map((arg) => `'${arg.value}'`)})`,
|
|
84
90
|
object: {
|
|
85
91
|
value: 'util',
|
|
86
92
|
tracked: false
|
|
@@ -17,19 +17,25 @@
|
|
|
17
17
|
const {
|
|
18
18
|
DataflowTag: { UNTRUSTED },
|
|
19
19
|
join,
|
|
20
|
-
Rule,
|
|
20
|
+
Rule: { CMD_INJECTION: ruleId },
|
|
21
21
|
isString,
|
|
22
22
|
inspect,
|
|
23
23
|
} = require('@contrast/common');
|
|
24
|
-
|
|
24
|
+
const { InstrumentationType: { RULE } } = require('../../../constants');
|
|
25
25
|
const { patchType } = require('../common');
|
|
26
26
|
|
|
27
|
+
/**
|
|
28
|
+
* @param {{
|
|
29
|
+
* assess: import('@contrast/assess').Assess,
|
|
30
|
+
* }} core
|
|
31
|
+
* @returns {import('@contrast/common').Installable}
|
|
32
|
+
*/
|
|
27
33
|
module.exports = function(core) {
|
|
28
34
|
const {
|
|
29
35
|
depHooks,
|
|
30
36
|
patcher,
|
|
31
|
-
scopes: { sources },
|
|
32
37
|
assess: {
|
|
38
|
+
getSourceContext,
|
|
33
39
|
eventFactory: { createSinkEvent },
|
|
34
40
|
dataflow: {
|
|
35
41
|
tracker,
|
|
@@ -92,7 +98,7 @@ module.exports = function(core) {
|
|
|
92
98
|
|
|
93
99
|
if (event) {
|
|
94
100
|
reportFindings({
|
|
95
|
-
ruleId
|
|
101
|
+
ruleId,
|
|
96
102
|
sinkEvent: event,
|
|
97
103
|
});
|
|
98
104
|
|
|
@@ -178,7 +184,7 @@ module.exports = function(core) {
|
|
|
178
184
|
|
|
179
185
|
if (event) {
|
|
180
186
|
reportFindings({
|
|
181
|
-
ruleId
|
|
187
|
+
ruleId,
|
|
182
188
|
sinkEvent: event,
|
|
183
189
|
});
|
|
184
190
|
}
|
|
@@ -193,10 +199,10 @@ module.exports = function(core) {
|
|
|
193
199
|
name,
|
|
194
200
|
patchType,
|
|
195
201
|
pre(data) {
|
|
196
|
-
|
|
197
|
-
const [command] = data.args;
|
|
202
|
+
if (!getSourceContext(RULE, ruleId)) return;
|
|
198
203
|
|
|
199
|
-
|
|
204
|
+
const [command] = data.args;
|
|
205
|
+
if (!command || !isString(command)) return;
|
|
200
206
|
|
|
201
207
|
const cpArgs = Array.isArray(data.args[1]) && data.args[1];
|
|
202
208
|
const options = cpArgs ? data.args[2] : data.args[1];
|
|
@@ -233,10 +239,10 @@ module.exports = function(core) {
|
|
|
233
239
|
name,
|
|
234
240
|
patchType,
|
|
235
241
|
pre(data) {
|
|
236
|
-
|
|
237
|
-
const [command, secondArg, thirdArg] = data.args;
|
|
242
|
+
if (!getSourceContext(RULE, ruleId)) return;
|
|
238
243
|
|
|
239
|
-
|
|
244
|
+
const [command, secondArg, thirdArg] = data.args;
|
|
245
|
+
if (!command || !isString(command)) return;
|
|
240
246
|
|
|
241
247
|
commandCheck(
|
|
242
248
|
name,
|
|
@@ -257,10 +263,10 @@ module.exports = function(core) {
|
|
|
257
263
|
name,
|
|
258
264
|
patchType,
|
|
259
265
|
pre(data) {
|
|
260
|
-
|
|
261
|
-
const [command] = data.args;
|
|
266
|
+
if (!getSourceContext(RULE, ruleId)) return;
|
|
262
267
|
|
|
263
|
-
|
|
268
|
+
const [command] = data.args;
|
|
269
|
+
if (!command || !isString(command)) return;
|
|
264
270
|
|
|
265
271
|
const cpArgs = Array.isArray(data.args[1]) && data.args[1];
|
|
266
272
|
const options = cpArgs ? data.args[2] : data.args[1];
|
|
@@ -25,8 +25,9 @@ const {
|
|
|
25
25
|
CUSTOM_VALIDATED,
|
|
26
26
|
LIMITED_CHARS,
|
|
27
27
|
},
|
|
28
|
-
Rule: { UNSAFE_CODE_EXECUTION },
|
|
28
|
+
Rule: { UNSAFE_CODE_EXECUTION: ruleId },
|
|
29
29
|
} = require('@contrast/common');
|
|
30
|
+
const { InstrumentationType: { RULE } } = require('../../../constants');
|
|
30
31
|
const { patchType, filterSafeTags } = require('../common');
|
|
31
32
|
|
|
32
33
|
const safeTags = [
|
|
@@ -37,13 +38,21 @@ const safeTags = [
|
|
|
37
38
|
LIMITED_CHARS,
|
|
38
39
|
];
|
|
39
40
|
|
|
41
|
+
/**
|
|
42
|
+
* @param {{
|
|
43
|
+
* assess: import('@contrast/assess').Assess,
|
|
44
|
+
* config: import('@contrast/config').Config,
|
|
45
|
+
* logger: import('@contrast/logger').Logger,
|
|
46
|
+
* }} core
|
|
47
|
+
* @returns {import('@contrast/common').Installable}
|
|
48
|
+
*/
|
|
40
49
|
module.exports = function (core) {
|
|
41
50
|
const {
|
|
42
51
|
config,
|
|
43
52
|
logger,
|
|
44
53
|
patcher,
|
|
45
|
-
scopes: { sources, instrumentation },
|
|
46
54
|
assess: {
|
|
55
|
+
getSourceContext,
|
|
47
56
|
eventFactory: { createSinkEvent },
|
|
48
57
|
dataflow: {
|
|
49
58
|
tracker,
|
|
@@ -63,19 +72,12 @@ module.exports = function (core) {
|
|
|
63
72
|
name: 'global.ContrastMethods.eval',
|
|
64
73
|
patchType,
|
|
65
74
|
pre({ args, orig }) {
|
|
66
|
-
|
|
75
|
+
if (!getSourceContext(RULE, ruleId)) return;
|
|
76
|
+
|
|
67
77
|
const script = args[0];
|
|
68
|
-
if (
|
|
69
|
-
!store ||
|
|
70
|
-
instrumentation.isLocked() ||
|
|
71
|
-
!script ||
|
|
72
|
-
!isString(script)
|
|
73
|
-
) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
78
|
+
if (!script || !isString(script)) return;
|
|
76
79
|
|
|
77
80
|
const strInfo = tracker.getData(script);
|
|
78
|
-
|
|
79
81
|
if (!strInfo) return;
|
|
80
82
|
|
|
81
83
|
const isArgVulnerable = isVulnerable(
|
|
@@ -93,7 +95,7 @@ module.exports = function (core) {
|
|
|
93
95
|
|
|
94
96
|
reportSafePositive({
|
|
95
97
|
name: 'eval',
|
|
96
|
-
ruleId
|
|
98
|
+
ruleId,
|
|
97
99
|
safeTags: foundSafeTags,
|
|
98
100
|
strInfo: safeStrInfo,
|
|
99
101
|
});
|
|
@@ -120,7 +122,7 @@ module.exports = function (core) {
|
|
|
120
122
|
|
|
121
123
|
if (event) {
|
|
122
124
|
reportFindings({
|
|
123
|
-
ruleId
|
|
125
|
+
ruleId,
|
|
124
126
|
sinkEvent: event,
|
|
125
127
|
});
|
|
126
128
|
}
|