@contrast/assess 1.16.1 → 1.17.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.
@@ -30,11 +30,12 @@ module.exports = function(core) {
30
30
  require('./install/array-prototype-join')(core);
31
31
  require('./install/buffer')(core);
32
32
  require('./install/decode-uri-component')(core);
33
- require('./install/encode-uri-component')(core);
33
+ require('./install/encode-uri')(core);
34
34
  require('./install/escape-html')(core);
35
35
  require('./install/escape')(core);
36
36
  require('./install/handlebars-utils-escape-expression')(core);
37
37
  require('./install/isnumeric-0')(core);
38
+ require('./install/mustache-escape')(core);
38
39
  require('./install/mysql-connection-escape')(core);
39
40
  require('./install/parse-int')(core);
40
41
  require('./install/pug-runtime-escape')(core);
@@ -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
+ };
@@ -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
+ };
@@ -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}`,
@@ -24,6 +24,12 @@ function ensureObject(tags) {
24
24
  }
25
25
 
26
26
  function atomicAppend(firstTagRanges, secondTagRanges, offset) {
27
+ if (!firstTagRanges.length) {
28
+ const ret = secondTagRanges.map((v) => v + offset);
29
+ return ret;
30
+ }
31
+
32
+
27
33
  const newTagRanges = [...firstTagRanges];
28
34
 
29
35
  for (let i = 0; i < secondTagRanges.length; i++) {
@@ -271,6 +277,40 @@ function createAdjustedQueryTags(path, tags, value, argString) {
271
277
  return idx > 0 ? createAppendTags([], tags, idx) : [...tags];
272
278
  }
273
279
 
280
+ function createEscapeTagRanges(input, result, tags) {
281
+ const inputArr = input.split('');
282
+ const escapedArr = result.split('');
283
+ const overlap = inputArr.filter((x) => {
284
+ if (escapedArr.includes(x)) {
285
+ return x;
286
+ }
287
+ });
288
+ if (overlap.length === 0) {
289
+ return [];
290
+ }
291
+ const newTagRanges = [];
292
+ let firstIndex = escapedArr.indexOf(overlap[0]);
293
+ let currIndex = firstIndex;
294
+ let nextIndex;
295
+ for (let i = 1; i < overlap.length; i++) {
296
+ nextIndex = escapedArr.indexOf(overlap[i], currIndex + 1);
297
+ if (nextIndex !== currIndex + 1) {
298
+ newTagRanges.push(firstIndex, currIndex);
299
+ firstIndex = nextIndex;
300
+ }
301
+ if (i === overlap.length - 1) {
302
+ newTagRanges.push(firstIndex, nextIndex);
303
+ }
304
+ currIndex = nextIndex;
305
+ }
306
+
307
+ const ret = Object.create(null);
308
+ for (const tagName of Object.keys(tags)) {
309
+ ret[tagName] = newTagRanges;
310
+ }
311
+ return ret;
312
+ }
313
+
274
314
  module.exports = {
275
315
  createSubsetTags,
276
316
  createAppendTags,
@@ -278,5 +318,6 @@ module.exports = {
278
318
  createMergedTags,
279
319
  createTagsWithExclusion,
280
320
  createAdjustedQueryTags,
281
- createOverlappingTags
321
+ createOverlappingTags,
322
+ createEscapeTagRanges
282
323
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/assess",
3
- "version": "1.16.1",
3
+ "version": "1.17.0",
4
4
  "description": "Contrast service providing framework-agnostic Assess support",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
@@ -1,98 +0,0 @@
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 }
20
- } = require('@contrast/common');
21
- const {
22
- createFullLengthCopyTags
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.encodeURIComponent = {
37
- install() {
38
- const name = 'global.encodeURIComponent';
39
-
40
- patcher.patch(global, 'encodeURIComponent', {
41
- name,
42
- patchType,
43
- post(data) {
44
- const { args, result, hooked, orig } = data;
45
- if (!result || !args[0] || !sources.getStore()?.assess || instrumentation.isLocked()) return;
46
-
47
- const argInfo = tracker.getData(args[0]);
48
-
49
- if (!argInfo) return;
50
-
51
- // This native method seems to create a new string instance
52
- // as even when it returns the argument without any change
53
- // the result is not tracked, so we don't need to check for that
54
- const history = [argInfo];
55
- const newTags = createFullLengthCopyTags(argInfo.tags, result.length);
56
-
57
- newTags[URL_ENCODED] = [0, result.length - 1];
58
-
59
- const event = createPropagationEvent({
60
- name,
61
- moduleName: 'global',
62
- methodName: 'encodeURIComponent',
63
- context: `encodeURIComponent('${argInfo.value}')`,
64
- object: {
65
- value: createObjectLabel('global'),
66
- tracked: false
67
- },
68
- result: {
69
- 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: [URL_ENCODED],
78
- stacktraceOpts: {
79
- constructorOpt: hooked,
80
- prependFrames: [orig]
81
- },
82
- });
83
-
84
- if (!event) return;
85
-
86
- const { extern } = tracker.track(result, event);
87
-
88
- if (extern) {
89
- data.result = extern;
90
- }
91
- }
92
- });
93
- },
94
- uninstall() {
95
- global.encodeURIComponent = patcher.unwrap(global.encodeURIComponent);
96
- },
97
- };
98
- };