@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
@@ -24,29 +24,31 @@ module.exports = function(core) {
24
24
  require('./install/ejs')(core);
25
25
  require('./install/mongoose')(core);
26
26
  require('./install/pug')(core);
27
+ require('./install/sequelize')(core);
27
28
  require('./install/string')(core);
28
29
  require('./install/url')(core);
29
30
  require('./install/validator')(core);
30
31
  require('./install/array-prototype-join')(core);
31
32
  require('./install/buffer')(core);
32
33
  require('./install/decode-uri-component')(core);
33
- require('./install/encode-uri-component')(core);
34
+ require('./install/encode-uri')(core);
34
35
  require('./install/escape-html')(core);
35
36
  require('./install/escape')(core);
36
37
  require('./install/handlebars-utils-escape-expression')(core);
37
38
  require('./install/isnumeric-0')(core);
39
+ require('./install/mustache-escape')(core);
38
40
  require('./install/mysql-connection-escape')(core);
39
41
  require('./install/parse-int')(core);
40
42
  require('./install/pug-runtime-escape')(core);
41
43
  require('./install/sql-template-strings')(core);
42
44
  require('./install/unescape')(core);
43
45
  require('./install/querystring')(core);
44
- require('./install/sequelize')(core);
45
46
  require('./install/JSON')(core);
46
47
  require('./install/path')(core);
47
48
  require('./install/reg-exp-prototype-exec')(core);
48
49
  require('./install/joi')(core);
49
50
  require('./install/send')(core);
51
+ require('./install/util-format')(core);
50
52
 
51
53
  propagation.install = function() {
52
54
  callChildComponentMethodsSync(propagation, 'install');
@@ -102,7 +102,7 @@ module.exports = function(core) {
102
102
  },
103
103
  args,
104
104
  tags: newTags,
105
- history: Array.from(history),
105
+ history: Array.from(history).map((el) => Object.assign({}, el)),
106
106
  source: delimiterInfo ? (history.size > 1 ? 'A' : 'P') : 'O',
107
107
  target: 'R',
108
108
  stacktraceOpts: {
@@ -0,0 +1,110 @@
1
+ /*
2
+ * Copyright: 2023 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
16
+ 'use strict';
17
+
18
+ const {
19
+ DataflowTag: { URL_ENCODED, WEAK_URL_ENCODED }
20
+ } = require('@contrast/common');
21
+ const {
22
+ createEscapeTagRanges
23
+ } = require('../../tag-utils');
24
+ const { patchType, createObjectLabel } = require('../common');
25
+
26
+ module.exports = function(core) {
27
+ const {
28
+ scopes: { sources, instrumentation },
29
+ patcher,
30
+ assess: {
31
+ eventFactory: { createPropagationEvent },
32
+ dataflow: { tracker }
33
+ }
34
+ } = core;
35
+
36
+ return core.assess.dataflow.propagation.encodeURI = {
37
+ install() {
38
+
39
+ [
40
+ {
41
+ methodName: 'encodeURIComponent',
42
+ tag: URL_ENCODED
43
+ },
44
+ {
45
+ methodName: 'encodeURI',
46
+ tag: WEAK_URL_ENCODED
47
+ }
48
+ ].forEach(({ methodName, tag }) => {
49
+ const name = `global.${methodName}`;
50
+ patcher.patch(global, methodName, {
51
+ name,
52
+ patchType,
53
+ post(data) {
54
+ const { args, result, hooked, orig } = data;
55
+ if (!result || !args[0] || !sources.getStore()?.assess || instrumentation.isLocked()) return;
56
+
57
+ const argInfo = tracker.getData(args[0]);
58
+
59
+ if (!argInfo) return;
60
+
61
+ // This native method seems to create a new string instance
62
+ // as even when it returns the argument without any change
63
+ // the result is not tracked, so we don't need to check for that
64
+ const history = [argInfo];
65
+ const newTags = createEscapeTagRanges(args[0], result, argInfo.tags);
66
+
67
+ newTags[tag] = [0, result.length - 1];
68
+
69
+ const event = createPropagationEvent({
70
+ name,
71
+ moduleName: 'global',
72
+ methodName,
73
+ context: `${methodName}('${argInfo.value}')`,
74
+ object: {
75
+ value: createObjectLabel('global'),
76
+ tracked: false
77
+ },
78
+ result: {
79
+ value: result,
80
+ tracked: true
81
+ },
82
+ args: [{ value: argInfo.value, tracked: true }],
83
+ tags: newTags,
84
+ history,
85
+ source: 'P',
86
+ target: 'R',
87
+ addedTags: [tag],
88
+ stacktraceOpts: {
89
+ constructorOpt: hooked,
90
+ prependFrames: [orig]
91
+ },
92
+ });
93
+
94
+ if (!event) return;
95
+
96
+ const { extern } = tracker.track(result, event);
97
+
98
+ if (extern) {
99
+ data.result = extern;
100
+ }
101
+ }
102
+ });
103
+ });
104
+ },
105
+ uninstall() {
106
+ global.encodeURI = patcher.unwrap(global.encodeURI);
107
+ global.encodeURIComponent = patcher.unwrap(global.encodeURIComponent);
108
+ },
109
+ };
110
+ };
@@ -19,7 +19,7 @@ const {
19
19
  DataflowTag: { HTML_ENCODED }
20
20
  } = require('@contrast/common');
21
21
  const {
22
- createFullLengthCopyTags
22
+ createEscapeTagRanges
23
23
  } = require('../../tag-utils');
24
24
  const { patchType } = require('../common');
25
25
 
@@ -52,7 +52,7 @@ module.exports = function(core) {
52
52
 
53
53
  const resultInfo = tracker.getData(result);
54
54
  const history = [{ ...argInfo }];
55
- const newTags = createFullLengthCopyTags(argInfo.tags, result.length);
55
+ const newTags = createEscapeTagRanges(args[0], result, argInfo.tags);
56
56
 
57
57
  newTags[HTML_ENCODED] = [0, result.length - 1];
58
58
 
@@ -0,0 +1,101 @@
1
+ /*
2
+ * Copyright: 2023 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
16
+ 'use strict';
17
+
18
+ const {
19
+ DataflowTag: { HTML_ENCODED }
20
+ } = require('@contrast/common');
21
+ const {
22
+ createEscapeTagRanges
23
+ } = require('../../tag-utils');
24
+ const { patchType } = require('../common');
25
+
26
+ module.exports = function(core) {
27
+ const {
28
+ scopes: { sources, instrumentation },
29
+ patcher,
30
+ depHooks,
31
+ assess: {
32
+ eventFactory: { createPropagationEvent },
33
+ dataflow: { tracker }
34
+ }
35
+ } = core;
36
+
37
+ return core.assess.dataflow.propagation.mustacheEscape = {
38
+ install() {
39
+ depHooks.resolve({ name: 'mustache' }, (mustache) => {
40
+ const name = 'mustache.escape';
41
+
42
+ return patcher.patch(mustache, 'escape', {
43
+ name,
44
+ patchType,
45
+ post(data) {
46
+ const { args, result, hooked, orig } = data;
47
+ if (!result || !args[0] || !sources.getStore()?.assess || instrumentation.isLocked()) return;
48
+
49
+ const argInfo = tracker.getData(args[0]);
50
+
51
+ if (!argInfo) return;
52
+
53
+ const resultInfo = tracker.getData(result);
54
+
55
+ const history = [{ ...argInfo }];
56
+ const newTags = createEscapeTagRanges(args[0], result, argInfo.tags);
57
+ newTags[HTML_ENCODED] = [0, result.length - 1];
58
+
59
+ const event = createPropagationEvent({
60
+ name,
61
+ moduleName: 'mustache',
62
+ methodName: 'escape',
63
+ context: `mustache.escape(${argInfo.value})`,
64
+ object: {
65
+ value: 'mustache',
66
+ tracked: false
67
+ },
68
+ result: {
69
+ value: resultInfo ? resultInfo.value : result,
70
+ tracked: true
71
+ },
72
+ args: [{ value: argInfo.value, tracked: true }],
73
+ tags: newTags,
74
+ history,
75
+ source: 'P',
76
+ target: 'R',
77
+ addedTags: [HTML_ENCODED],
78
+ stacktraceOpts: {
79
+ constructorOpt: hooked,
80
+ prependFrames: [orig]
81
+ },
82
+ });
83
+
84
+ if (!event) return;
85
+
86
+ if (resultInfo) {
87
+ Object.assign(resultInfo, event);
88
+ }
89
+
90
+ const { extern } = resultInfo || tracker.track(result, event);
91
+
92
+ if (extern) {
93
+ data.result = extern;
94
+ }
95
+ }
96
+ });
97
+ }
98
+ );
99
+ },
100
+ };
101
+ };
@@ -0,0 +1,113 @@
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 { patchType } = require('../../common');
18
+ const { isString } = require('@contrast/common');
19
+ const { createSubsetTags } = require('../../../tag-utils');
20
+ const {
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.extname = {
36
+ install() {
37
+ depHooks.resolve({ name: 'path' }, (path) => {
38
+ for (const os of ['posix', 'win32']) {
39
+ const isWin32 = os === 'win32';
40
+
41
+ patcher.patch(path[os], 'extname', {
42
+ name: `path.${os}.extname`,
43
+ patchType,
44
+ post(data) {
45
+ const { args, result, name, hooked, orig } = data;
46
+ if (
47
+ !result ||
48
+ !sources.getStore()?.assess ||
49
+ instrumentation.isLocked()
50
+ )
51
+ return;
52
+
53
+ const pathStr = args[0];
54
+
55
+ if (!pathStr || !isString(pathStr)) return;
56
+
57
+ const strInfo = tracker.getData(pathStr);
58
+
59
+ if (!strInfo) return;
60
+
61
+ let newTags = createSubsetTags(strInfo.tags, pathStr.indexOf(result), result.length);
62
+
63
+ newTags = excludeExtensionDotFromTags({
64
+ result,
65
+ tags: newTags,
66
+ isWin32
67
+ });
68
+
69
+ const event = newTags && createPropagationEvent({
70
+ name,
71
+ moduleName: 'path',
72
+ methodName: 'extname',
73
+ context: `path.extname('${strInfo.value}')`,
74
+ history: [strInfo],
75
+ object: {
76
+ value: 'path',
77
+ isTracked: false,
78
+ },
79
+ args: [
80
+ {
81
+ value: strInfo.value,
82
+ tracked: true,
83
+ },
84
+ ],
85
+ result: {
86
+ value: result,
87
+ tracked: true,
88
+ },
89
+ tags: newTags,
90
+ source: 'P',
91
+ target: 'R',
92
+ stacktraceOpts: {
93
+ constructorOpt: hooked,
94
+ prependFrames: [orig],
95
+ },
96
+ });
97
+
98
+ if (!event) return;
99
+
100
+ const { extern } = tracker.track(result, event);
101
+
102
+ if (extern) {
103
+ data.result = extern;
104
+ }
105
+ },
106
+ });
107
+ }
108
+ });
109
+ },
110
+ };
111
+
112
+ return core.assess.dataflow.propagation.pathInstrumentation.extname;
113
+ };
@@ -0,0 +1,133 @@
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 { patchType } = require('../../common');
18
+ const { isString, inspect } = require('@contrast/common');
19
+ const { createMergedTags } = require('../../../tag-utils');
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
+ eventFactory: { createPropagationEvent },
32
+ dataflow: { tracker },
33
+ },
34
+ } = core;
35
+
36
+ core.assess.dataflow.propagation.pathInstrumentation.format = {
37
+ install() {
38
+ depHooks.resolve({ name: 'path' }, (path) => {
39
+ for (const os of ['posix', 'win32']) {
40
+ const isWin32 = os === 'win32';
41
+
42
+ patcher.patch(path[os], 'format', {
43
+ name: `path.${os}.format`,
44
+ patchType,
45
+ post(data) {
46
+ const { args, result, name: patchName, hooked, orig } = data;
47
+ if (
48
+ !result ||
49
+ !sources.getStore()?.assess ||
50
+ instrumentation.isLocked()
51
+ )
52
+ return;
53
+
54
+ const pathProps = [];
55
+ const { dir, root, base, name, ext } = args[0];
56
+ base ? pathProps.push(base) : pathProps.push(ext, name);
57
+ pathProps.push(dir ?? root);
58
+
59
+ const eventArgs = [];
60
+ const history = [];
61
+ let fullTags = {};
62
+ let lastIndex = result.length;
63
+
64
+ for (const prop of pathProps) {
65
+ let newTags = {};
66
+ const propInfo = isString(prop) && tracker.getData(prop);
67
+ if (!propInfo) {
68
+ eventArgs.unshift({ value: prop, tracked: false });
69
+ continue;
70
+ }
71
+
72
+ eventArgs.unshift({ value: propInfo.value, tracked: true });
73
+ history.unshift(propInfo);
74
+
75
+ ({ newTags, lastIndex } = createArgTagsInResult({
76
+ argStr: prop,
77
+ argTags: propInfo.tags,
78
+ result,
79
+ lastIndex,
80
+ isWin32,
81
+ }));
82
+
83
+ fullTags = createMergedTags(fullTags, newTags);
84
+ }
85
+
86
+ fullTags = excludeExtensionDotFromTags({
87
+ result,
88
+ tags: fullTags,
89
+ isWin32,
90
+ });
91
+
92
+ if (!fullTags || !Object.keys(fullTags).length) return;
93
+
94
+ const event = createPropagationEvent({
95
+ name: patchName,
96
+ moduleName: 'path',
97
+ methodName: 'format',
98
+ context: `path.format('${inspect(...args)}')`,
99
+ history,
100
+ object: {
101
+ value: 'path',
102
+ isTracked: false,
103
+ },
104
+ args: eventArgs,
105
+ result: {
106
+ value: result,
107
+ tracked: true,
108
+ },
109
+ tags: fullTags,
110
+ source: 'P',
111
+ target: 'R',
112
+ stacktraceOpts: {
113
+ constructorOpt: hooked,
114
+ prependFrames: [orig],
115
+ },
116
+ });
117
+
118
+ if (!event) return;
119
+
120
+ const { extern } = tracker.track(result, event);
121
+
122
+ if (extern) {
123
+ data.result = extern;
124
+ }
125
+ },
126
+ });
127
+ }
128
+ });
129
+ },
130
+ };
131
+
132
+ return core.assess.dataflow.propagation.pathInstrumentation.format;
133
+ };
@@ -26,9 +26,13 @@ module.exports = function(core) {
26
26
 
27
27
  require('./basename')(core);
28
28
  require('./dirname')(core);
29
+ require('./extname')(core);
30
+ require('./format')(core);
29
31
  require('./join-and-resolve')(core);
30
32
  require('./normalize')(core);
33
+ require('./parse')(core);
31
34
  require('./relative')(core);
35
+ require('./toNamespacedPath')(core);
32
36
 
33
37
  return pathInstrumentation;
34
38
  };
@@ -0,0 +1,117 @@
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 { patchType } = require('../../common');
18
+ const { isString, inspect } = require('@contrast/common');
19
+ const { createSubsetTags } = require('../../../tag-utils');
20
+ const {
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.parse = {
36
+ install() {
37
+ depHooks.resolve({ name: 'path' }, (path) => {
38
+ for (const os of ['posix', 'win32']) {
39
+ const isWin32 = os === 'win32';
40
+
41
+ patcher.patch(path[os], 'parse', {
42
+ name: `path.${os}.parse`,
43
+ patchType,
44
+ post(data) {
45
+ const { args, result, name: patchName, hooked, orig } = data;
46
+ if (
47
+ !result ||
48
+ !sources.getStore()?.assess ||
49
+ instrumentation.isLocked()
50
+ )
51
+ return;
52
+
53
+ const [path] = args;
54
+
55
+ if (!path || !isString(path)) return;
56
+
57
+ const strInfo = tracker.getData(path);
58
+ if (!strInfo) return;
59
+
60
+ Object.keys(result).forEach((key) => {
61
+
62
+ const val = result[key];
63
+
64
+ let newTags = createSubsetTags(strInfo.tags, path.indexOf(val), val.length);
65
+
66
+ newTags = excludeExtensionDotFromTags({
67
+ result: val,
68
+ tags: newTags,
69
+ isWin32
70
+ });
71
+
72
+ const event = newTags && createPropagationEvent({
73
+ name: patchName,
74
+ moduleName: 'path',
75
+ methodName: 'parse',
76
+ context: `path.parse('${strInfo.value}')`,
77
+ history: [strInfo],
78
+ object: {
79
+ value: 'path',
80
+ isTracked: false,
81
+ },
82
+ args: [
83
+ {
84
+ value: strInfo.value,
85
+ tracked: true
86
+ }
87
+ ],
88
+ result: {
89
+ value: inspect(result),
90
+ tracked: true,
91
+ },
92
+ tags: newTags,
93
+ source: 'P',
94
+ target: 'R',
95
+ stacktraceOpts: {
96
+ constructorOpt: hooked,
97
+ prependFrames: [orig],
98
+ },
99
+ });
100
+
101
+ if (!event) return;
102
+
103
+ const { extern } = tracker.track(val, event);
104
+
105
+ if (extern) {
106
+ data.result[key] = extern;
107
+ }
108
+ });
109
+ },
110
+ });
111
+ }
112
+ });
113
+ },
114
+ };
115
+
116
+ return core.assess.dataflow.propagation.pathInstrumentation.parse;
117
+ };