@contrast/protect 1.84.0 → 1.85.1

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.
@@ -98,7 +98,13 @@ module.exports = function (core) {
98
98
 
99
99
  block = block || inputAnalysis.handleConnect(store.protect, connectInputs);
100
100
  } catch (err) {
101
- logger.error({ err, funcKey: data.funcKey }, 'Error during http input analysis');
101
+ if (SecurityException.isSecurityException(err)) {
102
+ // handlers such as virtual patches throw to block. there is no framework
103
+ // error path this early in the request, so convert it into a block here.
104
+ block = [err.mode, err.details.ruleId];
105
+ } else {
106
+ logger.error({ err, funcKey: data.funcKey }, 'Error during http input analysis');
107
+ }
102
108
  }
103
109
 
104
110
  if (!block) {
@@ -20,6 +20,7 @@ const { Event, primordials: { StringPrototypeToLowerCase, RegExpPrototypeTest }
20
20
  module.exports = (core) => {
21
21
  const {
22
22
  messages,
23
+ logger,
23
24
  protect: { inputAnalysis },
24
25
  } = core;
25
26
 
@@ -28,28 +29,26 @@ module.exports = (core) => {
28
29
  messages.on(Event.SERVER_SETTINGS_UPDATE, (serverUpdate) => {
29
30
  const virtualPatches = serverUpdate.protect?.['virtual-patches'];
30
31
  if (virtualPatches) {
31
- buildVPEvaluators(virtualPatches, virtualPatchesEvaluators);
32
+ buildVPEvaluators(virtualPatches, virtualPatchesEvaluators, logger);
32
33
  }
33
34
  });
34
35
  };
35
36
 
36
- function buildVPEvaluators(virtualPatches, evaluatorsArray) {
37
+ function buildVPEvaluators(virtualPatches, evaluatorsArray, logger) {
37
38
  evaluatorsArray.length = 0;
38
39
  for (const { headers, parameters, urls, uuid, name } of virtualPatches) {
39
40
  const evaluators = new Map();
40
41
 
41
42
  if (headers?.length) {
43
+ const checks = headers.map(({ evaluation, name, value }) => ({
44
+ name: StringPrototypeToLowerCase.call(name),
45
+ check: buildEvaluationCheck(evaluation, value, logger),
46
+ }));
47
+
42
48
  evaluators.set('HEADERS', (reqHeaders) => {
43
49
  let result;
44
- for (const { evaluation, name, value } of headers) {
45
- const evalCheck = buildEvaluationCheck(evaluation);
46
- const headersArray = Array.isArray(reqHeaders) ? reqHeaders : Object.entries(reqHeaders).reduce((acc, entry) => {
47
- acc.push(...entry);
48
- return acc;
49
- }, []);
50
- const keyIndex = headersArray.indexOf(StringPrototypeToLowerCase.call(name));
51
-
52
- result = keyIndex !== -1 && evalCheck(headersArray[keyIndex + 1], value);
50
+ for (const { name, check } of checks) {
51
+ result = check(findHeaderValue(reqHeaders, name));
53
52
  if (!result) break;
54
53
  }
55
54
 
@@ -58,12 +57,15 @@ function buildVPEvaluators(virtualPatches, evaluatorsArray) {
58
57
  }
59
58
 
60
59
  if (parameters?.length) {
60
+ const checks = parameters.map(({ evaluation, name, value }) => ({
61
+ name,
62
+ check: buildEvaluationCheck(evaluation, value, logger),
63
+ }));
64
+
61
65
  evaluators.set('PARAMETERS', (reqParameters) => {
62
66
  let result;
63
- for (const { evaluation, name, value } of parameters) {
64
- const evalCheck = buildEvaluationCheck(evaluation);
65
-
66
- result = evalCheck(reqParameters[name], value);
67
+ for (const { name, check } of checks) {
68
+ result = check(reqParameters[name]);
67
69
  if (!result) break;
68
70
  }
69
71
 
@@ -72,12 +74,12 @@ function buildVPEvaluators(virtualPatches, evaluatorsArray) {
72
74
  }
73
75
 
74
76
  if (urls?.length) {
77
+ const checks = urls.map(({ evaluation, value }) => buildEvaluationCheck(evaluation, value, logger));
78
+
75
79
  evaluators.set('URLS', (reqUrl) => {
76
80
  let result;
77
- for (const { evaluation, value } of urls) {
78
- const evalCheck = buildEvaluationCheck(evaluation);
79
-
80
- result = evalCheck(reqUrl, value);
81
+ for (const check of checks) {
82
+ result = check(reqUrl);
81
83
  if (!result) break;
82
84
  }
83
85
 
@@ -92,21 +94,59 @@ function buildVPEvaluators(virtualPatches, evaluatorsArray) {
92
94
  }
93
95
  }
94
96
 
95
- function buildEvaluationCheck(evaluation) {
97
+ // reqHeaders is either a flat [name, value, ...] array (rawHeaders) or an object.
98
+ // rawHeaders keep the case the client sent, so names are compared lowercased.
99
+ // lowerName must already be lowercased.
100
+ function findHeaderValue(reqHeaders, lowerName) {
101
+ if (Array.isArray(reqHeaders)) {
102
+ for (let i = 0; i < reqHeaders.length; i += 2) {
103
+ if (StringPrototypeToLowerCase.call(reqHeaders[i]) === lowerName) return reqHeaders[i + 1];
104
+ }
105
+ return undefined;
106
+ }
107
+
108
+ for (const key of Object.keys(reqHeaders)) {
109
+ if (StringPrototypeToLowerCase.call(key) === lowerName) return reqHeaders[key];
110
+ }
111
+ }
112
+
113
+ // returns a function that takes the request value and returns whether the
114
+ // evaluation is satisfied. a missing request value never satisfies an evaluation.
115
+ // the regex is compiled once here, and an invalid one never matches.
116
+ function buildEvaluationCheck(evaluation, matchedValue, logger) {
117
+ const check = buildCheck(evaluation, matchedValue, logger);
118
+
119
+ return (reqValue) => {
120
+ if (reqValue === undefined || reqValue === null) return false;
121
+ return check(String(reqValue));
122
+ };
123
+ }
124
+
125
+ function buildCheck(evaluation, matchedValue, logger) {
126
+ const expected = String(matchedValue);
127
+
96
128
  switch (evaluation) {
97
129
  case 'MATCHES':
98
- return (reqValue, matchedValue) => RegExpPrototypeTest.call(new RegExp(matchedValue, 'i'), reqValue);
130
+ case 'DOESNT_MATCH': {
131
+ let regex;
132
+ try {
133
+ regex = new RegExp(matchedValue, 'i');
134
+ } catch (err) {
135
+ logger?.warn({ err, evaluation }, 'invalid regular expression in virtual patch, it will never match');
136
+ return () => false;
137
+ }
138
+ const test = (reqValue) => RegExpPrototypeTest.call(regex, reqValue);
139
+ return evaluation === 'MATCHES' ? test : (reqValue) => !test(reqValue);
140
+ }
99
141
  case 'EQUALS':
100
- return (reqValue, matchedValue) => reqValue.toString() === matchedValue.toString();
142
+ return (reqValue) => reqValue === expected;
101
143
  case 'CONTAINS':
102
- return (reqValue, matchedValue) => reqValue.includes(matchedValue);
103
- case 'DOESNT_MATCH':
104
- return (reqValue, matchedValue) => !RegExpPrototypeTest.call(new RegExp(matchedValue, 'i'), reqValue);
105
- // This is a typo but it is how it's passed from ContrastUI
144
+ return (reqValue) => reqValue.includes(expected);
145
+ // DOESNT_EQUALS is a typo but it is how it's passed from ContrastUI
106
146
  case 'DOESNT_EQUALS':
107
- return (reqValue, matchedValue) => reqValue.toString() !== matchedValue.toString();
147
+ return (reqValue) => reqValue !== expected;
108
148
  case 'DOESNT_CONTAIN':
109
- return (reqValue, matchedValue) => !reqValue.includes(matchedValue);
149
+ return (reqValue) => !reqValue.includes(expected);
110
150
  default:
111
151
  return () => false;
112
152
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/protect",
3
- "version": "1.84.0",
3
+ "version": "1.85.1",
4
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)",
@@ -14,26 +14,26 @@
14
14
  "types": "lib/index.d.ts",
15
15
  "engines": {
16
16
  "npm": ">=8.5.5",
17
- "node": ">=18.7.0"
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/blocking": "1.1.0",
25
- "@contrast/common": "1.43.0",
26
- "@contrast/config": "1.64.0",
27
- "@contrast/core": "1.69.0",
28
- "@contrast/dep-hooks": "1.39.0",
29
- "@contrast/esm-hooks": "2.45.0",
30
- "@contrast/instrumentation": "1.49.0",
31
- "@contrast/logger": "1.42.0",
32
- "@contrast/patcher": "1.41.0",
33
- "@contrast/rewriter": "1.46.0",
34
- "@contrast/scopes": "1.40.0",
35
- "@contrast/sources": "1.15.0",
36
- "@contrast/stack-trace-factory": "1.11.0",
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",
37
37
  "async-hook-domain": "4.0.1",
38
38
  "ipaddr.js": "2.5.0",
39
39
  "on-finished": "2.4.1",