@contrast/assess 1.15.0 → 1.16.1

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.
@@ -130,31 +130,28 @@ module.exports = function (core) {
130
130
  name: 'JSON.prototype.parse',
131
131
  patchType,
132
132
  pre(data) {
133
- if (!sources.getStore()?.assess || instrumentation.isLocked()) return;
134
- const input = data.args[0];
133
+ if (!data.args[0] || !sources.getStore()?.assess || instrumentation.isLocked()) return;
134
+ const [input, reviver] = data.args;
135
135
 
136
136
  const strInfo = tracker.getData(input);
137
137
  if (!strInfo) return;
138
138
 
139
- const reviver = data.args[1];
140
139
  const stack = [];
141
- let keyValueIndexes;
140
+ let keyValueIndexes = [];
142
141
 
143
142
  try {
144
143
  keyValueIndexes = getKeyValueIndexes(input);
145
144
  } catch (error) {
146
145
  logger.warn({ error, string: input }, 'JSON.parse() propagation failed');
147
- keyValueIndexes = [];
148
146
  }
149
147
 
150
- // Error found, continue without instrumentation
151
148
  if (keyValueIndexes.length === 0) return;
152
149
 
153
150
  let i = 0;
154
151
  function contrastParseReviver(key, value) {
155
152
  const { value: [startIndex, endIndex] } = keyValueIndexes[i];
156
153
 
157
- if (!isString(value)) {
154
+ if (!value || !isString(value)) {
158
155
  return reviver ? reviver(key, value) : value;
159
156
  }
160
157
 
@@ -173,7 +170,7 @@ module.exports = function (core) {
173
170
  }
174
171
 
175
172
  data.args[1] = function (key, value) {
176
- if (typeof value === 'object') {
173
+ if (value && typeof value === 'object') {
177
174
  applyValuesToObject(value, stack);
178
175
  }
179
176
 
@@ -53,6 +53,9 @@ module.exports = function(core) {
53
53
  // the result is not tracked, so we don't need to check for that
54
54
  const history = [argInfo];
55
55
  const newTags = createFullLengthCopyTags(argInfo.tags, result.length);
56
+
57
+ if (!newTags) return;
58
+
56
59
  delete newTags[URL_ENCODED];
57
60
 
58
61
  if (!Object.keys(newTags).length) return;
@@ -0,0 +1,112 @@
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
+ } = require('./common');
22
+
23
+ module.exports = function(core) {
24
+ const {
25
+ depHooks,
26
+ patcher,
27
+ scopes: { sources, instrumentation },
28
+ assess: {
29
+ eventFactory: { createPropagationEvent },
30
+ dataflow: { tracker },
31
+ },
32
+ } = core;
33
+
34
+ core.assess.dataflow.propagation.pathInstrumentation.dirname = {
35
+ install() {
36
+ depHooks.resolve({ name: 'path' }, (path) => {
37
+ for (const os of ['posix', 'win32']) {
38
+ const isWin32 = os === 'win32';
39
+
40
+ patcher.patch(path[os], 'dirname', {
41
+ name: `path.${os}.dirname`,
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
+
58
+ if (!strInfo) return;
59
+
60
+ const { newTags } = createArgTagsInResult({
61
+ argStr: pathStr,
62
+ argTags: strInfo?.tags || {},
63
+ result,
64
+ lastIndex: result.length,
65
+ isWin32,
66
+ });
67
+
68
+ const event = newTags && createPropagationEvent({
69
+ name,
70
+ moduleName: 'path',
71
+ methodName: 'dirname',
72
+ context: `path.dirname('${strInfo.value}')`,
73
+ history: [strInfo],
74
+ object: {
75
+ value: 'path',
76
+ isTracked: false,
77
+ },
78
+ args: [
79
+ {
80
+ value: strInfo.value,
81
+ tracked: true,
82
+ },
83
+ ],
84
+ result: {
85
+ value: result,
86
+ tracked: true,
87
+ },
88
+ tags: newTags,
89
+ source: 'P',
90
+ target: 'R',
91
+ stacktraceOpts: {
92
+ constructorOpt: hooked,
93
+ prependFrames: [orig],
94
+ },
95
+ });
96
+
97
+ if (!event) return;
98
+
99
+ const { extern } = tracker.track(result, event);
100
+
101
+ if (extern) {
102
+ data.result = extern;
103
+ }
104
+ },
105
+ });
106
+ }
107
+ });
108
+ },
109
+ };
110
+
111
+ return core.assess.dataflow.propagation.pathInstrumentation.dirname;
112
+ };
@@ -25,8 +25,10 @@ module.exports = function(core) {
25
25
  };
26
26
 
27
27
  require('./basename')(core);
28
- require('./normalize')(core);
28
+ require('./dirname')(core);
29
29
  require('./join-and-resolve')(core);
30
+ require('./normalize')(core);
31
+ require('./relative')(core);
30
32
 
31
33
  return pathInstrumentation;
32
34
  };
@@ -0,0 +1,123 @@
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.relative = {
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], 'relative', {
42
+ name: `path.${os}.relative`,
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 [fromStr, toStr] = args;
54
+
55
+ if (!toStr || !isString(toStr)) return;
56
+ const fromStrInfo = tracker.getData(fromStr);
57
+ const toStrInfo = tracker.getData(toStr);
58
+
59
+ if (!toStrInfo) return;
60
+
61
+ let { newTags } = createArgTagsInResult({
62
+ argStr: toStr,
63
+ argTags: toStrInfo?.tags || {},
64
+ result,
65
+ lastIndex: result.length,
66
+ isWin32
67
+ });
68
+
69
+ newTags = excludeExtensionDotFromTags({
70
+ result,
71
+ tags: newTags,
72
+ isWin32,
73
+ });
74
+
75
+ const event = newTags && createPropagationEvent({
76
+ name,
77
+ moduleName: 'path',
78
+ methodName: 'relative',
79
+ context: `path.relative('${fromStr}', '${toStr}')`,
80
+ history: [toStrInfo],
81
+ object: {
82
+ value: 'path',
83
+ isTracked: false,
84
+ },
85
+ args: [
86
+ {
87
+ value: fromStrInfo?.value || fromStr,
88
+ tracked: !!fromStrInfo,
89
+ },
90
+ {
91
+ value: toStrInfo?.value,
92
+ tracked: !!toStrInfo,
93
+ }
94
+ ],
95
+ result: {
96
+ value: result,
97
+ tracked: true,
98
+ },
99
+ tags: newTags,
100
+ source: 'P',
101
+ target: 'R',
102
+ stacktraceOpts: {
103
+ constructorOpt: hooked,
104
+ prependFrames: [orig],
105
+ },
106
+ });
107
+
108
+ if (!event) return;
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.relative;
123
+ };
@@ -36,6 +36,8 @@ module.exports = function(core) {
36
36
  metadata,
37
37
  }) {
38
38
  const tags = createSubsetTags(strInfo.tags, startIdx, match.length);
39
+ if (!tags) return null;
40
+
39
41
  const { name, obj, hooked, orig } = metadata;
40
42
 
41
43
  return createPropagationEvent({
@@ -73,6 +73,7 @@ module.exports = function (core) {
73
73
  require('./install/eval')(core);
74
74
  require('./install/fs')(core);
75
75
  require('./install/function')(core);
76
+ require('./install/libxmljs')(core);
76
77
  require('./install/http')(core);
77
78
  require('./install/marsdb')(core);
78
79
  require('./install/mongodb')(core);
@@ -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
+
18
+ const { patchType } = require('../common');
19
+ const {
20
+ Rule: { XXE },
21
+ isString,
22
+ DataflowTag: {
23
+ UNTRUSTED,
24
+ ALPHANUM_SPACE_HYPHEN,
25
+ LIMITED_CHARS,
26
+ },
27
+ inspect
28
+ } = require('@contrast/common');
29
+
30
+ const safeTags = [
31
+ LIMITED_CHARS,
32
+ ALPHANUM_SPACE_HYPHEN
33
+ ];
34
+
35
+ module.exports = function(core) {
36
+ const {
37
+ depHooks,
38
+ patcher,
39
+ scopes: { sources, instrumentation },
40
+ assess: {
41
+ eventFactory: { createSinkEvent },
42
+ dataflow: {
43
+ tracker,
44
+ sinks: { isVulnerable, reportFindings }
45
+ },
46
+ },
47
+ } = core;
48
+
49
+ const handler = (moduleName, newApi) => (module) => {
50
+ const checkOptions = newApi
51
+ ? (opts) => !opts?.noent && !opts?.replaceEntities
52
+ : (opts) => !opts?.noent;
53
+
54
+ const methods = newApi
55
+ ? ['parseXml', 'parseXmlAsync']
56
+ : ['parseXml', 'parseXmlString'];
57
+
58
+ methods.forEach((method) => {
59
+ patcher.patch(module, method, {
60
+ name: `${moduleName}.${method}`,
61
+ patchType,
62
+ pre(data) {
63
+ const store = sources.getStore()?.assess;
64
+ if (
65
+ !store ||
66
+ !data.args[0] ||
67
+ instrumentation.isLocked()
68
+ ) return;
69
+
70
+ const [xmlString, opts] = data.args;
71
+
72
+ if (!xmlString || !isString(xmlString) || checkOptions(opts)) {
73
+ return;
74
+ }
75
+
76
+ const strInfo = tracker.getData(xmlString);
77
+ if (!strInfo || !isVulnerable(UNTRUSTED, safeTags, strInfo.tags)) {
78
+ return;
79
+ }
80
+
81
+ const event = createSinkEvent({
82
+ name: `${moduleName}.${method}`,
83
+ moduleName,
84
+ methodName: method,
85
+ context: `${moduleName}.${method}(${strInfo.value}, ${inspect(opts)})`,
86
+ history: [strInfo],
87
+ object: {
88
+ value: moduleName,
89
+ tracked: false,
90
+ },
91
+ args: [
92
+ {
93
+ value: strInfo.value,
94
+ tracked: true,
95
+ },
96
+ ],
97
+ tags: strInfo.tags,
98
+ source: 'P0',
99
+ stacktraceOpts: {
100
+ contructorOpt: data.hooked,
101
+ prependFrames: [data.orig]
102
+ },
103
+ });
104
+
105
+ if (event) {
106
+ reportFindings({
107
+ ruleId: XXE,
108
+ sinkEvent: event,
109
+ });
110
+ }
111
+ }
112
+ });
113
+ });
114
+ };
115
+
116
+ core.assess.dataflow.sinks.libxmljs = {
117
+ install() {
118
+ // libxmljs changed its API in version 1.0.0
119
+ depHooks.resolve({ name: 'libxmljs', version: '>=1' }, handler('libxmljs', true));
120
+
121
+ // libxmljs versions prior to 1.0.0 and libxmljs2 share the same API
122
+ depHooks.resolve({ name: 'libxmljs', version: '<1' }, handler('libxmljs', false));
123
+ depHooks.resolve({ name: 'libxmljs2' }, handler('libxmljs2', false));
124
+ },
125
+ };
126
+
127
+ return core.assess.dataflow.sinks.libxmljs;
128
+ };
@@ -32,6 +32,7 @@ module.exports = function (core) {
32
32
  require('./install/http')(core);
33
33
  require('./install/qs6')(core);
34
34
  require('./install/querystring')(core);
35
+ require('./install/multer1')(core);
35
36
 
36
37
  sources.install = function install() {
37
38
  callChildComponentMethodsSync(sources, 'install');
@@ -0,0 +1,119 @@
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 { InputType } = require('@contrast/common');
19
+ const { patchType } = require('../common');
20
+
21
+ module.exports = (core) => {
22
+ const {
23
+ depHooks,
24
+ patcher,
25
+ logger,
26
+ assess,
27
+ scopes: { wrap },
28
+ } = core;
29
+
30
+ function handleFile(sourceContext, constructorOpt, req) {
31
+ try {
32
+ assess.dataflow.sources.handle({
33
+ context: 'req',
34
+ data: req,
35
+ keys: ['file'],
36
+ name: 'multer',
37
+ inputType: InputType.BODY,
38
+ sourceContext,
39
+ stacktraceOpts: {
40
+ constructorOpt,
41
+ },
42
+ });
43
+ } catch (err) {
44
+ logger.error({ err }, 'error handling multer Assess dataflow req.file source');
45
+ }
46
+ }
47
+
48
+ function handleFiles(sourceContext, constructorOpt, req) {
49
+ let i;
50
+ for (i = 0; i < req.files.length; i++) {
51
+ try {
52
+ assess.dataflow.sources.handle({
53
+ context: 'req.files',
54
+ data: req.files[i],
55
+ keys: [i],
56
+ name: 'multer',
57
+ inputType: InputType.BODY,
58
+ sourceContext,
59
+ stacktraceOpts: {
60
+ constructorOpt,
61
+ },
62
+ });
63
+ } catch (err) {
64
+ logger.error({ err }, 'error handling multer Assess dataflow req.files[%s] source', i);
65
+ }
66
+ }
67
+ }
68
+
69
+ function handleBody(sourceContext, constructorOpt, req) {
70
+ try {
71
+ assess.dataflow.sources.handle({
72
+ context: 'req',
73
+ data: req,
74
+ keys: ['body'],
75
+ name: 'multer',
76
+ inputType: InputType.BODY,
77
+ sourceContext,
78
+ stacktraceOpts: {
79
+ constructorOpt,
80
+ },
81
+ });
82
+ } catch (err) {
83
+ logger.error({ err }, 'error handling multer Assess dataflow req.body source');
84
+ }
85
+ }
86
+
87
+ const multer1Instrumentation = (core.assess.dataflow.sources.multer1Instrumentation = {
88
+ install() {
89
+ depHooks.resolve(
90
+ { name: 'multer', file: 'lib/make-middleware.js' },
91
+ (_export) => patcher.patch(_export, {
92
+ name: 'multer._makeMiddleware',
93
+ patchType,
94
+ post(data) {
95
+ data.result = patcher.patch(data.result, {
96
+ name: 'multerMiddleware',
97
+ patchType,
98
+ pre(data) {
99
+ const [req, , next, hooked] = data.args;
100
+
101
+ const sourceContext = core.scopes.sources.getStore()?.assess;
102
+ if (!sourceContext) return;
103
+
104
+ data.args[2] = wrap(function (...args) {
105
+ if (req.file) handleFile(sourceContext, hooked, req);
106
+ if (Array.isArray(req.files)) handleFiles(sourceContext, hooked, req);
107
+ if (req.body && Object.keys(req.body).length) handleBody(sourceContext, hooked, req.body);
108
+ next(...args);
109
+ });
110
+ },
111
+ });
112
+ },
113
+ }));
114
+
115
+ },
116
+ });
117
+
118
+ return multer1Instrumentation;
119
+ };
package/lib/index.js CHANGED
@@ -22,21 +22,23 @@ const responseScanning = require('./response-scanning');
22
22
  const eventFactory = require('./event-factory');
23
23
 
24
24
  module.exports = function assess(core) {
25
- if (!core.config.assess.enable) return;
26
-
27
25
  const assess = core.assess = {};
28
26
 
29
- // Does this order matter? Probably not
30
- // 1. dataflow
31
27
  eventFactory(core);
32
28
  dataflow(core);
33
29
  responseScanning(core);
34
30
  sessionConfiguration(core);
35
31
 
36
- // crypto
37
- // static (in coordination with rewriter)
32
+ // todo
33
+ // crypto rule implementations
34
+ // static rule implementations in coordination with (CLI) rewriter
38
35
 
39
36
  assess.install = function() {
37
+ if (!core.config.getEffectiveValue('assess.enable')) {
38
+ core.logger.debug('assess is disabled, skipping installation');
39
+ return;
40
+ }
41
+
40
42
  core.rewriter.install('assess');
41
43
  callChildComponentMethodsSync(core.assess, 'install');
42
44
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/assess",
3
- "version": "1.15.0",
3
+ "version": "1.16.1",
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)",