@contrast/agent 4.27.2 → 4.28.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.
package/bin/VERSION CHANGED
@@ -1 +1 @@
1
- 2.28.22
1
+ 2.28.23
Binary file
Binary file
Binary file
Binary file
Binary file
package/bootstrap.js CHANGED
@@ -44,8 +44,12 @@ Module.runMain = async function (...args) {
44
44
 
45
45
  const diagnostics = process.env['CONTRAST__AGENT__DIAGNOSTICS__ENABLE'];
46
46
  if (diagnostics === 'true') {
47
- // eslint-disable-next-line no-process-exit
48
- process.exit(0);
47
+ // Delay just a little bit, so the console.log() in config-diagnostics can finish
48
+ // Might wanna look for a better solution
49
+ setTimeout(() => {
50
+ // eslint-disable-next-line no-process-exit
51
+ process.exit(0);
52
+ }, 100); // not sure why so long, in v5 it only needed 5ms more
49
53
  }
50
54
 
51
55
  loader.logTime(startTime, 'agent');
@@ -20,11 +20,11 @@ const path = require('path');
20
20
 
21
21
  const HELP_MESSAGE = `
22
22
  Description:
23
- The read-contrast-config utility returns the current effective node agent configuration.
23
+ The config-diagnostics utility returns the current effective node agent configuration.
24
24
 
25
25
  Usage:
26
- npx -p @contrast/agent read-contrast-config <path/to/entry/script> [options]
27
- npx -p @contrast/agent read-contrast-config --help
26
+ npx -p @contrast/agent config-diagnostics <path/to/entry/script> [options]
27
+ npx -p @contrast/agent config-diagnostics --help
28
28
 
29
29
  Options:
30
30
  --quiet -q Prevents output of the report to the console.
@@ -76,7 +76,9 @@ const diagnostics = {
76
76
 
77
77
  executeNodeAgent(args) {
78
78
  let agentEnvArgs =
79
- 'CONTRAST__SHOW__BANNER=false CONTRAST__AGENT__DIAGNOSTICS__ENABLE=true';
79
+ 'CONTRAST__SHOW__BANNER=false ' +
80
+ 'CONTRAST__AGENT__DIAGNOSTICS__ENABLE=true ' +
81
+ 'CONTRAST__AGENT__SYSTEM_DIAGNOSTICS__ENABLE=false';
80
82
 
81
83
  if (!args.quiet) {
82
84
  agentEnvArgs = `${agentEnvArgs} CONTRAST__AGENT__DIAGNOSTICS__QUIET=false`;
@@ -12,7 +12,7 @@ Copyright: 2022 Contrast Security, Inc
12
12
  engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
13
  way not consistent with the End User License Agreement.
14
14
  */
15
- 'use stict';
15
+ 'use strict';
16
16
 
17
17
  const agentEmitter = require('../../agent-emitter');
18
18
  const logger = require('../../core/logger')('contrast:express:route-coverage');
@@ -70,7 +70,7 @@ class ExpressRouteCoverage {
70
70
  const { args, result: router } = wrapCtx;
71
71
  const layer = Helpers.getLastLayer(router);
72
72
 
73
- if (!layer) {
73
+ if (!layer || !layer.route || !layer.route.stack) {
74
74
  return;
75
75
  }
76
76
 
@@ -106,7 +106,7 @@ class ExpressRouteCoverage {
106
106
 
107
107
  const layer = Helpers.getLastLayer(app._router);
108
108
 
109
- if (!layer || !layer.route) {
109
+ if (!layer || !layer.route || !layer.route.stack) {
110
110
  return;
111
111
  }
112
112
 
package/lib/contrast.js CHANGED
@@ -26,6 +26,7 @@ const sourceMapUtility = require('./util/source-map');
26
26
  const loggerFactory = require('./core/logger');
27
27
  const logger = loggerFactory('contrast:contrast-init');
28
28
  const { outputAgentConfigFile } = require('./util/config-diagnostics-utils');
29
+ const { fetchSystemInfo, outputSystemInfo } = require('../system-diagnostics');
29
30
 
30
31
  function getAgentSnippet() {
31
32
  // getting reference to name every time for testing purposes
@@ -335,6 +336,11 @@ contrastAgent.bootstrap = function(args) {
335
336
  } catch (err) {
336
337
  outputAgentConfigFile(agent, options, args, err);
337
338
  }
339
+
340
+ if (!(process.env['CONTRAST__AGENT__SYSTEM_DIAGNOSTICS__ENABLE'] === 'false')) {
341
+ const info = fetchSystemInfo();
342
+ outputSystemInfo({ skip: false, quiet: true, output: 'contrast_system_info.json' }, info);
343
+ }
338
344
  });
339
345
  };
340
346
 
@@ -372,6 +372,8 @@ const handleUseDecoration = (self, event, className) => {
372
372
  * @param {Layer[]} stack Express application stack
373
373
  */
374
374
  const instrumentHandler = (layer, id, self, stack) => {
375
+ if (!layer) return;
376
+
375
377
  const methodName = getLayerHandleMethod(layer);
376
378
 
377
379
  patcher.patch(layer, methodName, {
@@ -471,14 +473,16 @@ class RouteCoverage {
471
473
  alwaysRun: true,
472
474
  pre(data) {
473
475
  // There's a new stack item for each fn handler.
474
- data.nextIdx = this.stack.length;
476
+ if (this.stack) {
477
+ data.nextIdx = this.stack.length;
478
+ }
475
479
  },
476
480
  post(data) {
477
481
  // if the stack didn't actually grow
478
482
  // ie if router.get('/') with no callback is called
479
483
  // This is done to cache routes in Express, so we want to
480
- // skip this.
481
- if (this.stack.length === data.nextIdx) {
484
+ // skip this. In older versions of express stack will be undefined.
485
+ if (!this.stack || this.stack.length === data.nextIdx) {
482
486
  logger.debug('express router called without a callback.');
483
487
  data.result = undefined;
484
488
  return;
@@ -29,25 +29,25 @@ function getLoggerValues(option, config, tsData) {
29
29
  tsValue = tsData.logFile;
30
30
  break;
31
31
  case 'agent.security_logger.syslog.enable':
32
- tsValue = tsData.defend.syslog.enabled;
32
+ tsValue = tsData.defend.syslog && tsData.defend.syslog.enabled;
33
33
  break;
34
34
  case 'agent.security_logger.syslog.ip':
35
- tsValue = tsData.defend.syslog.ipAddress;
35
+ tsValue = tsData.defend.syslog && tsData.defend.syslog.ipAddress;
36
36
  break;
37
37
  case 'agent.security_logger.syslog.port':
38
- tsValue = tsData.defend.syslog.port;
38
+ tsValue = tsData.defend.syslog && tsData.defend.syslog.port;
39
39
  break;
40
40
  case 'agent.security_logger.syslog.fascility':
41
- tsValue = tsData.defend.syslog.fascilityCode;
41
+ tsValue = tsData.defend.syslog && tsData.defend.syslog.fascilityCode;
42
42
  break;
43
43
  case 'agent.security_logger.syslog.severity_exploited':
44
- tsValue = tsData.defend.syslog.severityExploited;
44
+ tsValue = tsData.defend.syslog && tsData.defend.syslog.severityExploited;
45
45
  break;
46
46
  case 'agent.security_logger.syslog.severity_blocked':
47
- tsValue = tsData.defend.syslog.severityBlocked;
47
+ tsValue = tsData.defend.syslog && tsData.defend.syslog.severityBlocked;
48
48
  break;
49
49
  case 'agent.security_logger.syslog.severity_probed':
50
- tsValue = tsData.defend.syslog.severityProbed;
50
+ tsValue = tsData.defend.syslog && tsData.defend.syslog.severityProbed;
51
51
  break;
52
52
  default:
53
53
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/agent",
3
- "version": "4.27.2",
3
+ "version": "4.28.1",
4
4
  "description": "Node.js security instrumentation by Contrast Security",
5
5
  "keywords": [
6
6
  "security",
@@ -55,7 +55,8 @@
55
55
  "node-contrast": "cli.js",
56
56
  "perf-logs": "perf-logs.js",
57
57
  "contrast-transpile": "cli-rewriter.js",
58
- "read-contrast-config": "config-diagnostics.js"
58
+ "config-diagnostics": "config-diagnostics.js",
59
+ "system-diagnostics": "system-diagnostics.js"
59
60
  },
60
61
  "files": [
61
62
  "bin/**",
@@ -66,7 +67,8 @@
66
67
  "esm.mjs",
67
68
  "perf-logs.js",
68
69
  "cli-rewriter.js",
69
- "config-diagnostics.js"
70
+ "config-diagnostics.js",
71
+ "system-diagnostics.js"
70
72
  ],
71
73
  "repository": {
72
74
  "type": "git"
@@ -119,7 +121,7 @@
119
121
  "@bmacnaughton/string-generator": "^1.0.0",
120
122
  "@contrast/eslint-config": "^3.0.2",
121
123
  "@contrast/fake-module": "file:test/mock/contrast-fake",
122
- "@contrast/screener-service": "^1.12.9",
124
+ "@contrast/screener-service": "^1.12.12",
123
125
  "@hapi/boom": "file:test/mock/boom",
124
126
  "@hapi/hapi": "file:test/mock/hapi",
125
127
  "@ls-lint/ls-lint": "^1.11.2",
@@ -0,0 +1,183 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ Copyright: 2022 Contrast Security, Inc
4
+ Contact: support@contrastsecurity.com
5
+ License: Commercial
6
+
7
+ NOTICE: This Software and the patented inventions embodied within may only be
8
+ used as part of Contrast Security’s commercial offerings. Even though it is
9
+ made available through public repositories, use of this Software is subject to
10
+ the applicable End User Licensing Agreement found at
11
+ https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
12
+ between Contrast Security and the End User. The Software may not be reverse
13
+ engineered, modified, repackaged, sold, redistributed or otherwise used in a
14
+ way not consistent with the End User License Agreement.
15
+ */
16
+ 'use strict';
17
+
18
+ const path = require('path');
19
+ const fs = require('fs');
20
+ const os = require('os');
21
+ const pkg = require('./package');
22
+ const { setup } = require('./lib/core/config/util');
23
+ const logger = require('./lib/core/logger')('contrast:system-diagnostics');
24
+
25
+ const HELP_MESSAGE = `
26
+ Description:
27
+ The system-diagnostics utility returns the system info for the server/container the agent is running on.
28
+
29
+ Usage:
30
+ npx -p @contrast/agent system-diagnostics --help
31
+ npx -p @contrast/agent system-diagnostics [options]
32
+
33
+ Options:
34
+ --quiet -q Prevents output of the report to the console.
35
+ --output -o The directory to write the report in. Defaults to the current directory.
36
+ `;
37
+
38
+ function isContainer() {
39
+ try {
40
+ fs.statSync('/.dockerenv');
41
+ return true;
42
+ } catch (err) {
43
+ // if no docker env, check /proc/self/cgroup
44
+ }
45
+
46
+ try {
47
+ return fs.readFileSync('/proc/self/cgroup', 'utf8').includes('docker');
48
+ } catch (err) {
49
+ return false;
50
+ }
51
+ }
52
+
53
+ function isUsingPM2() {
54
+ const used = !!process.env.pmx;
55
+ let version = null;
56
+
57
+ for (const pathVar of ['npm_package_json', 'PWD']) {
58
+ let packagePath;
59
+ if (packagePath = process.env[pathVar]) {
60
+ try {
61
+ version = require(path.join(packagePath, 'package.json'));
62
+ } catch(err) {
63
+ //
64
+ }
65
+ }
66
+ if (version) break;
67
+ }
68
+
69
+ return { used, version };
70
+ }
71
+
72
+ const diagnostics = {
73
+ parseArgv(argv) {
74
+ const args = {
75
+ help: false,
76
+ output: 'contrast_system_info.json',
77
+ quiet: false,
78
+ };
79
+
80
+ for (let i = 0; i < argv.length; i++) {
81
+ const arg = argv[i];
82
+
83
+ switch (arg) {
84
+ case '--help':
85
+ args.help = true;
86
+ return args;
87
+ case '--output':
88
+ case '-o': {
89
+ // grab the next value and skip in the next loop
90
+ const output = argv[++i];
91
+ if (!output) {
92
+ throw new Error('Expected a value to be provided after --output');
93
+ }
94
+ args.output = output;
95
+ break;
96
+ }
97
+ case '--quiet':
98
+ case '-q':
99
+ args.quiet = true;
100
+ break;
101
+ default:
102
+ if (!arg.startsWith('-')) {
103
+ args.entry = arg;
104
+ }
105
+ break;
106
+ }
107
+ }
108
+
109
+ return args;
110
+ },
111
+
112
+ outputSystemInfo(args, info) {
113
+ try {
114
+ fs.accessSync(path.join(args.output, '..'), fs.constants.RDWD_OK);
115
+ fs.writeFileSync(args.output, JSON.stringify(info, null, 2), 'utf-8');
116
+ } catch (err) {
117
+ console.log(`Couldn't create system info file: ${err}`);
118
+ }
119
+
120
+ if (!args.quiet) {
121
+ console.log(JSON.stringify(info, null, 2));
122
+ }
123
+ },
124
+
125
+ fetchSystemInfo() {
126
+ const yaml = setup({}, logger);
127
+
128
+ const info = {
129
+ ReportDate: new Date(),
130
+ MachineName: os.hostname(),
131
+ Contrast: {
132
+ Url: yaml.api.url,
133
+ Proxy: yaml.api.proxy,
134
+ Server: {
135
+ Name: yaml._flat['server.name'],
136
+ },
137
+ Agent: {
138
+ Name: '@contrast/agent',
139
+ Version: pkg.version,
140
+ },
141
+ },
142
+ Node: {
143
+ Version: process.version
144
+ },
145
+ PM2: isUsingPM2(),
146
+ OperatingSystem: {
147
+ Architecture: os.arch(),
148
+ Name: os.type(),
149
+ Version: os.release(),
150
+ KernelVersion: os.version(),
151
+ CPU: {
152
+ type: os.cpus()[0].model,
153
+ count: os.cpus().length,
154
+ }
155
+ },
156
+ Host: {
157
+ isDocker: isContainer(),
158
+ Memory: {
159
+ Total: (os.totalmem() / 1e6).toFixed(0).concat(' MB'),
160
+ Free: (os.freemem() / 1e6).toFixed(0).concat(' MB'),
161
+ Used: ((os.totalmem() - os.freemem()) / 1e6).toFixed(0).concat(' MB'),
162
+ }
163
+ },
164
+ };
165
+
166
+ return info;
167
+ }
168
+ };
169
+ module.exports = diagnostics;
170
+
171
+ // only run if this file is being executed as an entry script
172
+ // istanbul ignore next
173
+
174
+ if (require.main === module) {
175
+ const args = diagnostics.parseArgv(process.argv.slice(2));
176
+
177
+ if (args.help) {
178
+ console.log(HELP_MESSAGE);
179
+ } else {
180
+ const info = diagnostics.fetchSystemInfo();
181
+ diagnostics.outputSystemInfo(args, info);
182
+ }
183
+ }