@contrast/assess 1.17.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.
Files changed (53) hide show
  1. package/lib/constants.js +26 -0
  2. package/lib/crypto-analysis/common.js +20 -0
  3. package/lib/crypto-analysis/index.js +44 -0
  4. package/lib/crypto-analysis/install/crypto.js +151 -0
  5. package/lib/crypto-analysis/install/math.js +99 -0
  6. package/lib/dataflow/propagation/index.js +2 -1
  7. package/lib/dataflow/propagation/install/JSON/parse.js +12 -11
  8. package/lib/dataflow/propagation/install/JSON/stringify.js +1 -1
  9. package/lib/dataflow/propagation/install/array-prototype-join.js +1 -1
  10. package/lib/dataflow/propagation/install/ejs/escape-xml.js +2 -2
  11. package/lib/dataflow/propagation/install/ejs/index.js +1 -0
  12. package/lib/dataflow/propagation/install/ejs/template.js +77 -0
  13. package/lib/dataflow/propagation/install/sequelize/index.js +31 -0
  14. package/lib/dataflow/propagation/install/sequelize/query-generator.js +90 -0
  15. package/lib/dataflow/propagation/install/{sequelize.js → sequelize/sql-string.js} +3 -3
  16. package/lib/dataflow/propagation/install/util-format.js +126 -0
  17. package/lib/dataflow/sinks/index.js +1 -0
  18. package/lib/dataflow/sinks/install/child-process.js +20 -14
  19. package/lib/dataflow/sinks/install/eval.js +16 -14
  20. package/lib/dataflow/sinks/install/express/unvalidated-redirect.js +14 -8
  21. package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.js +12 -5
  22. package/lib/dataflow/sinks/install/fs.js +7 -7
  23. package/lib/dataflow/sinks/install/function.js +8 -12
  24. package/lib/dataflow/sinks/install/http/request.js +16 -8
  25. package/lib/dataflow/sinks/install/http/server-response.js +11 -2
  26. package/lib/dataflow/sinks/install/koa/unvalidated-redirect.js +15 -8
  27. package/lib/dataflow/sinks/install/libxmljs.js +15 -10
  28. package/lib/dataflow/sinks/install/marsdb.js +13 -8
  29. package/lib/dataflow/sinks/install/mongodb.js +25 -15
  30. package/lib/dataflow/sinks/install/mssql.js +20 -9
  31. package/lib/dataflow/sinks/install/mysql.js +15 -8
  32. package/lib/dataflow/sinks/install/node-serialize.js +101 -0
  33. package/lib/dataflow/sinks/install/postgres.js +17 -4
  34. package/lib/dataflow/sinks/install/sequelize.js +16 -9
  35. package/lib/dataflow/sinks/install/sqlite3.js +20 -7
  36. package/lib/dataflow/sinks/install/vm.js +19 -17
  37. package/lib/dataflow/sources/install/http.js +14 -42
  38. package/lib/dataflow/sources/install/koa/index.js +1 -0
  39. package/lib/dataflow/sources/install/koa/koa-multer.js +102 -0
  40. package/lib/dataflow/sources/install/multer1.js +25 -51
  41. package/lib/dataflow/sources/install/querystring.js +1 -4
  42. package/lib/event-factory.js +47 -0
  43. package/lib/get-policy.js +68 -0
  44. package/lib/get-source-context.js +62 -0
  45. package/lib/index.d.ts +50 -0
  46. package/lib/index.js +20 -19
  47. package/lib/make-source-context.js +74 -0
  48. package/lib/response-scanning/handlers/index.js +55 -28
  49. package/lib/response-scanning/install/http.js +13 -7
  50. package/lib/rule-scopes.js +48 -0
  51. package/lib/session-configuration/handlers.js +4 -3
  52. package/lib/session-configuration/install/express-session.js +8 -2
  53. package/package.json +2 -2
@@ -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
+ };
@@ -24,6 +24,7 @@ module.exports = function(core) {
24
24
  require('./install/ejs')(core);
25
25
  require('./install/mongoose')(core);
26
26
  require('./install/pug')(core);
27
+ require('./install/sequelize')(core);
27
28
  require('./install/string')(core);
28
29
  require('./install/url')(core);
29
30
  require('./install/validator')(core);
@@ -42,12 +43,12 @@ module.exports = function(core) {
42
43
  require('./install/sql-template-strings')(core);
43
44
  require('./install/unescape')(core);
44
45
  require('./install/querystring')(core);
45
- require('./install/sequelize')(core);
46
46
  require('./install/JSON')(core);
47
47
  require('./install/path')(core);
48
48
  require('./install/reg-exp-prototype-exec')(core);
49
49
  require('./install/joi')(core);
50
50
  require('./install/send')(core);
51
+ require('./install/util-format')(core);
51
52
 
52
53
  propagation.install = function() {
53
54
  callChildComponentMethodsSync(propagation, 'install');
@@ -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,
@@ -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
  });
@@ -102,7 +102,7 @@ module.exports = function(core) {
102
102
  },
103
103
  args,
104
104
  tags: newTags,
105
- history: Array.from(history),
105
+ history: Array.from(history).map((el) => Object.assign({}, el)),
106
106
  source: delimiterInfo ? (history.size > 1 ? 'A' : 'P') : 'O',
107
107
  target: 'R',
108
108
  stacktraceOpts: {
@@ -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: {
@@ -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,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
+ };
@@ -0,0 +1,31 @@
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 { callChildComponentMethodsSync } = require('@contrast/common');
19
+
20
+ module.exports = function(core) {
21
+ const component = core.assess.dataflow.propagation.sequelizeInstrumentation = {
22
+ install() {
23
+ callChildComponentMethodsSync(component, 'install');
24
+ },
25
+ };
26
+
27
+ require('./query-generator')(core);
28
+ require('./sql-string')(core);
29
+
30
+ return component;
31
+ };
@@ -0,0 +1,90 @@
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
+ 'use strict';
16
+
17
+ const {
18
+ DataflowTag: { SQL_ENCODED }
19
+ } = require('@contrast/common');
20
+ const { patchType } = require('../../common');
21
+
22
+ const DIALECTS = [
23
+ // 'db2',
24
+ // 'mariadb',
25
+ 'mssql',
26
+ 'mysql',
27
+ // 'oracle',
28
+ 'postgres',
29
+ // 'snowflake',
30
+ 'sqlite',
31
+ ];
32
+
33
+ module.exports = function(core) {
34
+ const {
35
+ assess: {
36
+ eventFactory: { createPropagationEvent },
37
+ dataflow: { tracker }
38
+ },
39
+ depHooks,
40
+ patcher,
41
+ } = core;
42
+
43
+ return core.assess.dataflow.propagation.sequelizeInstrumentation.queryGenerators = {
44
+ install() {
45
+ for (const dialect of DIALECTS) {
46
+ depHooks.resolve(
47
+ { name: 'sequelize', file: `lib/dialects/${dialect}/query-generator.js` },
48
+ (_export) => {
49
+ const constructor = _export.name;
50
+
51
+ patcher.patch(_export.prototype, 'quoteIdentifier', {
52
+ name: `sequelize.${constructor}.prototype.quoteIdentifier`,
53
+ patchType,
54
+ post(data) {
55
+ const strInfo = tracker.getData(data.result);
56
+ if (!strInfo) return;
57
+
58
+ const { value } = strInfo;
59
+ const event = createPropagationEvent({
60
+ // context: ``,
61
+ args: [{ tracked: true, value: data.args[0] }],
62
+ history: [strInfo],
63
+ moduleName: 'sequelize',
64
+ methodName: `${constructor}.prototype.quoteIdentifier`,
65
+ object: { tracked: false, value: constructor },
66
+ source: 'P',
67
+ result: { tracked: true, value },
68
+ tags: {
69
+ ...strInfo.tags,
70
+ [SQL_ENCODED]: [0, value.length - 1],
71
+ },
72
+ target: 'R',
73
+ name: 'foo',
74
+ });
75
+
76
+ if (event) {
77
+ const { extern } = tracker.track(value, event);
78
+ if (extern) {
79
+ data.result = extern;
80
+ }
81
+ }
82
+ }
83
+ });
84
+ }
85
+ );
86
+ }
87
+ },
88
+ uninstall() {}
89
+ };
90
+ };
@@ -19,7 +19,7 @@ const {
19
19
  isString,
20
20
  DataflowTag: { SQL_ENCODED },
21
21
  } = require('@contrast/common');
22
- const { patchType, createModuleLabel } = require('../common');
22
+ const { patchType, createModuleLabel } = require('../../common');
23
23
 
24
24
  module.exports = function(core) {
25
25
  const {
@@ -50,7 +50,7 @@ module.exports = function(core) {
50
50
  return Array.from(matches, (match) => ({ [match[1]]: match.index }));
51
51
  }
52
52
 
53
- return core.assess.dataflow.propagation.sequelizeInstrumentation = {
53
+ return core.assess.dataflow.propagation.sequelizeInstrumentation.sqlString = {
54
54
  install() {
55
55
  depHooks.resolve(
56
56
  { name: 'sequelize', file: 'lib/sql-string.js' },
@@ -89,7 +89,7 @@ module.exports = function(core) {
89
89
  moduleName: 'sequelize',
90
90
  methodName: 'escape',
91
91
  object: {
92
- value: createModuleLabel('sequelize/lib/sql-string.escape', version),
92
+ value: 'sequelize/lib/sql-string.js',
93
93
  tracked: false,
94
94
  },
95
95
  result: {