@contrast/assess 1.16.1 → 1.18.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 (24) hide show
  1. package/lib/dataflow/propagation/index.js +4 -2
  2. package/lib/dataflow/propagation/install/array-prototype-join.js +1 -1
  3. package/lib/dataflow/propagation/install/encode-uri.js +110 -0
  4. package/lib/dataflow/propagation/install/escape-html.js +2 -2
  5. package/lib/dataflow/propagation/install/mustache-escape.js +101 -0
  6. package/lib/dataflow/propagation/install/path/extname.js +113 -0
  7. package/lib/dataflow/propagation/install/path/format.js +133 -0
  8. package/lib/dataflow/propagation/install/path/index.js +4 -0
  9. package/lib/dataflow/propagation/install/path/parse.js +117 -0
  10. package/lib/dataflow/propagation/install/path/toNamespacedPath.js +128 -0
  11. package/lib/dataflow/propagation/install/querystring/escape.js +97 -0
  12. package/lib/dataflow/propagation/install/querystring/index.js +2 -0
  13. package/lib/dataflow/propagation/install/querystring/stringify.js +158 -0
  14. package/lib/dataflow/propagation/install/sequelize/index.js +31 -0
  15. package/lib/dataflow/propagation/install/sequelize/query-generator.js +90 -0
  16. package/lib/dataflow/propagation/install/{sequelize.js → sequelize/sql-string.js} +3 -3
  17. package/lib/dataflow/propagation/install/string/concat.js +1 -3
  18. package/lib/dataflow/propagation/install/string/trim.js +2 -7
  19. package/lib/dataflow/propagation/install/util-format.js +120 -0
  20. package/lib/dataflow/sinks/index.js +1 -0
  21. package/lib/dataflow/sinks/install/node-serialize.js +103 -0
  22. package/lib/dataflow/tag-utils.js +42 -1
  23. package/package.json +1 -1
  24. package/lib/dataflow/propagation/install/encode-uri-component.js +0 -98
@@ -0,0 +1,128 @@
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
+ eventFactory: { createPropagationEvent },
31
+ dataflow: { tracker },
32
+ },
33
+ } = core;
34
+
35
+ core.assess.dataflow.propagation.pathInstrumentation.toNamespacedPath = {
36
+ install() {
37
+ depHooks.resolve({ name: 'path' }, (path) => {
38
+ for (const os of ['posix', 'win32']) {
39
+
40
+ patcher.patch(path[os], 'toNamespacedPath', {
41
+ name: `path.${os}.toNamespacedPath`,
42
+ patchType,
43
+ post(data) {
44
+ const { args, result, name, hooked, orig } = data;
45
+ if (
46
+ !result ||
47
+ !sources.getStore()?.assess ||
48
+ instrumentation.isLocked()
49
+ )
50
+ return;
51
+
52
+ const pathStr = args[0];
53
+
54
+ if (!pathStr || !isString(pathStr)) return;
55
+
56
+ const strInfo = tracker.getData(pathStr);
57
+ const resultInfo = tracker.getData(result);
58
+
59
+ if (!strInfo) return;
60
+
61
+ let { tags } = strInfo;
62
+ if (os === 'win32') {
63
+ let { newTags } = createArgTagsInResult({
64
+ argStr: pathStr,
65
+ argTags: strInfo?.tags || {},
66
+ result,
67
+ lastIndex: result.length,
68
+ isWin32: true,
69
+ });
70
+
71
+ newTags = excludeExtensionDotFromTags({
72
+ result,
73
+ tags: newTags,
74
+ isWin32: true
75
+ });
76
+
77
+ tags = newTags;
78
+ }
79
+
80
+ const event = tags && createPropagationEvent({
81
+ name,
82
+ moduleName: 'path',
83
+ methodName: 'toNamespacedPath',
84
+ context: `path.toNamespacedPath('${strInfo.value}')`,
85
+ history: [{ ...strInfo }],
86
+ object: {
87
+ value: 'path',
88
+ isTracked: false,
89
+ },
90
+ args: [
91
+ {
92
+ value: strInfo.value,
93
+ tracked: true,
94
+ },
95
+ ],
96
+ result: {
97
+ value: resultInfo ? resultInfo.value : result,
98
+ tracked: true,
99
+ },
100
+ tags,
101
+ source: 'P',
102
+ target: 'R',
103
+ stacktraceOpts: {
104
+ constructorOpt: hooked,
105
+ prependFrames: [orig],
106
+ },
107
+ });
108
+
109
+ if (!event) return;
110
+
111
+ if (resultInfo) {
112
+ Object.assign(resultInfo, event);
113
+ }
114
+
115
+ const { extern } = resultInfo || tracker.track(result, event);
116
+
117
+ if (extern) {
118
+ data.result = extern;
119
+ }
120
+ },
121
+ });
122
+ }
123
+ });
124
+ },
125
+ };
126
+
127
+ return core.assess.dataflow.propagation.pathInstrumentation.toNamespacedPath;
128
+ };
@@ -0,0 +1,97 @@
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 { inspect } = require('util');
18
+ const { DataflowTag: { URL_ENCODED } } = require('@contrast/common');
19
+ const { createFullLengthCopyTags } = require('../../../tag-utils');
20
+ const { patchType } = require('../../common');
21
+
22
+ module.exports = function(core) {
23
+ const {
24
+ assess: {
25
+ eventFactory: { createPropagationEvent },
26
+ dataflow: { tracker }
27
+ },
28
+ depHooks,
29
+ patcher,
30
+ scopes,
31
+ } = core;
32
+
33
+ return core.assess.dataflow.propagation.querystringInstrumentation.escape = {
34
+ install() {
35
+ depHooks.resolve({ name: 'querystring' }, (_export) => {
36
+ patcher.patch(_export, 'escape', {
37
+ name: 'querystring.escape',
38
+ patchType,
39
+ post(data) {
40
+ const [value] = data.args;
41
+ if (!value?.length || !data.result?.length) return value;
42
+
43
+ const strInfo = tracker.getData(value);
44
+ if (!strInfo) return;
45
+
46
+ const sourceContext = scopes.sources.getStore()?.assess;
47
+ if (!sourceContext) return;
48
+
49
+ let tags;
50
+ if (value !== data.result) {
51
+ tags = createFullLengthCopyTags({
52
+ ...strInfo.tags,
53
+ [URL_ENCODED]: [0, 0],
54
+ }, data.result.length);
55
+ } else {
56
+ tags = {
57
+ ...strInfo.tags,
58
+ [URL_ENCODED]: [0, data.result.length - 1],
59
+ };
60
+ }
61
+
62
+ const argVal = inspect(strInfo.value);
63
+ const event = createPropagationEvent({
64
+ args: [{ tracked: true, value: argVal }],
65
+ context: `querystring.escape(${argVal})`,
66
+ history: [strInfo],
67
+ moduleName: 'querystring',
68
+ methodName: 'escape',
69
+ object: { tracked: false, value: 'querystring' },
70
+ result: { tracked: true, value: data.result },
71
+ name: 'querystring.escape',
72
+ source: 'P',
73
+ tags,
74
+ target: 'R',
75
+ });
76
+
77
+ if (event) {
78
+ const retInfo = tracker.getData(data.result);
79
+ let extern;
80
+
81
+ // if no transformation took place this will happen
82
+ if (retInfo) {
83
+ extern = tracker.track(retInfo.value, event)?.extern;
84
+ } else {
85
+ extern = tracker.track(data.result, event)?.extern;
86
+ }
87
+
88
+ if (extern) data.result = extern;
89
+ }
90
+ },
91
+ });
92
+ });
93
+ },
94
+ uninstall() {
95
+ }
96
+ };
97
+ };
@@ -24,7 +24,9 @@ module.exports = function(core) {
24
24
  }
25
25
  };
26
26
 
27
+ require('./escape')(core);
27
28
  require('./parse')(core);
29
+ require('./stringify')(core);
28
30
 
29
31
  return querystringInstrumentation;
30
32
  };
@@ -0,0 +1,158 @@
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 querystring = require('querystring');
18
+ const { inspect } = require('util');
19
+ const { isString } = require('@contrast/common');
20
+ const utils = require('../../../tag-utils');
21
+ const { patchType } = require('../../common');
22
+
23
+ const moduleName = 'querystring';
24
+
25
+ module.exports = function(core) {
26
+ const {
27
+ assess: {
28
+ dataflow: { tracker },
29
+ eventFactory: { createPropagationEvent },
30
+ },
31
+ depHooks,
32
+ patcher,
33
+ scopes,
34
+ } = core;
35
+
36
+ /**
37
+ * Adds custom encoding function to capture key/value tags and history during stringification
38
+ */
39
+ function pre(data) {
40
+ const sourceContext = scopes.sources.getStore()?.assess;
41
+ if (!sourceContext) return;
42
+
43
+ const [input] = data.args;
44
+ const escape = typeof data.args[3]?.encodeURIComponent === 'function'
45
+ ? data.args[3].encodeURIComponent
46
+ : querystring.escape;
47
+
48
+ let [, sep, equals] = data.args;
49
+ // check this
50
+ if (!isString(sep)) sep = '&';
51
+ if (!isString(equals)) equals = '=';
52
+
53
+ let counter = 0;
54
+ let offset = 0;
55
+ const keyState = {
56
+ value: '',
57
+ lastCount: 0,
58
+ nextCount: 0,
59
+ strInfo: null,
60
+ };
61
+
62
+ // state for post hook
63
+ data._args = [...data.args];
64
+ data._history = new Set();
65
+ data._tags = {};
66
+
67
+ // todo NODE-2835: account for custom encoding functions to apply URL_ENCODED
68
+ function encodeURIComponent(...args) {
69
+ const [rawValue] = args;
70
+ const encodedValue = escape(args[0]);
71
+
72
+ if (counter === keyState.nextCount) {
73
+ keyState.value = encodedValue;
74
+ keyState.lastCount = keyState.nextCount;
75
+ keyState.nextCount = counter + (Array.isArray(input[rawValue]) ? input[rawValue].length + 1 : 2);
76
+ keyState.strInfo = tracker.getData(encodedValue);
77
+ } else {
78
+ const strInfo = tracker.getData(encodedValue);
79
+
80
+ // capture key tags and increment counter for [key]=
81
+ if (keyState.strInfo) {
82
+ data._tags = utils.createAppendTags(data._tags, keyState.strInfo.tags, offset);
83
+ data._history.add(keyState.strInfo);
84
+ }
85
+ offset += keyState.value.length + equals.length;
86
+
87
+ // capture value tags and increment counter for [value]&
88
+ if (strInfo) {
89
+ const tags = Object.create(null);
90
+ for (const tag of Object.keys(strInfo.tags)) {
91
+ tags[tag] = [0, encodedValue.length - 1];
92
+ }
93
+
94
+ data._tags = utils.createAppendTags(data._tags, tags, offset);
95
+ data._history.add(strInfo);
96
+ }
97
+ offset += encodedValue.length + sep.length;
98
+ }
99
+
100
+ counter++;
101
+
102
+ return encodedValue;
103
+ }
104
+
105
+ data.args[3] = { ...data.args[3], encodeURIComponent };
106
+ }
107
+
108
+ /**
109
+ * if history/tags were captured, build event and track result
110
+ */
111
+ function post(data) {
112
+ if (!data._history?.size) return;
113
+
114
+ const { name } = data;
115
+ const args = data._args.map((a, i) => ({ tracked: i === 0, value: inspect(a) }));
116
+ const argString = args.reduce((acc, arg, i, arr) => acc + arg.value + (i === arr.length - 1 ? '' : ', '), '');
117
+ const context = `${name}(${argString})`;
118
+ const event = createPropagationEvent({
119
+ context,
120
+ args,
121
+ name,
122
+ moduleName,
123
+ // since this is an alias, it is always 'stringify'
124
+ methodName: data.orig.name,
125
+ history: Array.from(data._history),
126
+ object: { tracked: false, value: moduleName },
127
+ result: { tracked: true, value: data.result },
128
+ source: 'P',
129
+ stacktraceOpts: {
130
+ constructorOpt: data.hooked,
131
+ prependFrames: [data.orig],
132
+ },
133
+ tags: data._tags,
134
+ target: 'R',
135
+ });
136
+
137
+ if (event) {
138
+ const { extern } = tracker.track(data.result, event);
139
+ if (extern) data.result = extern;
140
+ }
141
+ }
142
+
143
+ return core.assess.dataflow.propagation.querystringInstrumentation.stringify = {
144
+ install() {
145
+ depHooks.resolve({ name: moduleName }, (_export) => {
146
+ for (const methodName of ['encode', 'stringify']) {
147
+ patcher.patch(_export, methodName, {
148
+ name: `querystring.${methodName}`,
149
+ patchType,
150
+ pre,
151
+ post
152
+ });
153
+ }
154
+ });
155
+ },
156
+ uninstall() { }
157
+ };
158
+ };
@@ -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: {
@@ -43,7 +43,6 @@ module.exports = function(core) {
43
43
  if (!result || !sources.getStore()?.assess || instrumentation.isLocked()) return;
44
44
 
45
45
  const rInfo = tracker.getData(result);
46
-
47
46
  if (rInfo) {
48
47
  // this may happen w/ trackedStr.concat('') => trackedStr
49
48
  return;
@@ -77,7 +76,7 @@ module.exports = function(core) {
77
76
  name,
78
77
  moduleName: 'String',
79
78
  methodName: 'prototype.concat',
80
- context: `${inspect(objInfo?.value) || String(obj)}.concat(${join(argsData.map(d => d.value), ', ')})`,
79
+ context: `${inspect(objInfo?.value) || String(obj)}.concat(${inspect(join(argsData.map(d => d.value)), ', ')})`,
81
80
  object: {
82
81
  value: objInfo?.value || String(obj),
83
82
  tracked: !!objInfo
@@ -98,7 +97,6 @@ module.exports = function(core) {
98
97
  });
99
98
 
100
99
  if (!event) return;
101
-
102
100
  const { extern } = tracker.track(result, event);
103
101
 
104
102
  if (extern) {
@@ -51,15 +51,10 @@ module.exports = function(core) {
51
51
 
52
52
  const history = [objInfo];
53
53
  const start = presetStart || obj.indexOf(result);
54
- const newTags = {};
55
54
  const objTags = objInfo.tags || {};
56
- Object.assign(newTags, createSubsetTags(objTags, start, result.length));
55
+ const newTags = createSubsetTags(objTags, start, result.length);
57
56
 
58
- if (!newTags.untrusted) {
59
- return;
60
- }
61
-
62
- const event = createPropagationEvent({
57
+ const event = newTags && createPropagationEvent({
63
58
  name: `String.prototype.${methodName}`,
64
59
  moduleName: 'String',
65
60
  methodName: `prototype.${methodName}`,