@contrast/protect 1.39.0 → 1.40.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.
Files changed (58) hide show
  1. package/lib/error-handlers/common-handler.test.js +52 -0
  2. package/lib/error-handlers/index.test.js +32 -0
  3. package/lib/error-handlers/init-domain.test.js +34 -0
  4. package/lib/error-handlers/install/express4.test.js +238 -0
  5. package/lib/error-handlers/install/fastify.test.js +130 -0
  6. package/lib/error-handlers/install/hapi.test.js +102 -0
  7. package/lib/error-handlers/install/koa2.test.js +83 -0
  8. package/lib/error-handlers/install/restify.test.js +57 -0
  9. package/lib/get-source-context.test.js +35 -0
  10. package/lib/hardening/handlers.test.js +89 -0
  11. package/lib/hardening/index.test.js +31 -0
  12. package/lib/hardening/install/node-serialize0.test.js +61 -0
  13. package/lib/index.test.js +53 -0
  14. package/lib/input-analysis/handlers.test.js +1604 -0
  15. package/lib/input-analysis/index.test.js +45 -0
  16. package/lib/input-analysis/install/body-parser1.test.js +134 -0
  17. package/lib/input-analysis/install/busboy1.test.js +81 -0
  18. package/lib/input-analysis/install/cookie-parser1.test.js +144 -0
  19. package/lib/input-analysis/install/express4.test.js +208 -0
  20. package/lib/input-analysis/install/fastify.test.js +96 -0
  21. package/lib/input-analysis/install/formidable1.test.js +114 -0
  22. package/lib/input-analysis/install/hapi.test.js +300 -0
  23. package/lib/input-analysis/install/http.test.js +264 -0
  24. package/lib/input-analysis/install/koa-body5.test.js +92 -0
  25. package/lib/input-analysis/install/koa-bodyparser4.test.js +92 -0
  26. package/lib/input-analysis/install/koa2.test.js +259 -0
  27. package/lib/input-analysis/install/multer1.test.js +209 -0
  28. package/lib/input-analysis/install/qs6.test.js +79 -0
  29. package/lib/input-analysis/install/restify.test.js +98 -0
  30. package/lib/input-analysis/install/universal-cookie4.test.js +70 -0
  31. package/lib/input-analysis/ip-analysis.test.js +71 -0
  32. package/lib/input-analysis/virtual-patches.test.js +106 -0
  33. package/lib/input-tracing/handlers/index.test.js +1236 -0
  34. package/lib/input-tracing/index.test.js +62 -0
  35. package/lib/input-tracing/install/child-process.test.js +133 -0
  36. package/lib/input-tracing/install/eval.test.js +78 -0
  37. package/lib/input-tracing/install/fs.test.js +108 -0
  38. package/lib/input-tracing/install/function.test.js +81 -0
  39. package/lib/input-tracing/install/http.test.js +85 -0
  40. package/lib/input-tracing/install/http2.test.js +83 -0
  41. package/lib/input-tracing/install/marsdb.test.js +126 -0
  42. package/lib/input-tracing/install/mongodb.test.js +282 -0
  43. package/lib/input-tracing/install/mssql.test.js +81 -0
  44. package/lib/input-tracing/install/mysql.test.js +108 -0
  45. package/lib/input-tracing/install/postgres.test.js +125 -0
  46. package/lib/input-tracing/install/sequelize.test.js +78 -0
  47. package/lib/input-tracing/install/spdy.test.js +76 -0
  48. package/lib/input-tracing/install/sqlite3.test.js +88 -0
  49. package/lib/input-tracing/install/vm.test.js +176 -0
  50. package/lib/make-response-blocker.test.js +99 -0
  51. package/lib/make-source-context.test.js +219 -0
  52. package/lib/policy.test.js +446 -0
  53. package/lib/semantic-analysis/handlers.test.js +379 -0
  54. package/lib/semantic-analysis/index.test.js +38 -0
  55. package/lib/semantic-analysis/install/libxmljs.test.js +156 -0
  56. package/lib/semantic-analysis/utils/xml-analysis.test.js +156 -0
  57. package/lib/throw-security-exception.test.js +37 -0
  58. package/package.json +5 -5
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const { initProtectFixture, createProtectStore } = require('@contrast/test/fixtures');
5
+ const SecurityException = require('../security-exception');
6
+
7
+ describe('protect error-handlers common-handler', function () {
8
+ let core, simulateRequestScope, commonHandler;
9
+
10
+ beforeEach(function () {
11
+ ({
12
+ core,
13
+ simulateRequestScope
14
+ } = initProtectFixture());
15
+
16
+ commonHandler = core.protect.errorHandlers.commonHandler;
17
+ });
18
+
19
+ it('should re-throw if not security excpetion', function () {
20
+ simulateRequestScope(() => {
21
+ const err = new Error('Not a SecurityException');
22
+ expect(() => {
23
+ commonHandler(err);
24
+ }).to.throw(err);
25
+ });
26
+ });
27
+
28
+ it('should catch exception and delegate to source context to block', function () {
29
+ const customStore = { protect: createProtectStore() };
30
+ customStore.protect.securityException = ['block', 'cmd-injection'];
31
+
32
+ simulateRequestScope(() => {
33
+ expect(() => {
34
+ commonHandler(new SecurityException());
35
+ expect(customStore.protect.block).to.have.been.calledWith('block', 'cmd-injection');
36
+ }).not.to.throw();
37
+ }, customStore);
38
+ });
39
+
40
+ it('should catch exception if request context is unavailable but will log and NOT block', function () {
41
+ const customStore = { protect: createProtectStore() };
42
+ customStore.protect.securityException = ['block', 'cmd-injection'];
43
+
44
+ expect(() => {
45
+ commonHandler(new SecurityException());
46
+ expect(customStore.protect.block).to.not.have.been.called;
47
+ expect(core.logger.info).to.have.been.calledWith(
48
+ 'SecurityException caught by Contrast but Protect store is unavailable for req handling'
49
+ );
50
+ }).not.to.throw();
51
+ });
52
+ });
@@ -0,0 +1,32 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const { initProtectFixture } = require('@contrast/test/fixtures');
6
+
7
+ describe('protect error-handlers', function () {
8
+ let core, errorHandlers;
9
+
10
+ const installerList = [
11
+ 'express4ErrorHandler',
12
+ 'fastifyErrorHandler',
13
+ 'hapiErrorHandler',
14
+ 'koa2ErrorHandler',
15
+ 'restifyErrorHandler'
16
+ ];
17
+
18
+ beforeEach(function () {
19
+ ({ core } = initProtectFixture());
20
+ errorHandlers = core.protect.errorHandlers;
21
+ installerList.forEach((installer) => {
22
+ sinon.stub(errorHandlers[installer], 'install');
23
+ });
24
+ });
25
+
26
+ it('installs subcomponents', function () {
27
+ errorHandlers.install();
28
+ installerList.forEach((installer) => {
29
+ expect(errorHandlers[installer].install).to.have.been.called;
30
+ });
31
+ });
32
+ });
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ const { expect } = require('chai');
4
+ const sinon = require('sinon');
5
+ const { initProtectFixture } = require('@contrast/test/fixtures');
6
+
7
+ describe('protect error-handlers init-domain', function () {
8
+ let core, errorHandlers, initDomain;
9
+
10
+ beforeEach(function () {
11
+ ({ core } = initProtectFixture());
12
+
13
+ sinon.stub(core.protect.errorHandlers, 'commonHandler');
14
+
15
+ errorHandlers = core.protect.errorHandlers;
16
+ initDomain = errorHandlers.initDomain;
17
+ });
18
+
19
+ describe('domain integration', function () {
20
+ beforeEach(function () {
21
+ sinon.stub(errorHandlers, 'AsyncHookDomain');
22
+ });
23
+
24
+ it('initializes correct domain', function () {
25
+ expect(core.protect.errorHandlers.AsyncHookDomain).to.be.a('function');
26
+ expect(core.protect.errorHandlers.Domain).to.be.undefined;
27
+ });
28
+
29
+ it('instantiates async-hook-domain with common error handler', function () {
30
+ initDomain();
31
+ expect(errorHandlers.AsyncHookDomain).to.have.been.calledWith(errorHandlers.commonHandler);
32
+ });
33
+ });
34
+ });
@@ -0,0 +1,238 @@
1
+ /* eslint-disable object-shorthand */
2
+ 'use strict';
3
+
4
+ const sinon = require('sinon');
5
+ const { expect } = require('chai');
6
+ const scopes = require('@contrast/scopes');
7
+ const patcher = require('@contrast/patcher');
8
+ const mocks = require('@contrast/test/mocks');
9
+ const SecurityException = require('../../security-exception');
10
+
11
+ describe('protect error-handlers express4', function () {
12
+ let core, store, errorHandlerInstr;
13
+
14
+ beforeEach(function () {
15
+ core = mocks.core();
16
+ core.config = mocks.config();
17
+ core.logger = mocks.logger();
18
+ core.scopes = scopes(core);
19
+ core.protect = mocks.protect();
20
+ require('../../get-source-context')(core);
21
+ core.depHooks = mocks.depHooks();
22
+ core.patcher = patcher(core);
23
+
24
+ store = {
25
+ protect: {
26
+ block: sinon.stub(),
27
+ securityException: ['block', 'cmd-injection']
28
+ }
29
+ };
30
+
31
+ sinon.spy(core.patcher, 'patch');
32
+ });
33
+
34
+ describe('finalhandler', function () {
35
+ let finalhandler, returnedFn;
36
+
37
+ beforeEach(function () {
38
+ finalhandler = function () {
39
+ return function () { };
40
+ };
41
+
42
+ core.depHooks.resolve.withArgs({ name: 'finalhandler' }).yields(finalhandler);
43
+
44
+ errorHandlerInstr = require('./express4')(core);
45
+ errorHandlerInstr.install();
46
+
47
+ const patchedFinalhandler = core.patcher.patch.getCall(0).returnValue;
48
+ returnedFn = patchedFinalhandler();
49
+ });
50
+
51
+ it('should block the request when there is SecurityException', function () {
52
+ const error = SecurityException.create();
53
+
54
+ core.scopes.sources.run(store, () => {
55
+ returnedFn(error);
56
+ expect(core.logger.info).not.to.have.been.called;
57
+ expect(store.protect.block).to.have.been.calledWith('block', 'cmd-injection');
58
+ });
59
+ });
60
+
61
+ it('should not block the request when there is SecurityException but sourceContext is missing', function () {
62
+ const error = SecurityException.create();
63
+
64
+ core.scopes.sources.run({}, () => {
65
+ returnedFn(error);
66
+ expect(core.logger.info).to.have.been.calledWith(
67
+ { funcKey: 'protect-error-handling:finalHandler.returnedFunction' },
68
+ 'source context not found; unable to handle response',
69
+ );
70
+ });
71
+ });
72
+
73
+ it('should skip the instrumentation when there is no SecurityException', function () {
74
+ const error = new Error('Error');
75
+
76
+ core.scopes.sources.run({}, () => {
77
+ returnedFn(error);
78
+ expect(core.logger.info).not.to.have.been.called;
79
+ expect(store.protect.block).not.to.have.been.called;
80
+ });
81
+ });
82
+ });
83
+
84
+ describe('Layer.prototype.handle_error', function () {
85
+ let Layer;
86
+
87
+ beforeEach(function () {
88
+ Layer = function () { };
89
+ Layer.prototype.handle_error = function () { };
90
+
91
+ core.depHooks.resolve.withArgs({ name: 'express', version: '>=4.0.0', file: 'lib/router/layer.js' }).yields(Layer);
92
+
93
+ errorHandlerInstr = require('./express4')(core);
94
+ errorHandlerInstr.install();
95
+ });
96
+
97
+ it('should block the request when there is SecurityException', function () {
98
+ const error = SecurityException.create();
99
+
100
+ core.scopes.sources.run(store, () => {
101
+ Layer.prototype.handle_error(error);
102
+ expect(core.logger.info).not.to.have.been.called;
103
+ expect(store.protect.block).to.have.been.calledWith('block', 'cmd-injection');
104
+ });
105
+ });
106
+
107
+ it('should not block the request when there is SecurityException but sourceContext is missing', function () {
108
+ const error = SecurityException.create();
109
+
110
+ core.scopes.sources.run({}, () => {
111
+ Layer.prototype.handle_error(error);
112
+ expect(core.logger.info).to.have.been.calledWith(
113
+ { funcKey: 'protect-error-handling:Layer.prototype.handle_error' },
114
+ 'source context not found; unable to handle response'
115
+ );
116
+ });
117
+ });
118
+
119
+ it('should skip the instrumentation when there is no SecurityException', function () {
120
+ const error = new Error('Error');
121
+
122
+ core.scopes.sources.run({}, () => {
123
+ Layer.prototype.handle_error(error);
124
+ expect(core.logger.info).not.to.have.been.called;
125
+ expect(store.protect.block).not.to.have.been.called;
126
+ });
127
+ });
128
+ });
129
+
130
+ describe('Layer.prototype.handle', function () {
131
+ const sampleFn = function () { };
132
+ let Layer;
133
+
134
+ beforeEach(function () {
135
+ Layer = function () { };
136
+
137
+ core.depHooks.resolve.withArgs({ name: 'express', version: '>=4.0.0', file: 'lib/router/layer.js' }).yields(Layer);
138
+
139
+ errorHandlerInstr = require('./express4')(core);
140
+ errorHandlerInstr.install();
141
+ });
142
+
143
+ it('should patch the function that is being set for `handle`', function () {
144
+ Layer.prototype.handle = sampleFn;
145
+
146
+ expect(core.patcher.patch).to.have.been.calledWith(sampleFn, sinon.match.object);
147
+ });
148
+
149
+ it('should return the patched function for the `handle` get', function () {
150
+ Layer.prototype.handle = sampleFn;
151
+
152
+ expect(Layer.prototype.handle).to.equal(Layer.prototype.__handle);
153
+ });
154
+ });
155
+
156
+ describe('Router.prototype.constructor.param', function () {
157
+ let Router;
158
+ const sampleFn = function () { };
159
+ const throwingHandler = (error) => async function () {
160
+ await Promise.reject(error);
161
+ };
162
+
163
+ beforeEach(function () {
164
+ Router = function () { };
165
+ Router.prototype.constructor = {
166
+ param: function () { },
167
+ };
168
+
169
+ core.depHooks.resolve.withArgs({ name: 'express', version: '>=4.0.0', file: 'lib/router/index.js' }).yields(Router);
170
+
171
+ core.patcher.patch.resetHistory();
172
+
173
+ errorHandlerInstr = require('./express4')(core);
174
+ errorHandlerInstr.install();
175
+ });
176
+
177
+ it('should patch the function for the param property', function () {
178
+ Router.prototype.constructor.param('sampleFn', sampleFn);
179
+
180
+ expect(core.patcher.patch).to.have.been.calledWith(sampleFn, sinon.match.object);
181
+ });
182
+
183
+ it('should block the request when there is SecurityException', async function () {
184
+ const error = SecurityException.create();
185
+ const fn = throwingHandler(error);
186
+
187
+ Router.prototype.constructor.param('fn', fn);
188
+
189
+ const patchedFn = core.patcher.patch.getCall(1).returnValue;
190
+
191
+ await core.scopes.sources.run(store, async () => {
192
+ await patchedFn();
193
+ });
194
+
195
+ expect(core.logger.info).not.to.have.been.called;
196
+ expect(store.protect.block).to.have.been.calledWith('block', 'cmd-injection');
197
+ });
198
+
199
+ it('should not block the request when there is SecurityException but sourceContext is missing', async function () {
200
+ const error = SecurityException.create();
201
+ const fn = throwingHandler(error);
202
+
203
+ Router.prototype.constructor.param('fn', fn);
204
+
205
+ const patchedFn = core.patcher.patch.getCall(1).returnValue;
206
+
207
+ await core.scopes.sources.run({}, async () => {
208
+ await patchedFn();
209
+ });
210
+
211
+ expect(core.logger.info).to.have.been.calledWith(
212
+ { funcKey: 'protect-error-handling:express.route-handler' },
213
+ 'source context not found; unable to handle response',
214
+ );
215
+ });
216
+
217
+ it('should skip the instrumentation when there is no SecurityException', async function () {
218
+ const error = new Error('Error');
219
+ const fn = throwingHandler(error);
220
+
221
+ Router.prototype.constructor.param('fn', fn);
222
+
223
+ const patchedFn = core.patcher.patch.getCall(1).returnValue;
224
+
225
+ try {
226
+ await core.scopes.sources.run(store, async () => {
227
+ await patchedFn();
228
+ });
229
+ } catch (err) {
230
+ expect(err).to.equal(error);
231
+ expect(core.logger.info).not.to.have.been.called;
232
+ expect(store.protect.block).not.to.have.been.called;
233
+ }
234
+ });
235
+ });
236
+
237
+
238
+ });
@@ -0,0 +1,130 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const scopes = require('@contrast/scopes');
6
+ const patcher = require('@contrast/patcher');
7
+ const mocks = require('@contrast/test/mocks');
8
+ const securityException = require('../../security-exception');
9
+
10
+ describe('protect error-handlers fastify', function () {
11
+ let core, errorHandler, fastify, server, reply;
12
+
13
+ beforeEach(function () {
14
+ core = mocks.core();
15
+ core.config = mocks.config();
16
+ core.logger = mocks.logger();
17
+ core.scopes = scopes(core);
18
+ core.protect = mocks.protect();
19
+ require('../../get-source-context')(core);
20
+ core.depHooks = mocks.depHooks();
21
+ core.patcher = patcher(core);
22
+
23
+ server = {
24
+ setErrorHandler: sinon.stub(),
25
+ errorHandler: sinon.stub()
26
+ };
27
+ fastify = function () {
28
+ return server;
29
+ };
30
+ core.depHooks.resolve.callsFake((desc, cb) => {
31
+ fastify = cb(fastify);
32
+ });
33
+ reply = {
34
+ log: {
35
+ info: sinon.stub(),
36
+ error: sinon.stub()
37
+ },
38
+ send: sinon.stub(),
39
+ statusCode: 500,
40
+ };
41
+
42
+ errorHandler = require('./fastify')(core);
43
+ sinon.spy(errorHandler, 'defaultErrorHandler');
44
+ errorHandler.install();
45
+
46
+ fastify();
47
+ });
48
+
49
+ describe('instrumentation adds custom handler', function () {
50
+ it('patches fastify', function () {
51
+ expect(server.setErrorHandler).to.have.been.calledWith(errorHandler.handler);
52
+ });
53
+ });
54
+
55
+ describe('defaultErrorHandler()', function () {
56
+ let request, err;
57
+
58
+ beforeEach(function () {
59
+ request = {};
60
+ err = {};
61
+ });
62
+
63
+ it('logs at error level when 500 status code', function () {
64
+ errorHandler.handler(err, request, reply);
65
+ expect(reply.log.error).to.have.been.calledWith({ req: request, res: reply, err });
66
+ });
67
+
68
+ it('logs at info level when <500 status code', function () {
69
+ reply.statusCode = 404;
70
+ errorHandler.handler(err, request, reply);
71
+ expect(reply.log.info).to.have.been.calledWith({ res: reply, err });
72
+ });
73
+ });
74
+
75
+ describe('handler()', function () {
76
+ let request, err, store, userHandler;
77
+
78
+ beforeEach(function () {
79
+ request = {};
80
+ err = securityException.create();
81
+ store = {
82
+ protect: {
83
+ block: sinon.stub(),
84
+ securityException: ['block', 'cmd-injection']
85
+ }
86
+ };
87
+ userHandler = sinon.stub();
88
+ });
89
+
90
+ it('calls default handler when the source context is not available', function () {
91
+ errorHandler.handler(err, request, reply);
92
+ });
93
+
94
+ it('when set, calls user handler when the source context is not available', function () {
95
+ server.setErrorHandler(userHandler);
96
+ errorHandler.handler(err, request, reply);
97
+ expect(userHandler).to.have.been.calledWith(err, request, reply);
98
+ });
99
+
100
+ it('calls source context\'s .block() with mode and id of rule which raised error', function () {
101
+ const {
102
+ protect: {
103
+ block,
104
+ securityException
105
+ }
106
+ } = store;
107
+ core.scopes.sources.run(store, () => {
108
+ errorHandler.handler(err, request, reply);
109
+ expect(block).to.have.been.calledWith(...securityException);
110
+ });
111
+ });
112
+
113
+ it('calls default error handler when error is not security exception', function () {
114
+ err = {};
115
+ core.scopes.sources.run(store, () => {
116
+ errorHandler.handler(err, request, reply);
117
+ expect(errorHandler.defaultErrorHandler).to.have.been.calledWith(err, request, reply);
118
+ });
119
+ });
120
+
121
+ it('when set, calls user handler when error is ont seecurity exception', function () {
122
+ err = {};
123
+ server.setErrorHandler(userHandler);
124
+ core.scopes.sources.run(store, () => {
125
+ errorHandler.handler(err, request, reply);
126
+ expect(userHandler).to.have.been.calledWith(err, request, reply);
127
+ });
128
+ });
129
+ });
130
+ });
@@ -0,0 +1,102 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const scopes = require('@contrast/scopes');
6
+ const patcher = require('@contrast/patcher');
7
+ const mocks = require('@contrast/test/mocks');
8
+ const SecurityException = require('../../security-exception');
9
+
10
+ describe('protect error-handlers hapi', function () {
11
+ let core,
12
+ errorHandler,
13
+ store;
14
+
15
+ const Boom = {
16
+ name: 'boom',
17
+ };
18
+ const HapiBoom = {
19
+ name: '@hapi/boom'
20
+ };
21
+
22
+ beforeEach(function () {
23
+ core = mocks.core();
24
+ core.config = mocks.config();
25
+ core.logger = mocks.logger();
26
+ core.scopes = scopes(core);
27
+ core.protect = mocks.protect();
28
+ require('../../get-source-context')(core);
29
+ core.depHooks = mocks.depHooks();
30
+ core.patcher = patcher(core);
31
+
32
+ store = {
33
+ protect: {
34
+ block: sinon.stub(),
35
+ securityException: ['block', 'cmd-injection']
36
+ }
37
+ };
38
+
39
+ Boom.boomify = function boom() { };
40
+ HapiBoom.boomify = function hapiBoom() { };
41
+
42
+ core.depHooks.resolve.withArgs({ name: Boom.name }).yields(Boom);
43
+ core.depHooks.resolve.withArgs({ name: HapiBoom.name }).yields(HapiBoom);
44
+
45
+ errorHandler = require('./hapi')(core);
46
+ errorHandler.install();
47
+ });
48
+
49
+ [Boom, HapiBoom].forEach((module) => {
50
+ it(`should block the request when there is SecurityException - ${module.name}`, function () {
51
+ const { boomify } = module;
52
+ const error = SecurityException.create();
53
+
54
+ core.scopes.sources.run(store, () => {
55
+ error.output = {
56
+ statusCode: 500,
57
+ };
58
+ error.reformat = () => ({});
59
+
60
+ boomify(error);
61
+ expect(core.logger.info).to.have.been.calledWith(
62
+ { funcKey: `protect-error-handling:${module.name}.boomify`, mode: 'block', ruleId: 'cmd-injection' },
63
+ 'Request blocked'
64
+ );
65
+ expect(error.output).to.have.property('statusCode', 403);
66
+ });
67
+ });
68
+ });
69
+
70
+ [Boom, HapiBoom].forEach((module) => {
71
+ it(`should not block the request when there is SecurityException but sourceContext is missing - ${module.name}`, function () {
72
+ const { boomify } = module;
73
+ const error = SecurityException.create();
74
+
75
+ core.scopes.sources.run({}, () => {
76
+ error.output = {
77
+ statusCode: 500,
78
+ };
79
+ error.reformat = () => ({});
80
+
81
+ boomify(error);
82
+ expect(core.logger.info).to.have.been.calledWith(
83
+ { funcKey: `protect-error-handling:${module.name}.boomify` },
84
+ 'source context not found; unable to handle response'
85
+ );
86
+ });
87
+ });
88
+ });
89
+
90
+ [Boom, HapiBoom].forEach((module) => {
91
+ it(`should skip the instrumentation when there is no SecurityException - ${module.name}`, function () {
92
+ const { boomify } = module;
93
+ const error = new Error('Error');
94
+
95
+ core.scopes.sources.run({}, () => {
96
+ boomify(error);
97
+ expect(core.logger.info).not.to.have.been.called;
98
+ expect(store.protect.block).not.to.have.been.called;
99
+ });
100
+ });
101
+ });
102
+ });
@@ -0,0 +1,83 @@
1
+ 'use strict';
2
+
3
+ const sinon = require('sinon');
4
+ const { expect } = require('chai');
5
+ const scopes = require('@contrast/scopes');
6
+ const patcher = require('@contrast/patcher');
7
+ const mocks = require('@contrast/test/mocks');
8
+ const SecurityException = require('../../security-exception');
9
+
10
+ describe('protect error-handlers koa2', function () {
11
+ let core,
12
+ errorHandler,
13
+ Koa,
14
+ onerror,
15
+ store,
16
+ ctx;
17
+
18
+ beforeEach(function () {
19
+ core = mocks.core();
20
+ core.config = mocks.config();
21
+ core.logger = mocks.logger();
22
+ core.scopes = scopes(core);
23
+ core.protect = mocks.protect();
24
+ require('../../get-source-context')(core);
25
+ core.depHooks = mocks.depHooks();
26
+ core.patcher = patcher(core);
27
+
28
+ store = {
29
+ protect: {
30
+ block: sinon.stub(),
31
+ securityException: ['block', 'cmd-injection']
32
+ }
33
+ };
34
+
35
+ Koa = function () { };
36
+
37
+ Koa.prototype.handleRequest = function () { };
38
+
39
+ core.depHooks.resolve.yields(Koa);
40
+
41
+ errorHandler = require('./koa2')(core);
42
+ errorHandler.install();
43
+
44
+ onerror = sinon.stub();
45
+ ctx = {
46
+ onerror,
47
+ };
48
+ Koa.prototype.handleRequest(ctx);
49
+ });
50
+
51
+ it('should block the request when there is SecurityException', function () {
52
+ const error = SecurityException.create();
53
+
54
+ core.scopes.sources.run(store, () => {
55
+ ctx.onerror(error);
56
+ expect(core.logger.info).not.to.have.been.called;
57
+ expect(store.protect.block).to.have.been.calledWith('block', 'cmd-injection');
58
+ expect(ctx.body).to.equal('');
59
+ });
60
+ });
61
+
62
+ it('should not block the request when there is SecurityException but sourceContext is missing', function () {
63
+ const error = SecurityException.create();
64
+
65
+ core.scopes.sources.run({}, () => {
66
+ ctx.onerror(error);
67
+ expect(core.logger.info).to.have.been.calledWith(
68
+ { funcKey: 'protect-error-handling:koa.ctx.onerror' },
69
+ 'source context not found; unable to handle response'
70
+ );
71
+ });
72
+ });
73
+
74
+ it('should skip the instrumentation when there is no SecurityException', function () {
75
+ const error = new Error('Error');
76
+
77
+ core.scopes.sources.run({}, () => {
78
+ ctx.onerror(error);
79
+ expect(core.logger.info).not.to.have.been.called;
80
+ expect(store.protect.block).not.to.have.been.called;
81
+ });
82
+ });
83
+ });