@contrast/assess 1.18.0 → 1.20.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/LICENSE +1 -1
- 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 +156 -0
- package/lib/crypto-analysis/install/math.js +104 -0
- package/lib/dataflow/index.js +1 -1
- package/lib/dataflow/propagation/common.js +1 -1
- package/lib/dataflow/propagation/index.js +1 -1
- package/lib/dataflow/propagation/install/JSON/index.js +1 -1
- package/lib/dataflow/propagation/install/JSON/parse-fn.js +1 -1
- package/lib/dataflow/propagation/install/JSON/parse.js +15 -14
- package/lib/dataflow/propagation/install/JSON/stringify.js +2 -2
- package/lib/dataflow/propagation/install/array-prototype-join.js +1 -1
- package/lib/dataflow/propagation/install/buffer.js +1 -1
- package/lib/dataflow/propagation/install/contrast-methods/add.js +1 -1
- package/lib/dataflow/propagation/install/contrast-methods/index.js +1 -1
- package/lib/dataflow/propagation/install/contrast-methods/number.js +4 -3
- package/lib/dataflow/propagation/install/contrast-methods/string.js +1 -1
- package/lib/dataflow/propagation/install/contrast-methods/tag.js +1 -1
- package/lib/dataflow/propagation/install/decode-uri-component.js +1 -1
- package/lib/dataflow/propagation/install/ejs/escape-xml.js +3 -3
- package/lib/dataflow/propagation/install/ejs/index.js +2 -1
- package/lib/dataflow/propagation/install/ejs/template.js +79 -0
- package/lib/dataflow/propagation/install/encode-uri.js +1 -1
- package/lib/dataflow/propagation/install/escape-html.js +1 -1
- package/lib/dataflow/propagation/install/escape.js +1 -1
- package/lib/dataflow/propagation/install/handlebars-utils-escape-expression.js +1 -1
- package/lib/dataflow/propagation/install/isnumeric-0.js +3 -3
- package/lib/dataflow/propagation/install/joi/any.js +1 -1
- package/lib/dataflow/propagation/install/joi/boolean.js +1 -1
- package/lib/dataflow/propagation/install/joi/expression.js +1 -1
- package/lib/dataflow/propagation/install/joi/index.js +1 -1
- package/lib/dataflow/propagation/install/joi/keys.js +1 -1
- package/lib/dataflow/propagation/install/joi/number.js +1 -1
- package/lib/dataflow/propagation/install/joi/object.js +1 -1
- package/lib/dataflow/propagation/install/joi/string-schema.js +1 -1
- package/lib/dataflow/propagation/install/joi/utils.js +1 -1
- package/lib/dataflow/propagation/install/joi/values.js +1 -1
- package/lib/dataflow/propagation/install/mongoose/common.js +1 -1
- package/lib/dataflow/propagation/install/mongoose/index.js +1 -1
- package/lib/dataflow/propagation/install/mongoose/schema-map.js +1 -1
- package/lib/dataflow/propagation/install/mongoose/schema-mixed.js +1 -1
- package/lib/dataflow/propagation/install/mongoose/schema-string.js +1 -1
- package/lib/dataflow/propagation/install/mustache-escape.js +1 -1
- package/lib/dataflow/propagation/install/mysql-connection-escape.js +1 -1
- package/lib/dataflow/propagation/install/parse-int.js +3 -3
- package/lib/dataflow/propagation/install/path/basename.js +1 -1
- package/lib/dataflow/propagation/install/path/common.js +1 -1
- package/lib/dataflow/propagation/install/path/dirname.js +1 -1
- package/lib/dataflow/propagation/install/path/extname.js +1 -1
- package/lib/dataflow/propagation/install/path/format.js +1 -1
- package/lib/dataflow/propagation/install/path/index.js +1 -1
- package/lib/dataflow/propagation/install/path/join-and-resolve.js +1 -1
- package/lib/dataflow/propagation/install/path/normalize.js +1 -1
- package/lib/dataflow/propagation/install/path/parse.js +1 -1
- package/lib/dataflow/propagation/install/path/relative.js +1 -1
- package/lib/dataflow/propagation/install/path/toNamespacedPath.js +1 -1
- package/lib/dataflow/propagation/install/pug/index.js +3 -3
- package/lib/dataflow/propagation/install/pug-runtime-escape.js +1 -1
- package/lib/dataflow/propagation/install/querystring/escape.js +1 -1
- package/lib/dataflow/propagation/install/querystring/index.js +1 -1
- package/lib/dataflow/propagation/install/querystring/parse.js +1 -1
- package/lib/dataflow/propagation/install/querystring/stringify.js +1 -1
- package/lib/dataflow/propagation/install/reg-exp-prototype-exec.js +1 -1
- package/lib/dataflow/propagation/install/send.js +1 -1
- package/lib/dataflow/propagation/install/sequelize/index.js +1 -1
- package/lib/dataflow/propagation/install/sequelize/query-generator.js +1 -1
- package/lib/dataflow/propagation/install/sequelize/sql-string.js +1 -1
- package/lib/dataflow/propagation/install/sql-template-strings.js +1 -1
- package/lib/dataflow/propagation/install/string/concat.js +1 -1
- package/lib/dataflow/propagation/install/string/format-methods.js +1 -1
- package/lib/dataflow/propagation/install/string/html-methods.js +1 -1
- package/lib/dataflow/propagation/install/string/index.js +1 -1
- package/lib/dataflow/propagation/install/string/match-all.js +1 -1
- package/lib/dataflow/propagation/install/string/match.js +1 -1
- package/lib/dataflow/propagation/install/string/replace.js +1 -1
- package/lib/dataflow/propagation/install/string/slice.js +1 -1
- package/lib/dataflow/propagation/install/string/split.js +1 -1
- package/lib/dataflow/propagation/install/string/substring.js +1 -1
- package/lib/dataflow/propagation/install/string/trim.js +1 -1
- package/lib/dataflow/propagation/install/unescape.js +1 -1
- package/lib/dataflow/propagation/install/url/domain-parsers.js +1 -1
- package/lib/dataflow/propagation/install/url/index.js +1 -1
- package/lib/dataflow/propagation/install/url/parse.js +1 -1
- package/lib/dataflow/propagation/install/url/searchParams.js +1 -1
- package/lib/dataflow/propagation/install/url/url.js +1 -1
- package/lib/dataflow/propagation/install/util-format.js +10 -4
- package/lib/dataflow/propagation/install/validator/hooks.js +1 -1
- package/lib/dataflow/propagation/install/validator/index.js +1 -1
- package/lib/dataflow/propagation/install/validator/methods.js +1 -1
- package/lib/dataflow/sinks/common.js +1 -1
- package/lib/dataflow/sinks/index.js +1 -1
- package/lib/dataflow/sinks/install/child-process.js +21 -15
- package/lib/dataflow/sinks/install/eval.js +17 -15
- package/lib/dataflow/sinks/install/express/index.js +1 -1
- package/lib/dataflow/sinks/install/express/unvalidated-redirect.js +15 -9
- package/lib/dataflow/sinks/install/fastify/index.js +1 -1
- package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.js +13 -6
- package/lib/dataflow/sinks/install/fs.js +8 -8
- package/lib/dataflow/sinks/install/function.js +9 -13
- package/lib/dataflow/sinks/install/http/index.js +1 -1
- package/lib/dataflow/sinks/install/http/request.js +17 -9
- package/lib/dataflow/sinks/install/http/server-response.js +12 -3
- package/lib/dataflow/sinks/install/koa/index.js +1 -1
- package/lib/dataflow/sinks/install/koa/unvalidated-redirect.js +16 -9
- package/lib/dataflow/sinks/install/libxmljs.js +16 -11
- package/lib/dataflow/sinks/install/marsdb.js +17 -12
- package/lib/dataflow/sinks/install/mongodb.js +32 -22
- package/lib/dataflow/sinks/install/mssql.js +21 -10
- package/lib/dataflow/sinks/install/mysql.js +16 -9
- package/lib/dataflow/sinks/install/node-serialize.js +16 -18
- package/lib/dataflow/sinks/install/postgres.js +18 -5
- package/lib/dataflow/sinks/install/sequelize.js +23 -17
- package/lib/dataflow/sinks/install/sqlite3.js +21 -8
- package/lib/dataflow/sinks/install/vm.js +20 -18
- package/lib/dataflow/sources/common.js +1 -1
- package/lib/dataflow/sources/handler.js +11 -10
- package/lib/dataflow/sources/index.js +2 -2
- package/lib/dataflow/sources/install/body-parser1.js +11 -13
- package/lib/dataflow/sources/install/{busboy1.js → busboy.js} +15 -15
- package/lib/dataflow/sources/install/cookie-parser1.js +7 -6
- package/lib/dataflow/sources/install/express/index.js +1 -1
- package/lib/dataflow/sources/install/express/params.js +9 -10
- package/lib/dataflow/sources/install/express/parsedUrl.js +1 -1
- package/lib/dataflow/sources/install/fastify/fastify.js +6 -7
- package/lib/dataflow/sources/install/fastify/index.js +1 -1
- package/lib/dataflow/sources/install/formidable1.js +8 -6
- package/lib/dataflow/sources/install/http.js +17 -45
- package/lib/dataflow/sources/install/koa/index.js +2 -1
- package/lib/dataflow/sources/install/koa/koa-bodyparsers.js +10 -9
- package/lib/dataflow/sources/install/koa/koa-multer.js +102 -0
- package/lib/dataflow/sources/install/koa/koa-routers.js +6 -8
- package/lib/dataflow/sources/install/koa/koa2.js +42 -38
- package/lib/dataflow/sources/install/multer1.js +26 -52
- package/lib/dataflow/sources/install/qs6.js +7 -6
- package/lib/dataflow/sources/install/querystring.js +5 -8
- package/lib/dataflow/tag-utils.js +1 -1
- package/lib/dataflow/tracker.js +1 -1
- package/lib/dataflow/utils/is-safe-content-type.js +1 -1
- package/lib/dataflow/utils/is-vulnerable.js +1 -1
- package/lib/event-factory.js +75 -26
- package/lib/get-policy.js +68 -0
- package/lib/get-source-context.js +62 -0
- package/lib/index.d.ts +64 -0
- package/lib/index.js +21 -20
- package/lib/make-source-context.js +78 -0
- package/lib/response-scanning/handlers/index.js +56 -29
- package/lib/response-scanning/handlers/utils.js +1 -1
- package/lib/response-scanning/index.js +1 -1
- package/lib/response-scanning/install/http.js +14 -8
- package/lib/rule-scopes.js +48 -0
- package/lib/session-configuration/common.js +1 -1
- package/lib/session-configuration/handlers.js +66 -47
- package/lib/session-configuration/index.js +3 -1
- package/lib/session-configuration/install/express-session.js +23 -26
- package/lib/session-configuration/install/fastify-cookie.js +110 -0
- package/lib/session-configuration/install/hapi.js +8 -11
- package/lib/session-configuration/install/koa.js +101 -0
- package/package.json +2 -2
package/LICENSE
CHANGED
package/lib/constants.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2024 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: 2024 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: 2024 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,156 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2024 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(
|
|
104
|
+
{ funcKey: data.funcKey },
|
|
105
|
+
'skipping reporting for %s - trusting %s',
|
|
106
|
+
Rule.CRYPTO_BAD_MAC,
|
|
107
|
+
lib
|
|
108
|
+
);
|
|
109
|
+
if (file.indexOf(lib) >= 0) return;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
cryptoAnalysis.report({ finding: event, ruleId: Rule.CRYPTO_BAD_MAC });
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
for (const method of ['createCipher', 'createCipheriv']) {
|
|
119
|
+
patcher.patch(_export, method, {
|
|
120
|
+
name: `crypto.${method}`,
|
|
121
|
+
patchType,
|
|
122
|
+
post(data) {
|
|
123
|
+
const [alg] = data.args;
|
|
124
|
+
if (!isString(alg) || !getSourceContext(RULE, Rule.CRYPTO_BAD_CIPHERS)) return;
|
|
125
|
+
|
|
126
|
+
const algLower = toLowerCase(alg);
|
|
127
|
+
for (const prefix of SAFE_CIPHER_ALGORITHM_PREFIXES) {
|
|
128
|
+
if (algLower.indexOf(prefix) === 0) return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const event = eventFactory.createCryptoAnalysisEvent({
|
|
132
|
+
args: [{ tracked: false, value: alg }],
|
|
133
|
+
context: `crypto.${method}(${inspect(alg)})`,
|
|
134
|
+
methodName: method,
|
|
135
|
+
moduleName: 'crypto',
|
|
136
|
+
name: `crypto.${method}`,
|
|
137
|
+
object: { tracked: false, value: 'crypto' },
|
|
138
|
+
result: { tracked: false, value: 'Cipher' },
|
|
139
|
+
source: 'P0',
|
|
140
|
+
stacktraceOpts: {
|
|
141
|
+
constructorOpt: data.hooked,
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
if (!event) return;
|
|
146
|
+
|
|
147
|
+
cryptoAnalysis.report({ finding: event, ruleId: Rule.CRYPTO_BAD_CIPHERS });
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
},
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
return installer;
|
|
156
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2024 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(
|
|
83
|
+
{ funcKey: data.funcKey },
|
|
84
|
+
'skipping reporting for %s - trusting %s',
|
|
85
|
+
ruleId,
|
|
86
|
+
lib,
|
|
87
|
+
);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
cryptoAnalysis.report({ finding: event, ruleId });
|
|
95
|
+
},
|
|
96
|
+
});
|
|
97
|
+
},
|
|
98
|
+
uninstall() {
|
|
99
|
+
Math.random = patcher.unwrap(Math.random);
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
return core.assess.cryptoAnalysis.installers.math;
|
|
104
|
+
};
|
package/lib/dataflow/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Copyright:
|
|
2
|
+
* Copyright: 2024 Contrast Security, Inc
|
|
3
3
|
* Contact: support@contrastsecurity.com
|
|
4
4
|
* License: Commercial
|
|
5
5
|
|
|
@@ -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,
|
|
@@ -141,8 +142,8 @@ module.exports = function (core) {
|
|
|
141
142
|
|
|
142
143
|
try {
|
|
143
144
|
keyValueIndexes = getKeyValueIndexes(input);
|
|
144
|
-
} catch (
|
|
145
|
-
logger.warn({
|
|
145
|
+
} catch (err) {
|
|
146
|
+
logger.warn({ err, funcKey: data.funcKey, string: input }, 'JSON.parse() propagation failed');
|
|
146
147
|
}
|
|
147
148
|
|
|
148
149
|
if (keyValueIndexes.length === 0) return;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Copyright:
|
|
2
|
+
* Copyright: 2024 Contrast Security, Inc
|
|
3
3
|
* Contact: support@contrastsecurity.com
|
|
4
4
|
* License: Commercial
|
|
5
5
|
|
|
@@ -148,7 +148,7 @@ module.exports = function(core) {
|
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
-
metadata.history.add(metadata[id]);
|
|
151
|
+
metadata.history.add(metadata.strInfos[id]);
|
|
152
152
|
// replace the canary with the starting quote
|
|
153
153
|
return '"';
|
|
154
154
|
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Copyright:
|
|
2
|
+
* Copyright: 2024 Contrast Security, Inc
|
|
3
3
|
* Contact: support@contrastsecurity.com
|
|
4
4
|
* License: Commercial
|
|
5
5
|
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
const { isString } = require('@contrast/common');
|
|
19
19
|
const { patchType } = require('../../common');
|
|
20
20
|
|
|
21
|
-
module.exports = function(core) {
|
|
21
|
+
module.exports = function (core) {
|
|
22
22
|
const {
|
|
23
23
|
logger,
|
|
24
24
|
scopes: { instrumentation, sources },
|
|
@@ -43,11 +43,12 @@ module.exports = function(core) {
|
|
|
43
43
|
!isString(value) ||
|
|
44
44
|
!sources.getStore()?.assess ||
|
|
45
45
|
instrumentation.isLocked() ||
|
|
46
|
+
// why not just do this first? won't need check for NaN, !value, !isString, etc.
|
|
46
47
|
!tracker.getData(value)
|
|
47
48
|
) return;
|
|
48
49
|
|
|
49
50
|
tracker.untrack(value);
|
|
50
|
-
logger.trace({ sanitizer: name, value }, 'untracked a string value');
|
|
51
|
+
logger.trace({ funcKey: data.funcKey, sanitizer: name, value }, 'untracked a string value');
|
|
51
52
|
}
|
|
52
53
|
});
|
|
53
54
|
},
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Copyright:
|
|
2
|
+
* Copyright: 2024 Contrast Security, Inc
|
|
3
3
|
* Contact: support@contrastsecurity.com
|
|
4
4
|
* License: Commercial
|
|
5
5
|
|
|
@@ -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: {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/*
|
|
2
|
-
* Copyright:
|
|
2
|
+
* Copyright: 2024 Contrast Security, Inc
|
|
3
3
|
* Contact: support@contrastsecurity.com
|
|
4
4
|
* License: Commercial
|
|
5
5
|
|
|
@@ -25,6 +25,7 @@ module.exports = function(core) {
|
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
require('./escape-xml')(core);
|
|
28
|
+
require('./template')(core);
|
|
28
29
|
|
|
29
30
|
return ejsInstrumentation;
|
|
30
31
|
};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright: 2024 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
|
+
patcher.patch(_export.Template.prototype, 'generateSource', {
|
|
60
|
+
name: 'ejs.Template.prototype.generateSource',
|
|
61
|
+
patchType,
|
|
62
|
+
post(data) {
|
|
63
|
+
if (!getSourceContext(PROPAGATOR)) return;
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
const { code } = rewriter.rewrite(`${WRAPPER_PREFIX}${data.obj.source}${WRAPPER_SUFFIX}`, REWRITE_OPTS);
|
|
67
|
+
data.obj.source = code.substring(code.indexOf('{') + 1, code.lastIndexOf('}'));
|
|
68
|
+
} catch (err) {
|
|
69
|
+
logger.error(
|
|
70
|
+
{ err, funcKey: data.funcKey, source: data.obj.source },
|
|
71
|
+
'error occurred while rewriting ejs source'
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
};
|