@contrast/protect 1.49.0 → 1.51.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/index.js +1 -1
- package/lib/error-handlers/index.test.js +1 -1
- package/lib/error-handlers/init-domain.js +4 -39
- package/lib/error-handlers/init-domain.test.js +3 -15
- package/lib/error-handlers/install/express.js +162 -0
- package/lib/error-handlers/install/express.test.js +290 -0
- package/lib/error-handlers/install/hapi.js +2 -2
- package/lib/error-handlers/install/hapi.test.js +2 -2
- package/lib/error-handlers/install/koa2.js +1 -1
- package/lib/error-handlers/install/restify.js +1 -1
- package/lib/error-handlers/install/restify.test.js +1 -1
- package/lib/hardening/install/node-serialize0.js +2 -2
- package/lib/hardening/install/node-serialize0.test.js +1 -4
- package/lib/index.d.ts +3 -3
- package/lib/input-analysis/index.js +1 -1
- package/lib/input-analysis/index.test.js +1 -1
- package/lib/input-analysis/install/body-parser1.js +2 -2
- package/lib/input-analysis/install/busboy1.js +1 -1
- package/lib/input-analysis/install/cookie-parser1.js +1 -1
- package/lib/input-analysis/install/{express4.js → express.js} +61 -20
- package/lib/input-analysis/install/{express4.test.js → express.test.js} +92 -59
- package/lib/input-analysis/install/formidable1.js +1 -1
- package/lib/input-analysis/install/hapi.js +1 -1
- package/lib/input-analysis/install/hapi.test.js +6 -14
- package/lib/input-analysis/install/http.js +2 -9
- package/lib/input-analysis/install/koa-body5.js +1 -1
- package/lib/input-analysis/install/koa-bodyparser4.js +1 -1
- package/lib/input-analysis/install/koa2.js +5 -5
- package/lib/input-analysis/install/multer1.js +1 -1
- package/lib/input-analysis/install/qs6.js +1 -1
- package/lib/input-analysis/install/restify.js +1 -1
- package/lib/input-analysis/install/restify.test.js +1 -1
- package/lib/input-analysis/install/universal-cookie4.js +1 -1
- package/lib/input-tracing/install/child-process.js +1 -1
- package/lib/input-tracing/install/fs.js +2 -2
- package/lib/input-tracing/install/fs.test.js +2 -2
- package/lib/input-tracing/install/http.js +2 -2
- package/lib/input-tracing/install/http2.js +2 -2
- package/lib/input-tracing/install/marsdb.js +2 -2
- package/lib/input-tracing/install/marsdb.test.js +1 -1
- package/lib/input-tracing/install/mongodb.js +2 -2
- package/lib/input-tracing/install/mongodb.test.js +2 -4
- package/lib/input-tracing/install/mssql.js +3 -3
- package/lib/input-tracing/install/mssql.test.js +2 -2
- package/lib/input-tracing/install/mysql.js +7 -9
- package/lib/input-tracing/install/postgres.js +3 -3
- package/lib/input-tracing/install/postgres.test.js +2 -10
- package/lib/input-tracing/install/sequelize.js +2 -2
- package/lib/input-tracing/install/spdy.js +2 -2
- package/lib/input-tracing/install/sqlite3.js +2 -2
- package/lib/input-tracing/install/vm.js +2 -2
- package/lib/semantic-analysis/install/libxmljs.js +3 -3
- package/lib/semantic-analysis/install/libxmljs.test.js +2 -2
- package/package.json +12 -14
- package/lib/error-handlers/install/express4.js +0 -138
- package/lib/error-handlers/install/express4.test.js +0 -238
|
@@ -1,238 +0,0 @@
|
|
|
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 <5.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 <5.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 <5.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
|
-
});
|