@contrast/assess 1.10.0 → 1.12.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 (119) hide show
  1. package/lib/dataflow/index.js +1 -2
  2. package/lib/dataflow/propagation/common.js +1 -1
  3. package/lib/dataflow/propagation/index.js +2 -1
  4. package/lib/dataflow/propagation/install/JSON/index.js +1 -1
  5. package/lib/dataflow/propagation/install/JSON/parse-fn.js +1 -1
  6. package/lib/dataflow/propagation/install/JSON/parse.js +3 -5
  7. package/lib/dataflow/propagation/install/JSON/stringify.js +3 -2
  8. package/lib/dataflow/propagation/install/array-prototype-join.js +3 -2
  9. package/lib/dataflow/propagation/install/buffer.js +3 -5
  10. package/lib/dataflow/propagation/install/contrast-methods/add.js +3 -2
  11. package/lib/dataflow/propagation/install/contrast-methods/index.js +1 -1
  12. package/lib/dataflow/propagation/install/contrast-methods/number.js +1 -1
  13. package/lib/dataflow/propagation/install/contrast-methods/string.js +3 -5
  14. package/lib/dataflow/propagation/install/contrast-methods/tag.js +3 -5
  15. package/lib/dataflow/propagation/install/decode-uri-component.js +3 -2
  16. package/lib/dataflow/propagation/install/ejs/escape-xml.js +3 -2
  17. package/lib/dataflow/propagation/install/ejs/index.js +1 -1
  18. package/lib/dataflow/propagation/install/encode-uri-component.js +3 -2
  19. package/lib/dataflow/propagation/install/escape-html.js +3 -2
  20. package/lib/dataflow/propagation/install/escape.js +3 -2
  21. package/lib/dataflow/propagation/install/handlebars-utils-escape-expression.js +3 -2
  22. package/lib/dataflow/propagation/install/isnumeric-0.js +1 -1
  23. package/lib/dataflow/propagation/install/mongoose/common.js +20 -0
  24. package/lib/dataflow/propagation/install/mongoose/index.js +5 -9
  25. package/lib/dataflow/propagation/install/mongoose/schema-map.js +149 -0
  26. package/lib/dataflow/propagation/install/mongoose/schema-mixed.js +162 -0
  27. package/lib/dataflow/propagation/install/mongoose/schema-string.js +91 -39
  28. package/lib/dataflow/propagation/install/mysql-connection-escape.js +3 -2
  29. package/lib/dataflow/propagation/install/parse-int.js +1 -1
  30. package/lib/dataflow/propagation/install/path/basename.js +3 -5
  31. package/lib/dataflow/propagation/install/path/common.js +1 -1
  32. package/lib/dataflow/propagation/install/path/index.js +1 -1
  33. package/lib/dataflow/propagation/install/path/join-and-resolve.js +3 -5
  34. package/lib/dataflow/propagation/install/path/normalize.js +3 -5
  35. package/lib/dataflow/propagation/install/pug/index.js +1 -1
  36. package/lib/dataflow/propagation/install/pug-runtime-escape.js +3 -2
  37. package/lib/dataflow/propagation/install/querystring/index.js +1 -1
  38. package/lib/dataflow/propagation/install/querystring/parse.js +3 -2
  39. package/lib/dataflow/propagation/install/reg-exp-prototype-exec.js +180 -0
  40. package/lib/dataflow/propagation/install/sequelize.js +3 -5
  41. package/lib/dataflow/propagation/install/sql-template-strings.js +3 -2
  42. package/lib/dataflow/propagation/install/string/concat.js +3 -2
  43. package/lib/dataflow/propagation/install/string/format-methods.js +3 -2
  44. package/lib/dataflow/propagation/install/string/html-methods.js +3 -2
  45. package/lib/dataflow/propagation/install/string/index.js +66 -1
  46. package/lib/dataflow/propagation/install/string/match-all.js +236 -0
  47. package/lib/dataflow/propagation/install/string/match.js +83 -37
  48. package/lib/dataflow/propagation/install/string/replace.js +4 -3
  49. package/lib/dataflow/propagation/install/string/slice.js +3 -2
  50. package/lib/dataflow/propagation/install/string/split.js +3 -2
  51. package/lib/dataflow/propagation/install/string/substring.js +3 -2
  52. package/lib/dataflow/propagation/install/string/trim.js +3 -2
  53. package/lib/dataflow/propagation/install/unescape.js +3 -2
  54. package/lib/dataflow/propagation/install/url/domain-parsers.js +3 -2
  55. package/lib/dataflow/propagation/install/url/index.js +3 -1
  56. package/lib/dataflow/propagation/install/url/parse.js +132 -0
  57. package/lib/dataflow/propagation/install/url/searchParams.js +140 -0
  58. package/lib/dataflow/propagation/install/url/url.js +11 -53
  59. package/lib/dataflow/propagation/install/validator/hooks.js +3 -2
  60. package/lib/dataflow/propagation/install/validator/index.js +1 -1
  61. package/lib/dataflow/propagation/install/validator/methods.js +1 -1
  62. package/lib/dataflow/sinks/common.js +1 -1
  63. package/lib/dataflow/sinks/index.js +1 -1
  64. package/lib/dataflow/sinks/install/child-process.js +2 -2
  65. package/lib/dataflow/sinks/install/eval.js +2 -2
  66. package/lib/dataflow/sinks/install/express/index.js +1 -1
  67. package/lib/dataflow/sinks/install/express/unvalidated-redirect.js +3 -3
  68. package/lib/dataflow/sinks/install/fastify/index.js +1 -1
  69. package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.js +2 -2
  70. package/lib/dataflow/sinks/install/fs.js +2 -2
  71. package/lib/dataflow/sinks/install/function.js +2 -2
  72. package/lib/dataflow/sinks/install/http/index.js +1 -1
  73. package/lib/dataflow/sinks/install/http/request.js +2 -2
  74. package/lib/dataflow/sinks/install/http/server-response.js +2 -2
  75. package/lib/dataflow/sinks/install/koa/index.js +1 -1
  76. package/lib/dataflow/sinks/install/koa/unvalidated-redirect.js +2 -2
  77. package/lib/dataflow/sinks/install/marsdb.js +2 -2
  78. package/lib/dataflow/sinks/install/mongodb.js +33 -26
  79. package/lib/dataflow/sinks/install/mssql.js +2 -2
  80. package/lib/dataflow/sinks/install/mysql.js +3 -3
  81. package/lib/dataflow/sinks/install/postgres.js +2 -2
  82. package/lib/dataflow/sinks/install/sequelize.js +2 -2
  83. package/lib/dataflow/sinks/install/sqlite3.js +2 -2
  84. package/lib/dataflow/sinks/install/vm.js +2 -2
  85. package/lib/dataflow/sources/common.js +1 -1
  86. package/lib/dataflow/sources/handler.js +3 -3
  87. package/lib/dataflow/sources/index.js +1 -1
  88. package/lib/dataflow/sources/install/body-parser1.js +1 -1
  89. package/lib/dataflow/sources/install/busboy1.js +1 -1
  90. package/lib/dataflow/sources/install/cookie-parser1.js +1 -1
  91. package/lib/dataflow/sources/install/express/index.js +1 -1
  92. package/lib/dataflow/sources/install/express/params.js +1 -1
  93. package/lib/dataflow/sources/install/express/parsedUrl.js +1 -1
  94. package/lib/dataflow/sources/install/fastify/fastify.js +1 -1
  95. package/lib/dataflow/sources/install/fastify/index.js +1 -1
  96. package/lib/dataflow/sources/install/formidable1.js +1 -1
  97. package/lib/dataflow/sources/install/http.js +2 -2
  98. package/lib/dataflow/sources/install/koa/index.js +1 -1
  99. package/lib/dataflow/sources/install/koa/koa-bodyparsers.js +1 -1
  100. package/lib/dataflow/sources/install/koa/koa-routers.js +1 -1
  101. package/lib/dataflow/sources/install/koa/koa2.js +1 -1
  102. package/lib/dataflow/sources/install/qs6.js +1 -1
  103. package/lib/dataflow/sources/install/querystring.js +1 -1
  104. package/lib/dataflow/tag-utils.js +1 -1
  105. package/lib/dataflow/tracker.js +2 -6
  106. package/lib/dataflow/utils/is-safe-content-type.js +1 -1
  107. package/lib/dataflow/utils/is-vulnerable.js +1 -1
  108. package/lib/{dataflow/event-factory.js → event-factory.js} +58 -2
  109. package/lib/index.js +4 -2
  110. package/lib/response-scanning/handlers/index.js +36 -30
  111. package/lib/response-scanning/handlers/utils.js +1 -1
  112. package/lib/response-scanning/index.js +1 -1
  113. package/lib/response-scanning/install/http.js +3 -3
  114. package/lib/session-configuration/common.js +19 -0
  115. package/lib/session-configuration/handlers.js +86 -0
  116. package/lib/session-configuration/index.js +6 -9
  117. package/lib/session-configuration/install/express-session.js +131 -0
  118. package/package.json +3 -3
  119. package/lib/session-configuration/install/http.js +0 -79
@@ -0,0 +1,162 @@
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
+ const { patchType } = require('../../common');
17
+ const { userDefinedType } = require('./common');
18
+ const { traverseValues, DataflowTag, substring } = require('@contrast/common');
19
+
20
+ module.exports = function (core) {
21
+ const {
22
+ config: { assess },
23
+ scopes: { sources },
24
+ patcher,
25
+ depHooks,
26
+ assess: {
27
+ eventFactory: { createPropagationEvent },
28
+ dataflow: {
29
+ tracker,
30
+ propagation: { mongooseInstrumentation },
31
+ },
32
+ },
33
+ } = core;
34
+
35
+ const schemaMixed = {};
36
+ mongooseInstrumentation.schemaMixed = schemaMixed;
37
+
38
+ const handleString = (strInfo, orig, value, name) => {
39
+ const methodName = substring(name, name.indexOf('.') + 1);
40
+
41
+ // copy because we mutate the metadata value inline
42
+ const history = [{ ...strInfo }];
43
+ const event = createPropagationEvent({
44
+ addedTags: [DataflowTag.CUSTOM_VALIDATED],
45
+ name,
46
+ moduleName: 'mongoose',
47
+ methodName,
48
+ history,
49
+ object: {
50
+ tracked: false,
51
+ value: 'mongoose.SchemaMixed',
52
+ },
53
+ args: [{ tracked: true, value: strInfo.value }],
54
+ result: {
55
+ tracked: false,
56
+ value: undefined,
57
+ },
58
+ source: 'P0',
59
+ tags: {
60
+ ...strInfo.tags,
61
+ [DataflowTag.CUSTOM_VALIDATED]: [0, value.length - 1],
62
+ },
63
+ target: 'P0',
64
+ stacktraceOpts: {
65
+ prependFrames: [orig],
66
+ },
67
+ });
68
+
69
+ if (!event) return;
70
+
71
+ // in case the event type changed e.g. Source->Propagation
72
+ for (const key of Object.keys(strInfo)) {
73
+ if (key === 'value' || key === 'extern') continue;
74
+ delete strInfo[key];
75
+ }
76
+
77
+ Object.assign(strInfo, event);
78
+ };
79
+
80
+ const mixedInstrumentation = (data, name) => {
81
+ const hasCustomValidator = data.obj.validators.some(
82
+ (validator) => validator.type === userDefinedType
83
+ );
84
+ if (!hasCustomValidator) return;
85
+
86
+ const { orig, args } = data;
87
+
88
+ const input = args[0];
89
+ const inputType = typeof input;
90
+
91
+ if (inputType !== 'string' && inputType !== 'object') return;
92
+
93
+ if (inputType === 'string') {
94
+ const strInfo = tracker.getData(input);
95
+ if (!strInfo) return;
96
+
97
+ return handleString(strInfo, orig, input, name);
98
+ }
99
+
100
+ const weakMapOfStrInfo = new WeakMap();
101
+ traverseValues(input, (path, type, value) => {
102
+ const strInfo = tracker.getData(value);
103
+ if (!strInfo || weakMapOfStrInfo.has(strInfo)) return;
104
+ weakMapOfStrInfo.set(strInfo, true);
105
+
106
+ handleString(strInfo, orig, value, name);
107
+ });
108
+ };
109
+
110
+ schemaMixed.install = function () {
111
+ depHooks.resolve(
112
+ { name: 'mongoose', file: 'lib/schema/mixed.js', version: '>=5.0.0' },
113
+ (SchemaMixed) => {
114
+ const doValidateSyncName = 'mongoose.mixed.prototype.doValidateSync';
115
+ patcher.patch(SchemaMixed.prototype, 'doValidateSync', {
116
+ name: doValidateSyncName,
117
+ patchType,
118
+ post: (data) => {
119
+ if (
120
+ !assess.trust_custom_validators ||
121
+ data.result ||
122
+ !sources.getStore()?.assess
123
+ ) {
124
+ return;
125
+ }
126
+
127
+ mixedInstrumentation(data, doValidateSyncName);
128
+ },
129
+ });
130
+
131
+ // called when `Model.validate(data)` static method is used, as well as `model.validate()` instance method
132
+ const doValidateName = 'mongoose.mixed.prototype.doValidate';
133
+ patcher.patch(SchemaMixed.prototype, 'doValidate', {
134
+ name: doValidateName,
135
+ patchType,
136
+ pre: (data) => {
137
+ if (!assess.trust_custom_validators || !sources.getStore()?.assess) {
138
+ return;
139
+ }
140
+
141
+ const [value, cb] = data.args;
142
+ if (!value || typeof cb !== 'function') {
143
+ return;
144
+ }
145
+
146
+ data.args[1] = patcher.patch(cb, {
147
+ name: doValidateName,
148
+ patchType,
149
+ pre({ args: [err] }) {
150
+ if (err) return;
151
+
152
+ mixedInstrumentation(data, doValidateName);
153
+ },
154
+ });
155
+ },
156
+ });
157
+ }
158
+ );
159
+ };
160
+
161
+ return schemaMixed;
162
+ };
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2022 Contrast Security, Inc
2
+ * Copyright: 2023 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -17,32 +17,21 @@
17
17
 
18
18
  const { DataflowTag, substring } = require('@contrast/common');
19
19
  const { patchType } = require('../../common');
20
+ const { userDefinedType } = require('./common');
20
21
 
21
- module.exports = function(core) {
22
+ module.exports = function (core) {
22
23
  const {
24
+ config: {
25
+ assess: { trust_custom_validators },
26
+ },
23
27
  scopes: { sources },
24
28
  patcher,
25
29
  depHooks,
26
30
  assess: {
27
- dataflow: {
28
- tracker,
29
- eventFactory: { createPropagationEvent }
30
- }
31
- }
32
- } = core;
33
-
34
- return core.assess.dataflow.propagation.mongooseInstrumentation.schemaString = {
35
- install() {
36
- depHooks.resolve(
37
- { name: 'mongoose', file: 'lib/schema/string.js', version: '>=6.0.0' },
38
- (SchemaString) => {
39
- patchEnum(SchemaString);
40
- patchDoValidate(SchemaString);
41
- patchDoValidateSync(SchemaString);
42
- }
43
- );
31
+ eventFactory: { createPropagationEvent },
32
+ dataflow: { tracker },
44
33
  },
45
- };
34
+ } = core;
46
35
 
47
36
  function patchEnum(SchemaString) {
48
37
  patcher.patch(SchemaString.prototype, 'enum', {
@@ -67,9 +56,9 @@ module.exports = function(core) {
67
56
  if (data.result) {
68
57
  tracker.untrack(data.args[0]);
69
58
  }
70
- }
59
+ },
71
60
  });
72
- }
61
+ },
73
62
  });
74
63
  }
75
64
 
@@ -81,16 +70,38 @@ module.exports = function(core) {
81
70
  patchType,
82
71
  pre(data) {
83
72
  const [value, cb] = data.args;
84
- if (value && typeof cb === 'function' && sources.getStore()?.assess) {
85
- data.args[1] = patcher.patch(cb, {
86
- name,
87
- patchType,
88
- pre({ args: [err] }) {
89
- if (!err) propagate(name, data.orig, value);
90
- }
91
- });
73
+ if (!value || typeof cb !== 'function' || !sources.getStore()?.assess) {
74
+ return;
92
75
  }
93
- }
76
+
77
+ const hasCustomValidator = data.obj.validators.some(
78
+ (validator) => validator.type === userDefinedType
79
+ );
80
+
81
+ data.args[1] = patcher.patch(cb, {
82
+ name,
83
+ patchType,
84
+ pre(data) {
85
+ const {
86
+ args: [err],
87
+ } = data;
88
+
89
+ if (!err) {
90
+ propagate(
91
+ name,
92
+ data.orig,
93
+ value,
94
+ trust_custom_validators
95
+ ? hasCustomValidator
96
+ : false
97
+ );
98
+ }
99
+
100
+ if (err && err.kind === userDefinedType)
101
+ propagate(name, data.orig, value, false);
102
+ },
103
+ });
104
+ },
94
105
  });
95
106
  }
96
107
 
@@ -99,16 +110,34 @@ module.exports = function(core) {
99
110
  patcher.patch(SchemaString.prototype, 'doValidateSync', {
100
111
  name,
101
112
  patchType,
102
- post({ orig, args: [value] }) {
103
- if (value?.length) {
104
- propagate(name, orig, value);
113
+ post(data) {
114
+ const {
115
+ args: [value],
116
+ result
117
+ } = data;
118
+
119
+ const hasCustomValidator = data.obj.validators.some(
120
+ (validator) => validator.type === userDefinedType
121
+ );
122
+
123
+ if (!result) {
124
+ propagate(
125
+ name,
126
+ data.orig,
127
+ value,
128
+ trust_custom_validators
129
+ ? hasCustomValidator
130
+ : false
131
+ );
105
132
  }
106
- }
107
- });
108
133
 
134
+ if (result && result.kind === userDefinedType)
135
+ propagate(name, data.orig, value, false);
136
+ },
137
+ });
109
138
  }
110
139
 
111
- function propagate(name, orig, value) {
140
+ function propagate(name, orig, value, customValidated = false) {
112
141
  const strInfo = tracker.getData(value);
113
142
  if (!strInfo) return;
114
143
 
@@ -116,7 +145,10 @@ module.exports = function(core) {
116
145
  // copy because we mutate the metadata value inline
117
146
  const history = [{ ...strInfo }];
118
147
  const event = createPropagationEvent({
119
- addedTags: [DataflowTag.STRING_TYPE_CHECKED],
148
+ addedTags: [
149
+ DataflowTag.STRING_TYPE_CHECKED,
150
+ customValidated ? DataflowTag.CUSTOM_VALIDATED : null,
151
+ ].filter(Boolean),
120
152
  name,
121
153
  moduleName: 'mongoose',
122
154
  methodName,
@@ -134,11 +166,14 @@ module.exports = function(core) {
134
166
  tags: {
135
167
  ...strInfo.tags,
136
168
  [DataflowTag.STRING_TYPE_CHECKED]: [0, value.length - 1],
169
+ ...(customValidated
170
+ ? { [DataflowTag.CUSTOM_VALIDATED]: [0, value.length - 1] }
171
+ : {}),
137
172
  },
138
173
  target: 'P0',
139
174
  stacktraceOpts: {
140
175
  prependFrames: [orig],
141
- }
176
+ },
142
177
  });
143
178
 
144
179
  if (!event) {
@@ -153,4 +188,21 @@ module.exports = function(core) {
153
188
 
154
189
  Object.assign(strInfo, event);
155
190
  }
191
+
192
+ return core.assess.dataflow.propagation.mongooseInstrumentation.schemaString = {
193
+ install() {
194
+ depHooks.resolve(
195
+ {
196
+ name: 'mongoose',
197
+ file: 'lib/schema/string.js',
198
+ version: '>=6.0.0',
199
+ },
200
+ (SchemaString) => {
201
+ patchEnum(SchemaString);
202
+ patchDoValidate(SchemaString);
203
+ patchDoValidateSync(SchemaString);
204
+ }
205
+ );
206
+ },
207
+ };
156
208
  };
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2022 Contrast Security, Inc
2
+ * Copyright: 2023 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -29,7 +29,8 @@ module.exports = function(core) {
29
29
  patcher,
30
30
  depHooks,
31
31
  assess: {
32
- dataflow: { tracker, eventFactory: { createPropagationEvent } }
32
+ eventFactory: { createPropagationEvent },
33
+ dataflow: { tracker }
33
34
  }
34
35
  } = core;
35
36
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2022 Contrast Security, Inc
2
+ * Copyright: 2023 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2022 Contrast Security, Inc
2
+ * Copyright: 2023 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -27,10 +27,8 @@ module.exports = function(core) {
27
27
  patcher,
28
28
  scopes: { sources, instrumentation },
29
29
  assess: {
30
- dataflow: {
31
- tracker,
32
- eventFactory: { createPropagationEvent },
33
- },
30
+ eventFactory: { createPropagationEvent },
31
+ dataflow: { tracker },
34
32
  },
35
33
  } = core;
36
34
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2022 Contrast Security, Inc
2
+ * Copyright: 2023 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2022 Contrast Security, Inc
2
+ * Copyright: 2023 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2022 Contrast Security, Inc
2
+ * Copyright: 2023 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -28,10 +28,8 @@ module.exports = function(core) {
28
28
  patcher,
29
29
  scopes: { sources, instrumentation },
30
30
  assess: {
31
- dataflow: {
32
- tracker,
33
- eventFactory: { createPropagationEvent },
34
- },
31
+ eventFactory: { createPropagationEvent },
32
+ dataflow: { tracker },
35
33
  },
36
34
  } = core;
37
35
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2022 Contrast Security, Inc
2
+ * Copyright: 2023 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -27,10 +27,8 @@ module.exports = function(core) {
27
27
  patcher,
28
28
  scopes: { sources, instrumentation },
29
29
  assess: {
30
- dataflow: {
31
- tracker,
32
- eventFactory: { createPropagationEvent },
33
- },
30
+ eventFactory: { createPropagationEvent },
31
+ dataflow: { tracker },
34
32
  },
35
33
  } = core;
36
34
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2022 Contrast Security, Inc
2
+ * Copyright: 2023 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2022 Contrast Security, Inc
2
+ * Copyright: 2023 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -29,7 +29,8 @@ module.exports = function(core) {
29
29
  patcher,
30
30
  depHooks,
31
31
  assess: {
32
- dataflow: { tracker, eventFactory: { createPropagationEvent } }
32
+ eventFactory: { createPropagationEvent },
33
+ dataflow: { tracker }
33
34
  }
34
35
  } = core;
35
36
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2022 Contrast Security, Inc
2
+ * Copyright: 2023 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright: 2022 Contrast Security, Inc
2
+ * Copyright: 2023 Contrast Security, Inc
3
3
  * Contact: support@contrastsecurity.com
4
4
  * License: Commercial
5
5
 
@@ -31,7 +31,8 @@ module.exports = function(core) {
31
31
  patcher,
32
32
  depHooks,
33
33
  assess: {
34
- dataflow: { tracker, eventFactory: { createPropagationEvent } }
34
+ eventFactory: { createPropagationEvent },
35
+ dataflow: { tracker }
35
36
  }
36
37
  } = core;
37
38
 
@@ -0,0 +1,180 @@
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
+ const { inspect } = require('@contrast/common');
18
+ const { createSubsetTags } = require('../../tag-utils');
19
+ const { patchType } = require('../common');
20
+
21
+ module.exports = function(core) {
22
+ const {
23
+ scopes: { sources, instrumentation },
24
+ patcher,
25
+ assess: {
26
+ eventFactory: { createPropagationEvent },
27
+ dataflow: { tracker },
28
+ },
29
+ } = core;
30
+
31
+ function createPropagationEventForMatch({
32
+ strInfo,
33
+ startIdx,
34
+ match,
35
+ untrackedResult,
36
+ metadata,
37
+ }) {
38
+ const tags = createSubsetTags(strInfo.tags, startIdx, match.length);
39
+ const { name, obj, hooked, orig } = metadata;
40
+
41
+ return createPropagationEvent({
42
+ name,
43
+ moduleName: 'RegExp',
44
+ methodName: 'prototype.exec',
45
+ context: `'${obj}'.exec('${strInfo.value}')`,
46
+ history: [strInfo],
47
+ object: {
48
+ value: obj,
49
+ tracked: false,
50
+ },
51
+ args: [
52
+ {
53
+ value: strInfo.value,
54
+ tracked: true,
55
+ },
56
+ ],
57
+ tags,
58
+ result: {
59
+ value: inspect(untrackedResult),
60
+ tracked: false,
61
+ },
62
+ stacktraceOpts: {
63
+ constructorOpt: hooked,
64
+ prependFrames: [orig],
65
+ },
66
+ source: 'P',
67
+ target: 'R',
68
+ });
69
+ }
70
+
71
+ return (core.assess.dataflow.propagation.regExpExec = {
72
+ install() {
73
+ const name = 'RegExp.prototype.exec';
74
+
75
+ patcher.patch(RegExp.prototype, 'exec', {
76
+ name,
77
+ patchType,
78
+ post(data) {
79
+ const { args, obj, result, hooked, orig } = data;
80
+
81
+ if (
82
+ !obj ||
83
+ !args[0] ||
84
+ !result?.length ||
85
+ !sources.getStore()?.assess ||
86
+ instrumentation.isLocked()
87
+ )
88
+ return;
89
+
90
+ const strInfo = tracker.getData(args[0]);
91
+
92
+ if (!strInfo) return;
93
+
94
+ const untrackedResult = [...result];
95
+ untrackedResult.groups = result.groups && { ...result.groups };
96
+ untrackedResult.input = strInfo.value;
97
+ untrackedResult.index = result.index;
98
+ result.indices && (untrackedResult.indices = result.indices);
99
+
100
+ let searchIdx = result.index;
101
+ const metadata = { name, obj, hooked, orig };
102
+
103
+ for (let i = 0; i < result.length; i++) {
104
+ let match = result[i];
105
+
106
+ if (!match) continue;
107
+
108
+ if (match === args[0]) {
109
+ // There is a case where the match
110
+ // is the whole original string in which case the value here is
111
+ // externalized and we need to make sure to track
112
+ // the non-exterrnalized one
113
+ match = strInfo.value;
114
+ }
115
+
116
+ const startIdx = strInfo.value.indexOf(match, searchIdx);
117
+ const event = createPropagationEventForMatch({
118
+ strInfo,
119
+ startIdx,
120
+ match,
121
+ untrackedResult,
122
+ metadata
123
+ });
124
+
125
+ if (i > 0) {
126
+ // The first match is the full match of the regex so we cannot
127
+ // increment the search index.
128
+ // Every match after the first will occur in order of appearance
129
+ // in the original string so we should increment the search index
130
+ searchIdx = startIdx + match.length;
131
+ }
132
+
133
+ if (!event) continue;
134
+
135
+ const { extern } = tracker.track(match, event);
136
+
137
+ if (extern) {
138
+ result[i] = extern;
139
+ }
140
+ }
141
+
142
+ if (result.groups) {
143
+ Object.keys(result.groups).forEach((key) => {
144
+ let res = result.groups[key];
145
+
146
+ if (!res) return;
147
+
148
+ if (res === args[0]) {
149
+ // There is a case where the match
150
+ // is the whole original string in which case the value here is
151
+ // externalized and we need to make sure to track
152
+ // the non-exterrnalized one
153
+ res = strInfo.value;
154
+ }
155
+
156
+ const startIdx = strInfo.value.indexOf(res, result.index);
157
+ const event = createPropagationEventForMatch({
158
+ strInfo,
159
+ startIdx,
160
+ match: res,
161
+ untrackedResult,
162
+ metadata
163
+ });
164
+
165
+ if (event) {
166
+ const { extern } = tracker.track(res, event);
167
+ if (extern) {
168
+ result.groups[key] = extern;
169
+ }
170
+ }
171
+ });
172
+ }
173
+ },
174
+ });
175
+ },
176
+ uninstall() {
177
+ RegExp.prototype.exec = patcher.unwrap(RegExp.prototype.exec);
178
+ },
179
+ });
180
+ };