@contrast/agent 4.7.0 → 4.7.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.
- package/bin/VERSION +1 -1
- package/bin/linux/contrast-service +0 -0
- package/bin/mac/contrast-service +0 -0
- package/bin/windows/contrast-service.exe +0 -0
- package/lib/assess/membrane/deserialization-membrane.js +4 -5
- package/lib/assess/membrane/source-membrane.js +15 -19
- package/lib/assess/models/call-context.js +1 -1
- package/lib/assess/policy/propagators.json +8 -0
- package/lib/assess/policy/rules.json +2 -2
- package/lib/assess/policy/signatures.json +27 -0
- package/lib/assess/policy/util.js +2 -1
- package/lib/assess/propagators/JSON/parse.js +1 -1
- package/lib/assess/propagators/JSON/stringify.js +3 -3
- package/lib/assess/propagators/array-prototype-join.js +7 -8
- package/lib/assess/propagators/common.js +7 -5
- package/lib/assess/propagators/handlebars-escape-expresssion.js +1 -1
- package/lib/assess/propagators/joi/boolean.js +1 -1
- package/lib/assess/propagators/joi/expression.js +1 -1
- package/lib/assess/propagators/joi/number.js +1 -1
- package/lib/assess/propagators/joi/string-base.js +1 -1
- package/lib/assess/propagators/joi/string-schema.js +12 -13
- package/lib/assess/propagators/joi/values.js +11 -11
- package/lib/assess/propagators/manager.js +12 -10
- package/lib/assess/propagators/mongoose/helpers.js +20 -0
- package/lib/assess/propagators/mongoose/index.js +18 -0
- package/lib/assess/propagators/mongoose/map.js +74 -0
- package/lib/assess/propagators/mongoose/string.js +104 -0
- package/lib/assess/propagators/number.js +54 -0
- package/lib/assess/propagators/object.js +6 -7
- package/lib/assess/propagators/path/basename.js +14 -13
- package/lib/assess/propagators/path/common.js +1 -1
- package/lib/assess/propagators/path/dirname.js +14 -13
- package/lib/assess/propagators/path/extname.js +14 -13
- package/lib/assess/propagators/path/parse.js +1 -1
- package/lib/assess/propagators/path/relative.js +7 -5
- package/lib/assess/propagators/querystring/escape.js +20 -18
- package/lib/assess/propagators/querystring/parse.js +7 -5
- package/lib/assess/propagators/querystring/stringify.js +25 -24
- package/lib/assess/propagators/querystring/unescape.js +20 -18
- package/lib/assess/propagators/sequelize/sql-string-escape.js +1 -1
- package/lib/assess/propagators/sequelize/sql-string-format-named-parameters.js +1 -1
- package/lib/assess/propagators/sequelize/sql-string-format.js +3 -3
- package/lib/assess/propagators/sequelize/utils.js +2 -2
- package/lib/assess/propagators/string-prototype-replace.js +30 -28
- package/lib/assess/propagators/string-prototype-split.js +36 -36
- package/lib/assess/propagators/string-prototype-trim.js +15 -17
- package/lib/assess/propagators/string.js +12 -16
- package/lib/assess/propagators/template-escape.js +21 -18
- package/lib/assess/propagators/templates.js +8 -8
- package/lib/assess/propagators/url/url-prototype-parse.js +5 -6
- package/lib/assess/propagators/url/url-url.js +51 -43
- package/lib/assess/propagators/util/format.js +1 -1
- package/lib/assess/propagators/v8/init-hooks.js +3 -3
- package/lib/assess/propagators/validator/init-hooks.js +22 -22
- package/lib/assess/sinks/common.js +10 -5
- package/lib/assess/sinks/libxmljs-xxe.js +1 -1
- package/lib/assess/sinks/mongodb.js +2 -1
- package/lib/assess/sinks/ssrf-url.js +1 -1
- package/lib/constants.js +4 -1
- package/lib/core/config/options.js +1 -1
- package/lib/core/rewrite/injections.js +8 -0
- package/lib/feature-set.js +1 -1
- package/lib/hooks/object-to-primitive.js +6 -7
- package/lib/hooks/patcher.js +1 -1
- package/lib/protect/rules/nosqli/nosql-injection-rule.js +228 -0
- package/lib/protect/rules/rule-factory.js +2 -2
- package/lib/protect/service.js +23 -11
- package/lib/protect/sinks/mongodb.js +56 -55
- package/lib/reporter/translations/to-protobuf/dtm/index.js +1 -1
- package/lib/reporter/translations/to-protobuf/dtm/ip-denylist-details.js +1 -1
- package/lib/reporter/translations/to-protobuf/dtm/rasp-rule-sample.js +1 -1
- package/lib/reporter/translations/to-protobuf/settings/defend-features.js +8 -6
- package/lib/reporter/translations/to-protobuf/settings/exclusions.js +5 -4
- package/lib/tracker.js +13 -65
- package/package.json +2 -1
- package/lib/protect/rules/nosqli/no-sql-injection-rule.js +0 -109
|
@@ -21,47 +21,41 @@ const BaseSensor = require('../../hooks/frameworks/base');
|
|
|
21
21
|
const patcher = require('../../hooks/patcher');
|
|
22
22
|
const { PATCH_TYPES } = require('../../constants');
|
|
23
23
|
const { emitSinkEvent } = require('../../hooks/frameworks/common');
|
|
24
|
+
const agentEmitter = require('../../agent-emitter');
|
|
25
|
+
const SinkEvent = require('../models/sink-event');
|
|
24
26
|
|
|
25
27
|
const { SINK_TYPES } = constants;
|
|
26
28
|
const ID = 'mongodb';
|
|
27
29
|
|
|
28
|
-
|
|
29
|
-
* json.stringify(data) in try-catch
|
|
30
|
-
* @param {any} data
|
|
31
|
-
* @returns {string}
|
|
32
|
-
*/
|
|
33
|
-
const safeStringify = (data) => {
|
|
34
|
-
try {
|
|
35
|
-
return JSON.stringify(data);
|
|
36
|
-
} catch (e) {
|
|
37
|
-
// ignore errors
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @param {any} query `cmd.query`
|
|
43
|
-
* @returns {string}
|
|
44
|
-
*/
|
|
45
|
-
const getQueryAsString = (args, version) => {
|
|
30
|
+
function getCursorQueryData(args, version) {
|
|
46
31
|
const query = semver.gte(version, '3.3.0')
|
|
47
32
|
? _.get(args, '0.cmd.query')
|
|
48
33
|
: _.get(args, '1.query');
|
|
49
34
|
|
|
50
|
-
if (
|
|
51
|
-
|
|
35
|
+
if (!query) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (_.isString(query)) {
|
|
40
|
+
return query.toString()
|
|
41
|
+
}
|
|
52
42
|
|
|
53
43
|
if (query['$where']) {
|
|
54
44
|
return query['$where'].toString();
|
|
55
45
|
}
|
|
56
46
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
47
|
+
return query;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getOpQueryData(op) {
|
|
51
|
+
if (!op.q) {
|
|
52
|
+
return;
|
|
61
53
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
54
|
+
if (op.q.$where) {
|
|
55
|
+
return op.q.$where;
|
|
56
|
+
}
|
|
57
|
+
return op.q;
|
|
58
|
+
}
|
|
65
59
|
|
|
66
60
|
class MongoDBSensor extends BaseSensor {
|
|
67
61
|
constructor(agent) {
|
|
@@ -76,8 +70,11 @@ class MongoDBSensor extends BaseSensor {
|
|
|
76
70
|
name: 'mongodb.CoreServer.prototype',
|
|
77
71
|
patchType: PATCH_TYPES.PROTECT_SINK,
|
|
78
72
|
pre: (wrapCtx) => {
|
|
79
|
-
|
|
80
|
-
|
|
73
|
+
emitSinkEvent(
|
|
74
|
+
getCursorQueryData(wrapCtx.args, version),
|
|
75
|
+
SINK_TYPES.NOSQL_QUERY,
|
|
76
|
+
ID
|
|
77
|
+
);
|
|
81
78
|
}
|
|
82
79
|
});
|
|
83
80
|
|
|
@@ -86,21 +83,12 @@ class MongoDBSensor extends BaseSensor {
|
|
|
86
83
|
alwaysRun: true,
|
|
87
84
|
name: 'mongodb.CoreServer.prototype',
|
|
88
85
|
patchType: PATCH_TYPES.PROTECT_SINK,
|
|
89
|
-
pre: (
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// insert(ns, opts, options, cb)
|
|
96
|
-
patcher.patch(mongodb.CoreServer.prototype, 'insert', {
|
|
97
|
-
alwaysRun: true,
|
|
98
|
-
name: 'mongodb.CoreServer.prototype',
|
|
99
|
-
patchType: PATCH_TYPES.PROTECT_SINK,
|
|
100
|
-
pre: (wrapCtx) => {
|
|
101
|
-
const arg = _.get(wrapCtx, 'args.1');
|
|
102
|
-
const data = safeStringify(arg);
|
|
103
|
-
emitSinkEvent(data, SINK_TYPES.NOSQL_QUERY, ID);
|
|
86
|
+
pre: (data) => {
|
|
87
|
+
emitSinkEvent(
|
|
88
|
+
getCursorQueryData(data.args, version),
|
|
89
|
+
SINK_TYPES.NOSQL_QUERY,
|
|
90
|
+
ID
|
|
91
|
+
);
|
|
104
92
|
}
|
|
105
93
|
});
|
|
106
94
|
|
|
@@ -109,10 +97,17 @@ class MongoDBSensor extends BaseSensor {
|
|
|
109
97
|
alwaysRun: true,
|
|
110
98
|
name: 'mongodb.CoreServer.prototype',
|
|
111
99
|
patchType: PATCH_TYPES.PROTECT_SINK,
|
|
112
|
-
pre: (
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
100
|
+
pre: (data) => {
|
|
101
|
+
const ops = Array.isArray(data.args[1])
|
|
102
|
+
? data.args[1]
|
|
103
|
+
: [data.args[1]];
|
|
104
|
+
|
|
105
|
+
for (const op of ops) {
|
|
106
|
+
const eData = getOpQueryData(op);
|
|
107
|
+
if (eData) {
|
|
108
|
+
emitSinkEvent(eData, SINK_TYPES.NOSQL_QUERY, ID);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
116
111
|
}
|
|
117
112
|
});
|
|
118
113
|
|
|
@@ -121,10 +116,17 @@ class MongoDBSensor extends BaseSensor {
|
|
|
121
116
|
alwaysRun: true,
|
|
122
117
|
name: 'mongodb.CoreServer.prototype',
|
|
123
118
|
patchType: PATCH_TYPES.PROTECT_SINK,
|
|
124
|
-
pre: (
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
|
|
119
|
+
pre: (data) => {
|
|
120
|
+
const ops = Array.isArray(data.args[1])
|
|
121
|
+
? data.args[1]
|
|
122
|
+
: [data.args[1]];
|
|
123
|
+
|
|
124
|
+
for (const op of ops) {
|
|
125
|
+
const eData = getOpQueryData(op);
|
|
126
|
+
if (eData) {
|
|
127
|
+
emitSinkEvent(eData, SINK_TYPES.NOSQL_QUERY, ID);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
128
130
|
}
|
|
129
131
|
});
|
|
130
132
|
|
|
@@ -133,9 +135,8 @@ class MongoDBSensor extends BaseSensor {
|
|
|
133
135
|
alwaysRun: true,
|
|
134
136
|
name: 'mongodb.Db.prototype',
|
|
135
137
|
patchType: PATCH_TYPES.PROTECT_SINK,
|
|
136
|
-
pre: (
|
|
137
|
-
|
|
138
|
-
emitSinkEvent(data, SINK_TYPES.NOSQL_QUERY, ID);
|
|
138
|
+
pre: (data) => {
|
|
139
|
+
emitSinkEvent(data.args[0], SINK_TYPES.NOSQL_QUERY, ID);
|
|
139
140
|
}
|
|
140
141
|
});
|
|
141
142
|
});
|
|
@@ -27,7 +27,7 @@ module.exports = {
|
|
|
27
27
|
Finding: require('./finding'),
|
|
28
28
|
HttpMethodTamperingDetails: require('./http-method-tampering-details'),
|
|
29
29
|
HttpRequest: require('./http-request'),
|
|
30
|
-
|
|
30
|
+
IpDenylistDetails: require('./ip-denylist-details'),
|
|
31
31
|
LibraryUsageUpdate: require('./library-usage-update'),
|
|
32
32
|
Poll: require('./poll'),
|
|
33
33
|
NoSqlInjectionDetails: require('./no-sql-injection-details'),
|
|
@@ -16,7 +16,7 @@ Copyright: 2021 Contrast Security, Inc
|
|
|
16
16
|
const { dtm } = require('@contrast/protobuf-api');
|
|
17
17
|
|
|
18
18
|
module.exports = function IpDenylistDetails(details = {}) {
|
|
19
|
-
return new dtm.
|
|
19
|
+
return new dtm.IpDenylistDetails({
|
|
20
20
|
0: details.ip,
|
|
21
21
|
1: details.uuid
|
|
22
22
|
});
|
|
@@ -33,22 +33,24 @@ function DefendFeatures(defend = {}) {
|
|
|
33
33
|
const {
|
|
34
34
|
auth,
|
|
35
35
|
botBlockers = [],
|
|
36
|
-
ipBlacklists = [],
|
|
37
|
-
ipWhitelists = [],
|
|
38
36
|
logEnhancers = [],
|
|
39
|
-
ruleDefinitionList = []
|
|
37
|
+
ruleDefinitionList = [],
|
|
38
|
+
ipDenylists = [],
|
|
39
|
+
ipAllowlists = []
|
|
40
40
|
} = defend;
|
|
41
41
|
return wrapToObject(
|
|
42
42
|
new settings.DefendFeatures([
|
|
43
43
|
defend.enabled, // 1 enabled
|
|
44
44
|
defend['bot-blocker'], // 2 bot_blocker
|
|
45
45
|
botBlockers.map((blocker) => BotBlocker(blocker).array), // 3 bot_blockers
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
undefined, // 4 -
|
|
47
|
+
undefined, // 5 -
|
|
48
48
|
logEnhancers.map((le) => LogEnhancer(le).array), // 6 log_enhancers (unsupported as of q2-2019)
|
|
49
49
|
ruleDefinitionList.map((def) => RuleDefinition(def).array), // 7 rule_definitions
|
|
50
50
|
Syslog(defend.syslog).array, // 8 syslog
|
|
51
|
-
Auth(auth).array //
|
|
51
|
+
Auth(auth).array, // 9 auth
|
|
52
|
+
ipDenylists.map((dl) => IpFilter(dl).array), // 10 ip_denylists
|
|
53
|
+
ipAllowlists.map((al) => IpFilter(al).array) // 11 ip_allowlists
|
|
52
54
|
]),
|
|
53
55
|
({ syslog, ruleDefinitionsList }) => {
|
|
54
56
|
ruleDefinitionsList.forEach(({ keywordsList, patternsList }) => {
|
|
@@ -25,7 +25,6 @@ const {
|
|
|
25
25
|
|
|
26
26
|
function Exclusion({
|
|
27
27
|
assessmentRules,
|
|
28
|
-
blacklist,
|
|
29
28
|
inputName,
|
|
30
29
|
inputType,
|
|
31
30
|
matchStrategy,
|
|
@@ -33,7 +32,8 @@ function Exclusion({
|
|
|
33
32
|
name,
|
|
34
33
|
rules,
|
|
35
34
|
type,
|
|
36
|
-
urls
|
|
35
|
+
urls,
|
|
36
|
+
denylist
|
|
37
37
|
}) {
|
|
38
38
|
return wrapToObject(
|
|
39
39
|
new settings.Exclusion([
|
|
@@ -44,11 +44,12 @@ function Exclusion({
|
|
|
44
44
|
rules, // 5 protection_rules
|
|
45
45
|
assessmentRules, // 6 assessment_rules
|
|
46
46
|
urls, // 7 urls
|
|
47
|
-
|
|
47
|
+
undefined, // 8 -
|
|
48
48
|
ExclusionInputType(inputType), // 9 input_type
|
|
49
49
|
inputName, // 10 input_name
|
|
50
50
|
modes.includes('assess'), // 11 assess
|
|
51
|
-
modes.includes('defend') //
|
|
51
|
+
modes.includes('defend'), // 12 protect
|
|
52
|
+
denylist // 13 denylist
|
|
52
53
|
]),
|
|
53
54
|
(exclusion) => {
|
|
54
55
|
exclusion.inputType = EXCLUSION_INPUT_TYPES[exclusion.inputType];
|
package/lib/tracker.js
CHANGED
|
@@ -45,12 +45,6 @@ const defaultContrastProperties = {
|
|
|
45
45
|
}
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
-
// NOTE: this function just exists for us to get a better view
|
|
49
|
-
// of the module's performance while profiling
|
|
50
|
-
function getExtStringProps(ext) {
|
|
51
|
-
return distringuish.getProperties(ext);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
48
|
// i'm not sure why this is a class. there are no methods, and externalized
|
|
55
49
|
// strings don't have an instance of the class; they have an object with the
|
|
56
50
|
// same property names.
|
|
@@ -74,37 +68,6 @@ class Tracker {
|
|
|
74
68
|
this.metadata = new WeakMap();
|
|
75
69
|
}
|
|
76
70
|
|
|
77
|
-
/**
|
|
78
|
-
* Map lookup for metadata of a value
|
|
79
|
-
*
|
|
80
|
-
* @param {*} value Tracked value
|
|
81
|
-
* @return {ContrastProperties|undefined}
|
|
82
|
-
*/
|
|
83
|
-
getData(value) {
|
|
84
|
-
if (typeof value === 'string') {
|
|
85
|
-
const props = getExtStringProps(value);
|
|
86
|
-
if (props == null) {
|
|
87
|
-
return defaultContrastProperties;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
return props;
|
|
91
|
-
}
|
|
92
|
-
return this.metadata.get(value) || defaultContrastProperties;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Resets a string's tracking metadata to the default contrast properties.
|
|
97
|
-
* This will effectively untrack the associated string, but it will still be
|
|
98
|
-
* the externalized value.
|
|
99
|
-
* @param {object} trackingData A tracked string's metadata
|
|
100
|
-
*/
|
|
101
|
-
untrack(str) {
|
|
102
|
-
const trackingData = this.getData(str);
|
|
103
|
-
if (trackingData.tracked) {
|
|
104
|
-
Object.assign(trackingData, defaultContrastProperties);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
71
|
trackString(str) {
|
|
109
72
|
if (str.length === 0) {
|
|
110
73
|
return str;
|
|
@@ -113,7 +76,7 @@ class Tracker {
|
|
|
113
76
|
// XXX: this is the closest we have to a dedup.
|
|
114
77
|
// it may be kind of expensive. we need to consider whether or not
|
|
115
78
|
// this is worthwhile
|
|
116
|
-
if (this.getData(str)
|
|
79
|
+
if (this.getData(str)) {
|
|
117
80
|
return str;
|
|
118
81
|
}
|
|
119
82
|
|
|
@@ -143,38 +106,15 @@ class Tracker {
|
|
|
143
106
|
return value;
|
|
144
107
|
}
|
|
145
108
|
|
|
146
|
-
// trackArray(value, parent, sourceType, parentKey) {}
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* Associate properties with a string.
|
|
150
|
-
*
|
|
151
|
-
* @param {*} value value to track
|
|
152
|
-
* @returns {*} the value - tracked if some type of string, otherwise untracked
|
|
153
|
-
*/
|
|
154
|
-
track(value) {
|
|
155
|
-
if (typeof value === 'string') {
|
|
156
|
-
return this.trackString(value);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (value instanceof String) {
|
|
160
|
-
return this.trackStringObject(value);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
return value;
|
|
164
|
-
}
|
|
165
109
|
|
|
166
110
|
/**
|
|
167
111
|
* Associate properties with a string. Returns null if str is not a string,
|
|
168
112
|
* is a zero-length string, or any internal error takes place.
|
|
169
113
|
*
|
|
170
|
-
* This behavior is different than track in that it requires the caller to check
|
|
171
|
-
* the return value. track always returned properties even if the value was not a
|
|
172
|
-
* string or there were no properties associated with the string value.
|
|
173
|
-
*
|
|
174
114
|
* @param {*} str a value to track.
|
|
175
115
|
* @returns {Object|null} {str, props} or null on error.
|
|
176
116
|
*/
|
|
177
|
-
|
|
117
|
+
track(str) {
|
|
178
118
|
if (typeof str === 'string') {
|
|
179
119
|
// is the string already tracked?
|
|
180
120
|
let props = distringuish.getProperties(str);
|
|
@@ -217,7 +157,7 @@ class Tracker {
|
|
|
217
157
|
* @param {*} str any value
|
|
218
158
|
* @return {ContrastProperties|null}
|
|
219
159
|
*/
|
|
220
|
-
|
|
160
|
+
getData(str) {
|
|
221
161
|
if (typeof str === 'string') {
|
|
222
162
|
return distringuish.getProperties(str);
|
|
223
163
|
}
|
|
@@ -227,12 +167,20 @@ class Tracker {
|
|
|
227
167
|
return null;
|
|
228
168
|
}
|
|
229
169
|
|
|
230
|
-
|
|
170
|
+
/**
|
|
171
|
+
* Resets a string's tracking metadata to the default contrast properties.
|
|
172
|
+
* This will effectively untrack the associated string, but it will still be
|
|
173
|
+
* the externalized value.
|
|
174
|
+
* @param {object} trackingData A tracked string's metadata
|
|
175
|
+
*/
|
|
176
|
+
untrack(str) {
|
|
231
177
|
if (typeof str === 'string') {
|
|
232
|
-
|
|
178
|
+
let props = distringuish.getProperties(str);
|
|
179
|
+
if (!props) {
|
|
233
180
|
return null;
|
|
234
181
|
}
|
|
235
182
|
// return an untracked version of the string
|
|
183
|
+
Object.assign(props, {event: null, tagRanges: [], tracked: false})
|
|
236
184
|
return distringuish.internalize(str);
|
|
237
185
|
}
|
|
238
186
|
if (str instanceof String) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrast/agent",
|
|
3
|
-
"version": "4.7.
|
|
3
|
+
"version": "4.7.1",
|
|
4
4
|
"description": "Node.js security instrumentation by Contrast Security",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"security",
|
|
@@ -152,6 +152,7 @@
|
|
|
152
152
|
"mochawesome": "^7.0.1",
|
|
153
153
|
"mongodb": "file:test/mock/mongodb",
|
|
154
154
|
"mongodb-npm": "npm:mongodb@^3.6.5",
|
|
155
|
+
"mongoose": "^6.1.1",
|
|
155
156
|
"mustache": "^3.0.1",
|
|
156
157
|
"mysql": "file:test/mock/mysql",
|
|
157
158
|
"nock": "^12.0.3",
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
Copyright: 2021 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
|
-
const _ = require('lodash');
|
|
16
|
-
|
|
17
|
-
const logger = require('../../../core/logger')('contrast:rules:protect');
|
|
18
|
-
const { INPUT_TYPES, SINK_TYPES } = require('../common');
|
|
19
|
-
|
|
20
|
-
const MONGODB = 'mongodb';
|
|
21
|
-
|
|
22
|
-
const ScannerKit = new Map([
|
|
23
|
-
[MONGODB, () => require('../nosqli/nosql-scanner').create('MongoDB')]
|
|
24
|
-
]);
|
|
25
|
-
|
|
26
|
-
class NoSqlInjectionRule extends require('../') {
|
|
27
|
-
constructor(policy = {}) {
|
|
28
|
-
policy.inputParseDepth = 3;
|
|
29
|
-
super(policy);
|
|
30
|
-
|
|
31
|
-
this._scanners = new Map();
|
|
32
|
-
|
|
33
|
-
this.id = 'nosql-injection';
|
|
34
|
-
this.name = 'NoSQL Injection';
|
|
35
|
-
this.applicableInputs = [
|
|
36
|
-
INPUT_TYPES.BODY,
|
|
37
|
-
INPUT_TYPES.JSON_VALUE,
|
|
38
|
-
INPUT_TYPES.JSON_ARRAYED_VALUE,
|
|
39
|
-
INPUT_TYPES.PARAMETER_NAME,
|
|
40
|
-
INPUT_TYPES.PARAMETER_VALUE,
|
|
41
|
-
INPUT_TYPES.QUERYSTRING,
|
|
42
|
-
INPUT_TYPES.XML_VALUE,
|
|
43
|
-
INPUT_TYPES.URI,
|
|
44
|
-
INPUT_TYPES.URL_PARAMETER
|
|
45
|
-
];
|
|
46
|
-
this.applicableSinks = [SINK_TYPES.NOSQL_QUERY];
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
evaluateAtSink({ event, applicableSamples }) {
|
|
50
|
-
if (_.isEmpty(applicableSamples)) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const scanner = this.getScanner(event.id);
|
|
55
|
-
|
|
56
|
-
for (const sample of applicableSamples) {
|
|
57
|
-
const injection = scanner.findInjection(sample.input.value, event.data);
|
|
58
|
-
|
|
59
|
-
if (injection) {
|
|
60
|
-
this.appendAttackDetails(sample, injection);
|
|
61
|
-
sample.captureAppContext(event);
|
|
62
|
-
logger.warn(`EFFECTIVE - rule: ${this.id}, mode: ${this.mode} `);
|
|
63
|
-
this.blockRequest(sample);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
getScanner(id) {
|
|
69
|
-
if (!ScannerKit.has(id)) {
|
|
70
|
-
throw new Error(`Unknown NoSQL scanner: ${id}`);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (!this._scanners.has(id)) {
|
|
74
|
-
this._scanners.set(id, ScannerKit.get(id)());
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return this._scanners.get(id);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Builds details for Sql Injection Attack.
|
|
82
|
-
* @param {UserInput} inputDtm The user input that resulted in attack
|
|
83
|
-
* @param {String} query The query that was analyzed
|
|
84
|
-
* @param {Object} results The repsults of the sql-scanner
|
|
85
|
-
* @returns {Object} The details
|
|
86
|
-
*/
|
|
87
|
-
buildDetails(sample, findings) {
|
|
88
|
-
if (!findings) {
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
const { boundary, location, query } = findings;
|
|
93
|
-
|
|
94
|
-
const inputBoundaryIndex = boundary.previous
|
|
95
|
-
? boundary.previous.start
|
|
96
|
-
: boundary.start;
|
|
97
|
-
|
|
98
|
-
return {
|
|
99
|
-
start: location[0],
|
|
100
|
-
end: location[1] + 1,
|
|
101
|
-
input: sample.input.toSerializable(),
|
|
102
|
-
boundaryOverrunIndex: boundary.stop + 1,
|
|
103
|
-
inputBoundaryIndex,
|
|
104
|
-
query
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
module.exports = NoSqlInjectionRule;
|