@contrast/protect 1.86.0 → 1.87.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/index.js CHANGED
@@ -52,7 +52,7 @@ module.exports = function(core) {
52
52
  };
53
53
 
54
54
  // append async state to store when request-scope sources are created
55
- sources.addHook('onSource', (ctx) => {
55
+ sources.hooks.add('onSource', (ctx) => {
56
56
  ctx.store.protect = protect.makeSourceContext(ctx);
57
57
  });
58
58
 
@@ -46,12 +46,12 @@ module.exports = function (core) {
46
46
  function callNext() {
47
47
  setImmediate(() => {
48
48
  blocking.initDomain();
49
- next.call(data.obj, ...data.args);
49
+ next(data);
50
50
  });
51
51
  }
52
52
 
53
53
  if (type !== 'request') {
54
- return next();
54
+ return next(data);
55
55
  }
56
56
 
57
57
  try {
@@ -145,14 +145,14 @@ module.exports = function(core) {
145
145
  const sourceCtx = getSourceContext();
146
146
 
147
147
  if (instrumentation.isLocked() || !sourceCtx) {
148
- return next();
148
+ return next(data);
149
149
  }
150
150
 
151
151
  for (const argIdx of argsIdxsToCheck) {
152
152
  getInfoMethod(args[argIdx], argIdx, sourceCtx, { name, hooked, orig });
153
153
  }
154
154
 
155
- return next();
155
+ return next(data);
156
156
  // return methodsWithNestedCalls.includes(method) ? runInActiveSink(NOSQL_INJECTION_MONGO, async () => await next()) : next();
157
157
  };
158
158
  }
@@ -15,13 +15,51 @@
15
15
 
16
16
  'use strict';
17
17
 
18
- const { isString } = require('@contrast/common');
18
+ const { isString, primordials: { BufferPrototypeToString } } = require('@contrast/common');
19
19
  const { SecurityException } = require('@contrast/blocking');
20
20
  const { patchType } = require('../constants');
21
21
 
22
+ /**
23
+ * libxml2 detects UTF-16 from a byte order mark, or from the first bytes of a
24
+ * document that starts with `<`. Decoding such input as UTF-8 interleaves NULs
25
+ * through the text, which hides entity declarations from the XXE matcher. So
26
+ * decode with the encoding libxml2 will pick.
27
+ * @param {Buffer} buf
28
+ * @returns {string}
29
+ */
30
+ function decodeXml(buf) {
31
+ const [b0, b1, b2, b3] = buf;
32
+ let le = false;
33
+ let be = false;
34
+ let skip = 0;
35
+
36
+ if (b0 === 0xFF && b1 === 0xFE) {
37
+ le = true;
38
+ skip = 2;
39
+ } else if (b0 === 0xFE && b1 === 0xFF) {
40
+ be = true;
41
+ skip = 2;
42
+ } else if (b0 === 0x3C && b1 === 0x00 && b2 === 0x3F && b3 === 0x00) {
43
+ le = true;
44
+ } else if (b0 === 0x00 && b1 === 0x3C && b2 === 0x00 && b3 === 0x3F) {
45
+ be = true;
46
+ }
47
+
48
+ if (!le && !be) {
49
+ return BufferPrototypeToString.call(buf, 'utf8');
50
+ }
51
+
52
+ // swap16 needs an even length and works in place, so copy before using it
53
+ const body = Buffer.from(buf.subarray(skip, buf.length - ((buf.length - skip) % 2)));
54
+ if (be) body.swap16();
55
+
56
+ return BufferPrototypeToString.call(body, 'utf16le');
57
+ }
58
+
22
59
  module.exports = function (core) {
23
60
  const {
24
61
  depHooks,
62
+ esmHooks,
25
63
  patcher,
26
64
  logger,
27
65
  protect,
@@ -75,6 +113,70 @@ module.exports = function (core) {
75
113
  });
76
114
  };
77
115
 
116
+ /**
117
+ * libxml2-wasm is ESM only and exposes its parser as static methods on
118
+ * `XmlDocument`. Options are `ParseOption` bit flags, where XML_PARSE_NOENT
119
+ * substitutes entities and loads external ones unless XML_PARSE_NO_XXE is set.
120
+ * @param {string} moduleName
121
+ */
122
+ const wasmHandler = (moduleName) => (mod) => {
123
+ const { XmlDocument, ParseOption } = mod;
124
+
125
+ if (!XmlDocument || typeof ParseOption?.XML_PARSE_NOENT !== 'number') {
126
+ logger.debug('%s does not export XmlDocument and ParseOption.XML_PARSE_NOENT, skipping xxe semantic analysis', moduleName);
127
+ return;
128
+ }
129
+
130
+ const noent = ParseOption.XML_PARSE_NOENT;
131
+ // not present in older versions, where external entities can't be disabled separately
132
+ const noXxe = ParseOption.XML_PARSE_NO_XXE ?? 0;
133
+
134
+ ['fromString', 'fromBuffer'].forEach((method) => {
135
+ const name = `${moduleName}:XmlDocument.${method}`;
136
+ patcher.patch(XmlDocument, method, {
137
+ name,
138
+ patchType,
139
+ pre({ args, hooked, orig, funcKey }) {
140
+ const sourceContext = protect.getSourceContext();
141
+ const [source, options] = args;
142
+
143
+ if (!sourceContext || !source) return;
144
+
145
+ const option = options?.option ?? 0;
146
+ if ((option & noent) === 0 || (option & noXxe) !== 0) return;
147
+
148
+ let value;
149
+ if (isString(source)) {
150
+ value = source;
151
+ } else if (ArrayBuffer.isView(source)) {
152
+ value = decodeXml(Buffer.from(source.buffer, source.byteOffset, source.byteLength));
153
+ } else {
154
+ return;
155
+ }
156
+
157
+ const sinkContext = {
158
+ name,
159
+ value,
160
+ stacktraceOpts: {
161
+ constructorOpt: hooked,
162
+ prependFrames: [orig]
163
+ },
164
+ };
165
+
166
+ try {
167
+ protect.semanticAnalysis.handleXXE(sourceContext, sinkContext);
168
+ } catch (err) {
169
+ if (SecurityException.isSecurityException(err)) {
170
+ throw err;
171
+ }
172
+
173
+ logger.error({ err, funcKey }, 'Unexpected error during semantic analysis');
174
+ }
175
+ }
176
+ });
177
+ });
178
+ };
179
+
78
180
  protect.semanticAnalysis.libxmljs = {
79
181
  install() {
80
182
  // libxmljs changed its API in version 1.0.0
@@ -83,6 +185,9 @@ module.exports = function (core) {
83
185
  // libxmljs versions prior to 1.0.0 and libxmljs2 share the same API
84
186
  depHooks.resolve({ name: 'libxmljs', version: '<1' }, handler(false));
85
187
  depHooks.resolve({ name: 'libxmljs2', version: '<1' }, handler(false));
188
+
189
+ // libxml2-wasm is ESM only, so it can't be reached through depHooks
190
+ esmHooks?.resolve({ name: 'libxml2-wasm', version: '<1' }, wasmHandler('libxml2-wasm'));
86
191
  }
87
192
  };
88
193
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/protect",
3
- "version": "1.86.0",
3
+ "version": "1.87.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.3.0",
24
+ "@contrast/blocking": "1.4.0",
25
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",
26
+ "@contrast/config": "1.67.0",
27
+ "@contrast/core": "1.72.0",
28
+ "@contrast/dep-hooks": "1.42.0",
29
+ "@contrast/esm-hooks": "2.48.0",
30
+ "@contrast/instrumentation": "1.52.0",
31
+ "@contrast/logger": "1.45.0",
32
+ "@contrast/patcher": "1.44.0",
33
+ "@contrast/rewriter": "1.49.0",
34
+ "@contrast/scopes": "1.43.0",
35
+ "@contrast/sources": "1.18.0",
36
+ "@contrast/stack-trace-factory": "1.14.0",
37
37
  "async-hook-domain": "4.0.1",
38
38
  "ipaddr.js": "2.5.0",
39
39
  "on-finished": "2.4.1",