@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.
Files changed (160) hide show
  1. package/LICENSE +1 -1
  2. package/lib/constants.js +26 -0
  3. package/lib/crypto-analysis/common.js +20 -0
  4. package/lib/crypto-analysis/index.js +44 -0
  5. package/lib/crypto-analysis/install/crypto.js +156 -0
  6. package/lib/crypto-analysis/install/math.js +104 -0
  7. package/lib/dataflow/index.js +1 -1
  8. package/lib/dataflow/propagation/common.js +1 -1
  9. package/lib/dataflow/propagation/index.js +1 -1
  10. package/lib/dataflow/propagation/install/JSON/index.js +1 -1
  11. package/lib/dataflow/propagation/install/JSON/parse-fn.js +1 -1
  12. package/lib/dataflow/propagation/install/JSON/parse.js +15 -14
  13. package/lib/dataflow/propagation/install/JSON/stringify.js +2 -2
  14. package/lib/dataflow/propagation/install/array-prototype-join.js +1 -1
  15. package/lib/dataflow/propagation/install/buffer.js +1 -1
  16. package/lib/dataflow/propagation/install/contrast-methods/add.js +1 -1
  17. package/lib/dataflow/propagation/install/contrast-methods/index.js +1 -1
  18. package/lib/dataflow/propagation/install/contrast-methods/number.js +4 -3
  19. package/lib/dataflow/propagation/install/contrast-methods/string.js +1 -1
  20. package/lib/dataflow/propagation/install/contrast-methods/tag.js +1 -1
  21. package/lib/dataflow/propagation/install/decode-uri-component.js +1 -1
  22. package/lib/dataflow/propagation/install/ejs/escape-xml.js +3 -3
  23. package/lib/dataflow/propagation/install/ejs/index.js +2 -1
  24. package/lib/dataflow/propagation/install/ejs/template.js +79 -0
  25. package/lib/dataflow/propagation/install/encode-uri.js +1 -1
  26. package/lib/dataflow/propagation/install/escape-html.js +1 -1
  27. package/lib/dataflow/propagation/install/escape.js +1 -1
  28. package/lib/dataflow/propagation/install/handlebars-utils-escape-expression.js +1 -1
  29. package/lib/dataflow/propagation/install/isnumeric-0.js +3 -3
  30. package/lib/dataflow/propagation/install/joi/any.js +1 -1
  31. package/lib/dataflow/propagation/install/joi/boolean.js +1 -1
  32. package/lib/dataflow/propagation/install/joi/expression.js +1 -1
  33. package/lib/dataflow/propagation/install/joi/index.js +1 -1
  34. package/lib/dataflow/propagation/install/joi/keys.js +1 -1
  35. package/lib/dataflow/propagation/install/joi/number.js +1 -1
  36. package/lib/dataflow/propagation/install/joi/object.js +1 -1
  37. package/lib/dataflow/propagation/install/joi/string-schema.js +1 -1
  38. package/lib/dataflow/propagation/install/joi/utils.js +1 -1
  39. package/lib/dataflow/propagation/install/joi/values.js +1 -1
  40. package/lib/dataflow/propagation/install/mongoose/common.js +1 -1
  41. package/lib/dataflow/propagation/install/mongoose/index.js +1 -1
  42. package/lib/dataflow/propagation/install/mongoose/schema-map.js +1 -1
  43. package/lib/dataflow/propagation/install/mongoose/schema-mixed.js +1 -1
  44. package/lib/dataflow/propagation/install/mongoose/schema-string.js +1 -1
  45. package/lib/dataflow/propagation/install/mustache-escape.js +1 -1
  46. package/lib/dataflow/propagation/install/mysql-connection-escape.js +1 -1
  47. package/lib/dataflow/propagation/install/parse-int.js +3 -3
  48. package/lib/dataflow/propagation/install/path/basename.js +1 -1
  49. package/lib/dataflow/propagation/install/path/common.js +1 -1
  50. package/lib/dataflow/propagation/install/path/dirname.js +1 -1
  51. package/lib/dataflow/propagation/install/path/extname.js +1 -1
  52. package/lib/dataflow/propagation/install/path/format.js +1 -1
  53. package/lib/dataflow/propagation/install/path/index.js +1 -1
  54. package/lib/dataflow/propagation/install/path/join-and-resolve.js +1 -1
  55. package/lib/dataflow/propagation/install/path/normalize.js +1 -1
  56. package/lib/dataflow/propagation/install/path/parse.js +1 -1
  57. package/lib/dataflow/propagation/install/path/relative.js +1 -1
  58. package/lib/dataflow/propagation/install/path/toNamespacedPath.js +1 -1
  59. package/lib/dataflow/propagation/install/pug/index.js +3 -3
  60. package/lib/dataflow/propagation/install/pug-runtime-escape.js +1 -1
  61. package/lib/dataflow/propagation/install/querystring/escape.js +1 -1
  62. package/lib/dataflow/propagation/install/querystring/index.js +1 -1
  63. package/lib/dataflow/propagation/install/querystring/parse.js +1 -1
  64. package/lib/dataflow/propagation/install/querystring/stringify.js +1 -1
  65. package/lib/dataflow/propagation/install/reg-exp-prototype-exec.js +1 -1
  66. package/lib/dataflow/propagation/install/send.js +1 -1
  67. package/lib/dataflow/propagation/install/sequelize/index.js +1 -1
  68. package/lib/dataflow/propagation/install/sequelize/query-generator.js +1 -1
  69. package/lib/dataflow/propagation/install/sequelize/sql-string.js +1 -1
  70. package/lib/dataflow/propagation/install/sql-template-strings.js +1 -1
  71. package/lib/dataflow/propagation/install/string/concat.js +1 -1
  72. package/lib/dataflow/propagation/install/string/format-methods.js +1 -1
  73. package/lib/dataflow/propagation/install/string/html-methods.js +1 -1
  74. package/lib/dataflow/propagation/install/string/index.js +1 -1
  75. package/lib/dataflow/propagation/install/string/match-all.js +1 -1
  76. package/lib/dataflow/propagation/install/string/match.js +1 -1
  77. package/lib/dataflow/propagation/install/string/replace.js +1 -1
  78. package/lib/dataflow/propagation/install/string/slice.js +1 -1
  79. package/lib/dataflow/propagation/install/string/split.js +1 -1
  80. package/lib/dataflow/propagation/install/string/substring.js +1 -1
  81. package/lib/dataflow/propagation/install/string/trim.js +1 -1
  82. package/lib/dataflow/propagation/install/unescape.js +1 -1
  83. package/lib/dataflow/propagation/install/url/domain-parsers.js +1 -1
  84. package/lib/dataflow/propagation/install/url/index.js +1 -1
  85. package/lib/dataflow/propagation/install/url/parse.js +1 -1
  86. package/lib/dataflow/propagation/install/url/searchParams.js +1 -1
  87. package/lib/dataflow/propagation/install/url/url.js +1 -1
  88. package/lib/dataflow/propagation/install/util-format.js +10 -4
  89. package/lib/dataflow/propagation/install/validator/hooks.js +1 -1
  90. package/lib/dataflow/propagation/install/validator/index.js +1 -1
  91. package/lib/dataflow/propagation/install/validator/methods.js +1 -1
  92. package/lib/dataflow/sinks/common.js +1 -1
  93. package/lib/dataflow/sinks/index.js +1 -1
  94. package/lib/dataflow/sinks/install/child-process.js +21 -15
  95. package/lib/dataflow/sinks/install/eval.js +17 -15
  96. package/lib/dataflow/sinks/install/express/index.js +1 -1
  97. package/lib/dataflow/sinks/install/express/unvalidated-redirect.js +15 -9
  98. package/lib/dataflow/sinks/install/fastify/index.js +1 -1
  99. package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.js +13 -6
  100. package/lib/dataflow/sinks/install/fs.js +8 -8
  101. package/lib/dataflow/sinks/install/function.js +9 -13
  102. package/lib/dataflow/sinks/install/http/index.js +1 -1
  103. package/lib/dataflow/sinks/install/http/request.js +17 -9
  104. package/lib/dataflow/sinks/install/http/server-response.js +12 -3
  105. package/lib/dataflow/sinks/install/koa/index.js +1 -1
  106. package/lib/dataflow/sinks/install/koa/unvalidated-redirect.js +16 -9
  107. package/lib/dataflow/sinks/install/libxmljs.js +16 -11
  108. package/lib/dataflow/sinks/install/marsdb.js +17 -12
  109. package/lib/dataflow/sinks/install/mongodb.js +32 -22
  110. package/lib/dataflow/sinks/install/mssql.js +21 -10
  111. package/lib/dataflow/sinks/install/mysql.js +16 -9
  112. package/lib/dataflow/sinks/install/node-serialize.js +16 -18
  113. package/lib/dataflow/sinks/install/postgres.js +18 -5
  114. package/lib/dataflow/sinks/install/sequelize.js +23 -17
  115. package/lib/dataflow/sinks/install/sqlite3.js +21 -8
  116. package/lib/dataflow/sinks/install/vm.js +20 -18
  117. package/lib/dataflow/sources/common.js +1 -1
  118. package/lib/dataflow/sources/handler.js +11 -10
  119. package/lib/dataflow/sources/index.js +2 -2
  120. package/lib/dataflow/sources/install/body-parser1.js +11 -13
  121. package/lib/dataflow/sources/install/{busboy1.js → busboy.js} +15 -15
  122. package/lib/dataflow/sources/install/cookie-parser1.js +7 -6
  123. package/lib/dataflow/sources/install/express/index.js +1 -1
  124. package/lib/dataflow/sources/install/express/params.js +9 -10
  125. package/lib/dataflow/sources/install/express/parsedUrl.js +1 -1
  126. package/lib/dataflow/sources/install/fastify/fastify.js +6 -7
  127. package/lib/dataflow/sources/install/fastify/index.js +1 -1
  128. package/lib/dataflow/sources/install/formidable1.js +8 -6
  129. package/lib/dataflow/sources/install/http.js +17 -45
  130. package/lib/dataflow/sources/install/koa/index.js +2 -1
  131. package/lib/dataflow/sources/install/koa/koa-bodyparsers.js +10 -9
  132. package/lib/dataflow/sources/install/koa/koa-multer.js +102 -0
  133. package/lib/dataflow/sources/install/koa/koa-routers.js +6 -8
  134. package/lib/dataflow/sources/install/koa/koa2.js +42 -38
  135. package/lib/dataflow/sources/install/multer1.js +26 -52
  136. package/lib/dataflow/sources/install/qs6.js +7 -6
  137. package/lib/dataflow/sources/install/querystring.js +5 -8
  138. package/lib/dataflow/tag-utils.js +1 -1
  139. package/lib/dataflow/tracker.js +1 -1
  140. package/lib/dataflow/utils/is-safe-content-type.js +1 -1
  141. package/lib/dataflow/utils/is-vulnerable.js +1 -1
  142. package/lib/event-factory.js +75 -26
  143. package/lib/get-policy.js +68 -0
  144. package/lib/get-source-context.js +62 -0
  145. package/lib/index.d.ts +64 -0
  146. package/lib/index.js +21 -20
  147. package/lib/make-source-context.js +78 -0
  148. package/lib/response-scanning/handlers/index.js +56 -29
  149. package/lib/response-scanning/handlers/utils.js +1 -1
  150. package/lib/response-scanning/index.js +1 -1
  151. package/lib/response-scanning/install/http.js +14 -8
  152. package/lib/rule-scopes.js +48 -0
  153. package/lib/session-configuration/common.js +1 -1
  154. package/lib/session-configuration/handlers.js +66 -47
  155. package/lib/session-configuration/index.js +3 -1
  156. package/lib/session-configuration/install/express-session.js +23 -26
  157. package/lib/session-configuration/install/fastify-cookie.js +110 -0
  158. package/lib/session-configuration/install/hapi.js +8 -11
  159. package/lib/session-configuration/install/koa.js +101 -0
  160. package/package.json +2 -2
package/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright: 2023 Contrast Security, Inc
1
+ Copyright: 2024 Contrast Security, Inc
2
2
  Contact: support@contrastsecurity.com
3
3
  License: Commercial
4
4
 
@@ -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
+ };
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
2
+ * Copyright: 2024 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
2
+ * Copyright: 2024 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
2
+ * Copyright: 2024 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
2
+ * Copyright: 2024 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
2
+ * Copyright: 2024 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
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 (error) {
145
- logger.warn({ error, string: input }, 'JSON.parse() propagation failed');
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: 2023 Contrast Security, Inc
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: 2023 Contrast Security, Inc
2
+ * Copyright: 2024 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
2
+ * Copyright: 2024 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
2
+ * Copyright: 2024 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
2
+ * Copyright: 2024 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
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: 2023 Contrast Security, Inc
2
+ * Copyright: 2024 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
2
+ * Copyright: 2024 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
2
+ * Copyright: 2024 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
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, createModuleLabel } = require('../../common');
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: `[${createModuleLabel('ejs', version)}].utils`,
64
+ value: 'ejs.utils',
65
65
  tracked: false
66
66
  },
67
67
  result: {
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2023 Contrast Security, Inc
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
+ };