@contrast/agent 4.28.0 → 4.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.
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');
@@ -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`;
package/lib/app-info.js CHANGED
@@ -18,22 +18,35 @@ const fs = require('fs');
18
18
  const path = require('path');
19
19
  const parentPackageJson = require('parent-package-json');
20
20
  const semver = require('semver');
21
+ const { v4: uuid } = require('uuid');
21
22
  const { AGENT_INFO } = require('./constants');
22
23
  const logger = require('./core/logger')('contrast:appInfo');
23
24
 
24
- const isContainer = () => {
25
+ const MOUNTINFO_REGEX = /\/docker\/containers\/(.*?)\//;
26
+ const CGROUP_REGEX = /:\/docker\/([^/]+)$/;
27
+
28
+ const getContainerId = () => {
25
29
  try {
26
- fs.statSync('/.dockerenv');
27
- return true;
30
+ const results = fs.readFileSync('/proc/self/mountinfo', 'utf8').match(MOUNTINFO_REGEX);
31
+
32
+ if (results) return results[1];
28
33
  } catch (err) {
29
- // if no docker env, check /proc/self/cgroup
34
+ //
30
35
  }
31
36
 
32
37
  try {
33
- return fs.readFileSync('/proc/self/cgroup', 'utf8').includes('docker');
38
+ const results = fs.readFileSync('/proc/self/cgroup', 'utf8').match(CGROUP_REGEX);
39
+
40
+ if (results) return results[1];
41
+ } catch (err) {
42
+ //
43
+ }
44
+
45
+ try {
46
+ fs.statSync('/.dockerenv');
47
+ return `_${uuid()}`;
34
48
  } catch (err) {
35
- // if file not present,
36
- return false;
49
+ return null;
37
50
  }
38
51
  };
39
52
 
@@ -69,7 +82,7 @@ class AppInfo {
69
82
  type: os.type()
70
83
  };
71
84
  this.hostname = os.hostname();
72
- this.isContainer = isContainer();
85
+ this.containerId = getContainerId();
73
86
 
74
87
  logger.info('finding package.json for %s', this.indexFile);
75
88
 
@@ -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
 
@@ -555,6 +555,48 @@
555
555
  }
556
556
  ]
557
557
  }
558
+ },
559
+ "mssql/lib/base/prepared-statement.prototype.prepare": {
560
+ "type": "dataflow",
561
+ "enabled": true,
562
+ "conditions": {
563
+ "mode": "or",
564
+ "args": [
565
+ {
566
+ "index": 0,
567
+ "requiredTags": ["untrusted"],
568
+ "disallowedTags": ["sql-encoded", "limited-chars"]
569
+ }
570
+ ]
571
+ }
572
+ },
573
+ "mssql/lib/base/request.prototype.batch": {
574
+ "type": "dataflow",
575
+ "enabled": true,
576
+ "conditions": {
577
+ "mode": "or",
578
+ "args": [
579
+ {
580
+ "index": 0,
581
+ "requiredTags": ["untrusted"],
582
+ "disallowedTags": ["sql-encoded", "limited-chars"]
583
+ }
584
+ ]
585
+ }
586
+ },
587
+ "mssql/lib/base/request.prototype.query": {
588
+ "type": "dataflow",
589
+ "enabled": true,
590
+ "conditions": {
591
+ "mode": "or",
592
+ "args": [
593
+ {
594
+ "index": 0,
595
+ "requiredTags": ["untrusted"],
596
+ "disallowedTags": ["sql-encoded", "limited-chars"]
597
+ }
598
+ ]
599
+ }
558
600
  }
559
601
  }
560
602
  },
@@ -626,6 +626,24 @@
626
626
  "methodName": "Database.prototype.prepare",
627
627
  "isModule": true
628
628
  },
629
+ "mssql/lib/base/prepared-statement.prototype.prepare": {
630
+ "moduleName": "mssql",
631
+ "version": ">=6.4.0",
632
+ "methodName": "PreparedStatement.prototype.prepare",
633
+ "isModule": true
634
+ },
635
+ "mssql/lib/base/request.prototype.batch": {
636
+ "moduleName": "mssql",
637
+ "version": ">=6.4.0",
638
+ "methodName": "Request.prototype.batch",
639
+ "isModule": true
640
+ },
641
+ "mssql/lib/base/request.prototype.query": {
642
+ "moduleName": "mssql",
643
+ "version": ">=6.4.0",
644
+ "methodName": "Request.prototype.query",
645
+ "isModule": true
646
+ },
629
647
  "path.format": {
630
648
  "moduleName": "path",
631
649
  "methodName": "format",
package/lib/contrast.js CHANGED
@@ -337,8 +337,10 @@ contrastAgent.bootstrap = function(args) {
337
337
  outputAgentConfigFile(agent, options, args, err);
338
338
  }
339
339
 
340
- const info = fetchSystemInfo();
341
- outputSystemInfo({ skip: false, quiet: true, output: 'contrast_system_info.json' }, info);
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
+ }
342
344
  });
343
345
  };
344
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;
package/lib/telemetry.js CHANGED
@@ -74,12 +74,18 @@ module.exports = class Telemetry {
74
74
  const mac = getMac();
75
75
 
76
76
  const hash = createHash('sha256').update(mac);
77
+ if (appInfo.containerId) {
78
+ hash.update(appInfo.containerId);
79
+ }
77
80
  this.instanceId = hash.copy().digest('hex');
78
81
  this.applicationId = hash.update(appInfo.name).digest('hex');
79
82
  } catch (err) {
80
83
  // If getMac fails we fall back to generating a UUID. "Unstable"
81
84
  // identifiers such as these are prefixed with an underscore.
82
- const id = uuid();
85
+ let id = uuid();
86
+ if (appInfo.containerId) {
87
+ id += `_${appInfo.containerId}`;
88
+ }
83
89
 
84
90
  this.instanceId = `_${id}`;
85
91
  this.applicationId = this.instanceId;
@@ -92,7 +98,7 @@ module.exports = class Telemetry {
92
98
  osArch: appInfo.os.architecture,
93
99
  osPlatform: appInfo.os.platform,
94
100
  osRelease: appInfo.os.release,
95
- isContainer: appInfo.isContainer,
101
+ isContainer: !!appInfo.containerId,
96
102
  agent: AGENT_INFO.NAME,
97
103
  agentVersion: AGENT_INFO.VERSION,
98
104
  isAssess: agent.isInAssessMode(),
@@ -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;
@@ -59,14 +59,12 @@ const init = function init(config) {
59
59
  let count = 0;
60
60
 
61
61
  const interval = setInterval(() => {
62
- if (count >= config.count) {
63
- clearInterval(interval);
64
- return;
65
- }
66
-
67
62
  writeHeapSnapshot(config.path);
68
-
69
63
  count++;
64
+
65
+ if (count === config.count) {
66
+ clearInterval(interval);
67
+ }
70
68
  }, config.window_ms);
71
69
  }, config.delay_ms).unref();
72
70
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/agent",
3
- "version": "4.28.0",
3
+ "version": "4.29.0",
4
4
  "description": "Node.js security instrumentation by Contrast Security",
5
5
  "keywords": [
6
6
  "security",
@@ -121,7 +121,7 @@
121
121
  "@bmacnaughton/string-generator": "^1.0.0",
122
122
  "@contrast/eslint-config": "^3.0.2",
123
123
  "@contrast/fake-module": "file:test/mock/contrast-fake",
124
- "@contrast/screener-service": "^1.12.9",
124
+ "@contrast/screener-service": "^1.12.12",
125
125
  "@hapi/boom": "file:test/mock/boom",
126
126
  "@hapi/hapi": "file:test/mock/hapi",
127
127
  "@ls-lint/ls-lint": "^1.11.2",
@@ -40,21 +40,41 @@ function isContainer() {
40
40
  fs.statSync('/.dockerenv');
41
41
  return true;
42
42
  } catch (err) {
43
- // if no docker env, check /proc/self/cgroup
43
+ // if no docker env, check /proc/self/mountinfo
44
+ }
45
+
46
+ try {
47
+ return fs.readFileSync('proc/self/mountinfo', 'utf8').includes('docker/containers/');
48
+ } catch (err) {
49
+ // else check /proc/self/cgroup
44
50
  }
45
51
 
46
52
  try {
47
53
  return fs.readFileSync('/proc/self/cgroup', 'utf8').includes('docker');
48
54
  } catch (err) {
49
- return false;
55
+ // if there's not such file we can conclude it's not docker env
50
56
  }
57
+
58
+ return false;
51
59
  }
52
60
 
53
61
  function isUsingPM2() {
54
- const externalPkgPath =
55
- process.env['npm_package_json'] || path.join(process.env['PWD'], 'package.json');
56
- const externalPkg = require(externalPkgPath);
57
- return { used: (process.env.pmx || false), version: externalPkg.dependencies.pm2 };
62
+ const used = !!process.env.pmx;
63
+ let version = null;
64
+
65
+ for (const pathVar of ['npm_package_json', 'PWD']) {
66
+ const packagePath = process.env[pathVar];
67
+ if (packagePath) {
68
+ try {
69
+ version = require(path.join(packagePath, 'package.json')).dependencies['pm2'];
70
+ } catch (err) {
71
+ //
72
+ }
73
+ }
74
+ if (version) break;
75
+ }
76
+
77
+ return { used, version };
58
78
  }
59
79
 
60
80
  const diagnostics = {
@@ -130,7 +150,6 @@ const diagnostics = {
130
150
  Node: {
131
151
  Version: process.version
132
152
  },
133
- PM2: isUsingPM2(),
134
153
  OperatingSystem: {
135
154
  Architecture: os.arch(),
136
155
  Name: os.type(),
@@ -143,12 +162,14 @@ const diagnostics = {
143
162
  },
144
163
  Host: {
145
164
  isDocker: isContainer(),
165
+ PM2: isUsingPM2(),
146
166
  Memory: {
147
167
  Total: (os.totalmem() / 1e6).toFixed(0).concat(' MB'),
148
168
  Free: (os.freemem() / 1e6).toFixed(0).concat(' MB'),
149
169
  Used: ((os.totalmem() - os.freemem()) / 1e6).toFixed(0).concat(' MB'),
150
170
  }
151
171
  },
172
+ Application: require(path.join(process.env['PWD'], 'package.json')),
152
173
  };
153
174
 
154
175
  return info;