@contrast/protect 1.83.0 → 1.85.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/README.md +33 -0
- package/lib/hardening/handlers.js +1 -2
- package/lib/index.d.ts +32 -0
- package/lib/index.js +21 -15
- package/lib/input-analysis/install/body-parser.js +1 -1
- package/lib/input-analysis/install/cookie-parser1.js +1 -1
- package/lib/input-analysis/install/express.js +1 -1
- package/lib/input-analysis/install/fastify.js +1 -1
- package/lib/input-analysis/install/hapi.js +1 -1
- package/lib/input-analysis/install/http.js +10 -7
- package/lib/input-analysis/install/multer.js +1 -1
- package/lib/input-analysis/install/restify.js +1 -1
- package/lib/input-tracing/handlers.js +1 -2
- package/lib/make-source-context.js +1 -4
- package/lib/semantic-analysis/handlers.js +1 -2
- package/lib/semantic-analysis/install/libxmljs.js +1 -1
- package/package.json +19 -17
- package/lib/blocker.js +0 -61
- package/lib/error-handlers/common-handler.js +0 -40
- package/lib/error-handlers/constants.js +0 -20
- package/lib/error-handlers/index.js +0 -39
- package/lib/error-handlers/init-domain.js +0 -26
- package/lib/error-handlers/install/express.js +0 -165
- package/lib/error-handlers/install/fastify.js +0 -101
- package/lib/error-handlers/install/hapi.js +0 -74
- package/lib/error-handlers/install/koa.js +0 -67
- package/lib/error-handlers/install/restify.js +0 -52
- package/lib/security-exception.js +0 -27
- package/lib/throw-security-exception.js +0 -39
package/README.md
CHANGED
|
@@ -1,9 +1,42 @@
|
|
|
1
1
|
# `@contrast/protect`
|
|
2
2
|
|
|
3
|
+
Runtime application protection for the Contrast Node.js agent. Enabled by the
|
|
4
|
+
`protect.enable` configuration option. Attacks that Protect detects are blocked
|
|
5
|
+
through `@contrast/blocking`.
|
|
6
|
+
|
|
3
7
|
## Sub-Components
|
|
4
8
|
|
|
9
|
+
### Policy
|
|
10
|
+
|
|
11
|
+
`lib/policy.js` resolves which rules are active and in which mode, from
|
|
12
|
+
`protect.rules.<rule_id>.mode`, `protect.rules.disabled_rules`, and Contrast UI
|
|
13
|
+
settings. Per-request exclusions narrow the active rules further.
|
|
14
|
+
|
|
5
15
|
### Request Correlation
|
|
6
16
|
|
|
17
|
+
`lib/make-source-context.js` and `lib/get-source-context.js` create and look up
|
|
18
|
+
the per-request context that the other sub-components share.
|
|
19
|
+
|
|
7
20
|
### Input Analysis
|
|
8
21
|
|
|
22
|
+
`lib/input-analysis/` inspects request inputs (headers, query strings, cookies,
|
|
23
|
+
bodies, uploads) as each web framework or body parser produces them, with one
|
|
24
|
+
installer per library in `lib/input-analysis/install/`. It also
|
|
25
|
+
handles IP allow and deny lists and virtual patches.
|
|
26
|
+
|
|
9
27
|
### Input Tracing
|
|
28
|
+
|
|
29
|
+
`lib/input-tracing/` checks whether untrusted input reaches dangerous sinks:
|
|
30
|
+
`child_process`, `eval`, `Function`, `vm`, `fs`, outbound HTTP, and database
|
|
31
|
+
drivers such as `mongodb`, `mysql`, `postgres`, `mssql`, `sqlite3`, and
|
|
32
|
+
`sequelize`.
|
|
33
|
+
|
|
34
|
+
### Semantic Analysis
|
|
35
|
+
|
|
36
|
+
`lib/semantic-analysis/` analyzes the structure of operations rather than only
|
|
37
|
+
their inputs, e.g. XML parsing with `libxmljs`.
|
|
38
|
+
|
|
39
|
+
### Hardening
|
|
40
|
+
|
|
41
|
+
`lib/hardening/` protects inherently unsafe library calls, e.g. `node-serialize`
|
|
42
|
+
deserialization.
|
|
@@ -29,7 +29,6 @@ module.exports = function(core) {
|
|
|
29
29
|
protect,
|
|
30
30
|
protect: {
|
|
31
31
|
hardening,
|
|
32
|
-
throwSecurityException,
|
|
33
32
|
},
|
|
34
33
|
stackTraceFactory,
|
|
35
34
|
} = core;
|
|
@@ -59,7 +58,7 @@ module.exports = function(core) {
|
|
|
59
58
|
|
|
60
59
|
protect.reportFinding({ findings, result, sinkContext });
|
|
61
60
|
|
|
62
|
-
if (blockInfo) throwSecurityException(sourceContext);
|
|
61
|
+
if (blockInfo) protect.throwSecurityException(sourceContext);
|
|
63
62
|
}
|
|
64
63
|
|
|
65
64
|
hardening.handleUntrustedDeserialization = function (sourceContext, sinkContext) {
|
package/lib/index.d.ts
CHANGED
|
@@ -14,6 +14,15 @@
|
|
|
14
14
|
*/
|
|
15
15
|
|
|
16
16
|
import { ReqData, ProtectMessage, ResultMap, ProtectRuleMode, Blocker, ProtectFindingEventArg } from '@contrast/common';
|
|
17
|
+
import { Config } from '@contrast/config';
|
|
18
|
+
import { Core as _Core } from '@contrast/core';
|
|
19
|
+
import { Core as BlockingCore } from '@contrast/blocking';
|
|
20
|
+
import { DepHooks } from '@contrast/dep-hooks';
|
|
21
|
+
import { Logger } from '@contrast/logger';
|
|
22
|
+
import { Patcher } from '@contrast/patcher';
|
|
23
|
+
import { Rewriter } from '@contrast/rewriter';
|
|
24
|
+
import { Scopes } from '@contrast/scopes';
|
|
25
|
+
import { Sources } from '@contrast/sources';
|
|
17
26
|
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
18
27
|
import * as http from 'node:http';
|
|
19
28
|
import * as https from 'node:https';
|
|
@@ -22,6 +31,29 @@ import { Domain } from 'async-hook-domain';
|
|
|
22
31
|
type Http = typeof http;
|
|
23
32
|
type Https = typeof https;
|
|
24
33
|
|
|
34
|
+
/**
|
|
35
|
+
* `@contrast/blocking` doesn't export its `Blocking` class directly (see its
|
|
36
|
+
* own `index.d.ts`), so its instance type is pulled off the `blocking` field
|
|
37
|
+
* of blocking's own augmented `Core`.
|
|
38
|
+
*/
|
|
39
|
+
type Blocking = BlockingCore['blocking'];
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Augments `@contrast/core`'s `Core` with the `protect` component and the
|
|
43
|
+
* other agent services `protect` consumes, per `package.json` dependencies.
|
|
44
|
+
*/
|
|
45
|
+
export interface Core extends _Core {
|
|
46
|
+
readonly protect: Protect;
|
|
47
|
+
readonly blocking: Blocking;
|
|
48
|
+
readonly config: Config;
|
|
49
|
+
readonly logger: Logger;
|
|
50
|
+
readonly patcher: Patcher;
|
|
51
|
+
readonly depHooks: DepHooks;
|
|
52
|
+
readonly rewriter: Rewriter;
|
|
53
|
+
readonly scopes: Scopes;
|
|
54
|
+
readonly sources: Sources;
|
|
55
|
+
}
|
|
56
|
+
|
|
25
57
|
export type Block = (mode: string, ruleId: string) => void;
|
|
26
58
|
export interface ProtectRequestStore {
|
|
27
59
|
reqData: ReqData;
|
package/lib/index.js
CHANGED
|
@@ -16,27 +16,17 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
const { isMainThread } = require('node:worker_threads');
|
|
19
|
-
const { callChildComponentMethodsSync, Event } = require('@contrast/common');
|
|
19
|
+
const { callChildComponentMethodsSync, Event, Feature } = require('@contrast/common');
|
|
20
20
|
const { ConfigSource } = require('@contrast/config');
|
|
21
21
|
|
|
22
|
+
/** @param {import('.').Core} core */
|
|
22
23
|
module.exports = function(core) {
|
|
23
|
-
const { config, logger, sources } = core;
|
|
24
|
+
const { config, logger, rewriter, sources } = core;
|
|
24
25
|
|
|
25
26
|
const protect = core.protect = {
|
|
26
27
|
agentLib: module.exports.instantiateAgentLib(),
|
|
27
28
|
};
|
|
28
29
|
|
|
29
|
-
require('./policy')(core);
|
|
30
|
-
require('./throw-security-exception')(core);
|
|
31
|
-
require('./blocker')(core);
|
|
32
|
-
require('./make-source-context')(core);
|
|
33
|
-
require('./get-source-context')(core);
|
|
34
|
-
require('./error-handlers')(core);
|
|
35
|
-
require('./input-analysis')(core);
|
|
36
|
-
require('./input-tracing')(core);
|
|
37
|
-
require('./hardening')(core);
|
|
38
|
-
require('./semantic-analysis')(core);
|
|
39
|
-
|
|
40
30
|
protect.install = function install() {
|
|
41
31
|
if (!config.getEffectiveValue('entitlements.protect.allow')) {
|
|
42
32
|
logger.debug('Protect is disabled: not allowed from entitlements');
|
|
@@ -56,7 +46,8 @@ module.exports = function(core) {
|
|
|
56
46
|
return;
|
|
57
47
|
}
|
|
58
48
|
|
|
59
|
-
|
|
49
|
+
rewriter.modes.add('protect');
|
|
50
|
+
|
|
60
51
|
callChildComponentMethodsSync(protect, 'install');
|
|
61
52
|
};
|
|
62
53
|
|
|
@@ -72,6 +63,21 @@ module.exports = function(core) {
|
|
|
72
63
|
});
|
|
73
64
|
};
|
|
74
65
|
|
|
66
|
+
protect.throwSecurityException = function throwSecurityException(sourceContext) {
|
|
67
|
+
if (!sourceContext) return;
|
|
68
|
+
|
|
69
|
+
const { securityException: [mode, ruleId] } = sourceContext;
|
|
70
|
+
core.blocking.throwSecurityException(Feature.PROTECT, mode, { ruleId });
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
require('./policy')(core);
|
|
74
|
+
require('./make-source-context')(core);
|
|
75
|
+
require('./get-source-context')(core);
|
|
76
|
+
require('./input-analysis')(core);
|
|
77
|
+
require('./input-tracing')(core);
|
|
78
|
+
require('./hardening')(core);
|
|
79
|
+
require('./semantic-analysis')(core);
|
|
80
|
+
|
|
75
81
|
return protect;
|
|
76
82
|
};
|
|
77
83
|
|
|
@@ -121,7 +127,7 @@ const skeletalAgentLib = {
|
|
|
121
127
|
version: '8.2.0',
|
|
122
128
|
};
|
|
123
129
|
/**
|
|
124
|
-
* Create an instance of agent-lib and attach the constants directly to it.
|
|
130
|
+
1 * Create an instance of agent-lib and attach the constants directly to it.
|
|
125
131
|
*
|
|
126
132
|
* @param {Object} lib the value returned by require('@contrast/agent-lib')
|
|
127
133
|
* @returns {Object} an agent-lib instance with the constants attached to it.
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
const { primordials: { JSONParse } } = require('@contrast/common');
|
|
19
|
-
const { isSecurityException } = require('
|
|
19
|
+
const { SecurityException: { isSecurityException } } = require('@contrast/blocking');
|
|
20
20
|
|
|
21
21
|
module.exports = (core) => {
|
|
22
22
|
const {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
const { patchType } = require('../constants');
|
|
19
|
-
const { isSecurityException } = require('
|
|
19
|
+
const { SecurityException: { isSecurityException } } = require('@contrast/blocking');
|
|
20
20
|
|
|
21
21
|
module.exports = (core) => {
|
|
22
22
|
const {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
const { patchType } = require('../constants');
|
|
19
|
-
const { isSecurityException } = require('
|
|
19
|
+
const { SecurityException: { isSecurityException } } = require('@contrast/blocking');
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Function that exports an install method to patch Express framework with our instrumentation
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
const { patchType } = require('../constants');
|
|
19
|
-
const { isSecurityException } = require('
|
|
19
|
+
const { SecurityException: { isSecurityException } } = require('@contrast/blocking');
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Function that exports an install method to patch Fastify framework with our instrumentation
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
const { patchType } = require('../constants');
|
|
19
|
-
const { isSecurityException } = require('
|
|
19
|
+
const { SecurityException: { isSecurityException } } = require('@contrast/blocking');
|
|
20
20
|
|
|
21
21
|
module.exports = (core) => {
|
|
22
22
|
const {
|
|
@@ -16,7 +16,8 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
const onFinished = require('on-finished');
|
|
19
|
-
const { primordials: { StringPrototypeToLowerCase, ArrayPrototypeSlice } } = require('@contrast/common');
|
|
19
|
+
const { Feature, primordials: { StringPrototypeToLowerCase, ArrayPrototypeSlice } } = require('@contrast/common');
|
|
20
|
+
const { SecurityException } = require('@contrast/blocking');
|
|
20
21
|
const { patchType } = require('../constants');
|
|
21
22
|
|
|
22
23
|
module.exports = function (core) {
|
|
@@ -24,10 +25,8 @@ module.exports = function (core) {
|
|
|
24
25
|
logger,
|
|
25
26
|
scopes: { sources },
|
|
26
27
|
instrumentation: { instrument },
|
|
27
|
-
protect: {
|
|
28
|
-
|
|
29
|
-
errorHandlers: { initDomain }
|
|
30
|
-
},
|
|
28
|
+
protect: { inputAnalysis },
|
|
29
|
+
blocking,
|
|
31
30
|
} = core;
|
|
32
31
|
|
|
33
32
|
function removeCookies(rawHeaders) {
|
|
@@ -46,7 +45,7 @@ module.exports = function (core) {
|
|
|
46
45
|
|
|
47
46
|
function callNext() {
|
|
48
47
|
setImmediate(() => {
|
|
49
|
-
initDomain();
|
|
48
|
+
blocking.initDomain();
|
|
50
49
|
next.call(data.obj, ...data.args);
|
|
51
50
|
});
|
|
52
51
|
}
|
|
@@ -104,8 +103,12 @@ module.exports = function (core) {
|
|
|
104
103
|
|
|
105
104
|
if (!block) {
|
|
106
105
|
callNext();
|
|
106
|
+
} else if (!store.blocker) {
|
|
107
|
+
logger.info({ funcKey: data.funcKey }, 'SecurityException detected but request store blocker unavailable');
|
|
108
|
+
callNext();
|
|
107
109
|
} else {
|
|
108
|
-
|
|
110
|
+
const [mode, ruleId] = block;
|
|
111
|
+
store.blocker.block(new SecurityException(Feature.PROTECT, mode, { ruleId }));
|
|
109
112
|
logger.debug({ block, funcKey: data.funcKey }, 'request blocked by not emitting request event');
|
|
110
113
|
}
|
|
111
114
|
}
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
const { patchType } = require('../constants');
|
|
19
|
-
const { isSecurityException } = require('
|
|
19
|
+
const { SecurityException: { isSecurityException } } = require('@contrast/blocking');
|
|
20
20
|
|
|
21
21
|
module.exports = (core) => {
|
|
22
22
|
const {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
|
-
const { isSecurityException } = require('
|
|
18
|
+
const { SecurityException: { isSecurityException } } = require('@contrast/blocking');
|
|
19
19
|
const { patchType } = require('../constants');
|
|
20
20
|
|
|
21
21
|
module.exports = function init(core) {
|
|
@@ -33,7 +33,6 @@ module.exports = function(core) {
|
|
|
33
33
|
protect: {
|
|
34
34
|
agentLib,
|
|
35
35
|
inputTracing,
|
|
36
|
-
throwSecurityException
|
|
37
36
|
},
|
|
38
37
|
stackTraceFactory
|
|
39
38
|
} = core;
|
|
@@ -58,7 +57,7 @@ module.exports = function(core) {
|
|
|
58
57
|
|
|
59
58
|
protect.reportFinding(eventArg);
|
|
60
59
|
|
|
61
|
-
if (blockInfo) throwSecurityException(sourceContext);
|
|
60
|
+
if (blockInfo) protect.throwSecurityException(sourceContext);
|
|
62
61
|
}
|
|
63
62
|
|
|
64
63
|
inputTracing.handlePathTraversal = function(sourceContext, sinkContext) {
|
|
@@ -29,15 +29,13 @@ module.exports = function(core) {
|
|
|
29
29
|
function makeSourceContext({
|
|
30
30
|
store: { sourceInfo },
|
|
31
31
|
// incomingMessage,
|
|
32
|
-
serverResponse,
|
|
32
|
+
// serverResponse,
|
|
33
33
|
}) {
|
|
34
34
|
const policy = protect.getPolicy({ uriPath: sourceInfo.uriPath });
|
|
35
35
|
const protectStore = {
|
|
36
36
|
resData: {
|
|
37
37
|
statusCode: null,
|
|
38
38
|
},
|
|
39
|
-
// block closure captures res so it isn't exposed to beyond here
|
|
40
|
-
blocker: new core.protect.Blocker(serverResponse),
|
|
41
39
|
policy,
|
|
42
40
|
exclusions: [],
|
|
43
41
|
virtualPatchesEvaluators: [],
|
|
@@ -53,7 +51,6 @@ module.exports = function(core) {
|
|
|
53
51
|
protectStore.allowed = true;
|
|
54
52
|
}
|
|
55
53
|
|
|
56
|
-
|
|
57
54
|
return protectStore;
|
|
58
55
|
}
|
|
59
56
|
|
|
@@ -48,7 +48,6 @@ module.exports = function(core) {
|
|
|
48
48
|
protect: {
|
|
49
49
|
agentLib,
|
|
50
50
|
semanticAnalysis,
|
|
51
|
-
throwSecurityException
|
|
52
51
|
},
|
|
53
52
|
stackTraceFactory,
|
|
54
53
|
} = core;
|
|
@@ -81,7 +80,7 @@ module.exports = function(core) {
|
|
|
81
80
|
|
|
82
81
|
protect.reportFinding({ findings, result, sinkContext });
|
|
83
82
|
|
|
84
|
-
if (blockInfo) throwSecurityException(sourceContext);
|
|
83
|
+
if (blockInfo) protect.throwSecurityException(sourceContext);
|
|
85
84
|
}
|
|
86
85
|
|
|
87
86
|
/**
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
18
|
const { isString } = require('@contrast/common');
|
|
19
|
-
const SecurityException = require('
|
|
19
|
+
const { SecurityException } = require('@contrast/blocking');
|
|
20
20
|
const { patchType } = require('../constants');
|
|
21
21
|
|
|
22
22
|
module.exports = function (core) {
|
package/package.json
CHANGED
|
@@ -1,37 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrast/protect",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.85.0",
|
|
4
|
+
"description": "Runtime application protection (Protect)",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
|
|
7
7
|
"files": [
|
|
8
8
|
"lib/",
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
9
|
+
"!**/*.test.*",
|
|
10
|
+
"!**/tsconfig.*",
|
|
11
|
+
"!**/*.map"
|
|
12
12
|
],
|
|
13
13
|
"main": "lib/index.js",
|
|
14
14
|
"types": "lib/index.d.ts",
|
|
15
15
|
"engines": {
|
|
16
16
|
"npm": ">=8.5.5",
|
|
17
|
-
"node": ">=
|
|
17
|
+
"node": ">=20.9.0"
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
20
|
"test": "bash ../scripts/test.sh"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"@contrast/agent-lib": "9.2.0",
|
|
24
|
-
"@contrast/
|
|
25
|
-
"@contrast/
|
|
26
|
-
"@contrast/
|
|
27
|
-
"@contrast/
|
|
28
|
-
"@contrast/
|
|
29
|
-
"@contrast/
|
|
30
|
-
"@contrast/
|
|
31
|
-
"@contrast/
|
|
32
|
-
"@contrast/
|
|
33
|
-
"@contrast/
|
|
34
|
-
"@contrast/
|
|
24
|
+
"@contrast/blocking": "1.2.0",
|
|
25
|
+
"@contrast/common": "1.44.0",
|
|
26
|
+
"@contrast/config": "1.65.0",
|
|
27
|
+
"@contrast/core": "1.70.0",
|
|
28
|
+
"@contrast/dep-hooks": "1.40.0",
|
|
29
|
+
"@contrast/esm-hooks": "2.46.0",
|
|
30
|
+
"@contrast/instrumentation": "1.50.0",
|
|
31
|
+
"@contrast/logger": "1.43.0",
|
|
32
|
+
"@contrast/patcher": "1.42.0",
|
|
33
|
+
"@contrast/rewriter": "1.47.0",
|
|
34
|
+
"@contrast/scopes": "1.41.0",
|
|
35
|
+
"@contrast/sources": "1.16.0",
|
|
36
|
+
"@contrast/stack-trace-factory": "1.12.0",
|
|
35
37
|
"async-hook-domain": "4.0.1",
|
|
36
38
|
"ipaddr.js": "2.5.0",
|
|
37
39
|
"on-finished": "2.4.1",
|
package/lib/blocker.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright: 2026 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 {
|
|
19
|
-
primordials: { StringPrototypeToUpperCase },
|
|
20
|
-
symbols: { kMetrics }
|
|
21
|
-
} = require('@contrast/common');
|
|
22
|
-
|
|
23
|
-
module.exports = function(core) {
|
|
24
|
-
// i think this should be a weakset. we don't want to accumulate
|
|
25
|
-
// blocked requests for the entire life of a program. the req
|
|
26
|
-
// object is not exactly tiny.
|
|
27
|
-
const blocked = new WeakSet();
|
|
28
|
-
const { patcher, logger } = core;
|
|
29
|
-
|
|
30
|
-
class Blocker {
|
|
31
|
-
#res;
|
|
32
|
-
|
|
33
|
-
constructor(response) {
|
|
34
|
-
this.#res = response;
|
|
35
|
-
this._blocked = blocked; //expose for testing
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
block(mode, ruleId) {
|
|
39
|
-
if (blocked.has(this.#res)) return;
|
|
40
|
-
|
|
41
|
-
blocked.add(this.#res);
|
|
42
|
-
mode = StringPrototypeToUpperCase.call(mode);
|
|
43
|
-
const end = patcher.unwrap(this.#res.end);
|
|
44
|
-
const writeHead = patcher.unwrap(this.#res.writeHead);
|
|
45
|
-
|
|
46
|
-
try {
|
|
47
|
-
clearTimeout(this.#res[kMetrics]?.timeout);
|
|
48
|
-
delete this.#res[kMetrics];
|
|
49
|
-
|
|
50
|
-
if (!this.#res.headersSent) writeHead.call(this.#res, 403);
|
|
51
|
-
end.call(this.#res, '');
|
|
52
|
-
logger.info({ mode, ruleId }, 'Request blocked');
|
|
53
|
-
} catch (err) {
|
|
54
|
-
logger.error({ err, mode, ruleId }, 'Error blocking request');
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
core.protect.Blocker = Blocker;
|
|
59
|
-
|
|
60
|
-
return Blocker;
|
|
61
|
-
};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright: 2026 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 { isSecurityException } = require('../security-exception');
|
|
19
|
-
|
|
20
|
-
module.exports = function (core) {
|
|
21
|
-
const {
|
|
22
|
-
logger,
|
|
23
|
-
protect: { getSourceContext },
|
|
24
|
-
} = core;
|
|
25
|
-
|
|
26
|
-
return core.protect.errorHandlers.commonHandler = function (err) {
|
|
27
|
-
if (!isSecurityException(err)) {
|
|
28
|
-
throw err;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const sourceContext = getSourceContext();
|
|
32
|
-
if (!sourceContext) {
|
|
33
|
-
logger.info('SecurityException caught by Contrast but Protect store is unavailable for req handling');
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const blockInfo = sourceContext.securityException;
|
|
38
|
-
sourceContext.blocker.block(...blockInfo);
|
|
39
|
-
};
|
|
40
|
-
};
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright: 2026 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-error-handling'
|
|
20
|
-
};
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright: 2026 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 { callChildComponentMethodsSync } = require('@contrast/common');
|
|
19
|
-
|
|
20
|
-
module.exports = function(core) {
|
|
21
|
-
const errorHandlers = core.protect.errorHandlers = {};
|
|
22
|
-
|
|
23
|
-
// api
|
|
24
|
-
require('./common-handler')(core);
|
|
25
|
-
require('./init-domain')(core);
|
|
26
|
-
|
|
27
|
-
// installers
|
|
28
|
-
require('./install/express')(core);
|
|
29
|
-
require('./install/fastify')(core);
|
|
30
|
-
require('./install/hapi')(core);
|
|
31
|
-
require('./install/koa')(core);
|
|
32
|
-
require('./install/restify')(core);
|
|
33
|
-
|
|
34
|
-
errorHandlers.install = function() {
|
|
35
|
-
callChildComponentMethodsSync(errorHandlers, 'install');
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
return errorHandlers;
|
|
39
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright: 2026 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 { Domain } = require('async-hook-domain');
|
|
19
|
-
|
|
20
|
-
module.exports = function(core) {
|
|
21
|
-
const { protect: { errorHandlers } } = core;
|
|
22
|
-
|
|
23
|
-
return errorHandlers.initDomain = function initDomain() {
|
|
24
|
-
return new Domain(errorHandlers.commonHandler);
|
|
25
|
-
};
|
|
26
|
-
};
|
|
@@ -1,165 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright: 2026 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
|
-
module.exports = function (core) {
|
|
22
|
-
const {
|
|
23
|
-
logger,
|
|
24
|
-
depHooks,
|
|
25
|
-
patcher,
|
|
26
|
-
protect,
|
|
27
|
-
} = core;
|
|
28
|
-
|
|
29
|
-
const expressErrorHandler = protect.errorHandlers.expressErrorHandler = {};
|
|
30
|
-
|
|
31
|
-
function checkSecurityException(err, funcKey, orig, throwErr = false) {
|
|
32
|
-
const sourceContext = protect.getSourceContext();
|
|
33
|
-
const isSecurityException = SecurityException.isSecurityException(err);
|
|
34
|
-
|
|
35
|
-
if (isSecurityException && sourceContext) {
|
|
36
|
-
const blockInfo = sourceContext.securityException;
|
|
37
|
-
|
|
38
|
-
sourceContext.blocker.block(...blockInfo);
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (!sourceContext && isSecurityException) {
|
|
43
|
-
logger.info({ funcKey }, 'source context not found; unable to handle response');
|
|
44
|
-
return;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (throwErr) {
|
|
48
|
-
throw err;
|
|
49
|
-
} else {
|
|
50
|
-
return orig();
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
expressErrorHandler.install = function () {
|
|
55
|
-
|
|
56
|
-
// Express 4 and 5
|
|
57
|
-
depHooks.resolve({ name: 'finalhandler', version: '<3' }, (finalhandler) =>
|
|
58
|
-
patcher.patch(finalhandler, {
|
|
59
|
-
name: 'finalHandler',
|
|
60
|
-
patchType,
|
|
61
|
-
post(data) {
|
|
62
|
-
// no need to perform this runtime patch if protect is disabled
|
|
63
|
-
if (!core.config.getEffectiveValue('protect.enable')) return;
|
|
64
|
-
|
|
65
|
-
data.result = patcher.patch(data.result, {
|
|
66
|
-
name: 'finalHandler.returnedFunction',
|
|
67
|
-
patchType,
|
|
68
|
-
around(orig, data) {
|
|
69
|
-
const [err] = data.args;
|
|
70
|
-
checkSecurityException(err, data.funcKey, orig);
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
},
|
|
74
|
-
})
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
function postHook() {
|
|
78
|
-
return function(data) {
|
|
79
|
-
patcher.patch(data.result, 'handle', {
|
|
80
|
-
name: 'handle',
|
|
81
|
-
patchType,
|
|
82
|
-
post(data) {
|
|
83
|
-
data.result = data.result?.catch?.((err) => {
|
|
84
|
-
checkSecurityException(err, data.funcKey, data.orig, true);
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
// Express 4
|
|
92
|
-
depHooks.resolve({ name: 'express', version: '4', file: 'lib/router/layer.js' }, (Layer) => {
|
|
93
|
-
|
|
94
|
-
patcher.patch(Layer.prototype, 'handle_error', {
|
|
95
|
-
name: 'Layer.prototype.handle_error',
|
|
96
|
-
patchType,
|
|
97
|
-
around(orig, data) {
|
|
98
|
-
const [err] = data.args;
|
|
99
|
-
checkSecurityException(err, data.funcKey, orig);
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
return patcher.patch(Layer, {
|
|
104
|
-
name: 'Layer',
|
|
105
|
-
patchType,
|
|
106
|
-
post: postHook()
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
// Express 5
|
|
112
|
-
depHooks.resolve({ name: 'router', version: '2', file: 'lib/layer.js' }, (Layer) => {
|
|
113
|
-
|
|
114
|
-
patcher.patch(Layer.prototype, 'handleError', {
|
|
115
|
-
name: 'Layer.prototype.handleError',
|
|
116
|
-
patchType,
|
|
117
|
-
around(orig, data) {
|
|
118
|
-
const [err] = data.args;
|
|
119
|
-
checkSecurityException(err, data.funcKey, orig);
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
return patcher.patch(Layer, {
|
|
124
|
-
name: 'router.Layer',
|
|
125
|
-
patchType,
|
|
126
|
-
post: postHook()
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
function preHook() {
|
|
131
|
-
return function(data) {
|
|
132
|
-
if (typeof data.args[1] === 'function') {
|
|
133
|
-
patcher.patch(data.args, '1', {
|
|
134
|
-
name: 'express.route-handler',
|
|
135
|
-
patchType,
|
|
136
|
-
post(data) {
|
|
137
|
-
data.result = data.result?.catch?.((err) => {
|
|
138
|
-
checkSecurityException(err, data.funcKey, data.orig, true);
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Express 4
|
|
147
|
-
depHooks.resolve({ name: 'express', version: '4', file: 'lib/router/index.js' }, (Router) => {
|
|
148
|
-
patcher.patch(Router.prototype.constructor, 'param', {
|
|
149
|
-
name: 'Router.prototype.constructor.param',
|
|
150
|
-
patchType,
|
|
151
|
-
pre: preHook()
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
// Express 5
|
|
156
|
-
depHooks.resolve({ name: 'router', version: '2' }, (Router) => {
|
|
157
|
-
patcher.patch(Router.prototype, 'param', {
|
|
158
|
-
name: 'Router.prototype.param',
|
|
159
|
-
patchType,
|
|
160
|
-
pre: preHook()
|
|
161
|
-
});
|
|
162
|
-
});
|
|
163
|
-
};
|
|
164
|
-
return expressErrorHandler;
|
|
165
|
-
};
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright: 2026 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 { isSecurityException } = require('../../security-exception');
|
|
19
|
-
const { patchType } = require('../constants');
|
|
20
|
-
|
|
21
|
-
module.exports = function (core) {
|
|
22
|
-
const {
|
|
23
|
-
depHooks,
|
|
24
|
-
patcher,
|
|
25
|
-
protect,
|
|
26
|
-
} = core;
|
|
27
|
-
|
|
28
|
-
const fastifyErrorHandler = protect.errorHandlers.fastifyErrorHandler = {
|
|
29
|
-
_userHandler: null
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* This is the default handler from fastify's source code. If it's not a
|
|
34
|
-
* Contrast error and the user didn't supply their own we should use this.
|
|
35
|
-
*/
|
|
36
|
-
fastifyErrorHandler.defaultErrorHandler = function (error, request, reply) {
|
|
37
|
-
if (reply.statusCode < 500) {
|
|
38
|
-
reply.log.info({ res: reply, err: error }, error && error.message);
|
|
39
|
-
} else {
|
|
40
|
-
reply.log.error(
|
|
41
|
-
{ req: request, res: reply, err: error },
|
|
42
|
-
error && error.message
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
reply.send(error);
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Check if the error being handled was thrown by Contrast. If not,
|
|
50
|
-
* use either the default fastify error handler or the user-defined handler
|
|
51
|
-
* if one was specified by calling fastify.setErrorHandler(fn).
|
|
52
|
-
* @param {error} err error being handled
|
|
53
|
-
* @param {object} request fastify request
|
|
54
|
-
* @param {object} reply fastify repoly
|
|
55
|
-
*/
|
|
56
|
-
fastifyErrorHandler.handler = function (err, request, reply) {
|
|
57
|
-
const normalHandler = fastifyErrorHandler._userHandler || fastifyErrorHandler.defaultErrorHandler;
|
|
58
|
-
|
|
59
|
-
if (isSecurityException(err)) {
|
|
60
|
-
const sourceContext = protect.getSourceContext();
|
|
61
|
-
|
|
62
|
-
if (!sourceContext) {
|
|
63
|
-
normalHandler.call(this, err, request, reply);
|
|
64
|
-
} else {
|
|
65
|
-
const blockInfo = sourceContext.securityException;
|
|
66
|
-
sourceContext.blocker.block(...blockInfo);
|
|
67
|
-
}
|
|
68
|
-
} else {
|
|
69
|
-
normalHandler.call(this, err, request, reply);
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Instruments fastify in order to add our custom error handler.
|
|
75
|
-
*/
|
|
76
|
-
fastifyErrorHandler.install = function () {
|
|
77
|
-
depHooks.resolve({ name: 'fastify', version: '>=4 <6' }, (fastify) => patcher.patch(fastify, {
|
|
78
|
-
name: 'fastify',
|
|
79
|
-
patchType,
|
|
80
|
-
post(data) {
|
|
81
|
-
const { result: server } = data;
|
|
82
|
-
// Set our custom handler initially
|
|
83
|
-
server.setErrorHandler(fastifyErrorHandler.handler);
|
|
84
|
-
|
|
85
|
-
// Patch, so that if someone sets their own, we override with ours. But,
|
|
86
|
-
// we do need to keep a reference to it so we can still call it for when
|
|
87
|
-
// non-Contrast errors need handling.
|
|
88
|
-
patcher.patch(server, 'setErrorHandler', {
|
|
89
|
-
name: 'fastify.setErrorHandler',
|
|
90
|
-
patchType,
|
|
91
|
-
pre({ args }) {
|
|
92
|
-
fastifyErrorHandler._userHandler = args[0];
|
|
93
|
-
args[0] = fastifyErrorHandler.handler;
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
}));
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
return fastifyErrorHandler;
|
|
101
|
-
};
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright: 2026 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 hapiErrorHandler = protect.errorHandlers.hapiErrorHandler = {};
|
|
31
|
-
|
|
32
|
-
const registerErrorHandler = (boom, name) => {
|
|
33
|
-
patcher.patch(boom, 'boomify', {
|
|
34
|
-
name: `${name}.boomify`,
|
|
35
|
-
patchType,
|
|
36
|
-
post(data) {
|
|
37
|
-
const [err] = data.args;
|
|
38
|
-
const sourceContext = protect.getSourceContext();
|
|
39
|
-
const isSecurityException = SecurityException.isSecurityException(err);
|
|
40
|
-
|
|
41
|
-
if (isSecurityException && sourceContext && err.output.statusCode !== 403) {
|
|
42
|
-
const [mode, ruleId] = sourceContext.securityException;
|
|
43
|
-
|
|
44
|
-
err.output.statusCode = 403;
|
|
45
|
-
err.reformat();
|
|
46
|
-
err.output.payload = undefined;
|
|
47
|
-
data.result = err;
|
|
48
|
-
|
|
49
|
-
logger.info({ funcKey: data.funcKey, mode, ruleId }, 'Request blocked');
|
|
50
|
-
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (!sourceContext && isSecurityException) {
|
|
55
|
-
logger.info({ funcKey: data.funcKey }, 'source context not found; unable to handle response');
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
hapiErrorHandler.install = function () {
|
|
62
|
-
depHooks.resolve(
|
|
63
|
-
{ name: 'boom', version: '<8' },
|
|
64
|
-
(boom) => registerErrorHandler(boom, 'boom'),
|
|
65
|
-
);
|
|
66
|
-
|
|
67
|
-
depHooks.resolve(
|
|
68
|
-
{ name: '@hapi/boom', version: '<11' },
|
|
69
|
-
(boom) => registerErrorHandler(boom, '@hapi/boom'),
|
|
70
|
-
);
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
return hapiErrorHandler;
|
|
74
|
-
};
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright: 2026 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 koaErrorHandler = protect.errorHandlers.koaErrorHandler = {};
|
|
31
|
-
|
|
32
|
-
koaErrorHandler.install = function () {
|
|
33
|
-
depHooks.resolve({ name: 'koa', version: '>=2.3.0 <4' }, (Koa) => {
|
|
34
|
-
patcher.patch(Koa.prototype, 'handleRequest', {
|
|
35
|
-
name: 'Koa.Application.handleRequest',
|
|
36
|
-
patchType,
|
|
37
|
-
pre(data) {
|
|
38
|
-
const [ctx] = data.args;
|
|
39
|
-
patcher.patch(ctx, 'onerror', {
|
|
40
|
-
name: 'koa.ctx.onerror',
|
|
41
|
-
patchType,
|
|
42
|
-
around(orig, data) {
|
|
43
|
-
const [err] = data.args;
|
|
44
|
-
const sourceContext = protect.getSourceContext();
|
|
45
|
-
const isSecurityException = SecurityException.isSecurityException(err);
|
|
46
|
-
|
|
47
|
-
if (isSecurityException && sourceContext) {
|
|
48
|
-
data.obj.body = '';
|
|
49
|
-
const blockInfo = sourceContext.securityException;
|
|
50
|
-
sourceContext.blocker.block(...blockInfo);
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (!sourceContext && isSecurityException) {
|
|
55
|
-
logger.info({ funcKey: data.funcKey }, 'source context not found; unable to handle response');
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return orig();
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
return koaErrorHandler;
|
|
67
|
-
};
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright: 2026 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 { isSecurityException } = require('../../security-exception');
|
|
19
|
-
const { patchType } = require('../constants');
|
|
20
|
-
|
|
21
|
-
module.exports = function init(core) {
|
|
22
|
-
const { depHooks, logger, patcher, protect } = core;
|
|
23
|
-
|
|
24
|
-
return protect.errorHandlers.restifyErrorHandler = {
|
|
25
|
-
install() {
|
|
26
|
-
depHooks.resolve(
|
|
27
|
-
{ name: 'restify', file: 'lib/server.js', version: '>=8 <12' },
|
|
28
|
-
(Server) => {
|
|
29
|
-
patcher.patch(Server.prototype, '_onHandlerError', {
|
|
30
|
-
name: 'restify.Server.prototype._onHandlerError',
|
|
31
|
-
patchType,
|
|
32
|
-
around(orig, { args: [err], funcKey }) {
|
|
33
|
-
if (isSecurityException(err)) {
|
|
34
|
-
const sourceContext = protect.getSourceContext();
|
|
35
|
-
|
|
36
|
-
if (!sourceContext) {
|
|
37
|
-
logger.info({ funcKey }, 'source context not found; unable to handle response');
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
sourceContext.blocker.block(...sourceContext.securityException);
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return orig();
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
};
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright: 2026 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 = class SecurityException extends Error {
|
|
19
|
-
static create() {
|
|
20
|
-
const err = new SecurityException('SecurityException');
|
|
21
|
-
return err;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
static isSecurityException(err) {
|
|
25
|
-
return err instanceof SecurityException;
|
|
26
|
-
}
|
|
27
|
-
};
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright: 2026 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
|
-
|
|
20
|
-
module.exports = function(core) {
|
|
21
|
-
const { logger, protect } = core;
|
|
22
|
-
|
|
23
|
-
function throwSecurityException(sourceContext) {
|
|
24
|
-
if (!sourceContext) return;
|
|
25
|
-
|
|
26
|
-
const {
|
|
27
|
-
securityException: [mode, ruleId]
|
|
28
|
-
} = sourceContext;
|
|
29
|
-
|
|
30
|
-
const err = securityException.create();
|
|
31
|
-
|
|
32
|
-
logger.debug({ err, mode, ruleId }, 'throwing security exception');
|
|
33
|
-
|
|
34
|
-
// TO-DO: NODE-2556 Research fail-safe handler for all security exceptions
|
|
35
|
-
throw err;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
return protect.throwSecurityException = throwSecurityException;
|
|
39
|
-
};
|