@contrast/protect 1.85.0 → 1.86.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.
@@ -25,11 +25,11 @@ const {
25
25
  traverseKeysAndValues,
26
26
  traverseValues,
27
27
  InputType,
28
+ matchAll,
28
29
  primordials: {
29
30
  ArrayPrototypeJoin,
30
31
  ArrayPrototypeSlice,
31
32
  StringPrototypeToLowerCase,
32
- StringPrototypeMatch,
33
33
  }
34
34
  } = require('@contrast/common');
35
35
  const { Core } = require('@contrast/core');
@@ -94,7 +94,6 @@ const acceptedMethods = new Set([
94
94
  'version-control',
95
95
  ]);
96
96
  const componentName = 'protect.inputAnalysis.handlers';
97
- const noInstrumentationStore = { lock: true, name: componentName };
98
97
 
99
98
  module.exports = Core.makeComponent({
100
99
  name: componentName,
@@ -106,7 +105,6 @@ module.exports = Core.makeComponent({
106
105
  agentLib,
107
106
  inputAnalysis,
108
107
  },
109
- scopes,
110
108
  config,
111
109
  } = core;
112
110
 
@@ -955,21 +953,15 @@ module.exports = Core.makeComponent({
955
953
  function ipListAnalysis(reqIp, reqHeaders, list) {
956
954
  const forwardedIps = [];
957
955
 
958
- // RegExp.exec will still get called under the hood even with using primordials
959
- scopes.instrumentation.run(noInstrumentationStore, () => {
960
- for (let i = 0; i < reqHeaders.length; i++) {
961
- if (reqHeaders[i] === 'x-forwarded-for') {
962
- // This matches one or more valid IPv4/IPv6 characters, automatically
963
- // skipping delimiters, whitespace, and invalid characters
964
- const matches = StringPrototypeMatch.call(reqHeaders[i + 1], /[0-9a-fA-F.:]+/g);
965
- if (matches) {
966
- for (const ip of matches) {
967
- forwardedIps.push(ip);
968
- }
969
- }
956
+ for (let i = 0; i < reqHeaders.length; i++) {
957
+ if (reqHeaders[i] === 'x-forwarded-for') {
958
+ // This matches one or more valid IPv4/IPv6 characters, automatically
959
+ // skipping delimiters, whitespace, and invalid characters
960
+ for (const [ip] of matchAll(reqHeaders[i + 1], /[0-9a-fA-F.:]+/g)) {
961
+ forwardedIps.push(ip);
970
962
  }
971
963
  }
972
- });
964
+ }
973
965
 
974
966
  const ipsToCheck = [reqIp, ...forwardedIps];
975
967
  const now = new Date().getTime();
@@ -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) {
@@ -15,11 +15,12 @@
15
15
 
16
16
  'use strict';
17
17
 
18
- const { Event, primordials: { StringPrototypeToLowerCase, RegExpPrototypeTest } } = require('@contrast/common');
18
+ const { Event, primordials: { StringPrototypeToLowerCase, RegExpPrototypeExec } } = require('@contrast/common');
19
19
 
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) => RegExpPrototypeExec.call(regex, reqValue) !== null;
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/lib/policy.js CHANGED
@@ -23,7 +23,7 @@ const {
23
23
  ArrayPrototypeJoin,
24
24
  StringPrototypeToLowerCase,
25
25
  StringPrototypeSplit,
26
- RegExpPrototypeTest
26
+ RegExpPrototypeExec
27
27
  },
28
28
  set,
29
29
  } = require('@contrast/common');
@@ -208,7 +208,7 @@ module.exports = function (core) {
208
208
  if (regExpNeeded) {
209
209
  const rx = new RegExp(`^${ArrayPrototypeJoin.call(urls, '|')}$`);
210
210
 
211
- return (uriPath) => rx ? RegExpPrototypeTest.call(rx, uriPath) : false;
211
+ return (uriPath) => rx ? RegExpPrototypeExec.call(rx, uriPath) !== null : false;
212
212
  }
213
213
 
214
214
  return (uriPath) => urls.some((url) => url === uriPath);
@@ -225,7 +225,7 @@ module.exports = function (core) {
225
225
  function createInputNameMatcher(dtmInputName) {
226
226
  if (regExpCheck(dtmInputName)) {
227
227
  const rx = new RegExp(`^${dtmInputName}$`);
228
- return (inputName) => rx ? RegExpPrototypeTest.call(rx, inputName) : false;
228
+ return (inputName) => rx ? RegExpPrototypeExec.call(rx, inputName) !== null : false;
229
229
  }
230
230
 
231
231
  return (inputName) => inputName === dtmInputName;
@@ -24,7 +24,7 @@ const {
24
24
  primordials: {
25
25
  StringPrototypeReplace,
26
26
  ArrayPrototypeSlice,
27
- RegExpPrototypeTest
27
+ RegExpPrototypeExec
28
28
  }
29
29
  } = require('@contrast/common');
30
30
 
@@ -152,7 +152,7 @@ module.exports = function(core) {
152
152
  return (
153
153
  normalizedValue === normalizedCommand ||
154
154
  (normalizedCommand.endsWith(normalizedValue) &&
155
- RegExpPrototypeTest.call(SINK_EXPLOIT_PATTERN_START, normalizedCommand))
155
+ RegExpPrototypeExec.call(SINK_EXPLOIT_PATTERN_START, normalizedCommand) !== null)
156
156
  );
157
157
  }
158
158
 
@@ -14,7 +14,7 @@
14
14
  */
15
15
  'use strict';
16
16
 
17
- const { primordials: { StringPrototypeSubstr, StringPrototypeToLowerCase, RegExpPrototypeTest, RegExpPrototypeExec } } = require('@contrast/common');
17
+ const { primordials: { StringPrototypeSubstr, StringPrototypeToLowerCase, RegExpPrototypeExec } } = require('@contrast/common');
18
18
 
19
19
  const PROTOCOLS = {
20
20
  FTP: 'FTP',
@@ -67,7 +67,7 @@ function isExternalEntity(str) {
67
67
  if (str.startsWith(val)) return true;
68
68
  }
69
69
 
70
- return RegExpPrototypeTest.call(FILE_PATTERN_WINDOWS, str);
70
+ return RegExpPrototypeExec.call(FILE_PATTERN_WINDOWS, str) !== null;
71
71
  }
72
72
 
73
73
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/protect",
3
- "version": "1.85.0",
3
+ "version": "1.86.0",
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)",
@@ -21,19 +21,19 @@
21
21
  },
22
22
  "dependencies": {
23
23
  "@contrast/agent-lib": "9.2.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",
24
+ "@contrast/blocking": "1.3.0",
25
+ "@contrast/common": "1.45.0",
26
+ "@contrast/config": "1.66.0",
27
+ "@contrast/core": "1.71.0",
28
+ "@contrast/dep-hooks": "1.41.0",
29
+ "@contrast/esm-hooks": "2.47.0",
30
+ "@contrast/instrumentation": "1.51.0",
31
+ "@contrast/logger": "1.44.0",
32
+ "@contrast/patcher": "1.43.0",
33
+ "@contrast/rewriter": "1.48.0",
34
+ "@contrast/scopes": "1.42.0",
35
+ "@contrast/sources": "1.17.0",
36
+ "@contrast/stack-trace-factory": "1.13.0",
37
37
  "async-hook-domain": "4.0.1",
38
38
  "ipaddr.js": "2.5.0",
39
39
  "on-finished": "2.4.1",