@contrast/protect 1.28.0 → 1.29.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.
@@ -14,4 +14,57 @@
14
14
  */
15
15
 
16
16
  import esmHooks from '@contrast/esm-hooks';
17
- export const { getSource, transformSource, load } = esmHooks();
17
+ import { createRequire } from 'node:module';
18
+
19
+ const ERROR_MESSAGE = 'A fatal agent installation error has occurred. The application will be run without instrumentation.';
20
+
21
+ let getSource = async function getSource(url, context, defaultGetSource) {
22
+ return defaultGetSource(url, context, defaultGetSource);
23
+ };
24
+
25
+ let transformSource = async function transformSource(url, context, defaultTransformSource) {
26
+ return defaultTransformSource(url, context, defaultTransformSource);
27
+ };
28
+
29
+ let load = async function load(url, context, defaultLoad) {
30
+ return defaultLoad(url, context, defaultLoad);
31
+ };
32
+
33
+ const require = createRequire(import.meta.url);
34
+ const { name: agentName, version: agentVersion } = require('../package.json');
35
+ const core = { agentName, agentVersion };
36
+ try {
37
+ require('@contrast/core/lib/messages')(core);
38
+ require('@contrast/config')(core);
39
+ require('@contrast/logger').default(core);
40
+
41
+ // @contrast/info ?
42
+ require('@contrast/core/lib/agent-info')(core);
43
+ require('@contrast/core/lib/system-info')(core);
44
+ require('@contrast/core/lib/app-info')(core);
45
+ require('@contrast/core/lib/sensitive-data-masking')(core);
46
+ require('@contrast/core/lib/is-agent-path')(core);
47
+ require('@contrast/core/lib/capture-stacktrace')(core);
48
+
49
+ require('@contrast/patcher')(core);
50
+ require('@contrast/rewriter')(core); // merge contrast-methods?
51
+ require('@contrast/core/lib/contrast-methods')(core); // can we remove dependency on patcher?
52
+
53
+ require('@contrast/dep-hooks')(core);
54
+ require('@contrast/scopes')(core);
55
+ require('@contrast/deadzones')(core);
56
+ require('@contrast/reporter').default(core);
57
+ require('@contrast/instrumentation')(core);
58
+
59
+ // TODO: i think we still need to INSTALL components.
60
+ ({ getSource, transformSource, load } = esmHooks(core));
61
+ } catch (err) {
62
+ // TODO: Consider proper UNINSTALLATION and normal startup w/o agent
63
+ if (core.logger) {
64
+ core.logger.error({ err }, ERROR_MESSAGE);
65
+ } else {
66
+ console.error(new Error(ERROR_MESSAGE, { cause: err }));
67
+ }
68
+ }
69
+
70
+ export { getSource, transformSource, load };
package/lib/index.d.ts CHANGED
@@ -131,4 +131,6 @@ export interface Protect {
131
131
  version: string;
132
132
  }
133
133
 
134
- export default function(core: Core): ProtectMessage;
134
+ declare function init(core: Core): ProtectMessage;
135
+
136
+ export = init;
@@ -301,7 +301,8 @@ module.exports = function(core) {
301
301
  const { policy: { rulesMask } } = sourceContext;
302
302
 
303
303
  const cookiesArr = Object.entries(cookies).reduce((acc, [key, value]) => {
304
- acc.push(key, value);
304
+ // things like booleans will cause agent-lib to throw
305
+ if (isString(value)) acc.push(key, value);
305
306
  return acc;
306
307
  }, []);
307
308
 
@@ -93,7 +93,7 @@ module.exports = (core) => {
93
93
  // we can exit early if
94
94
  // the layer doesn't match the request or
95
95
  // the layer doesn't recognize any parameters
96
- if (!data.result || !layer.keys || layer.keys.length === 0 || layer.route.path === '*') {
96
+ if (!data.result || !layer.keys || layer.keys.length === 0 || layer.route?.path === '*') {
97
97
  return;
98
98
  }
99
99
 
@@ -220,6 +220,11 @@ module.exports = function(core) {
220
220
  if (stringFindings) {
221
221
  const nosqlInjectionResult = { ...result, ruleId, mappedId: ruleId };
222
222
 
223
+ // don't modify ssjs-injection result items so use new exploit metadata array here
224
+ if (nosqlInjectionResult.idsList?.some?.((id) => id.startsWith('SSJS'))) {
225
+ nosqlInjectionResult.exploitMetadata = [];
226
+ }
227
+
223
228
  const nosqlInjectionResults = sourceContext.resultsMap[ruleId];
224
229
  const isAlreadyPresentInNosqlresults = result.idsList &&
225
230
  result.idsList.some(
@@ -18,7 +18,21 @@
18
18
  const { isString, isNonEmptyObject } = require('@contrast/common');
19
19
  const { patchType } = require('../constants');
20
20
 
21
- module.exports = function(core) {
21
+ const VM_METHODS = [
22
+ ['Script', 1],
23
+ ['createContext', 1],
24
+ ['compileFunction', 1],
25
+ ['runInContext', 2],
26
+ ['runInNewContext', 2],
27
+ ['runInThisContext', 1],
28
+ ];
29
+
30
+ const SCRIPT_METHODS = [
31
+ 'runInContext',
32
+ 'runInNewContext'
33
+ ];
34
+
35
+ module.exports = function (core) {
22
36
  const {
23
37
  scopes: { instrumentation },
24
38
  patcher,
@@ -27,15 +41,15 @@ module.exports = function(core) {
27
41
  protect: { inputTracing }
28
42
  } = core;
29
43
 
30
- function pre({ args, hooked, orig, name }) {
44
+ const createPre = (arity) => ({ args, hooked, orig, name }) => {
31
45
  if (instrumentation.isLocked()) return;
32
46
 
33
47
  const sourceContext = protect.getSourceContext(name);
34
48
  if (!sourceContext) return;
35
49
 
36
- for (let i = 0; i < (name === 'vm.runInNewContext' ? 2 : 1); i++) {
50
+ for (let i = 0; i < arity; i++) {
37
51
  const arg = args[i];
38
- if (!((arg && isString(arg)) || isNonEmptyObject(arg))) continue;
52
+ if (!arg || !(isString(arg) || isNonEmptyObject(arg))) continue;
39
53
 
40
54
  const sinkContext = {
41
55
  name,
@@ -44,37 +58,29 @@ module.exports = function(core) {
44
58
  };
45
59
  inputTracing.ssjsInjection(sourceContext, sinkContext);
46
60
  }
47
- }
61
+ };
48
62
 
49
- function install() {
50
- depHooks.resolve({ name: 'vm' }, (vm) => {
51
- [
52
- 'Script',
53
- 'createScript',
54
- 'runInContext',
55
- 'runInThisContext',
56
- 'createContext',
57
- 'runInNewContext'
58
- ].forEach(
59
- (method) => {
60
- const name = `vm.${method}`;
63
+ const vmInstrumentation = inputTracing.vmInstrumentation = {
64
+ install() {
65
+ depHooks.resolve({ name: 'vm' }, (vm) => {
66
+ VM_METHODS.forEach(([method, arity]) => {
61
67
  patcher.patch(vm, method, {
62
- name,
68
+ name: `vm.${method}`,
63
69
  patchType,
64
- pre
70
+ pre: createPre(arity)
65
71
  });
66
- }
67
- );
72
+ });
68
73
 
69
- patcher.patch(vm.Script.prototype, 'runInNewContext', {
70
- name: 'vm.Script.prototype.runInNewContext',
71
- patchType,
72
- pre
74
+ SCRIPT_METHODS.forEach((method) => {
75
+ patcher.patch(vm.Script.prototype, method, {
76
+ name: `vm.Script.prototype.${method}`,
77
+ patchType,
78
+ pre: createPre(1)
79
+ });
80
+ });
73
81
  });
74
- });
75
- }
76
-
77
- const vmInstrumentation = inputTracing.vmInstrumentation = { install };
82
+ }
83
+ };
78
84
 
79
85
  return vmInstrumentation;
80
86
  };
package/lib/policy.js CHANGED
@@ -39,7 +39,7 @@ const {
39
39
  UNTRUSTED_DESERIALIZATION,
40
40
  XXE,
41
41
  },
42
- Event: { SERVER_SETTINGS_UPDATE },
42
+ Event,
43
43
  toLowerCase,
44
44
  split,
45
45
  join
@@ -345,7 +345,7 @@ module.exports = function (core) {
345
345
  }
346
346
  }
347
347
 
348
- messages.on(SERVER_SETTINGS_UPDATE, (msg) => {
348
+ messages.on(Event.SERVER_SETTINGS_UPDATE, (msg) => {
349
349
  if (!config.getEffectiveValue('protect.enable')) return;
350
350
 
351
351
  updateExclusions(msg);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/protect",
3
- "version": "1.28.0",
3
+ "version": "1.29.0",
4
4
  "description": "Contrast service providing framework-agnostic Protect support",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
@@ -18,9 +18,9 @@
18
18
  },
19
19
  "dependencies": {
20
20
  "@contrast/agent-lib": "^7.0.1",
21
- "@contrast/common": "1.15.1",
22
- "@contrast/core": "1.26.0",
23
- "@contrast/esm-hooks": "1.22.0",
21
+ "@contrast/common": "1.16.0",
22
+ "@contrast/core": "1.27.0",
23
+ "@contrast/esm-hooks": "1.23.0",
24
24
  "@contrast/scopes": "1.4.0",
25
25
  "ipaddr.js": "^2.0.1",
26
26
  "semver": "^7.3.7"