@contrast/agent 4.17.0 → 4.19.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.
- package/changelog.config.js +56 -0
- package/lib/assess/propagators/joi/index.js +1 -0
- package/lib/assess/propagators/joi/keys.js +72 -0
- package/lib/assess/propagators/joi/string-base.js +32 -3
- package/lib/assess/propagators/joi/string-schema.js +43 -16
- package/lib/assess/propagators/joi/values.js +25 -14
- package/lib/assess/propagators/utils.js +40 -3
- package/lib/assess/sources/index.js +26 -5
- package/lib/cli-rewriter/index.js +1 -1
- package/lib/core/async-storage/hooks/{mongodb-core.js → mongodb.js} +41 -7
- package/lib/core/config/options.js +14 -1
- package/lib/core/config/util.js +39 -11
- package/lib/instrumentation.js +2 -2
- package/lib/protect/service.js +2 -0
- package/lib/util/trace-util.js +5 -4
- package/package.json +4 -3
|
@@ -0,0 +1,56 @@
|
|
|
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
|
+
'use strict';
|
|
16
|
+
|
|
17
|
+
module.exports = {
|
|
18
|
+
jira: {
|
|
19
|
+
baseUrl: `https://${process.env.JIRA_HOST}`,
|
|
20
|
+
ticketIDPattern: /(NODE+-?[0-9]{4})/i,
|
|
21
|
+
excludeIssueTypes: ['Sub-task', 'Release'],
|
|
22
|
+
api: {
|
|
23
|
+
host: process.env.JIRA_HOST,
|
|
24
|
+
email: process.env.JIRA_EMAIL,
|
|
25
|
+
token: process.env.JIRA_TOKEN
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
hideEmptyBlocks: true,
|
|
29
|
+
template: `## v<%= process.env.VERSION -%>
|
|
30
|
+
|
|
31
|
+
<% blockTickets = tickets.all.filter((t) => !t.reverted); -%>
|
|
32
|
+
<% if (blockTickets.length > 0 || !options.hideEmptyBlocks) { -%>
|
|
33
|
+
### Jira Tickets
|
|
34
|
+
---------------------
|
|
35
|
+
<% blockTickets.forEach(ticket => { -%>
|
|
36
|
+
* <<%= ticket.fields.issuetype.name %>> - <%- ticket.fields.summary %>
|
|
37
|
+
[<%= ticket.key %>](<%= jira.baseUrl + '/browse/' + ticket.key %>)
|
|
38
|
+
<% }); -%>
|
|
39
|
+
<% if (!blockTickets.length) {%> ~ None ~ <% } %>
|
|
40
|
+
<% } -%>
|
|
41
|
+
<% blockNoTickets = commits.noTickets; -%>
|
|
42
|
+
<% if (blockNoTickets.length > 0 || !options.hideEmptyBlocks) { -%>
|
|
43
|
+
|
|
44
|
+
### Other Commits
|
|
45
|
+
---------------------
|
|
46
|
+
<% blockNoTickets.forEach(commit => { -%>
|
|
47
|
+
* <%= commit.slackUser ? '@'+commit.slackUser.name : commit.authorName %> - <<%= commit.revision.substr(0, 7) %>> - <%= commit.summary %>
|
|
48
|
+
<% }); -%>
|
|
49
|
+
<% if (!blockNoTickets.length) {%> ~ None ~ <% } %>
|
|
50
|
+
<% } -%>
|
|
51
|
+
<% blockPendingByOwner = tickets.pendingByOwner; -%>
|
|
52
|
+
<% if (blockPendingByOwner.length > 0 || !options.hideEmptyBlocks) { -%>
|
|
53
|
+
<% } -%>
|
|
54
|
+
--------------------
|
|
55
|
+
`
|
|
56
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
'use strict';
|
|
16
|
+
const patcher = require('../../../hooks/patcher');
|
|
17
|
+
const requireHook = require('../../../hooks/require');
|
|
18
|
+
const {
|
|
19
|
+
PATCH_TYPES: { ASSESS_PROPAGATOR },
|
|
20
|
+
} = require('../../../constants');
|
|
21
|
+
const { isObject } = require('../utils');
|
|
22
|
+
|
|
23
|
+
requireHook.resolve(
|
|
24
|
+
{ name: 'joi', file: 'lib/types/keys.js', version: '>=17.0.0' },
|
|
25
|
+
(joi) => {
|
|
26
|
+
patcher.patch(joi.__proto__, 'keys', {
|
|
27
|
+
name: 'joi.keys',
|
|
28
|
+
alwaysRun: true,
|
|
29
|
+
patchType: ASSESS_PROPAGATOR,
|
|
30
|
+
pre(data) {
|
|
31
|
+
if (!data || !data.args) return;
|
|
32
|
+
const value = data.args[0];
|
|
33
|
+
if (value) {
|
|
34
|
+
traverseObject(data.obj.$_root, value, value);
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const traverseObject = (joi, currentValue, originalInput, currentPath = []) => {
|
|
42
|
+
if (joi.isSchema(currentValue) || joi.isExpression(currentValue)) return;
|
|
43
|
+
if (joi.isRef(currentValue)) {
|
|
44
|
+
const referenceInstance = currentValue;
|
|
45
|
+
|
|
46
|
+
const targetSchemaInstace = currentValue.path.reduce(
|
|
47
|
+
(acc, value) => acc[value] || acc,
|
|
48
|
+
originalInput
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
if (!targetSchemaInstace) return;
|
|
52
|
+
|
|
53
|
+
if (!targetSchemaInstace.__CONTRAST__) {
|
|
54
|
+
Object.defineProperty(targetSchemaInstace, '__CONTRAST__', {
|
|
55
|
+
enumerable: false,
|
|
56
|
+
configurable: true,
|
|
57
|
+
value: {
|
|
58
|
+
refInstances: {},
|
|
59
|
+
},
|
|
60
|
+
writable: true,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
targetSchemaInstace.__CONTRAST__.refInstances[currentPath.join('.')] = referenceInstance;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (isObject(currentValue)) {
|
|
68
|
+
for (const [objKey, objValue] of Object.entries(currentValue)) {
|
|
69
|
+
traverseObject(joi, objValue, originalInput, [...currentPath, objKey]);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
@@ -25,6 +25,7 @@ const { PropagationEvent, Signature, CallContext } = require('../../models');
|
|
|
25
25
|
const TagRange = require('../../models/tag-range');
|
|
26
26
|
const tagRangeUtil = require('../../models/tag-range/util');
|
|
27
27
|
const agent = require('../../../agent');
|
|
28
|
+
const { setProxyTaggedString, getContrastData } = require('../utils');
|
|
28
29
|
|
|
29
30
|
const areThereRules = (obj) =>
|
|
30
31
|
obj &&
|
|
@@ -43,7 +44,35 @@ function instrumentJoiString(string) {
|
|
|
43
44
|
name: 'joi.string.validate',
|
|
44
45
|
patchType: ASSESS_PROPAGATOR,
|
|
45
46
|
post(data) {
|
|
46
|
-
const
|
|
47
|
+
const input = data.args[0];
|
|
48
|
+
const contrastData = getContrastData(data);
|
|
49
|
+
const doesSchemaHaveReferences = !!contrastData;
|
|
50
|
+
|
|
51
|
+
let trackingData = tracker.getData(input);
|
|
52
|
+
|
|
53
|
+
if (!trackingData && !data.result && doesSchemaHaveReferences) {
|
|
54
|
+
const proxyTaggedString = setProxyTaggedString(
|
|
55
|
+
contrastData,
|
|
56
|
+
input,
|
|
57
|
+
tracker
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
trackingData = tracker.getData(proxyTaggedString);
|
|
61
|
+
|
|
62
|
+
Object.values(contrastData.refInstances).forEach(
|
|
63
|
+
(referenceInstance) => {
|
|
64
|
+
Object.defineProperty(referenceInstance, '__CONTRAST__', {
|
|
65
|
+
enumerable: false,
|
|
66
|
+
configurable: true,
|
|
67
|
+
value: {
|
|
68
|
+
proxyTaggedString,
|
|
69
|
+
},
|
|
70
|
+
writable: true,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
47
76
|
if (
|
|
48
77
|
areThereRules(data.args[1]) &&
|
|
49
78
|
data.args[1].schema._rules.find((rule) => rule.name === 'pattern') &&
|
|
@@ -51,11 +80,11 @@ function instrumentJoiString(string) {
|
|
|
51
80
|
)
|
|
52
81
|
return;
|
|
53
82
|
|
|
54
|
-
if (data.result
|
|
83
|
+
if (!data.result && trackingData) {
|
|
55
84
|
const { event } = trackingData;
|
|
56
85
|
trackingData.tagRanges = tagRangeUtil.add(
|
|
57
86
|
trackingData.tagRanges,
|
|
58
|
-
new TagRange(0,
|
|
87
|
+
new TagRange(0, input.length - 1, 'string-type-checked')
|
|
59
88
|
);
|
|
60
89
|
trackingData.event = new PropagationEvent({
|
|
61
90
|
context: new CallContext(data),
|
|
@@ -18,12 +18,13 @@ const _ = require('lodash');
|
|
|
18
18
|
const requireHook = require('../../../hooks/require');
|
|
19
19
|
const patcher = require('../../../hooks/patcher');
|
|
20
20
|
const {
|
|
21
|
-
PATCH_TYPES: { ASSESS_PROPAGATOR }
|
|
21
|
+
PATCH_TYPES: { ASSESS_PROPAGATOR },
|
|
22
22
|
} = require('../../../constants');
|
|
23
23
|
const { PropagationEvent, Signature, CallContext } = require('../../models');
|
|
24
24
|
const TagRange = require('../../models/tag-range');
|
|
25
25
|
const tagRangeUtil = require('../../models/tag-range/util');
|
|
26
26
|
const tracker = require('../../../tracker');
|
|
27
|
+
const { setProxyTaggedString, getContrastData } = require('../utils');
|
|
27
28
|
|
|
28
29
|
const VALIDATORS = {
|
|
29
30
|
base64: 'alphanum-space-hyphen',
|
|
@@ -36,7 +37,7 @@ const VALIDATORS = {
|
|
|
36
37
|
creditCard: 'limited-chars',
|
|
37
38
|
ip: 'limited-chars',
|
|
38
39
|
hostname: 'alphanum-space-hyphen',
|
|
39
|
-
domain: 'alphanum-space-hyphen'
|
|
40
|
+
domain: 'alphanum-space-hyphen',
|
|
40
41
|
};
|
|
41
42
|
|
|
42
43
|
requireHook.resolve(
|
|
@@ -96,12 +97,12 @@ function reTrackCoercedValue(coerce, rule) {
|
|
|
96
97
|
trackedArgsData: argContrastProperties,
|
|
97
98
|
tagRanges: tracked.props.tagRanges,
|
|
98
99
|
target: 'R',
|
|
99
|
-
joiMethod: rule
|
|
100
|
+
joiMethod: rule,
|
|
100
101
|
});
|
|
101
102
|
|
|
102
103
|
data.result = { value: tracked.str };
|
|
103
104
|
}
|
|
104
|
-
}
|
|
105
|
+
},
|
|
105
106
|
});
|
|
106
107
|
}
|
|
107
108
|
|
|
@@ -119,27 +120,53 @@ function wrapRuleAsValidator(rules, rule, tagName) {
|
|
|
119
120
|
}
|
|
120
121
|
|
|
121
122
|
const argContrastProperties = tracker.getData(args[0]);
|
|
122
|
-
|
|
123
|
+
|
|
124
|
+
const contrastData = getContrastData(data);
|
|
125
|
+
const doesSchemaHaveReferences = !!contrastData;
|
|
126
|
+
|
|
127
|
+
if (!argContrastProperties && !doesSchemaHaveReferences) {
|
|
123
128
|
return;
|
|
124
129
|
}
|
|
125
130
|
|
|
126
|
-
|
|
131
|
+
let trackingData = tracker.getData(result);
|
|
132
|
+
|
|
133
|
+
if (!trackingData && doesSchemaHaveReferences) {
|
|
134
|
+
const proxyTaggedString = setProxyTaggedString(
|
|
135
|
+
contrastData,
|
|
136
|
+
result,
|
|
137
|
+
tracker
|
|
138
|
+
);
|
|
139
|
+
trackingData = tracker.getData(proxyTaggedString);
|
|
140
|
+
|
|
141
|
+
Object.values(contrastData.refInstances).forEach(
|
|
142
|
+
(referenceInstance) => {
|
|
143
|
+
Object.defineProperty(referenceInstance, '__CONTRAST__', {
|
|
144
|
+
enumerable: false,
|
|
145
|
+
configurable: true,
|
|
146
|
+
value: {
|
|
147
|
+
proxyTaggedString,
|
|
148
|
+
},
|
|
149
|
+
writable: true,
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
);
|
|
153
|
+
}
|
|
127
154
|
|
|
128
|
-
if (
|
|
129
|
-
|
|
130
|
-
|
|
155
|
+
if (trackingData) {
|
|
156
|
+
trackingData.tagRanges = tagRangeUtil.add(
|
|
157
|
+
trackingData.tagRanges,
|
|
131
158
|
new TagRange(0, result.length - 1, tagName)
|
|
132
159
|
);
|
|
133
160
|
|
|
134
|
-
|
|
161
|
+
trackingData.event = createPropagationEvent({
|
|
135
162
|
data,
|
|
136
|
-
trackedArgsData:
|
|
137
|
-
tagRanges:
|
|
163
|
+
trackedArgsData: trackingData,
|
|
164
|
+
tagRanges: trackingData.tagRanges,
|
|
138
165
|
target: 'A',
|
|
139
|
-
joiMethod: rule
|
|
166
|
+
joiMethod: rule,
|
|
140
167
|
});
|
|
141
168
|
}
|
|
142
|
-
}
|
|
169
|
+
},
|
|
143
170
|
});
|
|
144
171
|
}
|
|
145
172
|
|
|
@@ -148,7 +175,7 @@ function createPropagationEvent({
|
|
|
148
175
|
trackedArgsData,
|
|
149
176
|
tagRanges,
|
|
150
177
|
target,
|
|
151
|
-
joiMethod
|
|
178
|
+
joiMethod,
|
|
152
179
|
}) {
|
|
153
180
|
const { event: lastEvent } = trackedArgsData;
|
|
154
181
|
|
|
@@ -160,7 +187,7 @@ function createPropagationEvent({
|
|
|
160
187
|
signature,
|
|
161
188
|
tagRanges,
|
|
162
189
|
source: 'P',
|
|
163
|
-
target
|
|
190
|
+
target,
|
|
164
191
|
});
|
|
165
192
|
|
|
166
193
|
event.parents.push(lastEvent);
|
|
@@ -18,7 +18,7 @@ const _ = require('lodash');
|
|
|
18
18
|
const requireHook = require('../../../hooks/require');
|
|
19
19
|
const patcher = require('../../../hooks/patcher');
|
|
20
20
|
const {
|
|
21
|
-
PATCH_TYPES: { ASSESS_PROPAGATOR }
|
|
21
|
+
PATCH_TYPES: { ASSESS_PROPAGATOR },
|
|
22
22
|
} = require('../../../constants');
|
|
23
23
|
const tracker = require('../../../tracker');
|
|
24
24
|
const tagRangeUtil = require('../../models/tag-range/util');
|
|
@@ -41,9 +41,9 @@ function instrumentJoiValues(values) {
|
|
|
41
41
|
name: 'joi.values',
|
|
42
42
|
patchType: ASSESS_PROPAGATOR,
|
|
43
43
|
post(data) {
|
|
44
|
-
|
|
44
|
+
const {
|
|
45
45
|
args: [value],
|
|
46
|
-
result
|
|
46
|
+
result,
|
|
47
47
|
} = data;
|
|
48
48
|
|
|
49
49
|
// value not found during lookup
|
|
@@ -52,12 +52,21 @@ function instrumentJoiValues(values) {
|
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
if (result.ref) {
|
|
55
|
-
|
|
55
|
+
const resultValue = result.ref.__CONTRAST__
|
|
56
|
+
? result.ref.__CONTRAST__.proxyTaggedString
|
|
57
|
+
: result.value;
|
|
58
|
+
|
|
59
|
+
handler(resultValue, value, data);
|
|
60
|
+
|
|
61
|
+
if (result.ref.__CONTRAST__) {
|
|
62
|
+
delete result.ref.__CONTRAST__;
|
|
63
|
+
}
|
|
64
|
+
|
|
56
65
|
} else if (_.isString(result.value)) {
|
|
57
66
|
// use case is .valid() - safe
|
|
58
67
|
result.value = tracker.untrack(result.value) || result.value;
|
|
59
68
|
}
|
|
60
|
-
}
|
|
69
|
+
},
|
|
61
70
|
});
|
|
62
71
|
}
|
|
63
72
|
|
|
@@ -118,7 +127,7 @@ function getRefHandler(resolvedTrackData, refTrackData) {
|
|
|
118
127
|
*/
|
|
119
128
|
function handleTargetOnlyTracked(data, resolvedTrackData, refTrackData) {
|
|
120
129
|
const {
|
|
121
|
-
args: [value]
|
|
130
|
+
args: [value],
|
|
122
131
|
} = data;
|
|
123
132
|
data.result.value = toUntrackedString(value);
|
|
124
133
|
}
|
|
@@ -131,9 +140,10 @@ function handleTargetOnlyTracked(data, resolvedTrackData, refTrackData) {
|
|
|
131
140
|
* @param {object} refTrackData tracking data for reference value
|
|
132
141
|
*/
|
|
133
142
|
function handleBothTracked(data, resolvedTrackData, refTrackData) {
|
|
134
|
-
let
|
|
135
|
-
|
|
136
|
-
|
|
143
|
+
let value = data.args[0];
|
|
144
|
+
const {
|
|
145
|
+
args: [, , prefs],
|
|
146
|
+
result,
|
|
137
147
|
} = data;
|
|
138
148
|
|
|
139
149
|
// We can't reliably validate values that get adjusted
|
|
@@ -160,9 +170,10 @@ function handleBothTracked(data, resolvedTrackData, refTrackData) {
|
|
|
160
170
|
* @param {object} refTrackData tracking data for reference value
|
|
161
171
|
*/
|
|
162
172
|
function handleRefOnlyTracked(data, resolvedTrackData, refTrackData) {
|
|
163
|
-
let
|
|
164
|
-
|
|
165
|
-
|
|
173
|
+
let value = data.args[0];
|
|
174
|
+
const {
|
|
175
|
+
args: [, , prefs],
|
|
176
|
+
result,
|
|
166
177
|
} = data;
|
|
167
178
|
|
|
168
179
|
if (prefs.convert) {
|
|
@@ -221,7 +232,7 @@ function copyValidationHistory(targetTrackData, refTrackData) {
|
|
|
221
232
|
*/
|
|
222
233
|
function buildEventsAndTagsToReplay(joiEvents) {
|
|
223
234
|
const hist = [];
|
|
224
|
-
const tagRangesSeen = new WeakSet(_.get(joiEvents,
|
|
235
|
+
const tagRangesSeen = new WeakSet(_.get(joiEvents, '0.parents.0.tagRanges'));
|
|
225
236
|
|
|
226
237
|
for (const event of joiEvents) {
|
|
227
238
|
const newEventRanges = [];
|
|
@@ -239,7 +250,7 @@ function buildEventsAndTagsToReplay(joiEvents) {
|
|
|
239
250
|
if (newEventRanges.length) {
|
|
240
251
|
hist.push({
|
|
241
252
|
event,
|
|
242
|
-
tagRanges: newEventRanges
|
|
253
|
+
tagRanges: newEventRanges,
|
|
243
254
|
});
|
|
244
255
|
}
|
|
245
256
|
}
|
|
@@ -12,6 +12,8 @@ Copyright: 2022 Contrast Security, Inc
|
|
|
12
12
|
engineered, modified, repackaged, sold, redistributed or otherwise used in a
|
|
13
13
|
way not consistent with the End User License Agreement.
|
|
14
14
|
*/
|
|
15
|
+
'use strict';
|
|
16
|
+
|
|
15
17
|
/**
|
|
16
18
|
* Propagation Utility
|
|
17
19
|
*
|
|
@@ -41,7 +43,7 @@ function deepOrigValue(val, max = 10) {
|
|
|
41
43
|
function walk(item, depth) {
|
|
42
44
|
if (Array.isArray(item) && depth < max) {
|
|
43
45
|
depth++;
|
|
44
|
-
return item.map(function(item) {
|
|
46
|
+
return item.map(function (item) {
|
|
45
47
|
return walk(item, depth);
|
|
46
48
|
});
|
|
47
49
|
}
|
|
@@ -94,18 +96,53 @@ function getArgValue({ arg, inclusive, allowNegatives = true, sourceLength }) {
|
|
|
94
96
|
*/
|
|
95
97
|
function sortParamTypes(params, args, sourceLength) {
|
|
96
98
|
const paramTypes = {};
|
|
97
|
-
params.forEach(function(param) {
|
|
99
|
+
params.forEach(function (param) {
|
|
98
100
|
paramTypes[param.type] = getArgValue({
|
|
99
101
|
arg: args[param.index],
|
|
100
102
|
inclusive: param.inclusive,
|
|
101
103
|
allowNegatives: param.allowNegatives,
|
|
102
|
-
sourceLength
|
|
104
|
+
sourceLength,
|
|
103
105
|
});
|
|
104
106
|
});
|
|
105
107
|
|
|
106
108
|
return paramTypes;
|
|
107
109
|
}
|
|
108
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Checks whether the input is an object
|
|
113
|
+
* @param {*} value
|
|
114
|
+
* @return {boolean}
|
|
115
|
+
*/
|
|
116
|
+
function isObject(value) {
|
|
117
|
+
if (typeof value === 'object' && !Array.isArray(value) && value !== null) {
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function setProxyTaggedString(contrastData, input, tracker) {
|
|
125
|
+
let proxyTaggedString;
|
|
126
|
+
|
|
127
|
+
if (contrastData.proxyTaggedString) {
|
|
128
|
+
proxyTaggedString = contrastData.proxyTaggedString;
|
|
129
|
+
} else {
|
|
130
|
+
const { str } = tracker.track(input);
|
|
131
|
+
proxyTaggedString = str;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
contrastData.proxyTaggedString = proxyTaggedString;
|
|
135
|
+
|
|
136
|
+
return proxyTaggedString;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function getContrastData(data) {
|
|
140
|
+
return data.args[1].schema && data.args[1].schema.__CONTRAST__;
|
|
141
|
+
}
|
|
142
|
+
|
|
109
143
|
module.exports.origValue = origValue;
|
|
110
144
|
module.exports.deepOrigValue = deepOrigValue;
|
|
111
145
|
module.exports.sortParamTypes = sortParamTypes;
|
|
146
|
+
module.exports.isObject = isObject;
|
|
147
|
+
module.exports.setProxyTaggedString = setProxyTaggedString;
|
|
148
|
+
module.exports.getContrastData = getContrastData;
|
|
@@ -55,6 +55,25 @@ sources.track = function(type, parent, key, membrane) {
|
|
|
55
55
|
parent[key] = membrane.wrap(object, metadata);
|
|
56
56
|
};
|
|
57
57
|
|
|
58
|
+
/**
|
|
59
|
+
* Chooses a strategy for tracking the source events
|
|
60
|
+
* @param {any} config Current configuration for the agent
|
|
61
|
+
* @param {Logger} logger A logger instance
|
|
62
|
+
* @returns {Boolean} whether lazy tracking is enabled or not
|
|
63
|
+
*/
|
|
64
|
+
sources.getLazyTrackingConfig = function(config, logger) {
|
|
65
|
+
if (config._default['agent.traverse_and_track']) {
|
|
66
|
+
return config.assess.enable_lazy_tracking;
|
|
67
|
+
}
|
|
68
|
+
if (config._default['assess.enable_lazy_tracking']) {
|
|
69
|
+
logger.error('agent.traverse_and_track option is deprecated. Please use assess.enable_lazy_tracking from now on. It\'s value should be the opposite of this one');
|
|
70
|
+
return !config.agent.traverse_and_track;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
logger.error('Conflicting options set: `agent.traverse_and_track` and `assess.enable_lazy_tracking`. `agent.traverse_and_track` is deprecated, so `assess.enable_lazy_tracking` takes precedence');
|
|
74
|
+
return config.assess.enable_lazy_tracking;
|
|
75
|
+
};
|
|
76
|
+
|
|
58
77
|
/**
|
|
59
78
|
* Registers an event to add URL and input exclusions to async storage if they
|
|
60
79
|
* pertain to the current request path. Also registers all the source events
|
|
@@ -62,8 +81,10 @@ sources.track = function(type, parent, key, membrane) {
|
|
|
62
81
|
* object in a membrane
|
|
63
82
|
*/
|
|
64
83
|
sources.registerListeners = function({ config, exclusions }) {
|
|
84
|
+
const isLazyTrackingEnabled = sources.getLazyTrackingConfig(config, logger);
|
|
85
|
+
|
|
65
86
|
agentEmitter.on('assess.body', (obj, prop) => {
|
|
66
|
-
if (
|
|
87
|
+
if (isLazyTrackingEnabled) {
|
|
67
88
|
return sources.handleSourceEvent(config, exclusions, BODY, obj, prop);
|
|
68
89
|
}
|
|
69
90
|
|
|
@@ -77,7 +98,7 @@ sources.registerListeners = function({ config, exclusions }) {
|
|
|
77
98
|
});
|
|
78
99
|
});
|
|
79
100
|
agentEmitter.on('assess.headers', (obj, prop) => {
|
|
80
|
-
if (
|
|
101
|
+
if (isLazyTrackingEnabled) {
|
|
81
102
|
return sources.handleSourceEvent(config, exclusions, HEADER, obj, prop);
|
|
82
103
|
}
|
|
83
104
|
|
|
@@ -89,7 +110,7 @@ sources.registerListeners = function({ config, exclusions }) {
|
|
|
89
110
|
});
|
|
90
111
|
});
|
|
91
112
|
agentEmitter.on('assess.params', (obj, prop) => {
|
|
92
|
-
if (
|
|
113
|
+
if (isLazyTrackingEnabled) {
|
|
93
114
|
return sources.handleSourceEvent(
|
|
94
115
|
config,
|
|
95
116
|
exclusions,
|
|
@@ -107,7 +128,7 @@ sources.registerListeners = function({ config, exclusions }) {
|
|
|
107
128
|
});
|
|
108
129
|
});
|
|
109
130
|
agentEmitter.on('assess.query', (obj, prop) => {
|
|
110
|
-
if (
|
|
131
|
+
if (isLazyTrackingEnabled) {
|
|
111
132
|
return sources.handleSourceEvent(
|
|
112
133
|
config,
|
|
113
134
|
exclusions,
|
|
@@ -126,7 +147,7 @@ sources.registerListeners = function({ config, exclusions }) {
|
|
|
126
147
|
});
|
|
127
148
|
|
|
128
149
|
agentEmitter.on('assess.cookies', (obj, prop) => {
|
|
129
|
-
if (
|
|
150
|
+
if (isLazyTrackingEnabled) {
|
|
130
151
|
return sources.handleSourceEvent(config, exclusions, COOKIE, obj, prop);
|
|
131
152
|
}
|
|
132
153
|
|
|
@@ -215,7 +215,7 @@ class CLIRewriter {
|
|
|
215
215
|
|
|
216
216
|
const content = await readFile(filename, 'utf8');
|
|
217
217
|
const rewriteData = this.rewriter.rewriteFile(content, filename, {
|
|
218
|
-
sourceType: type
|
|
218
|
+
sourceType: type === 'commonjs' ? 'script' : 'module'
|
|
219
219
|
});
|
|
220
220
|
|
|
221
221
|
if (rewriteData.code) {
|
|
@@ -26,9 +26,9 @@ const utils = require('./utils');
|
|
|
26
26
|
* @param {Object} prototype to hook
|
|
27
27
|
* @param {String} method to hook
|
|
28
28
|
*/
|
|
29
|
-
function hookMethod(
|
|
30
|
-
patcher.patch(
|
|
31
|
-
name:
|
|
29
|
+
function hookMethod(obj, method, patchName) {
|
|
30
|
+
patcher.patch(obj, method, {
|
|
31
|
+
name: patchName,
|
|
32
32
|
patchType: ASYNC_CONTEXT,
|
|
33
33
|
alwaysRun: true,
|
|
34
34
|
pre: (data) => {
|
|
@@ -42,17 +42,18 @@ function hookMethod(prototype, method) {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
|
-
* Registers the hooks for
|
|
45
|
+
* Registers the hooks for mongodb and mongodb-core
|
|
46
46
|
*/
|
|
47
47
|
function init() {
|
|
48
|
-
logger.info('applying non-policy hook: mongodb
|
|
48
|
+
logger.info('applying non-policy hook: mongodb');
|
|
49
49
|
|
|
50
50
|
requireHook.resolve(
|
|
51
51
|
{ name: 'mongodb-core', file: 'lib/topologies/server.js' },
|
|
52
52
|
(server) => {
|
|
53
53
|
const methods = ['command', 'insert', 'update', 'remove', 'logout'];
|
|
54
54
|
for (const method of methods) {
|
|
55
|
-
|
|
55
|
+
const name = `mongodb-core.${server.prototype.constructor.name}.prototype`;
|
|
56
|
+
hookMethod(server.prototype, method, name);
|
|
56
57
|
}
|
|
57
58
|
}
|
|
58
59
|
);
|
|
@@ -60,9 +61,42 @@ function init() {
|
|
|
60
61
|
requireHook.resolve(
|
|
61
62
|
{ name: 'mongodb-core', file: 'lib/cursor.js' },
|
|
62
63
|
(cursor) => {
|
|
63
|
-
|
|
64
|
+
const name = `mongodb-core.${cursor.prototype.constructor.name}.prototype`;
|
|
65
|
+
hookMethod(cursor.prototype, 'next', name);
|
|
64
66
|
}
|
|
65
67
|
);
|
|
68
|
+
|
|
69
|
+
requireHook.resolve(
|
|
70
|
+
{
|
|
71
|
+
name: 'mongodb',
|
|
72
|
+
file: 'lib/topologies/server.js',
|
|
73
|
+
version: '>=3.3.0 <4.0.0'
|
|
74
|
+
},
|
|
75
|
+
(server) => patcher.patch(server, {
|
|
76
|
+
name: 'mongodb.Server',
|
|
77
|
+
patchType: ASYNC_CONTEXT,
|
|
78
|
+
alwaysRun: true,
|
|
79
|
+
post: (data) => {
|
|
80
|
+
const methods = ['command', 'insert', 'update', 'remove'];
|
|
81
|
+
for (const method of methods) {
|
|
82
|
+
hookMethod(data.result, method, 'mongodb.Server');
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
requireHook.resolve(
|
|
89
|
+
{ name: 'mongodb', file: 'lib/cursor.js', version: '>=3.3.0 <4.0.0' },
|
|
90
|
+
(cursor) => patcher.patch(cursor,
|
|
91
|
+
{
|
|
92
|
+
name: 'mongodb.Cursor',
|
|
93
|
+
patchType: ASYNC_CONTEXT,
|
|
94
|
+
alwaysRun: true,
|
|
95
|
+
post: (data) => {
|
|
96
|
+
hookMethod(data.result, '_next', 'mongodb.Cursor');
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
);
|
|
66
100
|
}
|
|
67
101
|
|
|
68
102
|
module.exports = init;
|
|
@@ -476,7 +476,13 @@ const agent = [
|
|
|
476
476
|
name: 'agent.traverse_and_track',
|
|
477
477
|
arg: '<traverse-and-track>',
|
|
478
478
|
default: false,
|
|
479
|
-
|
|
479
|
+
fn: (val, logger) => {
|
|
480
|
+
if (val) {
|
|
481
|
+
logger.error('agent.traverse_and_track option is deprecated. Please use assess.enable_lazy_tracking from now on. It\'s value should be the opposite of this one');
|
|
482
|
+
}
|
|
483
|
+
return val;
|
|
484
|
+
},
|
|
485
|
+
desc: 'source membrane alternative (DEPRECATED)',
|
|
480
486
|
},
|
|
481
487
|
{
|
|
482
488
|
name: 'agent.polling.app_activity_ms',
|
|
@@ -747,6 +753,13 @@ const assess = [
|
|
|
747
753
|
arg: '<tags>',
|
|
748
754
|
desc: 'comma-separated list of tags to apply to each application vulnerability reported by the agent',
|
|
749
755
|
},
|
|
756
|
+
{
|
|
757
|
+
name: 'assess.enable_lazy_tracking',
|
|
758
|
+
arg: '[true]',
|
|
759
|
+
default: true,
|
|
760
|
+
fn: castBoolean,
|
|
761
|
+
desc: 'When set to `false` won\'t track the source events lazily but will track the first up to 250 source events',
|
|
762
|
+
},
|
|
750
763
|
];
|
|
751
764
|
|
|
752
765
|
const protect = [
|
package/lib/core/config/util.js
CHANGED
|
@@ -72,8 +72,8 @@ class Config {
|
|
|
72
72
|
assess: {},
|
|
73
73
|
protect: {},
|
|
74
74
|
server: {},
|
|
75
|
-
dev: {}
|
|
76
|
-
}
|
|
75
|
+
dev: {},
|
|
76
|
+
},
|
|
77
77
|
};
|
|
78
78
|
}
|
|
79
79
|
|
|
@@ -146,9 +146,10 @@ class Config {
|
|
|
146
146
|
* @return {string|void} path, if valid
|
|
147
147
|
*/
|
|
148
148
|
function checkConfigPath() {
|
|
149
|
-
const configDir =
|
|
150
|
-
|
|
151
|
-
|
|
149
|
+
const configDir =
|
|
150
|
+
os.platform() === 'win32'
|
|
151
|
+
? `${process.env['ProgramData']}\\contrast`
|
|
152
|
+
: '/etc/contrast';
|
|
152
153
|
|
|
153
154
|
for (const dir of [process.cwd(), configDir]) {
|
|
154
155
|
const checkPath = path.resolve(dir, 'contrast_security.yaml');
|
|
@@ -159,7 +160,6 @@ function checkConfigPath() {
|
|
|
159
160
|
return;
|
|
160
161
|
}
|
|
161
162
|
|
|
162
|
-
|
|
163
163
|
/**
|
|
164
164
|
* @param {Object} cliOptions
|
|
165
165
|
* @param {string} cliOptions.script
|
|
@@ -202,7 +202,7 @@ function readConfig(cliOptions, logger) {
|
|
|
202
202
|
// parse yaml and JSON separately.
|
|
203
203
|
try {
|
|
204
204
|
config = yaml.parse(fileContents, {
|
|
205
|
-
prettyErrors: true
|
|
205
|
+
prettyErrors: true,
|
|
206
206
|
});
|
|
207
207
|
} catch (e) {
|
|
208
208
|
logger.error(
|
|
@@ -264,7 +264,7 @@ function mergeCliOptions(cliOptions, logger) {
|
|
|
264
264
|
enum: optEnum,
|
|
265
265
|
fn = _.identity,
|
|
266
266
|
name,
|
|
267
|
-
required
|
|
267
|
+
required,
|
|
268
268
|
} = option;
|
|
269
269
|
|
|
270
270
|
const env = process.env[option.env];
|
|
@@ -294,7 +294,7 @@ function mergeCliOptions(cliOptions, logger) {
|
|
|
294
294
|
// set from default
|
|
295
295
|
if (value === undefined) {
|
|
296
296
|
if (required) {
|
|
297
|
-
logger.error(
|
|
297
|
+
logger.error("Missing required option '%s'", name);
|
|
298
298
|
return options;
|
|
299
299
|
}
|
|
300
300
|
|
|
@@ -307,10 +307,38 @@ function mergeCliOptions(cliOptions, logger) {
|
|
|
307
307
|
}, new Config());
|
|
308
308
|
}
|
|
309
309
|
|
|
310
|
+
/**
|
|
311
|
+
* When you run an appliaction with pm2 on cluster mode, pm2 attaches the
|
|
312
|
+
* process.env to a property pm2_env(only for the agent since we start it with -r flag), so
|
|
313
|
+
* the function checks if there is pm2_env property and merge contrast
|
|
314
|
+
* related properties to process.env
|
|
315
|
+
*/
|
|
316
|
+
function mergePM2Envs() {
|
|
317
|
+
if (!process.env.pm2_env) return;
|
|
318
|
+
|
|
319
|
+
const pm2_env = JSON.parse(process.env.pm2_env);
|
|
320
|
+
|
|
321
|
+
const contrastEnvs = ['DEBUG', 'PGHOST', 'PGPORT'];
|
|
322
|
+
const objectEntries = Object.entries(pm2_env.env)
|
|
323
|
+
.concat(Object.entries(pm2_env))
|
|
324
|
+
.concat(['DEBUG', 'PGHOST', 'PGPORT']);
|
|
325
|
+
|
|
326
|
+
objectEntries.forEach(([key, value]) => {
|
|
327
|
+
if (
|
|
328
|
+
!process.env[key] &&
|
|
329
|
+
(key.toLocaleLowerCase().includes('contrast') ||
|
|
330
|
+
contrastEnvs.includes(key))
|
|
331
|
+
) {
|
|
332
|
+
process.env[key] = value;
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
|
|
310
337
|
util.Config = Config;
|
|
311
338
|
util.setup = function setup(cliOptions, logger) {
|
|
312
|
-
|
|
339
|
+
mergePM2Envs();
|
|
313
340
|
|
|
341
|
+
const mergedCliOptions = mergeCliOptions(cliOptions, logger);
|
|
314
342
|
mergedCliOptions.script = cliOptions.script;
|
|
315
343
|
mergedCliOptions.configFile = cliOptions.configFile;
|
|
316
344
|
mergedCliOptions.validate();
|
|
@@ -323,7 +351,7 @@ util.logConfig = function logConfig(mergedConfig, logger) {
|
|
|
323
351
|
'Current Configuration: %s',
|
|
324
352
|
stringify(mergedConfig._flat, {
|
|
325
353
|
replacer: (k, v) => (v == undefined ? null : v),
|
|
326
|
-
space: 2
|
|
354
|
+
space: 2,
|
|
327
355
|
})
|
|
328
356
|
);
|
|
329
357
|
};
|
package/lib/instrumentation.js
CHANGED
|
@@ -129,7 +129,7 @@ function protectModeFeatures({ agent, reporter }) {
|
|
|
129
129
|
// needs the || '.' for testing...
|
|
130
130
|
const logDir = agent.config.agent.node.analysis_log_dir || '.';
|
|
131
131
|
const agentLib = new lib.Agent(
|
|
132
|
-
{ enableLogging: true, logDir, logLevel:
|
|
132
|
+
{ enableLogging: true, logDir, logLevel: 'INFO' }
|
|
133
133
|
);
|
|
134
134
|
// attach the constants so lib.Agent() isn't exposed.
|
|
135
135
|
for (const c in lib.constants) {
|
|
@@ -159,7 +159,7 @@ function protectModeFeatures({ agent, reporter }) {
|
|
|
159
159
|
function nonPolicyHooks(agent) {
|
|
160
160
|
require('./core/async-storage/hooks/bluebird')();
|
|
161
161
|
require('./core/async-storage/hooks/redis')();
|
|
162
|
-
require('./core/async-storage/hooks/mongodb
|
|
162
|
+
require('./core/async-storage/hooks/mongodb')();
|
|
163
163
|
require('./core/async-storage/hooks/mysql')();
|
|
164
164
|
require('./hooks/require');
|
|
165
165
|
require('./hooks/cluster')(agent);
|
package/lib/protect/service.js
CHANGED
|
@@ -202,9 +202,11 @@ class ProtectService {
|
|
|
202
202
|
headers: req.rawHeaders.map((h, ix) => (ix & 1 ? h : h.toLowerCase()))
|
|
203
203
|
};
|
|
204
204
|
|
|
205
|
+
arg.uriPath = req.url;
|
|
205
206
|
const questionMark = req.url.indexOf('?');
|
|
206
207
|
if (questionMark >= 0) {
|
|
207
208
|
arg.queries = req.url.slice(questionMark + 1);
|
|
209
|
+
arg.uriPath = req.url.slice(0, questionMark);
|
|
208
210
|
}
|
|
209
211
|
|
|
210
212
|
const findings = this.agentLib.scoreRequestConnect(rules, arg, evalOptions);
|
package/lib/util/trace-util.js
CHANGED
|
@@ -46,12 +46,13 @@ function getRequest(agent, ruleId) {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
const ruleCount = context.rules[ruleId];
|
|
49
|
-
|
|
50
49
|
const { sampling } = agent.config.assess;
|
|
51
|
-
if (sampling && sampling.enable && ruleCount
|
|
52
|
-
|
|
53
|
-
return request;
|
|
50
|
+
if (sampling && sampling.enable && ruleCount >= sampling.baseline) {
|
|
51
|
+
return;
|
|
54
52
|
}
|
|
53
|
+
|
|
54
|
+
context.rules[ruleId]++;
|
|
55
|
+
return request;
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrast/agent",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.19.0",
|
|
4
4
|
"description": "Node.js security instrumentation by Contrast Security",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"security",
|
|
@@ -76,7 +76,7 @@
|
|
|
76
76
|
"@babel/template": "^7.10.4",
|
|
77
77
|
"@babel/traverse": "^7.12.1",
|
|
78
78
|
"@babel/types": "^7.12.1",
|
|
79
|
-
"@contrast/agent-lib": "^
|
|
79
|
+
"@contrast/agent-lib": "^4.0.0",
|
|
80
80
|
"@contrast/distringuish-prebuilt": "^2.2.0",
|
|
81
81
|
"@contrast/flat": "^4.1.1",
|
|
82
82
|
"@contrast/fn-inspect": "^2.4.4",
|
|
@@ -138,7 +138,7 @@
|
|
|
138
138
|
"csv-writer": "^1.2.0",
|
|
139
139
|
"deasync": "^0.1.24",
|
|
140
140
|
"dustjs-linkedin": "^3.0.1",
|
|
141
|
-
"ejs": "^3.1.
|
|
141
|
+
"ejs": "^3.1.7",
|
|
142
142
|
"escape-html": "^1.0.3",
|
|
143
143
|
"eslint": "^8.9.0",
|
|
144
144
|
"eslint-plugin-mocha": "^10.0.3",
|
|
@@ -152,6 +152,7 @@
|
|
|
152
152
|
"handlebars": "^4.7.7",
|
|
153
153
|
"husky": "^6.0.0",
|
|
154
154
|
"inquirer": "^8.1.2",
|
|
155
|
+
"jira-client": "^8.1.0",
|
|
155
156
|
"joi": "^17.4.0",
|
|
156
157
|
"jsdoc": "^3.6.10",
|
|
157
158
|
"libxmljs": "file:test/mock/libxmljs",
|