@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
@@ -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,23 @@
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
31
  dataflow: {
28
32
  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
- );
33
+ eventFactory: { createPropagationEvent },
34
+ },
44
35
  },
45
- };
36
+ } = core;
46
37
 
47
38
  function patchEnum(SchemaString) {
48
39
  patcher.patch(SchemaString.prototype, 'enum', {
@@ -67,9 +58,9 @@ module.exports = function(core) {
67
58
  if (data.result) {
68
59
  tracker.untrack(data.args[0]);
69
60
  }
70
- }
61
+ },
71
62
  });
72
- }
63
+ },
73
64
  });
74
65
  }
75
66
 
@@ -81,16 +72,38 @@ module.exports = function(core) {
81
72
  patchType,
82
73
  pre(data) {
83
74
  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
- });
75
+ if (!value || typeof cb !== 'function' || !sources.getStore()?.assess) {
76
+ return;
92
77
  }
93
- }
78
+
79
+ const hasCustomValidator = data.obj.validators.some(
80
+ (validator) => validator.type === userDefinedType
81
+ );
82
+
83
+ data.args[1] = patcher.patch(cb, {
84
+ name,
85
+ patchType,
86
+ pre(data) {
87
+ const {
88
+ args: [err],
89
+ } = data;
90
+
91
+ if (!err) {
92
+ propagate(
93
+ name,
94
+ data.orig,
95
+ value,
96
+ trust_custom_validators
97
+ ? hasCustomValidator
98
+ : false
99
+ );
100
+ }
101
+
102
+ if (err && err.kind === userDefinedType)
103
+ propagate(name, data.orig, value, false);
104
+ },
105
+ });
106
+ },
94
107
  });
95
108
  }
96
109
 
@@ -99,16 +112,34 @@ module.exports = function(core) {
99
112
  patcher.patch(SchemaString.prototype, 'doValidateSync', {
100
113
  name,
101
114
  patchType,
102
- post({ orig, args: [value] }) {
103
- if (value?.length) {
104
- propagate(name, orig, value);
115
+ post(data) {
116
+ const {
117
+ args: [value],
118
+ result
119
+ } = data;
120
+
121
+ const hasCustomValidator = data.obj.validators.some(
122
+ (validator) => validator.type === userDefinedType
123
+ );
124
+
125
+ if (!result) {
126
+ propagate(
127
+ name,
128
+ data.orig,
129
+ value,
130
+ trust_custom_validators
131
+ ? hasCustomValidator
132
+ : false
133
+ );
105
134
  }
106
- }
107
- });
108
135
 
136
+ if (result && result.kind === userDefinedType)
137
+ propagate(name, data.orig, value, false);
138
+ },
139
+ });
109
140
  }
110
141
 
111
- function propagate(name, orig, value) {
142
+ function propagate(name, orig, value, customValidated = false) {
112
143
  const strInfo = tracker.getData(value);
113
144
  if (!strInfo) return;
114
145
 
@@ -116,7 +147,10 @@ module.exports = function(core) {
116
147
  // copy because we mutate the metadata value inline
117
148
  const history = [{ ...strInfo }];
118
149
  const event = createPropagationEvent({
119
- addedTags: [DataflowTag.STRING_TYPE_CHECKED],
150
+ addedTags: [
151
+ DataflowTag.STRING_TYPE_CHECKED,
152
+ customValidated ? DataflowTag.CUSTOM_VALIDATED : null,
153
+ ].filter(Boolean),
120
154
  name,
121
155
  moduleName: 'mongoose',
122
156
  methodName,
@@ -134,11 +168,14 @@ module.exports = function(core) {
134
168
  tags: {
135
169
  ...strInfo.tags,
136
170
  [DataflowTag.STRING_TYPE_CHECKED]: [0, value.length - 1],
171
+ ...(customValidated
172
+ ? { [DataflowTag.CUSTOM_VALIDATED]: [0, value.length - 1] }
173
+ : {}),
137
174
  },
138
175
  target: 'P0',
139
176
  stacktraceOpts: {
140
177
  prependFrames: [orig],
141
- }
178
+ },
142
179
  });
143
180
 
144
181
  if (!event) {
@@ -153,4 +190,21 @@ module.exports = function(core) {
153
190
 
154
191
  Object.assign(strInfo, event);
155
192
  }
193
+
194
+ return core.assess.dataflow.propagation.mongooseInstrumentation.schemaString = {
195
+ install() {
196
+ depHooks.resolve(
197
+ {
198
+ name: 'mongoose',
199
+ file: 'lib/schema/string.js',
200
+ version: '>=6.0.0',
201
+ },
202
+ (SchemaString) => {
203
+ patchEnum(SchemaString);
204
+ patchDoValidate(SchemaString);
205
+ patchDoValidateSync(SchemaString);
206
+ }
207
+ );
208
+ },
209
+ };
156
210
  };
@@ -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
 
@@ -0,0 +1,124 @@
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, join } = require('@contrast/common');
18
+ const { patchType } = require('../../common');
19
+ const {
20
+ excludeExtensionDotFromTags,
21
+ createBasenameTagsInResult,
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.basename = {
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], 'basename', {
44
+ name: `path.${os}.basename`,
45
+ patchType,
46
+ post(data) {
47
+ const { args: origArgs, result, name, hooked, orig } = data;
48
+ if (
49
+ !result ||
50
+ !sources.getStore()?.assess ||
51
+ instrumentation.isLocked()
52
+ )
53
+ return;
54
+
55
+ const [pathStr, suffixStr] = origArgs;
56
+
57
+ if (!pathStr || !isString(pathStr)) return;
58
+
59
+ const strInfo = tracker.getData(pathStr);
60
+
61
+ if (!strInfo) return;
62
+
63
+ const suffixInfo = tracker.getData(suffixStr);
64
+ const args = [
65
+ { value: strInfo.value, tracked: true },
66
+ suffixStr && { value: suffixInfo ? suffixInfo.value : suffixStr, tracked: !!suffixInfo }
67
+ ].filter(Boolean);
68
+
69
+ let tags = createBasenameTagsInResult({
70
+ argStr: strInfo.value,
71
+ argTags: strInfo.tags,
72
+ suffixStr,
73
+ result,
74
+ isWin32
75
+ });
76
+
77
+ tags = excludeExtensionDotFromTags({
78
+ result,
79
+ tags,
80
+ isWin32,
81
+ });
82
+
83
+ const event = tags && createPropagationEvent({
84
+ name,
85
+ moduleName: 'path',
86
+ methodName: 'basename',
87
+ context: `path.basename(${join(args.map(a => `'${a.value}'`))})`,
88
+ history: [strInfo],
89
+ object: {
90
+ value: 'path',
91
+ isTracked: false,
92
+ },
93
+ args,
94
+ result: {
95
+ value: result,
96
+ tracked: true,
97
+ },
98
+ tags,
99
+ source: 'P',
100
+ target: 'R',
101
+ stacktraceOpts: {
102
+ constructorOpt: hooked,
103
+ prependFrames: [orig],
104
+ },
105
+ });
106
+
107
+ if (!event) {
108
+ return;
109
+ }
110
+
111
+ const { extern } = tracker.track(result, event);
112
+
113
+ if (extern) {
114
+ data.result = extern;
115
+ }
116
+ },
117
+ });
118
+ }
119
+ });
120
+ },
121
+ };
122
+
123
+ return core.assess.dataflow.propagation.pathInstrumentation.basename;
124
+ };
@@ -0,0 +1,176 @@
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 { matchAll, substring, replace } = require('@contrast/common');
19
+ const {
20
+ createSubsetTags,
21
+ createAppendTags,
22
+ createMergedTags,
23
+ createTagsWithExclusion,
24
+ } = require('../../../tag-utils');
25
+
26
+ const posixRegExp = /(?:^|\/)([^/]+?)(?=(?:\/|$))/g;
27
+ // Windows accepts both '/' and '\' as path separators
28
+ const win32RegExp = /(?:^|\/|\\)([^/\\]+?)(?=(?:\/|\\|$))/g;
29
+
30
+ function createBasenameTagsInResult({
31
+ argStr,
32
+ argTags,
33
+ suffixStr,
34
+ result,
35
+ isWin32,
36
+ }) {
37
+ const segments = Array.from(
38
+ matchAll(argStr, isWin32 ? win32RegExp : posixRegExp)
39
+ );
40
+ const basename = segments[segments.length - 1][1];
41
+ const isExtensionRemoved = suffixStr && basename.includes(suffixStr);
42
+
43
+ const startIdx = isExtensionRemoved
44
+ ? argStr.lastIndexOf(`${result}${suffixStr}`)
45
+ : argStr.lastIndexOf(result);
46
+
47
+ return createSubsetTags(argTags, startIdx, result.length);
48
+ }
49
+
50
+ /**
51
+ * createArgTagsInResult()
52
+ *
53
+ * "Transfers" the tags from a path method argument to the result.
54
+ * This method needs to be called on all arguments in reverse order
55
+ *
56
+ * @param {Object} input
57
+ * @param {String} input.argStr the argument string value
58
+ * @param {Object} input.argTags the tags object of the argument
59
+ * @param {Object} input.argTags the tags object of the argument
60
+ * @param {String} input.result the result string value
61
+ * @param {Number} input.lastIndex the index from wich we should look
62
+ * for (backwards) for a segment match usually
63
+ * starting with the argument length
64
+ * @param {Boolean} input.isWin32 a boolean for the platform
65
+ */
66
+ function createArgTagsInResult({
67
+ argStr,
68
+ argTags,
69
+ result,
70
+ lastIndex,
71
+ isWin32,
72
+ }) {
73
+ let newTags = {};
74
+ let tempTags = Object.assign({}, argTags);
75
+
76
+ // The matched segments are going to be the segments between
77
+ // the separators eventually including
78
+ // only 1 path separator at the start
79
+ const matchedSegments = Array.from(
80
+ matchAll(argStr, isWin32 ? win32RegExp : posixRegExp)
81
+ ).reverse();
82
+
83
+ for (let i = 0; i < matchedSegments.length; i++) {
84
+ if (lastIndex < 0) break;
85
+ const [match, segment] = matchedSegments[i];
86
+ let { index: startIdx } = matchedSegments[i];
87
+ if (
88
+ ['.', '..'].includes(segment) &&
89
+ result.substring(lastIndex - segment.length, lastIndex + 1) !== segment
90
+ )
91
+ continue;
92
+ let idxInResult = result.lastIndexOf(segment, lastIndex);
93
+
94
+ if (idxInResult === -1) continue;
95
+
96
+ /*
97
+ * Up to the call to `createSubsetTags` the logic is
98
+ * mainly accounting for two things:
99
+ * - eventual separators that are transfered to the result
100
+ * - Windows replacing '/' with '\' thus making it non-user supplied
101
+ * and required to be excluded from the tags
102
+ */
103
+ const replacedSeparatorsIdxs = [];
104
+ const previousSegmentIdx =
105
+ i === matchedSegments.length - 1 ? 0 : matchedSegments[i + 1].index;
106
+ const previousSegmentLength =
107
+ i === matchedSegments.length - 1 ? 0 : matchedSegments[i + 1][0].length;
108
+ const segmentStartIdx = startIdx + match.length - segment.length;
109
+ const separators =
110
+ substring(
111
+ argStr,
112
+ previousSegmentIdx + previousSegmentLength,
113
+ segmentStartIdx
114
+ ) || '';
115
+ const separatorsInResult = isWin32
116
+ ? replace(separators, /\//g, (_match, idx) => {
117
+ replacedSeparatorsIdxs.push(startIdx - idx);
118
+
119
+ return '\\';
120
+ })
121
+ : separators;
122
+ let foundInResultLength = segment.length;
123
+
124
+ let sepIdxInResult;
125
+ const separatorsLastIdx = separators.length - 1;
126
+ for (let j = separatorsLastIdx; j >= 0; j--) {
127
+ if (j === separators.length - 1) {
128
+ sepIdxInResult = idxInResult + j - separators.length;
129
+ } else {
130
+ --sepIdxInResult;
131
+ }
132
+ if (result[sepIdxInResult] === separatorsInResult[j]) {
133
+ idxInResult--;
134
+ foundInResultLength++;
135
+ } else if (j === separatorsLastIdx) {
136
+ startIdx++;
137
+ }
138
+ }
139
+
140
+ lastIndex = idxInResult - 1;
141
+ if (replacedSeparatorsIdxs.length) {
142
+ replacedSeparatorsIdxs.forEach((idx) => {
143
+ tempTags = createTagsWithExclusion(tempTags, [idx, idx]);
144
+ });
145
+ }
146
+
147
+ const segmentTags = createSubsetTags(
148
+ tempTags,
149
+ startIdx,
150
+ foundInResultLength
151
+ );
152
+
153
+ const tagsInResult = createAppendTags({}, segmentTags, idxInResult);
154
+ newTags = createMergedTags(newTags, tagsInResult);
155
+ }
156
+
157
+ return {
158
+ newTags,
159
+ lastIndex,
160
+ };
161
+ }
162
+
163
+ function excludeExtensionDotFromTags({ result, tags, isWin32 }) {
164
+ const extensionDotIdx = result.lastIndexOf('.');
165
+ const separatorLastIdx = result.lastIndexOf(isWin32 ? '\\' : '/');
166
+
167
+ return extensionDotIdx > separatorLastIdx
168
+ ? createTagsWithExclusion(tags, [extensionDotIdx, extensionDotIdx])
169
+ : tags;
170
+ }
171
+
172
+ module.exports = {
173
+ createBasenameTagsInResult,
174
+ createArgTagsInResult,
175
+ excludeExtensionDotFromTags,
176
+ };
@@ -0,0 +1,32 @@
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 pathInstrumentation = core.assess.dataflow.propagation.pathInstrumentation = {
22
+ install() {
23
+ callChildComponentMethodsSync(pathInstrumentation, 'install');
24
+ },
25
+ };
26
+
27
+ require('./basename')(core);
28
+ require('./normalize')(core);
29
+ require('./join-and-resolve')(core);
30
+
31
+ return pathInstrumentation;
32
+ };
@@ -0,0 +1,141 @@
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, join } = require('@contrast/common');
18
+ const { createMergedTags } = require('../../../tag-utils');
19
+ const { patchType } = require('../../common');
20
+ const {
21
+ createArgTagsInResult,
22
+ excludeExtensionDotFromTags,
23
+ } = require('./common');
24
+
25
+ module.exports = function(core) {
26
+ const {
27
+ depHooks,
28
+ patcher,
29
+ scopes: { sources, instrumentation },
30
+ assess: {
31
+ dataflow: {
32
+ tracker,
33
+ eventFactory: { createPropagationEvent },
34
+ },
35
+ },
36
+ } = core;
37
+
38
+ core.assess.dataflow.propagation.pathInstrumentation.joinAndResolve = {
39
+ install() {
40
+ depHooks.resolve({ name: 'path' }, (path) => {
41
+ for (const method of ['join', 'resolve']) {
42
+ for (const os of ['posix', 'win32']) {
43
+ const name = `path.${os}.${method}`;
44
+ const isWin32 = os === 'win32';
45
+
46
+ patcher.patch(path[os], method, {
47
+ name,
48
+ patchType,
49
+ post(data) {
50
+ const { args: origArgs, result, hooked, orig } = data;
51
+ if (
52
+ !result ||
53
+ !sources.getStore()?.assess ||
54
+ instrumentation.isLocked()
55
+ )
56
+ return;
57
+
58
+ const pathSegments = [...origArgs].reverse();
59
+ const args = [];
60
+ const history = [];
61
+
62
+ if (!pathSegments.length) return;
63
+
64
+ let fullTags = {};
65
+ let lastIndex = result.length;
66
+
67
+ for (const segment of pathSegments) {
68
+ let newTags = {};
69
+ const argInfo = isString(segment) && tracker.getData(segment);
70
+
71
+ if (!argInfo) {
72
+ args.unshift({ value: segment, tracked: false });
73
+ continue;
74
+ }
75
+
76
+ args.unshift({ value: argInfo.value, tracked: true });
77
+ history.unshift(argInfo);
78
+
79
+ ({ newTags, lastIndex } = createArgTagsInResult({
80
+ argStr: segment,
81
+ argTags: argInfo.tags,
82
+ result,
83
+ lastIndex,
84
+ isWin32,
85
+ }));
86
+
87
+ fullTags = createMergedTags(fullTags, newTags);
88
+ }
89
+
90
+ fullTags = excludeExtensionDotFromTags({
91
+ result,
92
+ tags: fullTags,
93
+ isWin32,
94
+ });
95
+
96
+ if (!fullTags || !Object.keys(fullTags).length) return;
97
+
98
+ const event = createPropagationEvent({
99
+ name,
100
+ moduleName: 'path',
101
+ methodName: method,
102
+ context: `path.${method}(${join(args
103
+ .map((a) => `'${a.value}'`), ',')})`,
104
+ history,
105
+ object: {
106
+ value: 'path',
107
+ isTracked: false,
108
+ },
109
+ args,
110
+ result: {
111
+ value: result,
112
+ tracked: true,
113
+ },
114
+ tags: fullTags,
115
+ source: 'P',
116
+ target: 'R',
117
+ stacktraceOpts: {
118
+ constructorOpt: hooked,
119
+ prependFrames: [orig],
120
+ },
121
+ });
122
+
123
+ if (!event) {
124
+ return;
125
+ }
126
+
127
+ const { extern } = tracker.track(result, event);
128
+
129
+ if (extern) {
130
+ data.result = extern;
131
+ }
132
+ },
133
+ });
134
+ }
135
+ }
136
+ });
137
+ },
138
+ };
139
+
140
+ return core.assess.dataflow.propagation.pathInstrumentation.joinAndResolve;
141
+ };