@contrast/protect 1.2.1 → 1.4.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/lib/error-handlers/constants.js +15 -0
- package/lib/error-handlers/index.js +17 -0
- package/lib/error-handlers/install/express4.js +89 -0
- package/lib/error-handlers/install/fastify3.js +17 -4
- package/lib/error-handlers/install/koa2.js +16 -2
- package/lib/esm-loader.mjs +15 -0
- package/lib/get-source-context.js +33 -0
- package/lib/hardening/constants.js +20 -0
- package/lib/hardening/handlers.js +65 -0
- package/lib/hardening/index.js +29 -0
- package/lib/hardening/install/node-serialize0.js +59 -0
- package/lib/index.d.ts +127 -19
- package/lib/index.js +19 -0
- package/lib/input-analysis/constants.js +20 -0
- package/lib/input-analysis/handlers.js +201 -16
- package/lib/input-analysis/index.js +40 -3
- package/lib/input-analysis/install/body-parser1.js +122 -0
- package/lib/input-analysis/install/cookie-parser1.js +80 -0
- package/lib/input-analysis/install/express4.js +103 -0
- package/lib/input-analysis/install/fastify3.js +51 -24
- package/lib/input-analysis/install/formidable1.js +72 -0
- package/lib/input-analysis/install/http.js +30 -4
- package/lib/input-analysis/install/koa-body5.js +63 -0
- package/lib/input-analysis/install/koa-bodyparser4.js +64 -0
- package/lib/input-analysis/install/koa2.js +38 -48
- package/lib/input-analysis/install/multer1.js +88 -0
- package/lib/input-analysis/install/qs6.js +57 -0
- package/lib/input-analysis/install/universal-cookie4.js +52 -0
- package/lib/input-analysis/ip-analysis.js +76 -0
- package/lib/input-analysis/virtual-patches.js +109 -0
- package/lib/input-tracing/constants.js +15 -0
- package/lib/input-tracing/handlers/index.js +225 -66
- package/lib/input-tracing/index.js +25 -2
- package/lib/input-tracing/install/child-process.js +28 -7
- package/lib/input-tracing/install/eval.js +60 -0
- package/lib/input-tracing/install/fs.js +21 -4
- package/lib/input-tracing/install/http.js +63 -0
- package/lib/input-tracing/install/mongodb.js +233 -0
- package/lib/input-tracing/install/mysql.js +21 -4
- package/lib/input-tracing/install/postgres.js +20 -4
- package/lib/input-tracing/install/sequelize.js +22 -5
- package/lib/input-tracing/install/sqlite3.js +21 -4
- package/lib/input-tracing/install/vm.js +132 -0
- package/lib/make-response-blocker.js +15 -0
- package/lib/make-source-context.js +22 -1
- package/lib/security-exception.js +15 -0
- package/lib/semantic-analysis/handlers.js +160 -0
- package/lib/semantic-analysis/index.js +38 -0
- package/lib/throw-security-exception.js +17 -6
- package/package.json +10 -12
- package/lib/cli-rewriter.js +0 -20
- package/lib/input-analysis/install/co-body.js +0 -51
- package/lib/input-analysis/install/cookie-parser.js +0 -48
- package/lib/input-analysis/install/formidable.js +0 -53
- package/lib/input-analysis/install/multer.js +0 -52
- package/lib/input-analysis/install/qs.js +0 -40
- package/lib/input-analysis/install/universal-cookie.js +0 -34
- package/lib/input-tracing/handlers/nosql-injection-mongo.js +0 -48
- package/lib/utils.js +0 -88
|
@@ -0,0 +1,233 @@
|
|
|
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 { patchType } = require('../constants');
|
|
19
|
+
const semver = require('semver');
|
|
20
|
+
|
|
21
|
+
module.exports = function (core) {
|
|
22
|
+
const {
|
|
23
|
+
depHooks,
|
|
24
|
+
patcher,
|
|
25
|
+
captureStacktrace,
|
|
26
|
+
protect,
|
|
27
|
+
protect: { inputTracing },
|
|
28
|
+
} = core;
|
|
29
|
+
|
|
30
|
+
function getCursorQueryData(args, version) {
|
|
31
|
+
const query = semver.gte(version, '3.3.0')
|
|
32
|
+
? args[0]?.cmd?.query
|
|
33
|
+
: args[1]?.query;
|
|
34
|
+
|
|
35
|
+
if (!query) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (Object.prototype.toString.call(query) === '[object String]') {
|
|
40
|
+
return query.toString();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (query['$where']) {
|
|
44
|
+
return query['$where'].toString();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return query;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function getOpQueryData(op) {
|
|
51
|
+
if (!op.q) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (op.q.$where) {
|
|
55
|
+
return op.q.$where;
|
|
56
|
+
}
|
|
57
|
+
return op.q;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function hookV3CommandAndCursor(obj, method, patchName, version) {
|
|
61
|
+
patcher.patch(obj, method, {
|
|
62
|
+
name: patchName,
|
|
63
|
+
patchType,
|
|
64
|
+
pre: ({ args, hooked, name, orig }) => {
|
|
65
|
+
const value = getCursorQueryData(args, version);
|
|
66
|
+
const sourceContext = protect.getSourceContext(patchName);
|
|
67
|
+
|
|
68
|
+
if (!sourceContext || !value) return;
|
|
69
|
+
|
|
70
|
+
const sinkContext = captureStacktrace(
|
|
71
|
+
{ name, value },
|
|
72
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
73
|
+
);
|
|
74
|
+
inputTracing.nosqlInjectionMongo(sourceContext, sinkContext);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function hookV3UpdateAndRemove(obj, method, patchName) {
|
|
80
|
+
patcher.patch(obj, method, {
|
|
81
|
+
name: patchName,
|
|
82
|
+
patchType,
|
|
83
|
+
pre: ({ args, hooked, name, orig }) => {
|
|
84
|
+
const sourceContext = protect.getSourceContext(patchName);
|
|
85
|
+
|
|
86
|
+
if (!sourceContext) return;
|
|
87
|
+
|
|
88
|
+
const ops = Array.isArray(args[1])
|
|
89
|
+
? args[1]
|
|
90
|
+
: [args[1]];
|
|
91
|
+
for (const op of ops) {
|
|
92
|
+
const value = op && getOpQueryData(op);
|
|
93
|
+
if (value) {
|
|
94
|
+
const sinkContext = captureStacktrace(
|
|
95
|
+
{ name, value },
|
|
96
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
97
|
+
);
|
|
98
|
+
inputTracing.nosqlInjectionMongo(sourceContext, sinkContext);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function install() {
|
|
106
|
+
const v4MethodsWithFilter = [
|
|
107
|
+
'updateOne',
|
|
108
|
+
'replaceOne',
|
|
109
|
+
'updateMany',
|
|
110
|
+
'deleteOne',
|
|
111
|
+
'deleteMany',
|
|
112
|
+
'findOneAndDelete',
|
|
113
|
+
'findOneAndReplace',
|
|
114
|
+
'findOneAndUpdate',
|
|
115
|
+
'countDocuments',
|
|
116
|
+
'count',
|
|
117
|
+
'distinct',
|
|
118
|
+
];
|
|
119
|
+
|
|
120
|
+
depHooks.resolve(
|
|
121
|
+
{
|
|
122
|
+
name: 'mongodb', version: '>=4.0.0'
|
|
123
|
+
},
|
|
124
|
+
(mongodb) => {
|
|
125
|
+
v4MethodsWithFilter.forEach((method) => {
|
|
126
|
+
patcher.patch(mongodb.Collection.prototype, method, {
|
|
127
|
+
name: `mongodb.Collection.prototype.${method}`,
|
|
128
|
+
patchType,
|
|
129
|
+
pre: ({ args, hooked, name, orig }) => {
|
|
130
|
+
const value = typeof args[0] == 'function' ? null : args[0];
|
|
131
|
+
const sourceContext = protect.getSourceContext(`mongodb.Collection.prototype.${method}`);
|
|
132
|
+
|
|
133
|
+
if (!sourceContext || !value) return;
|
|
134
|
+
|
|
135
|
+
const sinkContext = captureStacktrace(
|
|
136
|
+
{ name, value },
|
|
137
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
138
|
+
);
|
|
139
|
+
inputTracing.nosqlInjectionMongo(sourceContext, sinkContext);
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
patcher.patch(mongodb.Db.prototype, 'command', {
|
|
145
|
+
name: 'mongodb.Db.prototype.command',
|
|
146
|
+
patchType,
|
|
147
|
+
pre: ({ args, hooked, name, orig }) => {
|
|
148
|
+
const value = args[0]?.filter;
|
|
149
|
+
const sourceContext = protect.getSourceContext('mongodb.Collection.prototype.command');
|
|
150
|
+
|
|
151
|
+
if (!sourceContext || !value) return;
|
|
152
|
+
|
|
153
|
+
const sinkContext = captureStacktrace(
|
|
154
|
+
{ name, value },
|
|
155
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
156
|
+
);
|
|
157
|
+
inputTracing.nosqlInjectionMongo(sourceContext, sinkContext);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
depHooks.resolve(
|
|
163
|
+
{
|
|
164
|
+
name: 'mongodb', version: '<4.0.0'
|
|
165
|
+
}, (mongodb, { version }) => {
|
|
166
|
+
hookV3CommandAndCursor(mongodb.CoreServer.prototype, 'cursor', 'mongodb.CoreServer.prototype.cursor', version);
|
|
167
|
+
|
|
168
|
+
patcher.patch(mongodb.Db.prototype, 'eval', {
|
|
169
|
+
name: 'mongodb.Db.prototype.eval',
|
|
170
|
+
patchType,
|
|
171
|
+
pre: ({ args, hooked, name, orig }) => {
|
|
172
|
+
const value = args[0];
|
|
173
|
+
const sourceContext = protect.getSourceContext('mongodb.Db.prototype.eval');
|
|
174
|
+
|
|
175
|
+
if (!sourceContext || !value) return;
|
|
176
|
+
|
|
177
|
+
const sinkContext = captureStacktrace(
|
|
178
|
+
{ name, value },
|
|
179
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
inputTracing.nosqlInjectionMongo(sourceContext, sinkContext);
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
depHooks.resolve({ name: 'mongodb', file: 'lib/cursor/find_cursor', version: '>=4.0.0' }, (cursor) => patcher.patch(cursor, 'FindCursor', {
|
|
188
|
+
name: 'mongodb.FindCursor',
|
|
189
|
+
patchType,
|
|
190
|
+
pre: ({ args, hooked, name, orig }) => {
|
|
191
|
+
const value = args[2];
|
|
192
|
+
const sourceContext = protect.getSourceContext('mongodb.FindCursor');
|
|
193
|
+
|
|
194
|
+
if (!sourceContext || !value) return;
|
|
195
|
+
|
|
196
|
+
const sinkContext = captureStacktrace(
|
|
197
|
+
{ name, value },
|
|
198
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
199
|
+
);
|
|
200
|
+
inputTracing.nosqlInjectionMongo(sourceContext, sinkContext);
|
|
201
|
+
}
|
|
202
|
+
}));
|
|
203
|
+
|
|
204
|
+
const mongoDBTopologiesMethods = ['update', 'remove'];
|
|
205
|
+
|
|
206
|
+
depHooks.resolve({
|
|
207
|
+
name: 'mongodb',
|
|
208
|
+
file: 'lib/topologies/topology_base.js',
|
|
209
|
+
version: '<4.0.0'
|
|
210
|
+
}, (tpl, { version }) => {
|
|
211
|
+
mongoDBTopologiesMethods.forEach((method) => {
|
|
212
|
+
hookV3UpdateAndRemove(tpl.TopologyBase.prototype, method, `mongodb.TopologyBase.prototype.${method}`);
|
|
213
|
+
});
|
|
214
|
+
hookV3CommandAndCursor(tpl.TopologyBase.prototype, 'command', 'mongodb.TopologyBase.prototype.command', version);
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
depHooks.resolve({
|
|
218
|
+
name: 'mongodb',
|
|
219
|
+
file: 'lib/topologies/native_topology.js',
|
|
220
|
+
version: '<4.0.0'
|
|
221
|
+
}, (NativeTopology, { version }) => {
|
|
222
|
+
mongoDBTopologiesMethods.forEach((method) => {
|
|
223
|
+
hookV3UpdateAndRemove(NativeTopology.prototype, method, `mongodb.NativeTopology.prototype.${method}`);
|
|
224
|
+
});
|
|
225
|
+
hookV3CommandAndCursor(NativeTopology.prototype, 'command', 'mongodb.NativeTopology.prototype.command', version);
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
const mongodbInstr = (core.protect.inputTracing.mongodbInstrumentation = {
|
|
229
|
+
install,
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
return mongodbInstr;
|
|
233
|
+
};
|
|
@@ -1,3 +1,18 @@
|
|
|
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
|
+
|
|
1
16
|
'use strict';
|
|
2
17
|
|
|
3
18
|
const { isString } = require('@contrast/common');
|
|
@@ -8,7 +23,7 @@ module.exports = function(core) {
|
|
|
8
23
|
depHooks,
|
|
9
24
|
patcher,
|
|
10
25
|
captureStacktrace,
|
|
11
|
-
|
|
26
|
+
protect,
|
|
12
27
|
protect: { inputTracing }
|
|
13
28
|
} = core;
|
|
14
29
|
|
|
@@ -32,8 +47,10 @@ module.exports = function(core) {
|
|
|
32
47
|
patcher.patch(conn.prototype, method, {
|
|
33
48
|
name,
|
|
34
49
|
patchType,
|
|
35
|
-
pre(
|
|
36
|
-
const
|
|
50
|
+
pre(data) {
|
|
51
|
+
const { args, hooked, name, orig } = data;
|
|
52
|
+
const sourceContext = protect.getSourceContext(name);
|
|
53
|
+
|
|
37
54
|
if (!sourceContext) return;
|
|
38
55
|
|
|
39
56
|
const value = mysqlInstr.getValueFromArgs(args);
|
|
@@ -41,7 +58,7 @@ module.exports = function(core) {
|
|
|
41
58
|
|
|
42
59
|
const sinkContext = captureStacktrace(
|
|
43
60
|
{ name, value },
|
|
44
|
-
{ constructorOpt: hooked }
|
|
61
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
45
62
|
);
|
|
46
63
|
|
|
47
64
|
inputTracing.handleSqlInjection(sourceContext, sinkContext);
|
|
@@ -1,3 +1,18 @@
|
|
|
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
|
+
|
|
1
16
|
'use strict';
|
|
2
17
|
|
|
3
18
|
const { isString } = require('@contrast/common');
|
|
@@ -7,8 +22,8 @@ module.exports = function(core) {
|
|
|
7
22
|
const {
|
|
8
23
|
depHooks,
|
|
9
24
|
patcher,
|
|
10
|
-
scopes: { sources },
|
|
11
25
|
captureStacktrace,
|
|
26
|
+
protect,
|
|
12
27
|
protect: { inputTracing }
|
|
13
28
|
} = core;
|
|
14
29
|
|
|
@@ -17,8 +32,9 @@ module.exports = function(core) {
|
|
|
17
32
|
if (query && isString(query)) return query;
|
|
18
33
|
}
|
|
19
34
|
|
|
20
|
-
function preHook({ args, hooked, name }) {
|
|
21
|
-
const sourceContext =
|
|
35
|
+
function preHook({ args, hooked, name, orig }) {
|
|
36
|
+
const sourceContext = protect.getSourceContext(name);
|
|
37
|
+
|
|
22
38
|
if (!sourceContext) return;
|
|
23
39
|
|
|
24
40
|
const value = getQueryFromArgs(args);
|
|
@@ -26,7 +42,7 @@ module.exports = function(core) {
|
|
|
26
42
|
|
|
27
43
|
const sinkContext = captureStacktrace(
|
|
28
44
|
{ name, value },
|
|
29
|
-
{ constructorOpt: hooked }
|
|
45
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
30
46
|
);
|
|
31
47
|
|
|
32
48
|
inputTracing.handleSqlInjection(sourceContext, sinkContext);
|
|
@@ -1,3 +1,18 @@
|
|
|
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
|
+
|
|
1
16
|
'use strict';
|
|
2
17
|
|
|
3
18
|
const { isString } = require('@contrast/common');
|
|
@@ -5,10 +20,11 @@ const { patchType } = require('../constants');
|
|
|
5
20
|
|
|
6
21
|
module.exports = function(core) {
|
|
7
22
|
const {
|
|
8
|
-
scopes: {
|
|
23
|
+
scopes: { instrumentation },
|
|
9
24
|
patcher,
|
|
10
25
|
depHooks,
|
|
11
26
|
captureStacktrace,
|
|
27
|
+
protect,
|
|
12
28
|
protect: { inputTracing }
|
|
13
29
|
} = core;
|
|
14
30
|
|
|
@@ -23,18 +39,19 @@ module.exports = function(core) {
|
|
|
23
39
|
patcher.patch(sequelize.prototype, 'query', {
|
|
24
40
|
name,
|
|
25
41
|
patchType,
|
|
26
|
-
pre({ args, hooked, name }) {
|
|
42
|
+
pre({ args, hooked, name, orig }) {
|
|
27
43
|
if (instrumentation.isLocked()) return;
|
|
28
44
|
|
|
29
|
-
const sourceContext =
|
|
30
|
-
|
|
45
|
+
const sourceContext = protect.getSourceContext(name);
|
|
46
|
+
|
|
47
|
+
if (!sourceContext || sourceContext.allowed) return;
|
|
31
48
|
|
|
32
49
|
const value = getQueryFromArgs(args);
|
|
33
50
|
if (!value) return;
|
|
34
51
|
|
|
35
52
|
const sinkContext = captureStacktrace(
|
|
36
53
|
{ name, value },
|
|
37
|
-
{ constructorOpt: hooked }
|
|
54
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
38
55
|
);
|
|
39
56
|
inputTracing.handleSqlInjection(sourceContext, sinkContext);
|
|
40
57
|
}
|
|
@@ -1,3 +1,18 @@
|
|
|
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
|
+
|
|
1
16
|
'use strict';
|
|
2
17
|
|
|
3
18
|
const { isString } = require('@contrast/common');
|
|
@@ -5,10 +20,11 @@ const { patchType } = require('../constants');
|
|
|
5
20
|
|
|
6
21
|
module.exports = function(core) {
|
|
7
22
|
const {
|
|
8
|
-
scopes: {
|
|
23
|
+
scopes: { instrumentation },
|
|
9
24
|
patcher,
|
|
10
25
|
depHooks,
|
|
11
26
|
captureStacktrace,
|
|
27
|
+
protect,
|
|
12
28
|
protect: { inputTracing }
|
|
13
29
|
} = core;
|
|
14
30
|
|
|
@@ -19,10 +35,10 @@ module.exports = function(core) {
|
|
|
19
35
|
patcher.patch(sqlite3.Database.prototype, method, {
|
|
20
36
|
name,
|
|
21
37
|
patchType,
|
|
22
|
-
pre({ args, hooked, name }) {
|
|
38
|
+
pre({ args, hooked, name, orig }) {
|
|
23
39
|
if (instrumentation.isLocked()) return;
|
|
24
40
|
|
|
25
|
-
const sourceContext =
|
|
41
|
+
const sourceContext = protect.getSourceContext(name);
|
|
26
42
|
if (!sourceContext) return;
|
|
27
43
|
|
|
28
44
|
const value = args[0];
|
|
@@ -30,8 +46,9 @@ module.exports = function(core) {
|
|
|
30
46
|
|
|
31
47
|
const sinkContext = captureStacktrace(
|
|
32
48
|
{ name, value },
|
|
33
|
-
{ constructorOpt: hooked }
|
|
49
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
34
50
|
);
|
|
51
|
+
|
|
35
52
|
inputTracing.handleSqlInjection(sourceContext, sinkContext);
|
|
36
53
|
}
|
|
37
54
|
});
|
|
@@ -0,0 +1,132 @@
|
|
|
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, isNonEmptyObject } = require('@contrast/common');
|
|
19
|
+
const { patchType } = require('../constants');
|
|
20
|
+
|
|
21
|
+
module.exports = function(core) {
|
|
22
|
+
const {
|
|
23
|
+
scopes: { instrumentation },
|
|
24
|
+
patcher,
|
|
25
|
+
depHooks,
|
|
26
|
+
captureStacktrace,
|
|
27
|
+
protect,
|
|
28
|
+
protect: { inputTracing }
|
|
29
|
+
} = core;
|
|
30
|
+
|
|
31
|
+
function install() {
|
|
32
|
+
depHooks.resolve({ name: 'vm' }, (vm) => {
|
|
33
|
+
['Script', 'createScript', 'runInContext', 'runInThisContext'].forEach(
|
|
34
|
+
(method) => {
|
|
35
|
+
const name = `vm.${method}`;
|
|
36
|
+
|
|
37
|
+
patcher.patch(vm, method, {
|
|
38
|
+
name,
|
|
39
|
+
patchType,
|
|
40
|
+
pre({ args, hooked, name, orig }) {
|
|
41
|
+
if (instrumentation.isLocked()) return;
|
|
42
|
+
|
|
43
|
+
const sourceContext = protect.getSourceContext(name);
|
|
44
|
+
if (!sourceContext) return;
|
|
45
|
+
|
|
46
|
+
const codeString = args[0];
|
|
47
|
+
if (!codeString || !isString(codeString)) return;
|
|
48
|
+
|
|
49
|
+
const sinkContext = captureStacktrace(
|
|
50
|
+
{ name, value: codeString },
|
|
51
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
52
|
+
);
|
|
53
|
+
inputTracing.ssjsInjection(sourceContext, sinkContext);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
patcher.patch(vm, 'runInNewContext', {
|
|
60
|
+
name: 'vm.runInNewContext',
|
|
61
|
+
patchType,
|
|
62
|
+
pre: ({ args, hooked, orig }) => {
|
|
63
|
+
if (instrumentation.isLocked()) return;
|
|
64
|
+
|
|
65
|
+
const sourceContext = protect.getSourceContext('vm.runInNewContext');
|
|
66
|
+
if (!sourceContext) return;
|
|
67
|
+
|
|
68
|
+
const codeString = args[0];
|
|
69
|
+
const envObj = args[1];
|
|
70
|
+
|
|
71
|
+
if ((!codeString || !isString(codeString)) && (!isNonEmptyObject(envObj))) return;
|
|
72
|
+
|
|
73
|
+
const codeStringSinkContext = (codeString && isString(codeString)) ? captureStacktrace(
|
|
74
|
+
{ name: 'vm.runInNewContext', value: codeString },
|
|
75
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
76
|
+
) : null;
|
|
77
|
+
const envObjSinkContext = isNonEmptyObject(envObj) ? captureStacktrace(
|
|
78
|
+
{ name: 'vm.runInNewContext', value: envObj },
|
|
79
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
80
|
+
) : null;
|
|
81
|
+
|
|
82
|
+
codeStringSinkContext && inputTracing.ssjsInjection(sourceContext, codeStringSinkContext);
|
|
83
|
+
envObjSinkContext && inputTracing.ssjsInjection(sourceContext, envObjSinkContext);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
patcher.patch(vm, 'createContext', {
|
|
88
|
+
name: 'vm.createContext',
|
|
89
|
+
patchType,
|
|
90
|
+
pre: ({ args, hooked, orig }) => {
|
|
91
|
+
if (instrumentation.isLocked()) return;
|
|
92
|
+
|
|
93
|
+
const sourceContext = protect.getSourceContext('vm.createContext');
|
|
94
|
+
if (!sourceContext) return;
|
|
95
|
+
|
|
96
|
+
const envObj = args[0];
|
|
97
|
+
if (!isNonEmptyObject(envObj)) return;
|
|
98
|
+
|
|
99
|
+
const sinkContext = captureStacktrace(
|
|
100
|
+
{ name: 'vm.createContext', value: envObj },
|
|
101
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
102
|
+
);
|
|
103
|
+
inputTracing.ssjsInjection(sourceContext, sinkContext);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
patcher.patch(vm.Script.prototype, 'runInNewContext', {
|
|
108
|
+
name: 'vm.Script.prototype.runInNewContext',
|
|
109
|
+
patchType,
|
|
110
|
+
pre: ({ args, hooked, orig }) => {
|
|
111
|
+
if (instrumentation.isLocked()) return;
|
|
112
|
+
|
|
113
|
+
const sourceContext = protect.getSourceContext('vm.Script.prototype.runInNewContext');
|
|
114
|
+
if (!sourceContext) return;
|
|
115
|
+
|
|
116
|
+
const envObj = args[0];
|
|
117
|
+
if (!isNonEmptyObject(envObj)) return;
|
|
118
|
+
|
|
119
|
+
const sinkContext = captureStacktrace(
|
|
120
|
+
{ name: 'vm.Script.prototype.runInNewContext', value: envObj },
|
|
121
|
+
{ constructorOpt: hooked, prependFrames: [orig] }
|
|
122
|
+
);
|
|
123
|
+
inputTracing.ssjsInjection(sourceContext, sinkContext);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const vmInstrumentation = inputTracing.vmInstrumentation = { install };
|
|
130
|
+
|
|
131
|
+
return vmInstrumentation;
|
|
132
|
+
};
|
|
@@ -1,3 +1,18 @@
|
|
|
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
|
+
|
|
1
16
|
'use strict';
|
|
2
17
|
|
|
3
18
|
module.exports = function(core) {
|
|
@@ -1,3 +1,18 @@
|
|
|
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
|
+
|
|
1
16
|
'use strict';
|
|
2
17
|
|
|
3
18
|
module.exports = function(core) {
|
|
@@ -23,6 +38,7 @@ module.exports = function(core) {
|
|
|
23
38
|
// lowercase header keys and capture content-type
|
|
24
39
|
let contentType = '';
|
|
25
40
|
const headers = Array(req.rawHeaders.length);
|
|
41
|
+
|
|
26
42
|
for (let i = 0; i < req.rawHeaders.length; i += 2) {
|
|
27
43
|
headers[i] = req.rawHeaders[i].toLowerCase();
|
|
28
44
|
headers[i + 1] = req.rawHeaders[i + 1];
|
|
@@ -45,6 +61,8 @@ module.exports = function(core) {
|
|
|
45
61
|
// so here is typically better; it makes clear what information is used to
|
|
46
62
|
// make decisions by different handlers.
|
|
47
63
|
const reqData = {
|
|
64
|
+
ip: req.socket.remoteAddress,
|
|
65
|
+
httpVersion: req.httpVersion,
|
|
48
66
|
method: req.method,
|
|
49
67
|
headers,
|
|
50
68
|
uriPath,
|
|
@@ -65,7 +83,7 @@ module.exports = function(core) {
|
|
|
65
83
|
// particular request (if any route exclusions, etc.)
|
|
66
84
|
rules: core.protect.rules,
|
|
67
85
|
exclusions: [],
|
|
68
|
-
|
|
86
|
+
virtualPatchesEvaluators: [],
|
|
69
87
|
|
|
70
88
|
// maybe better as result, findings... but my bad naming choice is
|
|
71
89
|
// past the point of return.
|
|
@@ -76,6 +94,9 @@ module.exports = function(core) {
|
|
|
76
94
|
// successfully.
|
|
77
95
|
bodyType: undefined,
|
|
78
96
|
resultsMap: Object.create(null),
|
|
97
|
+
hardeningResultsMap: Object.create(null),
|
|
98
|
+
semanticResultsMap: Object.create(null),
|
|
99
|
+
serverFeaturesResultsMap: Object.create(null)
|
|
79
100
|
},
|
|
80
101
|
|
|
81
102
|
/*
|
|
@@ -1,3 +1,18 @@
|
|
|
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
|
+
|
|
1
16
|
'use strict';
|
|
2
17
|
|
|
3
18
|
module.exports = class SecurityException extends Error {
|