@contrast/protect 1.4.0 → 1.5.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.
@@ -18,14 +18,17 @@
18
18
  module.exports = function(core) {
19
19
  const errorHandlers = core.protect.errorHandlers = {};
20
20
 
21
- require('./install/fastify3')(core);
21
+ require('./install/fastify')(core);
22
22
  require('./install/koa2')(core);
23
23
  require('./install/express4')(core);
24
+ require('./install/hapi')(core);
24
25
 
25
26
  errorHandlers.install = function() {
26
- errorHandlers.fastify3ErrorHandler.install();
27
- errorHandlers.koa2ErrorHandler.install();
28
- errorHandlers.express4ErrorHandler.install();
27
+ for (const component of Object.values(errorHandlers)) {
28
+ if (component.install) {
29
+ component.install();
30
+ }
31
+ }
29
32
  };
30
33
 
31
34
  return errorHandlers;
@@ -25,7 +25,7 @@ module.exports = function(core) {
25
25
  protect,
26
26
  } = core;
27
27
 
28
- const fastify3ErrorHandler = protect.errorHandlers.fastify3ErrorHandler = {
28
+ const fastifyErrorHandler = protect.errorHandlers.fastifyErrorHandler = {
29
29
  _userHandler: null
30
30
  };
31
31
 
@@ -33,7 +33,7 @@ module.exports = function(core) {
33
33
  * This is the default handler from fastify's source code. If it's not a
34
34
  * Contrast error and the user didn't supply their own we should use this.
35
35
  */
36
- fastify3ErrorHandler.defaultErrorHandler = function (error, request, reply) {
36
+ fastifyErrorHandler.defaultErrorHandler = function (error, request, reply) {
37
37
  if (reply.statusCode < 500) {
38
38
  reply.log.info({ res: reply, err: error }, error && error.message);
39
39
  } else {
@@ -47,17 +47,17 @@ module.exports = function(core) {
47
47
 
48
48
  /**
49
49
  * Check if the error being handled was thrown by Contrast. If not,
50
- * use either the default fastify3 error handler or the user-defined handler
50
+ * use either the default fastify error handler or the user-defined handler
51
51
  * if one was specified by calling fastify.setErrorHandler(fn).
52
52
  * @param {error} err error being handled
53
53
  * @param {object} request fastify request
54
54
  * @param {object} reply fastify repoly
55
55
  */
56
- fastify3ErrorHandler.handler = function(err, request, reply) {
57
- const normalHandler = fastify3ErrorHandler._userHandler || fastify3ErrorHandler.defaultErrorHandler;
56
+ fastifyErrorHandler.handler = function(err, request, reply) {
57
+ const normalHandler = fastifyErrorHandler._userHandler || fastifyErrorHandler.defaultErrorHandler;
58
58
 
59
59
  if (isSecurityException(err)) {
60
- const sourceContext = protect.getSourceContext('fastify3.errorHandler');
60
+ const sourceContext = protect.getSourceContext('fastify.errorHandler');
61
61
 
62
62
  if (!sourceContext) {
63
63
  normalHandler.call(this, err, request, reply);
@@ -73,14 +73,14 @@ module.exports = function(core) {
73
73
  /**
74
74
  * Instruments fastify in order to add our custom error handler.
75
75
  */
76
- fastify3ErrorHandler.install = function() {
77
- depHooks.resolve({ name: 'fastify', version: '<=4.0.0' }, (fastify) => patcher.patch(fastify, {
76
+ fastifyErrorHandler.install = function() {
77
+ depHooks.resolve({ name: 'fastify', version: '<=3 <5' }, (fastify) => patcher.patch(fastify, {
78
78
  name: 'fastify',
79
79
  patchType,
80
80
  post(data) {
81
81
  const { result: server } = data;
82
82
  // Set our custom handler initially
83
- server.setErrorHandler(fastify3ErrorHandler.handler);
83
+ server.setErrorHandler(fastifyErrorHandler.handler);
84
84
 
85
85
  // Patch, so that if someone sets their own, we override with ours. But,
86
86
  // we do need to keep a reference to it so we can still call it for when
@@ -89,13 +89,13 @@ module.exports = function(core) {
89
89
  name: 'fastify.setErrorHandler',
90
90
  patchType,
91
91
  pre({ args }) {
92
- fastify3ErrorHandler._userHandler = args[0];
93
- args[0] = fastify3ErrorHandler.handler;
92
+ fastifyErrorHandler._userHandler = args[0];
93
+ args[0] = fastifyErrorHandler.handler;
94
94
  }
95
95
  });
96
96
  }
97
97
  }));
98
98
  };
99
99
 
100
- return fastify3ErrorHandler;
100
+ return fastifyErrorHandler;
101
101
  };
@@ -0,0 +1,75 @@
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 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('Hapi.boom.boomify');
39
+ const isSecurityException = SecurityException.isSecurityException(err);
40
+
41
+ if (isSecurityException && sourceContext && err.output.statusCode !== 403) {
42
+ const [mode, ruleId] = sourceContext.findings.securityException;
43
+ sourceContext.block('block', 'cmd-injection');
44
+
45
+ err.output.statusCode = 403;
46
+ err.reformat();
47
+ err.output.payload = undefined;
48
+ data.result = null;
49
+
50
+ logger.info({ mode, ruleId }, 'Request blocked');
51
+
52
+ return;
53
+ }
54
+
55
+ if (!sourceContext && isSecurityException) {
56
+ logger.info('source context not found; unable to handle response');
57
+ }
58
+ }
59
+ });
60
+ };
61
+
62
+ hapiErrorHandler.install = function () {
63
+ depHooks.resolve(
64
+ { name: 'boom' },
65
+ (boom) => registerErrorHandler(boom, 'boom'),
66
+ );
67
+
68
+ depHooks.resolve(
69
+ { name: '@hapi/boom' },
70
+ (boom) => registerErrorHandler(boom, '@hapi/boom'),
71
+ );
72
+ };
73
+
74
+ return hapiErrorHandler;
75
+ };
@@ -37,7 +37,7 @@ module.exports = function(core) {
37
37
 
38
38
  hardening.handleUntrustedDeserialization = function(sourceContext, sinkContext) {
39
39
  const ruleId = 'untrusted-deserialization';
40
- const { mode } = sourceContext.rules.agentRules[ruleId];
40
+ const mode = sourceContext.policy[ruleId];
41
41
  const { name, value } = sinkContext;
42
42
 
43
43
  if (mode === 'off') return;
package/lib/index.js CHANGED
@@ -16,20 +16,14 @@
16
16
  'use strict';
17
17
 
18
18
  const agentLib = require('@contrast/agent-lib');
19
+ const { installChildComponentsSync } = require('@contrast/common');
19
20
 
20
21
  module.exports = function(core) {
21
22
  const protect = core.protect = {
22
23
  agentLib: module.exports.instantiateAgentLib(agentLib),
23
24
  };
24
25
 
25
- const rules = instantiateRulesFromConfig(
26
- core.config.protect.rules,
27
- core.config.protect.disabled_rules,
28
- protect.agentLib,
29
- );
30
-
31
- protect.rules = rules;
32
-
26
+ require('./policy')(core);
33
27
  require('./throw-security-exception')(core);
34
28
  require('./make-response-blocker')(core);
35
29
  require('./make-source-context')(core);
@@ -44,10 +38,7 @@ module.exports = function(core) {
44
38
  protect.version = pkj.version;
45
39
 
46
40
  protect.install = function() {
47
- protect.inputAnalysis.install();
48
- protect.inputTracing.install();
49
- protect.hardening.install();
50
- protect.errorHandlers.install();
41
+ installChildComponentsSync(protect)
51
42
  };
52
43
 
53
44
  return protect;
@@ -71,38 +62,3 @@ function instantiateAgentLib(lib) {
71
62
  }
72
63
  return agentLib;
73
64
  }
74
-
75
- /**
76
- * This function instatiates the rules as defined in the configuration into
77
- * some structure. I'm in no way convinced or asserting that this is the right
78
- * structure but it does get a usable definition of rules in place. The final
79
- * structure will change based on what exactly TS sends as well as what the needs
80
- * of the code accessing the rules, exclusions, virtual-patches, etc.
81
- *
82
- * @param {Object} rules the rules object in the config.protect object.
83
- * @param {string[]} disabled array of disabled rules from config.protect
84
- * @param {Object} agentLib the agent-lib instance
85
- * @returns {Object} { agentLibRules, agentLibRulesMask, agentRules }
86
- */
87
- function instantiateRulesFromConfig(rules, disabled, agentLib) {
88
- const agentLibRules = {};
89
- let agentLibRulesMask = 0;
90
- const agentRules = {};
91
-
92
- for (const ruleId in rules) {
93
- if (disabled.indexOf(ruleId) >= 0 || rules[ruleId].mode === 'off') {
94
- continue;
95
- }
96
- // [matt] this is awkward. we should probably make each nosql-injection-x
97
- // rule separate in the config and only convert them to 'nosql-injection'
98
- // for reporting.
99
- if (agentLib.RuleType[ruleId]) {
100
- agentLibRules[ruleId] = rules[ruleId];
101
- agentLibRulesMask = agentLibRulesMask | agentLib.RuleType[ruleId];
102
- } else {
103
- agentRules[ruleId] = rules[ruleId];
104
- }
105
- }
106
-
107
- return { agentLibRules, agentLibRulesMask, agentRules };
108
- }
@@ -113,15 +113,15 @@ module.exports = function(core) {
113
113
  inputAnalysis.handleConnect = function handleConnect(sourceContext, connectInputs) {
114
114
  if (!sourceContext || sourceContext.allowed) return;
115
115
 
116
- const { rules: { agentLibRules, agentLibRulesMask: mask } } = sourceContext;
116
+ const { policy: { rulesMask } } = sourceContext;
117
117
 
118
118
  inputAnalysis.handleVirtualPatches(sourceContext, { URLS: connectInputs.rawUrl, HEADERS: connectInputs.headers });
119
119
 
120
120
  // initialize findings to the basics
121
121
  let block = undefined;
122
- if (mask !== 0) {
123
- const findings = agentLib.scoreRequestConnect(mask, connectInputs, preferWW);
124
- block = mergeFindings(agentLibRules, sourceContext.findings, findings);
122
+ if (rulesMask !== 0) {
123
+ const findings = agentLib.scoreRequestConnect(rulesMask, connectInputs, preferWW);
124
+ block = mergeFindings(sourceContext, findings);
125
125
  }
126
126
 
127
127
  return block;
@@ -198,7 +198,7 @@ module.exports = function(core) {
198
198
 
199
199
  inputAnalysis.handleVirtualPatches(sourceContext, { PARAMETERS: urlParams });
200
200
 
201
- const { rules, rules: { agentLibRulesMask: mask } } = sourceContext;
201
+ const { policy: { rulesMask } } = sourceContext;
202
202
  const resultsList = [];
203
203
  const { UrlParameter } = agentLib.InputType;
204
204
 
@@ -207,7 +207,7 @@ module.exports = function(core) {
207
207
  if (type !== 'Value') {
208
208
  return;
209
209
  }
210
- const items = agentLib.scoreAtom(mask, value, UrlParameter, preferWW);
210
+ const items = agentLib.scoreAtom(rulesMask, value, UrlParameter, preferWW);
211
211
  if (!items) {
212
212
  return;
213
213
  }
@@ -236,7 +236,7 @@ module.exports = function(core) {
236
236
  resultsList,
237
237
  };
238
238
 
239
- const block = mergeFindings(rules.agentLibRules, sourceContext.findings, urlParamsFindings);
239
+ const block = mergeFindings(sourceContext, urlParamsFindings);
240
240
 
241
241
  if (block) {
242
242
  core.protect.throwSecurityException(sourceContext);
@@ -262,10 +262,10 @@ module.exports = function(core) {
262
262
 
263
263
  inputAnalysis.handleVirtualPatches(sourceContext, { HEADERS: cookies });
264
264
 
265
- const { rules, rules: { agentLibRulesMask: mask } } = sourceContext;
266
- const cookieFindings = agentLib.scoreRequestConnect(mask, { cookies: cookiesArr }, preferWW);
265
+ const { policy: { rulesMask } } = sourceContext;
266
+ const cookieFindings = agentLib.scoreRequestConnect(rulesMask, { cookies: cookiesArr }, preferWW);
267
267
 
268
- const block = mergeFindings(rules.agentLibRules, sourceContext.findings, cookieFindings);
268
+ const block = mergeFindings(sourceContext, cookieFindings);
269
269
 
270
270
  if (block) {
271
271
  core.protect.throwSecurityException(sourceContext);
@@ -397,11 +397,12 @@ module.exports = function(core) {
397
397
  * @returns {Array | undefined} returns an array with block info if vulnerability was found.
398
398
  */
399
399
  function commonObjectAnalyzer(sourceContext, object, inputTypes) {
400
- const { rules, rules: { agentLibRulesMask: mask } } = sourceContext;
400
+ const { policy: { rulesMask } } = sourceContext;
401
+ if (!rulesMask) return;
402
+
401
403
  // use inputTypes to set params...
402
404
  const { keyType, inputType } = inputTypes;
403
405
  const inputTypeStr = inputTypes === jsonInputTypes ? 'Json' : 'Parameter';
404
- const { Where } = agentLib.MongoQueryType;
405
406
  const resultsList = [];
406
407
 
407
408
  // it's possible to optimize this if qs (or a similar package) is not loaded
@@ -420,7 +421,7 @@ module.exports = function(core) {
420
421
  /* eslint-disable-next-line complexity */
421
422
  simpleTraverse(object, function(path, type, value) {
422
423
  let itemType;
423
- let mongoQueryType;
424
+ let isMongoQueryType;
424
425
  // this is a bit awkward now because nosql-injection-mongo is not integrated
425
426
  // into the scoreAtom() function (or the check_input() function it uses). as
426
427
  // a result, the two rules need to be checked independently and the results
@@ -428,14 +429,14 @@ module.exports = function(core) {
428
429
  // TODO AGENT-205
429
430
  if (type === 'Key') {
430
431
  itemType = keyType;
431
- if (mask & agentLib.RuleType['nosql-injection-mongo']) {
432
- mongoQueryType = agentLib.getMongoQueryType(value);
432
+ if (rulesMask & agentLib.RuleType['nosql-injection-mongo']) {
433
+ isMongoQueryType = agentLib.isMongoQueryType(value);
433
434
  }
434
435
  } else {
435
436
  itemType = inputType;
436
437
  }
437
- let items = agentLib.scoreAtom(mask, value, itemType, preferWW);
438
- if (!items && !mongoQueryType) {
438
+ let items = agentLib.scoreAtom(rulesMask, value, itemType, preferWW);
439
+ if (!items && !isMongoQueryType) {
439
440
  return;
440
441
  }
441
442
  if (!items) {
@@ -444,18 +445,13 @@ module.exports = function(core) {
444
445
  let mongoPath;
445
446
  // if the key was a mongo query key, then add it to the items. it requires
446
447
  // that additional information is kept as well.
447
- if (mongoQueryType) {
448
+ if (isMongoQueryType) {
448
449
  const inputToCheck = getValueAtKey(object, path, value);
449
450
  // because scoreRequestConnect() returns the query type in the value, we
450
451
  // mimic it here (where scoreAtom() was used). the actual object/string
451
452
  // to match is stored as `inputToCheck`.
452
- const inputType = typeof inputToCheck;
453
- // query types up to Where, inclusive, accept either string or object values. Where and above accept only string values
454
- if (mongoQueryType <= Where || inputType === 'string') {
455
- // the query-type/input-type combination is valid. add a synthesized item.
456
- const item = { ruleId: 'nosql-injection-mongo', score: 10, mongoContext: { inputToCheck } };
457
- items.push(item);
458
- }
453
+ const item = { ruleId: 'nosql-injection-mongo', score: 10, mongoContext: { inputToCheck } };
454
+ items.push(item);
459
455
  }
460
456
  // make each item a complete Finding
461
457
  for (const item of items) {
@@ -464,8 +460,7 @@ module.exports = function(core) {
464
460
  inputType: `${inputTypeStr}${type}`,
465
461
  path: mongoPath || path.slice(),
466
462
  key: type === 'Key' ? value : path[path.length - 1],
467
- // mimic scoreRequestConnect() returning the query type as the value
468
- value: mongoQueryType || value,
463
+ value,
469
464
  score: item.score,
470
465
  idsList: [],
471
466
  };
@@ -488,7 +483,7 @@ module.exports = function(core) {
488
483
  resultsList,
489
484
  };
490
485
 
491
- return mergeFindings(rules.agentLibRules, sourceContext.findings, findings);
486
+ return mergeFindings(sourceContext, findings);
492
487
  }
493
488
 
494
489
  function ipListAnalysis(reqIp, reqHeaders, list) {
@@ -543,11 +538,13 @@ module.exports = function(core) {
543
538
  * @param {Object} newFindings the findings, in {trackRequest, resultsList} format.
544
539
  * @returns {undefined|[String]} undefined to permit else [mode, rule] to block.
545
540
  */
546
- function mergeFindings(rules, findings, newFindings) {
541
+ function mergeFindings(sourceContext, newFindings) {
542
+ const { findings, policy } = sourceContext;
543
+
547
544
  if (!newFindings.trackRequest) {
548
545
  return findings.securityException;
549
546
  }
550
- normalizeFindings(rules, newFindings);
547
+ normalizeFindings(policy, newFindings);
551
548
 
552
549
  findings.trackRequest = findings.trackRequest || newFindings.trackRequest;
553
550
  findings.securityException = findings.securityException || newFindings.securityException;
@@ -566,7 +563,7 @@ function mergeFindings(rules, findings, newFindings) {
566
563
  //
567
564
  // add common fields to findings.
568
565
  //
569
- function normalizeFindings(rules, findings) {
566
+ function normalizeFindings(policy, findings) {
570
567
  // now both augment the rules and check to see if any require blocking
571
568
  // at perimeter.
572
569
  for (const r of findings.resultsList) {
@@ -590,7 +587,7 @@ function normalizeFindings(rules, findings) {
590
587
  // option and that implies that there is no sink, so this is the only place at
591
588
  // which the block can occur. so at a minimum 'block' should also result in a
592
589
  // block.
593
- const { mode } = rules[r.ruleId];
590
+ const mode = policy[r.ruleId];
594
591
  if (r.score >= 90 && BLOCKING_MODES.includes(mode)) {
595
592
  r.blocked = true;
596
593
  findings.securityException = [mode, r.ruleId];
@@ -15,6 +15,8 @@
15
15
 
16
16
  'use strict';
17
17
 
18
+ const { installChildComponentsSync } = require('@contrast/common');
19
+
18
20
  module.exports = function(core) {
19
21
  const inputAnalysis = core.protect.inputAnalysis = {};
20
22
 
@@ -35,20 +37,17 @@ module.exports = function(core) {
35
37
  require('./install/universal-cookie4')(core);
36
38
 
37
39
  // framework specific instrumentation
38
- require('./install/fastify3')(core);
40
+ require('./install/fastify')(core);
39
41
  require('./install/koa2')(core);
40
42
  require('./install/express4')(core);
43
+ require('./install/hapi')(core);
41
44
 
42
45
  // virtual patches
43
46
  require('./virtual-patches')(core);
44
47
  require('./ip-analysis')(core);
45
48
 
46
49
  inputAnalysis.install = function() {
47
- Object.values(inputAnalysis)
48
- .filter((property) => property.install)
49
- .forEach((library) => {
50
- library.install();
51
- });
50
+ installChildComponentsSync(inputAnalysis);
52
51
  };
53
52
 
54
53
  return inputAnalysis;
@@ -33,8 +33,18 @@ module.exports = (core) => {
33
33
  if (sourceContext && req.body && Object.keys(req.body).length) {
34
34
  sourceContext.parsedBody = req.body;
35
35
 
36
+ if (fnName === 'bodyParser.text' && typeof req.body === 'string') {
37
+ try {
38
+ sourceContext.parsedBody = JSON.parse(req.body);
39
+ } catch (err) {
40
+ logger.error({ err }, 'Error parsing with bodyParser.text()');
41
+ origNext();
42
+ return;
43
+ }
44
+ }
45
+
36
46
  try {
37
- inputAnalysis.handleParsedBody(sourceContext, req.body);
47
+ inputAnalysis.handleParsedBody(sourceContext, sourceContext.parsedBody);
38
48
  } catch (err) {
39
49
  if (isSecurityException(err)) {
40
50
  securityException = err;
@@ -0,0 +1,86 @@
1
+ /*
2
+ * Copyright: 2022 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
16
+ 'use strict';
17
+
18
+ const { patchType } = require('../constants');
19
+ const { isSecurityException } = require('../../security-exception');
20
+
21
+ /**
22
+ * Function that exports an install method to patch Fastify framework with our instrumentation
23
+ * @param {Object} core - the core Contrast object
24
+ * @return {Object} object with install method and the other relative functions exported for testing purposes
25
+ */
26
+ module.exports = (core) => {
27
+ const {
28
+ depHooks,
29
+ patcher,
30
+ logger,
31
+ protect,
32
+ protect: { inputAnalysis },
33
+ } = core;
34
+
35
+ /**
36
+ * registers a depHook for fastify module instrumentation
37
+ */
38
+ function install() {
39
+ depHooks.resolve({ name: 'fastify', version: '>=3 <5' }, (fastify) => {
40
+ return patcher.patch(fastify, {
41
+ name: 'fastify.build',
42
+ patchType,
43
+ post({ result: server }) {
44
+ server.addHook('preValidation', function(request, reply, done) {
45
+ let securityException;
46
+ const sourceContext = protect.getSourceContext('Fastify.preValidationHook');
47
+
48
+ if (sourceContext) {
49
+ try {
50
+ if (request.params) {
51
+ sourceContext.parsedParams = request.params;
52
+ inputAnalysis.handleUrlParams(sourceContext, request.params);
53
+ }
54
+ if (request.cookies) {
55
+ sourceContext.parsedCookies = request.cookies;
56
+ inputAnalysis.handleCookies(sourceContext, request.cookies);
57
+ }
58
+ if (request.body) {
59
+ sourceContext.parsedBody = request.body;
60
+ inputAnalysis.handleParsedBody(sourceContext, request.body);
61
+ }
62
+
63
+ if (request.query) {
64
+ sourceContext.parsedQuery = request.query;
65
+ inputAnalysis.handleQueryParams(sourceContext, request.query);
66
+ }
67
+ } catch (err) {
68
+ if (isSecurityException(err)) {
69
+ securityException = err;
70
+ } else {
71
+ logger.error({ err }, 'Unexpected error during input analysis');
72
+ }
73
+ }
74
+ }
75
+
76
+ done(securityException);
77
+ });
78
+ },
79
+ });
80
+ });
81
+ }
82
+
83
+ return inputAnalysis.fastifyInstrumentation = {
84
+ install,
85
+ };
86
+ };
@@ -0,0 +1,106 @@
1
+ /*
2
+ * Copyright: 2022 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
16
+ 'use strict';
17
+
18
+ const { patchType } = require('../constants');
19
+ const { isSecurityException } = require('../../security-exception');
20
+
21
+ module.exports = (core) => {
22
+ const {
23
+ depHooks,
24
+ patcher,
25
+ logger,
26
+ protect,
27
+ protect: { inputAnalysis },
28
+ } = core;
29
+
30
+ /**
31
+ * registers a depHook for hapi module instrumentation
32
+ */
33
+ function install() {
34
+ depHooks.resolve(
35
+ { name: 'hapi', version: '>=18 <21' },
36
+ registerServerHandler
37
+ );
38
+ depHooks.resolve(
39
+ { name: '@hapi/hapi', version: '>=18 <21' },
40
+ registerServerHandler
41
+ );
42
+ }
43
+
44
+ const registerServerHandler = (hapi) => {
45
+ patcher.patch(hapi, 'server', {
46
+ name: 'hapi.server',
47
+ patchType,
48
+ post(data) {
49
+ const server = data.result;
50
+ if (server) {
51
+ instrumentServer(server);
52
+ } else {
53
+ logger.error('Hapi Server is called but there is no server instance!');
54
+ }
55
+ }
56
+ });
57
+ };
58
+
59
+ const instrumentServer = (server) => {
60
+ server.ext('onPreStart', function onPreStart(core) {
61
+ logger.info('hapi version %s', core.version);
62
+ });
63
+
64
+ server.ext('onPreHandler', function onPreHandler(req, h) {
65
+ const sourceContext = protect.getSourceContext('hapi.onPreHandler');
66
+
67
+ if (sourceContext) {
68
+ try {
69
+ if (req.params && Object.keys(req.params).length) {
70
+ sourceContext.parsedParams = req.params;
71
+ inputAnalysis.handleUrlParams(sourceContext, req.params);
72
+ }
73
+
74
+ if (req.cookies && Object.keys(req.cookies).length) {
75
+ sourceContext.parsedCookies = req.cookies;
76
+ inputAnalysis.handleCookies(sourceContext, req.cookies);
77
+ }
78
+
79
+ if (req.payload && Object.keys(req.payload).length) {
80
+ sourceContext.parsedBody = req.payload;
81
+ inputAnalysis.handleParsedBody(sourceContext, req.payload);
82
+ }
83
+
84
+ if (req.query && Object.keys(req.query).length) {
85
+ sourceContext.parsedQuery = req.query;
86
+ inputAnalysis.handleQueryParams(sourceContext, req.query);
87
+ }
88
+ } catch (err) {
89
+ if (isSecurityException(err)) {
90
+ throw err;
91
+ } else {
92
+ logger.error({ err }, 'Unexpected error during input analysis');
93
+ }
94
+ }
95
+ }
96
+
97
+ return h.continue;
98
+ });
99
+ };
100
+
101
+ const hapiInstrumentation = inputAnalysis.hapiInstrumentation = {
102
+ install
103
+ };
104
+
105
+ return hapiInstrumentation;
106
+ };