@contrast/assess 1.8.0 → 1.10.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 (75) hide show
  1. package/lib/dataflow/event-factory.js +17 -13
  2. package/lib/dataflow/propagation/index.js +4 -0
  3. package/lib/dataflow/propagation/install/JSON/index.js +1 -0
  4. package/lib/dataflow/propagation/install/JSON/parse-fn.js +248 -0
  5. package/lib/dataflow/propagation/install/JSON/parse.js +196 -0
  6. package/lib/dataflow/propagation/install/JSON/stringify.js +5 -3
  7. package/lib/dataflow/propagation/install/array-prototype-join.js +21 -14
  8. package/lib/dataflow/propagation/install/buffer.js +2 -0
  9. package/lib/dataflow/propagation/install/contrast-methods/add.js +2 -0
  10. package/lib/dataflow/propagation/install/contrast-methods/index.js +1 -0
  11. package/lib/dataflow/propagation/install/contrast-methods/number.js +58 -0
  12. package/lib/dataflow/propagation/install/contrast-methods/string.js +53 -6
  13. package/lib/dataflow/propagation/install/contrast-methods/tag.js +3 -1
  14. package/lib/dataflow/propagation/install/decode-uri-component.js +9 -2
  15. package/lib/dataflow/propagation/install/ejs/escape-xml.js +8 -2
  16. package/lib/dataflow/propagation/install/encode-uri-component.js +9 -2
  17. package/lib/dataflow/propagation/install/escape-html.js +13 -5
  18. package/lib/dataflow/propagation/install/escape.js +9 -2
  19. package/lib/dataflow/propagation/install/handlebars-utils-escape-expression.js +11 -4
  20. package/lib/dataflow/propagation/install/isnumeric-0.js +59 -0
  21. package/lib/dataflow/propagation/install/mongoose/index.js +36 -0
  22. package/lib/dataflow/propagation/install/mongoose/schema-string.js +156 -0
  23. package/lib/dataflow/propagation/install/mysql-connection-escape.js +5 -0
  24. package/lib/dataflow/propagation/install/parse-int.js +60 -0
  25. package/lib/dataflow/propagation/install/path/basename.js +124 -0
  26. package/lib/dataflow/propagation/install/path/common.js +176 -0
  27. package/lib/dataflow/propagation/install/path/index.js +32 -0
  28. package/lib/dataflow/propagation/install/path/join-and-resolve.js +141 -0
  29. package/lib/dataflow/propagation/install/path/normalize.js +123 -0
  30. package/lib/dataflow/propagation/install/pug-runtime-escape.js +9 -2
  31. package/lib/dataflow/propagation/install/querystring/parse.js +12 -10
  32. package/lib/dataflow/propagation/install/sequelize.js +6 -3
  33. package/lib/dataflow/propagation/install/sql-template-strings.js +9 -2
  34. package/lib/dataflow/propagation/install/string/concat.js +8 -2
  35. package/lib/dataflow/propagation/install/string/format-methods.js +7 -2
  36. package/lib/dataflow/propagation/install/string/html-methods.js +15 -5
  37. package/lib/dataflow/propagation/install/string/match.js +16 -11
  38. package/lib/dataflow/propagation/install/string/replace.js +23 -15
  39. package/lib/dataflow/propagation/install/string/slice.js +14 -6
  40. package/lib/dataflow/propagation/install/string/split.js +16 -12
  41. package/lib/dataflow/propagation/install/string/substring.js +18 -8
  42. package/lib/dataflow/propagation/install/string/trim.js +4 -1
  43. package/lib/dataflow/propagation/install/unescape.js +9 -2
  44. package/lib/dataflow/propagation/install/url/domain-parsers.js +7 -2
  45. package/lib/dataflow/propagation/install/url/index.js +1 -0
  46. package/lib/dataflow/propagation/install/url/url.js +228 -0
  47. package/lib/dataflow/propagation/install/validator/hooks.js +6 -2
  48. package/lib/dataflow/sinks/index.js +8 -4
  49. package/lib/dataflow/sinks/install/child-process.js +116 -50
  50. package/lib/dataflow/sinks/install/eval.js +138 -0
  51. package/lib/dataflow/sinks/install/express/unvalidated-redirect.js +7 -4
  52. package/lib/dataflow/sinks/install/fastify/unvalidated-redirect.js +9 -5
  53. package/lib/dataflow/sinks/install/fs.js +45 -13
  54. package/lib/dataflow/sinks/install/function.js +160 -0
  55. package/lib/dataflow/sinks/install/http/index.js +31 -0
  56. package/lib/dataflow/sinks/install/http/request.js +152 -0
  57. package/lib/dataflow/sinks/install/{http.js → http/server-response.js} +7 -4
  58. package/lib/dataflow/sinks/install/koa/unvalidated-redirect.js +8 -5
  59. package/lib/dataflow/sinks/install/marsdb.js +3 -0
  60. package/lib/dataflow/sinks/install/mongodb.js +7 -24
  61. package/lib/dataflow/sinks/install/mssql.js +49 -29
  62. package/lib/dataflow/sinks/install/mysql.js +9 -4
  63. package/lib/dataflow/sinks/install/postgres.js +6 -3
  64. package/lib/dataflow/sinks/install/sequelize.js +7 -5
  65. package/lib/dataflow/sinks/install/sqlite3.js +7 -3
  66. package/lib/dataflow/sinks/install/vm.js +276 -0
  67. package/lib/dataflow/sources/handler.js +2 -1
  68. package/lib/dataflow/sources/install/http.js +1 -1
  69. package/lib/dataflow/tag-utils.js +95 -2
  70. package/lib/dataflow/tracker.js +6 -6
  71. package/lib/index.js +2 -0
  72. package/lib/response-scanning/handlers/utils.js +2 -2
  73. package/lib/session-configuration/index.js +34 -0
  74. package/lib/session-configuration/install/http.js +79 -0
  75. package/package.json +2 -2
@@ -0,0 +1,60 @@
1
+ /*
2
+ * Copyright: 2022 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 { isString } = require('@contrast/common');
19
+ const { patchType } = require('../common');
20
+
21
+ module.exports = function(core) {
22
+ const {
23
+ logger,
24
+ scopes: { instrumentation, sources },
25
+ patcher,
26
+ assess: {
27
+ dataflow: { tracker }
28
+ }
29
+ } = core;
30
+
31
+ const name = 'global.parseInt';
32
+
33
+ return core.assess.dataflow.propagation.parseIntInstrumentation = {
34
+ install() {
35
+ patcher.patch(global, 'parseInt', {
36
+ name,
37
+ patchType,
38
+ post(data) {
39
+ const { args: [value], result } = data;
40
+ if (
41
+ isNaN(result) ||
42
+ !value ||
43
+ !isString(value) ||
44
+ !sources.getStore()?.assess ||
45
+ instrumentation.isLocked() ||
46
+ !tracker.getData(value)
47
+ ) return;
48
+
49
+ // todo NODE-3118 to handle when value has trailing non-integer values
50
+
51
+ tracker.untrack(value);
52
+ logger.trace({ sanitizer: name, value }, 'untracked a string value');
53
+ }
54
+ });
55
+ },
56
+ uninstall() {
57
+ global.parseInt = patcher.unwrap(global.parseInt);
58
+ },
59
+ };
60
+ };
@@ -0,0 +1,124 @@
1
+ /*
2
+ * Copyright: 2022 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: 2022 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: 2022 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: 2022 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
+ };
@@ -0,0 +1,123 @@
1
+ /*
2
+ * Copyright: 2022 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
16
+ 'use strict';
17
+ const { isString } = require('@contrast/common');
18
+ const { patchType } = require('../../common');
19
+ const {
20
+ createArgTagsInResult,
21
+ excludeExtensionDotFromTags,
22
+ } = require('./common');
23
+
24
+ module.exports = function(core) {
25
+ const {
26
+ depHooks,
27
+ patcher,
28
+ scopes: { sources, instrumentation },
29
+ assess: {
30
+ dataflow: {
31
+ tracker,
32
+ eventFactory: { createPropagationEvent },
33
+ },
34
+ },
35
+ } = core;
36
+
37
+ core.assess.dataflow.propagation.pathInstrumentation.normalize = {
38
+ install() {
39
+ depHooks.resolve({ name: 'path' }, (path) => {
40
+ for (const os of ['posix', 'win32']) {
41
+ const isWin32 = os === 'win32';
42
+
43
+ patcher.patch(path[os], 'normalize', {
44
+ name: `path.${os}.normalize`,
45
+ patchType,
46
+ post(data) {
47
+ const { args, result, name, hooked, orig } = data;
48
+ if (
49
+ !result ||
50
+ !sources.getStore()?.assess ||
51
+ instrumentation.isLocked()
52
+ )
53
+ return;
54
+
55
+ const pathStr = args[0];
56
+
57
+ if (!pathStr || !isString(pathStr)) return;
58
+
59
+ const strInfo = tracker.getData(pathStr);
60
+
61
+ if (!strInfo) return;
62
+
63
+ let { newTags } = createArgTagsInResult({
64
+ argStr: pathStr,
65
+ argTags: strInfo?.tags || {},
66
+ result,
67
+ lastIndex: result.length,
68
+ isWin32,
69
+ });
70
+
71
+ newTags = excludeExtensionDotFromTags({
72
+ result,
73
+ tags: newTags,
74
+ isWin32,
75
+ });
76
+
77
+ const event = createPropagationEvent({
78
+ name,
79
+ moduleName: 'path',
80
+ methodName: 'normalize',
81
+ context: `path.normalize('${strInfo.value}')`,
82
+ history: [strInfo],
83
+ object: {
84
+ value: 'path',
85
+ isTracked: false,
86
+ },
87
+ args: [
88
+ {
89
+ value: strInfo.value,
90
+ tracked: true,
91
+ },
92
+ ],
93
+ result: {
94
+ value: result,
95
+ tracked: true,
96
+ },
97
+ tags: newTags,
98
+ source: 'P',
99
+ target: 'R',
100
+ stacktraceOpts: {
101
+ constructorOpt: hooked,
102
+ prependFrames: [orig],
103
+ },
104
+ });
105
+
106
+ if (!event) {
107
+ return;
108
+ }
109
+
110
+ const { extern } = tracker.track(result, event);
111
+
112
+ if (extern) {
113
+ data.result = extern;
114
+ }
115
+ },
116
+ });
117
+ }
118
+ });
119
+ },
120
+ };
121
+
122
+ return core.assess.dataflow.propagation.pathInstrumentation.normalize;
123
+ };
@@ -36,8 +36,10 @@ module.exports = function(core) {
36
36
  return core.assess.dataflow.propagation.pugRuntimeEscape = {
37
37
  install() {
38
38
  depHooks.resolve({ name: 'pug-runtime' }, (pugRuntime, version) => {
39
+ const name = 'pug-runtime.escape';
40
+
39
41
  patcher.patch(pugRuntime, 'escape', {
40
- name: 'pug-runtime.escape',
42
+ name,
41
43
  patchType,
42
44
  post(data) {
43
45
  const { args, result, hooked, orig } = data;
@@ -54,7 +56,10 @@ module.exports = function(core) {
54
56
  newTags[WEAK_URL_ENCODED] = [0, result.length - 1];
55
57
 
56
58
  const event = createPropagationEvent({
57
- name: 'pug-runtime.escape',
59
+ name,
60
+ moduleName: 'pug-runtime',
61
+ methodName: 'escape',
62
+ context: `pugRuntime.escape('${argInfo.value}')`,
58
63
  object: {
59
64
  value: createModuleLabel('pug-runtime', version),
60
65
  tracked: false
@@ -67,6 +72,8 @@ module.exports = function(core) {
67
72
  tags: newTags,
68
73
  addedTags: [WEAK_URL_ENCODED],
69
74
  history,
75
+ source: 'P',
76
+ target: 'R',
70
77
  stacktraceOpts: {
71
78
  constructorOpt: hooked,
72
79
  prependFrames: [orig]