@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
|
@@ -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 = {
|
|
@@ -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) {
|
|
@@ -5,10 +20,12 @@ module.exports = function(core) {
|
|
|
5
20
|
|
|
6
21
|
require('./install/fastify3')(core);
|
|
7
22
|
require('./install/koa2')(core);
|
|
23
|
+
require('./install/express4')(core);
|
|
8
24
|
|
|
9
25
|
errorHandlers.install = function() {
|
|
10
26
|
errorHandlers.fastify3ErrorHandler.install();
|
|
11
27
|
errorHandlers.koa2ErrorHandler.install();
|
|
28
|
+
errorHandlers.express4ErrorHandler.install();
|
|
12
29
|
};
|
|
13
30
|
|
|
14
31
|
return errorHandlers;
|
|
@@ -0,0 +1,89 @@
|
|
|
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 SecurityException = require('../../security-exception');
|
|
19
|
+
const { patchType } = require('../constants');
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
module.exports = function(core) {
|
|
23
|
+
const {
|
|
24
|
+
logger,
|
|
25
|
+
depHooks,
|
|
26
|
+
patcher,
|
|
27
|
+
protect,
|
|
28
|
+
} = core;
|
|
29
|
+
|
|
30
|
+
const express4ErrorHandler = protect.errorHandlers.express4ErrorHandler = {};
|
|
31
|
+
|
|
32
|
+
express4ErrorHandler.install = function () {
|
|
33
|
+
depHooks.resolve({ name: 'finalhandler' }, (finalhandler) =>
|
|
34
|
+
patcher.patch(finalhandler, {
|
|
35
|
+
name: 'finalHandler',
|
|
36
|
+
patchType,
|
|
37
|
+
post(data) {
|
|
38
|
+
data.result = patcher.patch(data.result, {
|
|
39
|
+
name: 'finalHandler.returnedFunction',
|
|
40
|
+
patchType,
|
|
41
|
+
around(org, data) {
|
|
42
|
+
const [err] = data.args;
|
|
43
|
+
const sourceContext = protect.getSourceContext('finalHandler');
|
|
44
|
+
const isSecurityException = SecurityException.isSecurityException(err);
|
|
45
|
+
|
|
46
|
+
if (isSecurityException && sourceContext) {
|
|
47
|
+
const blockInfo = sourceContext.findings.securityException;
|
|
48
|
+
|
|
49
|
+
sourceContext.block(...blockInfo);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (!sourceContext && isSecurityException) {
|
|
54
|
+
logger.info('source context not found; unable to handle response');
|
|
55
|
+
}
|
|
56
|
+
org();
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
depHooks.resolve({ name: 'express', version: '>=4.0.0', file: 'lib/router/layer.js' }, (Layer) => {
|
|
64
|
+
patcher.patch(Layer.prototype, 'handle_error', {
|
|
65
|
+
name: 'Layer.prototype.handle_error',
|
|
66
|
+
patchType,
|
|
67
|
+
around(org, data) {
|
|
68
|
+
const [err] = data.args;
|
|
69
|
+
const sourceContext = protect.getSourceContext('express.Layer.handle_error');
|
|
70
|
+
const isSecurityException = SecurityException.isSecurityException(err);
|
|
71
|
+
|
|
72
|
+
if (isSecurityException && sourceContext) {
|
|
73
|
+
const blockInfo = sourceContext.findings.securityException;
|
|
74
|
+
|
|
75
|
+
sourceContext.block(...blockInfo);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (!sourceContext && isSecurityException) {
|
|
80
|
+
logger.info('source context not found; unable to handle response');
|
|
81
|
+
}
|
|
82
|
+
org();
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
return express4ErrorHandler;
|
|
89
|
+
};
|
|
@@ -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 { isSecurityException } = require('../../security-exception');
|
|
@@ -5,10 +20,8 @@ const { patchType } = require('../constants');
|
|
|
5
20
|
|
|
6
21
|
module.exports = function(core) {
|
|
7
22
|
const {
|
|
8
|
-
logger,
|
|
9
23
|
depHooks,
|
|
10
24
|
patcher,
|
|
11
|
-
scopes: { sources },
|
|
12
25
|
protect,
|
|
13
26
|
} = core;
|
|
14
27
|
|
|
@@ -44,9 +57,9 @@ module.exports = function(core) {
|
|
|
44
57
|
const normalHandler = fastify3ErrorHandler._userHandler || fastify3ErrorHandler.defaultErrorHandler;
|
|
45
58
|
|
|
46
59
|
if (isSecurityException(err)) {
|
|
47
|
-
const sourceContext =
|
|
60
|
+
const sourceContext = protect.getSourceContext('fastify3.errorHandler');
|
|
61
|
+
|
|
48
62
|
if (!sourceContext) {
|
|
49
|
-
logger.info('source context not found; unable to handle response');
|
|
50
63
|
normalHandler.call(this, err, request, reply);
|
|
51
64
|
} else {
|
|
52
65
|
const blockInfo = sourceContext.findings.securityException;
|
|
@@ -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 SecurityException = require('../../security-exception');
|
|
@@ -9,7 +24,6 @@ module.exports = function(core) {
|
|
|
9
24
|
logger,
|
|
10
25
|
depHooks,
|
|
11
26
|
patcher,
|
|
12
|
-
scopes: { sources },
|
|
13
27
|
protect,
|
|
14
28
|
} = core;
|
|
15
29
|
|
|
@@ -27,7 +41,7 @@ module.exports = function(core) {
|
|
|
27
41
|
patchType,
|
|
28
42
|
around(org, data) {
|
|
29
43
|
const [err] = data.args;
|
|
30
|
-
const sourceContext =
|
|
44
|
+
const sourceContext = protect.getSourceContext('Koa.Application.handleRequest');
|
|
31
45
|
const isSecurityException = SecurityException.isSecurityException(err);
|
|
32
46
|
|
|
33
47
|
if (isSecurityException && sourceContext) {
|
package/lib/esm-loader.mjs
CHANGED
|
@@ -1,2 +1,17 @@
|
|
|
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
|
import esmHooks from '@contrast/esm-hooks';
|
|
2
17
|
export const { getSource, transformSource, load } = esmHooks();
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
module.exports = function(core) {
|
|
19
|
+
const { scopes: { sources }, logger } = core;
|
|
20
|
+
|
|
21
|
+
function getSourceContext(callPoint) {
|
|
22
|
+
const sourceContext = sources.getStore()?.protect;
|
|
23
|
+
|
|
24
|
+
if (!sourceContext) {
|
|
25
|
+
logger.debug(`source context not available in ${callPoint}`);
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return sourceContext.allowed ? null : sourceContext;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
core.protect.getSourceContext = getSourceContext;
|
|
33
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
module.exports = {
|
|
19
|
+
patchType: 'protect-hardening'
|
|
20
|
+
};
|
|
@@ -0,0 +1,65 @@
|
|
|
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 { BLOCKING_MODES, isString } = require('@contrast/common');
|
|
19
|
+
|
|
20
|
+
const NODE_SERIALIZE_RCE_TOKEN = '_$$ND_FUNC$$_';
|
|
21
|
+
|
|
22
|
+
module.exports = function(core) {
|
|
23
|
+
const {
|
|
24
|
+
protect: {
|
|
25
|
+
hardening,
|
|
26
|
+
throwSecurityException,
|
|
27
|
+
}
|
|
28
|
+
} = core;
|
|
29
|
+
|
|
30
|
+
function getResults(sourceContext, ruleId) {
|
|
31
|
+
let results = sourceContext.findings.hardeningResultsMap[ruleId];
|
|
32
|
+
if (!results) {
|
|
33
|
+
results = sourceContext.findings.hardeningResultsMap[ruleId] = [];
|
|
34
|
+
}
|
|
35
|
+
return results;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
hardening.handleUntrustedDeserialization = function(sourceContext, sinkContext) {
|
|
39
|
+
const ruleId = 'untrusted-deserialization';
|
|
40
|
+
const { mode } = sourceContext.rules.agentRules[ruleId];
|
|
41
|
+
const { name, value } = sinkContext;
|
|
42
|
+
|
|
43
|
+
if (mode === 'off') return;
|
|
44
|
+
|
|
45
|
+
if (name === 'node-serialize.unserialize') {
|
|
46
|
+
if (!isString(value) || !value.indexOf(NODE_SERIALIZE_RCE_TOKEN)) return;
|
|
47
|
+
|
|
48
|
+
const blocked = BLOCKING_MODES.includes(mode);
|
|
49
|
+
const results = getResults(sourceContext, ruleId);
|
|
50
|
+
|
|
51
|
+
results.push({
|
|
52
|
+
blocked,
|
|
53
|
+
findings: { deserializer: name, command: false },
|
|
54
|
+
sinkContext,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (blocked) {
|
|
58
|
+
sourceContext.findings.securityException = [mode, ruleId];
|
|
59
|
+
throwSecurityException(sourceContext);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
return hardening;
|
|
65
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
module.exports = function(core) {
|
|
19
|
+
const hardening = core.protect.hardening = {};
|
|
20
|
+
|
|
21
|
+
require('./handlers')(core);
|
|
22
|
+
|
|
23
|
+
require('./install/node-serialize0')(core);
|
|
24
|
+
hardening.install = function() {
|
|
25
|
+
hardening.nodeSerialize0Instrumentation.install();
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
return hardening;
|
|
29
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
|
|
20
|
+
module.exports = function(core) {
|
|
21
|
+
const {
|
|
22
|
+
depHooks,
|
|
23
|
+
patcher,
|
|
24
|
+
captureStacktrace,
|
|
25
|
+
protect,
|
|
26
|
+
protect: {
|
|
27
|
+
hardening
|
|
28
|
+
}
|
|
29
|
+
} = core;
|
|
30
|
+
|
|
31
|
+
function install() {
|
|
32
|
+
const name = 'node-serialize';
|
|
33
|
+
const method = 'unserialize';
|
|
34
|
+
|
|
35
|
+
depHooks.resolve(
|
|
36
|
+
{ name, version: '<1.0.0' },
|
|
37
|
+
(nodeSerialize) => {
|
|
38
|
+
patcher.patch(nodeSerialize, method, {
|
|
39
|
+
name,
|
|
40
|
+
patchType,
|
|
41
|
+
pre({ args: [value], hooked, orig }) {
|
|
42
|
+
const sourceContext = protect.getSourceContext(`${name}.${method}`);
|
|
43
|
+
|
|
44
|
+
if (!sourceContext || !value) return;
|
|
45
|
+
|
|
46
|
+
const sinkContext = captureStacktrace(
|
|
47
|
+
{ name: `${name}.${method}`, value },
|
|
48
|
+
{ constructorOpt: hooked, prependFrames: [orig] },
|
|
49
|
+
);
|
|
50
|
+
hardening.handleUntrustedDeserialization(sourceContext, sinkContext);
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return hardening.nodeSerialize0Instrumentation = {
|
|
57
|
+
install
|
|
58
|
+
};
|
|
59
|
+
};
|
package/lib/index.d.ts
CHANGED
|
@@ -1,29 +1,139 @@
|
|
|
1
|
+
|
|
2
|
+
/*
|
|
3
|
+
* Copyright: 2022 Contrast Security, Inc
|
|
4
|
+
* Contact: support@contrastsecurity.com
|
|
5
|
+
* License: Commercial
|
|
6
|
+
|
|
7
|
+
* NOTICE: This Software and the patented inventions embodied within may only be
|
|
8
|
+
* used as part of Contrast Security’s commercial offerings. Even though it is
|
|
9
|
+
* made available through public repositories, use of this Software is subject to
|
|
10
|
+
* the applicable End User Licensing Agreement found at
|
|
11
|
+
* https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
|
|
12
|
+
* between Contrast Security and the End User. The Software may not be reverse
|
|
13
|
+
* engineered, modified, repackaged, sold, redistributed or otherwise used in a
|
|
14
|
+
* way not consistent with the End User License Agreement.
|
|
15
|
+
*/
|
|
16
|
+
|
|
1
17
|
import { Core } from '@contrast/core';
|
|
18
|
+
import { Logger } from '@contrast/logger';
|
|
19
|
+
import { Sources } from '@contrast/scopes';
|
|
20
|
+
import RequireHook from '@contrast/require-hook';
|
|
21
|
+
import { RulesConfig, Messages, ReqData, ProtectMessage, Findings } from '@contrast/common';
|
|
22
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
23
|
+
import { Config } from '@contrast/config';
|
|
24
|
+
import * as http from 'node:http';
|
|
25
|
+
import * as https from 'node:https';
|
|
26
|
+
|
|
27
|
+
type Http = typeof http;
|
|
28
|
+
type Https = typeof https;
|
|
29
|
+
|
|
30
|
+
export type Block = (mode: string, ruleId: string) => void;
|
|
31
|
+
export class HttpInstrumentation {
|
|
32
|
+
messages: Messages;
|
|
33
|
+
scope: Sources;
|
|
34
|
+
config: Config;
|
|
35
|
+
logger: Logger;
|
|
36
|
+
depHooks: RequireHook;
|
|
37
|
+
protect: ProtectMessage;
|
|
38
|
+
makeSourceContext: Protect['makeSourceContext'];
|
|
39
|
+
maxBodySize: number;
|
|
40
|
+
installed: boolean;
|
|
41
|
+
|
|
42
|
+
constructor(core: Core);
|
|
43
|
+
|
|
44
|
+
install(): void;
|
|
45
|
+
uninstall(): void; //NYI
|
|
46
|
+
hookHttp(): void;
|
|
47
|
+
hookHttps(): void;
|
|
48
|
+
hookServer(xport: Http | Https): void;
|
|
49
|
+
initiateRequestHandling(fnContext: { instance: any, method: any, args: any }): void; //TODO
|
|
50
|
+
removeCookies(headers: string[]): string[];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ProtectRequestStore {
|
|
54
|
+
reqData: ReqData;
|
|
55
|
+
block: Block;
|
|
56
|
+
rules: {
|
|
57
|
+
agentLibRules: RulesConfig;
|
|
58
|
+
agentLibRulesMask: number;
|
|
59
|
+
agentRules: RulesConfig;
|
|
60
|
+
};
|
|
61
|
+
exclusions: any[]; // TODO
|
|
62
|
+
virtualPatches: any[]; // TODO
|
|
63
|
+
findings: Findings;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface ConnectInputs {
|
|
67
|
+
headers: string[],
|
|
68
|
+
uriPath: string,
|
|
69
|
+
method: string,
|
|
70
|
+
queries?: string
|
|
71
|
+
}
|
|
2
72
|
|
|
3
|
-
// TODO
|
|
4
73
|
export interface Protect {
|
|
5
|
-
makeResponseBlocker: () =>
|
|
6
|
-
makeSourceContext: () =>
|
|
74
|
+
makeResponseBlocker: (res: ServerResponse) => Block,
|
|
75
|
+
makeSourceContext: (req: IncomingMessage, res: ServerResponse) => ProtectRequestStore,
|
|
76
|
+
throwSecurityException: (sourceContext: ProtectRequestStore) => void,
|
|
7
77
|
inputAnalysis: {
|
|
8
|
-
handleConnect: () =>
|
|
9
|
-
handleRequestEnd: () => void,
|
|
10
|
-
|
|
11
|
-
handleQueryParams: () => void,
|
|
12
|
-
handleUrlParams: () => void,
|
|
13
|
-
handleCookies: () => void,
|
|
14
|
-
|
|
15
|
-
|
|
78
|
+
handleConnect: (sourceContext: ProtectRequestStore, connectInputs: ConnectInputs) => undefined | [string, string],
|
|
79
|
+
handleRequestEnd: (sourceContext: ProtectRequestStore) => void, //NYI
|
|
80
|
+
handleParsedBody: (sourceContext: ProtectRequestStore, parsedBody: { [key: string]: any }) => void,
|
|
81
|
+
handleQueryParams: (sourceContext: ProtectRequestStore, queryParams: { [key: string]: any }) => void,
|
|
82
|
+
handleUrlParams: (sourceContext: ProtectRequestStore, urlParams: { [key: string]: any }) => void,
|
|
83
|
+
handleCookies: (sourceContext: ProtectRequestStore, cookies: { [key: string]: any }) => void,
|
|
84
|
+
handleFileuploadName: (sourceContext: ProtectRequestStore, name: string) => void, //NYI
|
|
85
|
+
librariesInstrumentation: {
|
|
86
|
+
bodyParser: { install: () => void },
|
|
87
|
+
coBody: { install: () => void },
|
|
88
|
+
cookieParser: { install: () => void },
|
|
89
|
+
formidable: { install: () => void },
|
|
90
|
+
multer: { install: () => void },
|
|
91
|
+
qs: { install: () => void },
|
|
92
|
+
universalCookie: { install: () => void },
|
|
93
|
+
},
|
|
94
|
+
expressInstrumentation: { install: () => void },
|
|
95
|
+
fastifyInstrumentation: { install: () => void },
|
|
96
|
+
koaInstrumentation: { install: () => void },
|
|
97
|
+
httpInstrumentation: HttpInstrumentation,
|
|
16
98
|
install: () => void,
|
|
17
99
|
},
|
|
18
100
|
inputTracing: {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
101
|
+
handlePathTraversal: (sourceContext: ProtectRequestStore, sinkContext: { name: string, value: string, stack: { [key: string]: any }[] }) => void,
|
|
102
|
+
handleCommandInjection: (sourceContext: ProtectRequestStore, sinkContext: { name: string, value: string, stack: { [key: string]: any }[] }) => void,
|
|
103
|
+
handleSqlInjection: (sourceContext: ProtectRequestStore, sinkContext: { name: string, value: string, stack: { [key: string]: any }[] }) => void,
|
|
104
|
+
nosqlInjectionMongo: (sourceContext: ProtectRequestStore, sinkContext: { name: string, value: any, stack: { [key: string]: any }[] }) => void,
|
|
105
|
+
ssjsInjection: () => void, //NYI
|
|
106
|
+
fsInstrumentation: {
|
|
107
|
+
getValues: (indices: number[], args: any) => string[],
|
|
108
|
+
install: () => void
|
|
109
|
+
},
|
|
110
|
+
cpInstrumentation: { install: () => void },
|
|
111
|
+
mysqlInstrumentation: {
|
|
112
|
+
getValueFromArgs: ([value]: any[]) => string | undefined,
|
|
113
|
+
install: () => void
|
|
114
|
+
},
|
|
115
|
+
postgresInstrumentation: {
|
|
116
|
+
getQueryFromArgs: ([value]: any[]) => string | undefined,
|
|
117
|
+
install: () => void
|
|
118
|
+
},
|
|
119
|
+
mongodbInstrumentation: { install: () => void },
|
|
120
|
+
sqlite3Instrumentation: { install: () => void },
|
|
121
|
+
sequelizeInstrumentation: {
|
|
122
|
+
getQueryFromArgs: ([value]: any[]) => string | undefined,
|
|
123
|
+
install: () => void
|
|
124
|
+
},
|
|
125
|
+
httpInstrumentation: { install: () => void },
|
|
24
126
|
install: () => void
|
|
25
127
|
}
|
|
26
128
|
errorHandlers: {
|
|
129
|
+
fastify3ErrorHandler: {
|
|
130
|
+
_userHandler: null | ((...args: any[]) => any),
|
|
131
|
+
defaultErrorHandler: (error: Error, request: IncomingMessage, reply: ServerResponse) => void,
|
|
132
|
+
handler: (err: Error, request: IncomingMessage, reply: ServerResponse) => void,
|
|
133
|
+
install: () => void
|
|
134
|
+
}
|
|
135
|
+
koa2ErrorHandler: { install: () => void },
|
|
136
|
+
express4ErrorHandler: { install: () => void },
|
|
27
137
|
install: () => void,
|
|
28
138
|
},
|
|
29
139
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -31,6 +141,4 @@ export interface Protect {
|
|
|
31
141
|
version: string,
|
|
32
142
|
}
|
|
33
143
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
export default init;
|
|
144
|
+
export default function(core: Core): ProtectMessage;
|
package/lib/index.js
CHANGED
|
@@ -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 agentLib = require('@contrast/agent-lib');
|
|
@@ -18,8 +33,11 @@ module.exports = function(core) {
|
|
|
18
33
|
require('./throw-security-exception')(core);
|
|
19
34
|
require('./make-response-blocker')(core);
|
|
20
35
|
require('./make-source-context')(core);
|
|
36
|
+
require('./get-source-context')(core);
|
|
21
37
|
require('./input-analysis')(core);
|
|
22
38
|
require('./input-tracing')(core);
|
|
39
|
+
require('./hardening')(core);
|
|
40
|
+
require('./semantic-analysis')(core);
|
|
23
41
|
require('./error-handlers')(core);
|
|
24
42
|
|
|
25
43
|
const pkj = require('../package.json');
|
|
@@ -28,6 +46,7 @@ module.exports = function(core) {
|
|
|
28
46
|
protect.install = function() {
|
|
29
47
|
protect.inputAnalysis.install();
|
|
30
48
|
protect.inputTracing.install();
|
|
49
|
+
protect.hardening.install();
|
|
31
50
|
protect.errorHandlers.install();
|
|
32
51
|
};
|
|
33
52
|
|
|
@@ -0,0 +1,20 @@
|
|
|
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
|
+
module.exports = {
|
|
19
|
+
patchType: 'protect-input-analysis',
|
|
20
|
+
};
|