@contrast/assess 1.9.0 → 1.11.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 (116) hide show
  1. package/lib/dataflow/event-factory.js +1 -1
  2. package/lib/dataflow/index.js +1 -1
  3. package/lib/dataflow/propagation/common.js +1 -1
  4. package/lib/dataflow/propagation/index.js +3 -1
  5. package/lib/dataflow/propagation/install/JSON/index.js +1 -1
  6. package/lib/dataflow/propagation/install/JSON/parse-fn.js +1 -1
  7. package/lib/dataflow/propagation/install/JSON/parse.js +1 -1
  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/buffer.js +1 -1
  11. package/lib/dataflow/propagation/install/contrast-methods/add.js +1 -1
  12. package/lib/dataflow/propagation/install/contrast-methods/index.js +1 -1
  13. package/lib/dataflow/propagation/install/contrast-methods/number.js +1 -1
  14. package/lib/dataflow/propagation/install/contrast-methods/string.js +1 -1
  15. package/lib/dataflow/propagation/install/contrast-methods/tag.js +1 -1
  16. package/lib/dataflow/propagation/install/decode-uri-component.js +1 -1
  17. package/lib/dataflow/propagation/install/ejs/escape-xml.js +1 -1
  18. package/lib/dataflow/propagation/install/ejs/index.js +1 -1
  19. package/lib/dataflow/propagation/install/encode-uri-component.js +1 -1
  20. package/lib/dataflow/propagation/install/escape-html.js +1 -1
  21. package/lib/dataflow/propagation/install/escape.js +1 -1
  22. package/lib/dataflow/propagation/install/handlebars-utils-escape-expression.js +1 -1
  23. package/lib/dataflow/propagation/install/isnumeric-0.js +1 -1
  24. package/lib/dataflow/propagation/install/mongoose/common.js +20 -0
  25. package/lib/dataflow/propagation/install/mongoose/index.js +5 -9
  26. package/lib/dataflow/propagation/install/mongoose/schema-map.js +149 -0
  27. package/lib/dataflow/propagation/install/mongoose/schema-mixed.js +162 -0
  28. package/lib/dataflow/propagation/install/mongoose/schema-string.js +91 -37
  29. package/lib/dataflow/propagation/install/mysql-connection-escape.js +1 -1
  30. package/lib/dataflow/propagation/install/parse-int.js +1 -1
  31. package/lib/dataflow/propagation/install/path/basename.js +124 -0
  32. package/lib/dataflow/propagation/install/path/common.js +176 -0
  33. package/lib/dataflow/propagation/install/path/index.js +32 -0
  34. package/lib/dataflow/propagation/install/path/join-and-resolve.js +141 -0
  35. package/lib/dataflow/propagation/install/path/normalize.js +123 -0
  36. package/lib/dataflow/propagation/install/pug/index.js +1 -1
  37. package/lib/dataflow/propagation/install/pug-runtime-escape.js +1 -1
  38. package/lib/dataflow/propagation/install/querystring/index.js +1 -1
  39. package/lib/dataflow/propagation/install/querystring/parse.js +2 -2
  40. package/lib/dataflow/propagation/install/reg-exp-prototype-exec.js +182 -0
  41. package/lib/dataflow/propagation/install/sequelize.js +1 -1
  42. package/lib/dataflow/propagation/install/sql-template-strings.js +1 -1
  43. package/lib/dataflow/propagation/install/string/concat.js +1 -1
  44. package/lib/dataflow/propagation/install/string/format-methods.js +1 -1
  45. package/lib/dataflow/propagation/install/string/html-methods.js +1 -1
  46. package/lib/dataflow/propagation/install/string/index.js +65 -1
  47. package/lib/dataflow/propagation/install/string/match-all.js +236 -0
  48. package/lib/dataflow/propagation/install/string/match.js +84 -38
  49. package/lib/dataflow/propagation/install/string/replace.js +3 -3
  50. package/lib/dataflow/propagation/install/string/slice.js +2 -2
  51. package/lib/dataflow/propagation/install/string/split.js +2 -2
  52. package/lib/dataflow/propagation/install/string/substring.js +3 -3
  53. package/lib/dataflow/propagation/install/string/trim.js +2 -2
  54. package/lib/dataflow/propagation/install/unescape.js +1 -1
  55. package/lib/dataflow/propagation/install/url/domain-parsers.js +1 -1
  56. package/lib/dataflow/propagation/install/url/index.js +4 -1
  57. package/lib/dataflow/propagation/install/url/parse.js +131 -0
  58. package/lib/dataflow/propagation/install/url/searchParams.js +133 -0
  59. package/lib/dataflow/propagation/install/url/url.js +185 -0
  60. package/lib/dataflow/propagation/install/validator/hooks.js +1 -1
  61. package/lib/dataflow/propagation/install/validator/index.js +1 -1
  62. package/lib/dataflow/propagation/install/validator/methods.js +1 -1
  63. package/lib/dataflow/sinks/common.js +1 -1
  64. package/lib/dataflow/sinks/index.js +9 -5
  65. package/lib/dataflow/sinks/install/child-process.js +1 -1
  66. package/lib/dataflow/sinks/install/eval.js +138 -0
  67. package/lib/dataflow/sinks/install/express/index.js +1 -1
  68. package/lib/dataflow/sinks/install/express/unvalidated-redirect.js +2 -2
  69. package/lib/dataflow/sinks/install/fastify/index.js +1 -1
  70. package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.js +3 -2
  71. package/lib/dataflow/sinks/install/fs.js +4 -4
  72. package/lib/dataflow/sinks/install/function.js +160 -0
  73. package/lib/dataflow/sinks/install/http/index.js +31 -0
  74. package/lib/dataflow/sinks/install/http/request.js +152 -0
  75. package/lib/dataflow/sinks/install/{http.js → http/server-response.js} +3 -3
  76. package/lib/dataflow/sinks/install/koa/index.js +1 -1
  77. package/lib/dataflow/sinks/install/koa/unvalidated-redirect.js +2 -2
  78. package/lib/dataflow/sinks/install/marsdb.js +1 -1
  79. package/lib/dataflow/sinks/install/mongodb.js +34 -46
  80. package/lib/dataflow/sinks/install/mssql.js +45 -32
  81. package/lib/dataflow/sinks/install/mysql.js +1 -1
  82. package/lib/dataflow/sinks/install/postgres.js +1 -1
  83. package/lib/dataflow/sinks/install/sequelize.js +1 -1
  84. package/lib/dataflow/sinks/install/sqlite3.js +1 -1
  85. package/lib/dataflow/sinks/install/vm.js +276 -0
  86. package/lib/dataflow/sources/common.js +1 -1
  87. package/lib/dataflow/sources/handler.js +1 -1
  88. package/lib/dataflow/sources/index.js +1 -1
  89. package/lib/dataflow/sources/install/body-parser1.js +1 -1
  90. package/lib/dataflow/sources/install/busboy1.js +1 -1
  91. package/lib/dataflow/sources/install/cookie-parser1.js +1 -1
  92. package/lib/dataflow/sources/install/express/index.js +1 -1
  93. package/lib/dataflow/sources/install/express/params.js +1 -1
  94. package/lib/dataflow/sources/install/express/parsedUrl.js +1 -1
  95. package/lib/dataflow/sources/install/fastify/fastify.js +1 -1
  96. package/lib/dataflow/sources/install/fastify/index.js +1 -1
  97. package/lib/dataflow/sources/install/formidable1.js +1 -1
  98. package/lib/dataflow/sources/install/http.js +1 -1
  99. package/lib/dataflow/sources/install/koa/index.js +1 -1
  100. package/lib/dataflow/sources/install/koa/koa-bodyparsers.js +1 -1
  101. package/lib/dataflow/sources/install/koa/koa-routers.js +1 -1
  102. package/lib/dataflow/sources/install/koa/koa2.js +1 -1
  103. package/lib/dataflow/sources/install/qs6.js +1 -1
  104. package/lib/dataflow/sources/install/querystring.js +1 -1
  105. package/lib/dataflow/tag-utils.js +71 -2
  106. package/lib/dataflow/tracker.js +1 -1
  107. package/lib/dataflow/utils/is-safe-content-type.js +1 -1
  108. package/lib/dataflow/utils/is-vulnerable.js +1 -1
  109. package/lib/index.js +1 -1
  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/index.js +1 -1
  115. package/lib/session-configuration/install/http.js +1 -1
  116. package/package.json +2 -2
@@ -0,0 +1,123 @@
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 { isString } = require('@contrast/common');
18
+ const { patchType } = require('../../common');
19
+ const {
20
+ createArgTagsInResult,
21
+ excludeExtensionDotFromTags,
22
+ } = require('./common');
23
+
24
+ module.exports = function(core) {
25
+ const {
26
+ depHooks,
27
+ patcher,
28
+ scopes: { sources, instrumentation },
29
+ assess: {
30
+ dataflow: {
31
+ tracker,
32
+ eventFactory: { createPropagationEvent },
33
+ },
34
+ },
35
+ } = core;
36
+
37
+ core.assess.dataflow.propagation.pathInstrumentation.normalize = {
38
+ install() {
39
+ depHooks.resolve({ name: 'path' }, (path) => {
40
+ for (const os of ['posix', 'win32']) {
41
+ const isWin32 = os === 'win32';
42
+
43
+ patcher.patch(path[os], 'normalize', {
44
+ name: `path.${os}.normalize`,
45
+ patchType,
46
+ post(data) {
47
+ const { args, result, name, hooked, orig } = data;
48
+ if (
49
+ !result ||
50
+ !sources.getStore()?.assess ||
51
+ instrumentation.isLocked()
52
+ )
53
+ return;
54
+
55
+ const pathStr = args[0];
56
+
57
+ if (!pathStr || !isString(pathStr)) return;
58
+
59
+ const strInfo = tracker.getData(pathStr);
60
+
61
+ if (!strInfo) return;
62
+
63
+ let { newTags } = createArgTagsInResult({
64
+ argStr: pathStr,
65
+ argTags: strInfo?.tags || {},
66
+ result,
67
+ lastIndex: result.length,
68
+ isWin32,
69
+ });
70
+
71
+ newTags = excludeExtensionDotFromTags({
72
+ result,
73
+ tags: newTags,
74
+ isWin32,
75
+ });
76
+
77
+ const event = createPropagationEvent({
78
+ name,
79
+ moduleName: 'path',
80
+ methodName: 'normalize',
81
+ context: `path.normalize('${strInfo.value}')`,
82
+ history: [strInfo],
83
+ object: {
84
+ value: 'path',
85
+ isTracked: false,
86
+ },
87
+ args: [
88
+ {
89
+ value: strInfo.value,
90
+ tracked: true,
91
+ },
92
+ ],
93
+ result: {
94
+ value: result,
95
+ tracked: true,
96
+ },
97
+ tags: newTags,
98
+ source: 'P',
99
+ target: 'R',
100
+ stacktraceOpts: {
101
+ constructorOpt: hooked,
102
+ prependFrames: [orig],
103
+ },
104
+ });
105
+
106
+ if (!event) {
107
+ return;
108
+ }
109
+
110
+ const { extern } = tracker.track(result, event);
111
+
112
+ if (extern) {
113
+ data.result = extern;
114
+ }
115
+ },
116
+ });
117
+ }
118
+ });
119
+ },
120
+ };
121
+
122
+ return core.assess.dataflow.propagation.pathInstrumentation.normalize;
123
+ };
@@ -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
 
@@ -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
 
@@ -42,7 +42,7 @@ module.exports = function(core) {
42
42
  function unescapeWrapper(part) {
43
43
  let result = unescape(part);
44
44
  const start = input.indexOf(part, data.idx);
45
- const tagRanges = createSubsetTags(trackingData.tags, start, result.length - 1);
45
+ const tagRanges = createSubsetTags(trackingData.tags, start, result.length);
46
46
 
47
47
  if (!tagRanges) return result;
48
48
 
@@ -0,0 +1,182 @@
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
+ dataflow: {
27
+ tracker,
28
+ eventFactory: { createPropagationEvent },
29
+ },
30
+ },
31
+ } = core;
32
+
33
+ function createPropagationEventForMatch({
34
+ strInfo,
35
+ startIdx,
36
+ match,
37
+ untrackedResult,
38
+ metadata,
39
+ }) {
40
+ const tags = createSubsetTags(strInfo.tags, startIdx, match.length);
41
+ const { name, obj, hooked, orig } = metadata;
42
+
43
+ return createPropagationEvent({
44
+ name,
45
+ moduleName: 'RegExp',
46
+ methodName: 'prototype.exec',
47
+ context: `'${obj}'.exec('${strInfo.value}')`,
48
+ history: [strInfo],
49
+ object: {
50
+ value: obj,
51
+ tracked: false,
52
+ },
53
+ args: [
54
+ {
55
+ value: strInfo.value,
56
+ tracked: true,
57
+ },
58
+ ],
59
+ tags,
60
+ result: {
61
+ value: inspect(untrackedResult),
62
+ tracked: false,
63
+ },
64
+ stacktraceOpts: {
65
+ constructorOpt: hooked,
66
+ prependFrames: [orig],
67
+ },
68
+ source: 'P',
69
+ target: 'R',
70
+ });
71
+ }
72
+
73
+ return (core.assess.dataflow.propagation.regExpExec = {
74
+ install() {
75
+ const name = 'RegExp.prototype.exec';
76
+
77
+ patcher.patch(RegExp.prototype, 'exec', {
78
+ name,
79
+ patchType,
80
+ post(data) {
81
+ const { args, obj, result, hooked, orig } = data;
82
+
83
+ if (
84
+ !obj ||
85
+ !args[0] ||
86
+ !result?.length ||
87
+ !sources.getStore()?.assess ||
88
+ instrumentation.isLocked()
89
+ )
90
+ return;
91
+
92
+ const strInfo = tracker.getData(args[0]);
93
+
94
+ if (!strInfo) return;
95
+
96
+ const untrackedResult = [...result];
97
+ untrackedResult.groups = result.groups && { ...result.groups };
98
+ untrackedResult.input = strInfo.value;
99
+ untrackedResult.index = result.index;
100
+ result.indices && (untrackedResult.indices = result.indices);
101
+
102
+ let searchIdx = result.index;
103
+ const metadata = { name, obj, hooked, orig };
104
+
105
+ for (let i = 0; i < result.length; i++) {
106
+ let match = result[i];
107
+
108
+ if (!match) continue;
109
+
110
+ if (match === args[0]) {
111
+ // There is a case where the match
112
+ // is the whole original string in which case the value here is
113
+ // externalized and we need to make sure to track
114
+ // the non-exterrnalized one
115
+ match = strInfo.value;
116
+ }
117
+
118
+ const startIdx = strInfo.value.indexOf(match, searchIdx);
119
+ const event = createPropagationEventForMatch({
120
+ strInfo,
121
+ startIdx,
122
+ match,
123
+ untrackedResult,
124
+ metadata
125
+ });
126
+
127
+ if (i > 0) {
128
+ // The first match is the full match of the regex so we cannot
129
+ // increment the search index.
130
+ // Every match after the first will occur in order of appearance
131
+ // in the original string so we should increment the search index
132
+ searchIdx = startIdx + match.length;
133
+ }
134
+
135
+ if (!event) continue;
136
+
137
+ const { extern } = tracker.track(match, event);
138
+
139
+ if (extern) {
140
+ result[i] = extern;
141
+ }
142
+ }
143
+
144
+ if (result.groups) {
145
+ Object.keys(result.groups).forEach((key) => {
146
+ let res = result.groups[key];
147
+
148
+ if (!res) return;
149
+
150
+ if (res === args[0]) {
151
+ // There is a case where the match
152
+ // is the whole original string in which case the value here is
153
+ // externalized and we need to make sure to track
154
+ // the non-exterrnalized one
155
+ res = strInfo.value;
156
+ }
157
+
158
+ const startIdx = strInfo.value.indexOf(res, result.index);
159
+ const event = createPropagationEventForMatch({
160
+ strInfo,
161
+ startIdx,
162
+ match: res,
163
+ untrackedResult,
164
+ metadata
165
+ });
166
+
167
+ if (event) {
168
+ const { extern } = tracker.track(res, event);
169
+ if (extern) {
170
+ result.groups[key] = extern;
171
+ }
172
+ }
173
+ });
174
+ }
175
+ },
176
+ });
177
+ },
178
+ uninstall() {
179
+ RegExp.prototype.exec = patcher.unwrap(RegExp.prototype.exec);
180
+ },
181
+ });
182
+ };
@@ -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
 
@@ -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
 
@@ -16,21 +16,85 @@
16
16
  'use strict';
17
17
 
18
18
  const { callChildComponentMethodsSync } = require('@contrast/common');
19
+ const { inspect, split } = require('@contrast/common');
19
20
 
20
21
  module.exports = function(core) {
22
+ const {
23
+ scopes: { sources, instrumentation },
24
+ patcher,
25
+ assess: {
26
+ dataflow: { tracker, eventFactory: { createPropagationEvent } }
27
+ }
28
+ } = core;
21
29
  const stringInstrumentation = core.assess.dataflow.propagation.stringInstrumentation = {
22
30
  install() {
23
31
  callChildComponentMethodsSync(stringInstrumentation, 'install');
24
32
  },
25
33
  uninstall() {
26
34
  callChildComponentMethodsSync(stringInstrumentation, 'uninstall');
35
+ },
36
+ utils: {
37
+ patchCustomMatcher
27
38
  }
28
39
  };
29
40
 
41
+ function patchCustomMatcher(matcherFn, objInfo, methodArg, name, patchType) {
42
+ const [, , methodName] = split(name, '.');
43
+
44
+ return patcher.patch(matcherFn, {
45
+ name,
46
+ patchType,
47
+ pre(data) {
48
+ const { args: origArgs, hooked, orig } = data;
49
+ if (
50
+ !origArgs.length ||
51
+ typeof origArgs[0] !== 'string' ||
52
+ !sources.getStore()?.assess ||
53
+ instrumentation.isLocked()
54
+ ) return;
55
+
56
+ const args = [{
57
+ value: inspect(methodArg),
58
+ tracked: false
59
+ }];
60
+
61
+ const event = createPropagationEvent({
62
+ name,
63
+ moduleName: 'String',
64
+ methodName: `prototype.${methodName}`,
65
+ context: `'${objInfo.value}'.${methodName}(${args[0].value})`,
66
+ history: [objInfo],
67
+ object: {
68
+ value: objInfo.value,
69
+ tracked: true,
70
+ },
71
+ args,
72
+ tags: objInfo.tags,
73
+ result: undefined,
74
+ stacktraceOpts: {
75
+ constructorOpt: hooked,
76
+ prependFrames: [orig]
77
+ },
78
+ source: 'P',
79
+ target: 'P'
80
+ });
81
+
82
+ if (!event) return;
83
+
84
+ const { extern } = tracker.track(objInfo.value, event);
85
+
86
+ if (extern) {
87
+ origArgs[0] = extern;
88
+ }
89
+ }
90
+ });
91
+ }
92
+
30
93
  require('./concat')(core);
31
94
  require('./format-methods')(core);
32
95
  require('./html-methods')(core);
33
96
  require('./match')(core);
97
+ require('./match-all')(core);
34
98
  require('./replace')(core);
35
99
  require('./split')(core);
36
100
  require('./slice')(core);