@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,120 @@
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 { patchType } = require('../common');
19
+ const { inspect, isString } = require('@contrast/common');
20
+ const { createAppendTags } = require('../../tag-utils');
21
+
22
+ module.exports = function(core) {
23
+ const {
24
+ scopes: { sources, instrumentation },
25
+ patcher,
26
+ depHooks,
27
+ assess: {
28
+ eventFactory: { createPropagationEvent },
29
+ dataflow: { tracker }
30
+ }
31
+ } = core;
32
+
33
+ return core.assess.dataflow.propagation.utilFormat = {
34
+ install() {
35
+ depHooks.resolve({ name: 'util' }, (util) => {
36
+ const name = 'util.format';
37
+
38
+ return patcher.patch(util, 'format', {
39
+ name,
40
+ patchType,
41
+ post(data) {
42
+ const { args, result, hooked, orig } = data;
43
+ if (!result || !args[0] || !isString(args[0]) || !sources.getStore()?.assess || instrumentation.isLocked()) return;
44
+
45
+ let idx = 0;
46
+ let newTags = {};
47
+ const history = [];
48
+ const eventArgs = [];
49
+ const formatChars = args[0].includes('%') ? args[0].match(/[^%]+/g).map((x) => x[0]) : [];
50
+ let i = formatChars.length > 0 ? 1 : 0;
51
+ for (i; i < args.length; i++) {
52
+ let arg = args[i];
53
+ const formatChar = formatChars[i - 1];
54
+ if (typeof arg === 'object') {
55
+
56
+ if (formatChar === 'j') {
57
+ arg = JSON.stringify(arg);
58
+ } else if (formatChar.toLowerCase() === 'o') {
59
+ // TO-DO: NODE-3235
60
+ }
61
+ }
62
+ const currIdx = result.indexOf(arg, idx);
63
+ if (currIdx > -1) {
64
+ idx = currIdx + arg.length;
65
+ } else {
66
+ continue;
67
+ }
68
+
69
+ const argInfo = tracker.getData(arg);
70
+ if (!argInfo) continue;
71
+
72
+ newTags = createAppendTags(newTags, argInfo.tags, currIdx);
73
+
74
+ history.push({ ...argInfo });
75
+ eventArgs.push({ value: argInfo ? argInfo.value : arg, tracked: !!argInfo });
76
+ }
77
+
78
+ const resultInfo = tracker.getData(result);
79
+ const event = createPropagationEvent({
80
+ name,
81
+ moduleName: 'util',
82
+ methodName: 'format',
83
+ context: `util.format(${inspect(args.join(', '))})`,
84
+ object: {
85
+ value: 'util',
86
+ tracked: false
87
+ },
88
+ result: {
89
+ value: resultInfo ? resultInfo.value : result,
90
+ tracked: true
91
+ },
92
+ args: eventArgs,
93
+ tags: newTags,
94
+ history,
95
+ source: 'P',
96
+ target: 'R',
97
+ stacktraceOpts: {
98
+ constructorOpt: hooked,
99
+ prependFrames: [orig]
100
+ },
101
+ });
102
+
103
+ if (!event) return;
104
+
105
+ if (resultInfo) {
106
+ Object.assign(resultInfo, event);
107
+ } else {
108
+ const { extern } = tracker.track(result, event);
109
+
110
+ if (extern) {
111
+ data.result = extern;
112
+ }
113
+ }
114
+ }
115
+ });
116
+ }
117
+ );
118
+ },
119
+ };
120
+ };
@@ -79,6 +79,7 @@ module.exports = function (core) {
79
79
  require('./install/mongodb')(core);
80
80
  require('./install/mssql')(core);
81
81
  require('./install/mysql')(core);
82
+ require('./install/node-serialize')(core);
82
83
  require('./install/postgres')(core);
83
84
  require('./install/sequelize')(core);
84
85
  require('./install/sqlite3')(core);
@@ -0,0 +1,103 @@
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 { patchType } = require('../common');
19
+ const {
20
+ Rule: { UNTRUSTED_DESERIALIZATION },
21
+ isString,
22
+ DataflowTag: {
23
+ UNTRUSTED
24
+ }
25
+ } = require('@contrast/common');
26
+
27
+ module.exports = function(core) {
28
+ const {
29
+ depHooks,
30
+ patcher,
31
+ scopes: { sources, instrumentation },
32
+ assess: {
33
+ eventFactory: { createSinkEvent },
34
+ dataflow: {
35
+ tracker,
36
+ sinks: { isVulnerable, reportFindings }
37
+ },
38
+ },
39
+ } = core;
40
+
41
+ core.assess.dataflow.sinks.nodeSerialize = {
42
+ install() {
43
+ depHooks.resolve({ name: 'node-serialize' }, (nodeSerialize) => {
44
+ patcher.patch(nodeSerialize, 'unserialize', {
45
+ name: 'node-serialize.unserialize',
46
+ patchType,
47
+ pre(data) {
48
+ const store = sources.getStore()?.assess;
49
+ if (
50
+ !store ||
51
+ !data.args[0] ||
52
+ instrumentation.isLocked()
53
+ ) return;
54
+
55
+ const [input] = data.args;
56
+
57
+ if (!isString(input)) {
58
+ return;
59
+ }
60
+
61
+ const strInfo = tracker.getData(input);
62
+ if (!strInfo || !isVulnerable(UNTRUSTED, [], strInfo.tags)) {
63
+ return;
64
+ }
65
+
66
+ const sinkEvent = createSinkEvent({
67
+ name: 'node-serialize.unserialize',
68
+ moduleName: 'node-serialize',
69
+ methodName: 'unserialize',
70
+ context: `node-serialize.unserialize(${strInfo.value})`,
71
+ history: [strInfo],
72
+ object: {
73
+ value: 'node-serialize',
74
+ tracked: false
75
+ },
76
+ args: [
77
+ {
78
+ value: strInfo.value,
79
+ tracked: true
80
+ },
81
+ ],
82
+ tags: strInfo.tags,
83
+ source: 'P0',
84
+ stacktraceOpts: {
85
+ contructorOpt: data.hooked,
86
+ prependFrames: [data.orig]
87
+ },
88
+ });
89
+
90
+ if (sinkEvent) {
91
+ reportFindings({
92
+ ruleId: UNTRUSTED_DESERIALIZATION,
93
+ sinkEvent,
94
+ });
95
+ }
96
+ }
97
+ });
98
+ });
99
+ },
100
+ };
101
+
102
+ return core.assess.dataflow.sinks.nodeSerialize;
103
+ };
@@ -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.18.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
- };