@contrast/agent 4.27.0 → 4.27.2

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.
@@ -16,88 +16,80 @@ Copyright: 2022 Contrast Security, Inc
16
16
  'use strict';
17
17
 
18
18
  const { exec } = require('child_process');
19
+ const path = require('path');
20
+
21
+ const HELP_MESSAGE = `
22
+ Description:
23
+ The read-contrast-config utility returns the current effective node agent configuration.
24
+
25
+ Usage:
26
+ npx -p @contrast/agent read-contrast-config <path/to/entry/script> [options]
27
+ npx -p @contrast/agent read-contrast-config --help
28
+
29
+ Options:
30
+ --quiet -q Prevents output of the report to the console.
31
+ --output -o The directory to write the report in. Defaults to the current directory.
32
+ `;
33
+
34
+ const diagnostics = {
35
+ parseArgv(argv) {
36
+ const args = {
37
+ help: false,
38
+ output: 'contrast_effective_config.json',
39
+ quiet: false,
40
+ };
41
+
42
+ for (let i = 0; i < argv.length; i++) {
43
+ const arg = argv[i];
44
+ switch (arg) {
45
+ case '--help':
46
+ args.help = true;
47
+ return args;
48
+ case '-o':
49
+ case '--output': {
50
+ // grab the next value and skip in the next loop
51
+ const output = argv[++i];
52
+ if (!output) {
53
+ throw new Error('Expected a value to be provided after --output');
54
+ }
55
+ args.output = output;
56
+ break;
57
+ }
58
+ case '-q':
59
+ case '--quiet':
60
+ args.quiet = true;
61
+ break;
62
+ default:
63
+ if (!arg.startsWith('-')) {
64
+ args.entry = arg;
65
+ }
66
+ break;
67
+ }
68
+ }
19
69
 
20
- function handleDependentParameter(args, currParameter, nextParameter) {
21
- switch (currParameter) {
22
- case '--output':
23
- case '-o':
24
- args.output = nextParameter;
25
- break;
26
- default:
27
- throw new Error(`Invalid parameter ${currParameter}`);
28
- }
29
- }
70
+ if (!args.entry) {
71
+ throw new Error('No entry point provided');
72
+ }
30
73
 
31
- function handleIndependentParameter(args, parameter) {
32
- switch (parameter) {
33
- case '--help':
34
- args.help = true;
35
- break;
36
- case '--quiet':
37
- case '-q':
38
- args.quiet = true;
39
- break;
40
- default:
41
- throw new Error(`Invalid parameter ${parameter}`);
42
- }
43
- }
74
+ return args;
75
+ },
44
76
 
45
- function printHelpMessage() {
46
- console.log(
47
- '\nDescription:\n' +
48
- 'The config-diagnostics utility returns the current effective node agent configuration\n' +
49
- '\nUsage:\n' +
50
- 'npx -p @contrast/agent config-diagnostics <path/to/entry/script> [options]\n' +
51
- 'npx -p @contrast/agent config-diagnostics --help\n' +
52
- '\nOptions:\n' +
53
- '--quiet -q Prevents output of the report to the console.\n' +
54
- '--output -o The directory to write the report in. Defaults to the current directory.\n'
55
- );
56
- }
77
+ executeNodeAgent(args) {
78
+ let agentEnvArgs =
79
+ 'CONTRAST__SHOW__BANNER=false CONTRAST__AGENT__DIAGNOSTICS__ENABLE=true';
57
80
 
58
- function parseArgv() {
59
- const argv = process.argv.slice(2);
60
- const args = {
61
- output: 'contrast_effective_config.json',
62
- quiet: false,
63
- help: false,
64
- };
65
- let currParameter = null;
66
-
67
- for (let i = 0; i < argv.length; i++) {
68
- if (!currParameter) {
69
- currParameter = argv[i];
70
- }
71
- if (!(currParameter.startsWith('--') || currParameter.startsWith('-'))) {
72
- currParameter = null;
73
- continue;
81
+ if (!args.quiet) {
82
+ agentEnvArgs = `${agentEnvArgs} CONTRAST__AGENT__DIAGNOSTICS__QUIET=false`;
74
83
  }
75
84
 
76
- if (i + 1 == argv.length || argv[i + 1].startsWith('--') || argv[i + 1].startsWith('-')) {
77
- handleIndependentParameter(args, currParameter);
78
- currParameter = null;
79
- } else {
80
- handleDependentParameter(args, currParameter, argv[i + 1]);
81
- currParameter = null;
85
+ if (args.output) {
86
+ agentEnvArgs = `${agentEnvArgs} CONTRAST__AGENT__DIAGNOSTICS__REPORT_PATH=${args.output}`;
82
87
  }
83
- }
84
88
 
85
- return args;
86
- }
89
+ const bootstrap = path.join(__dirname, 'bootstrap.js');
90
+ const command = `${agentEnvArgs} node -r ${bootstrap} ${args.entry}`;
87
91
 
88
- function executeNodeAgent(args) {
89
- let agentEnvArgs = 'CONTRAST__SHOW__BANNER=false CONTRAST__AGENT__DIAGNOSTICS__ENABLE=true';
90
- const entry = process.argv.slice(2).shift();
91
-
92
- if (!args.quiet) {
93
- agentEnvArgs = agentEnvArgs.concat(' CONTRAST__AGENT__DIAGNOSTICS__QUIET=false');
94
- }
95
- if (args.output) {
96
- agentEnvArgs = agentEnvArgs.concat(` CONTRAST__AGENT__DIAGNOSTICS__REPORT_PATH=${args.output}`);
97
- }
98
-
99
- exec(`${agentEnvArgs} node -r ./node_modules/@contrast/agent/bootstrap.js ${entry}`,
100
- (error, stdout, stderr) => {
92
+ exec(command, (error, stdout, stderr) => {
101
93
  if (error && !args.quiet) {
102
94
  console.log(`error: ${error.message}`);
103
95
  return;
@@ -112,19 +104,23 @@ function executeNodeAgent(args) {
112
104
  if (!args.quiet) {
113
105
  console.log(stdout);
114
106
  }
115
- }
116
- );
117
- }
107
+ });
108
+ },
118
109
 
119
- function fetchAgentConfig(args) {
120
- if (process.argv.slice(2).length == 1 && args.help) {
121
- printHelpMessage();
122
- } else {
123
- executeNodeAgent(args);
124
- }
125
- }
110
+ fetchAgentConfig(args) {
111
+ if (args.help) {
112
+ console.log(HELP_MESSAGE);
113
+ } else {
114
+ this.executeNodeAgent(args);
115
+ }
116
+ },
117
+ };
126
118
 
119
+ module.exports = diagnostics;
127
120
 
128
- fetchAgentConfig(parseArgv());
129
- // exporting this stuff for testing purposes only
130
- module.exports = { printHelpMessage, parseArgv, executeNodeAgent, fetchAgentConfig };
121
+ // only run if this file is being executed as an entry script
122
+ // istanbul ignore next
123
+ if (require.main === module) {
124
+ const args = diagnostics.parseArgv(process.argv.slice(2));
125
+ diagnostics.fetchAgentConfig(args);
126
+ }
@@ -93,9 +93,11 @@ Object.defineProperty(Function.prototype, '__isContrastHooked', {
93
93
  }
94
94
  });
95
95
 
96
- Function.prototype._contrast_toString = function() {
97
- return Reflect.apply(functionToString, this, arguments);
98
- };
96
+ Object.defineProperty(Function.prototype, '_contrast_toString', {
97
+ value: function _contrast_toString() {
98
+ return Reflect.apply(functionToString, this, arguments);
99
+ }
100
+ });
99
101
 
100
102
  function runHooks(type, data, thisTarget, fnHooks) {
101
103
  fnHooks.forEach((hook, key) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/agent",
3
- "version": "4.27.0",
3
+ "version": "4.27.2",
4
4
  "description": "Node.js security instrumentation by Contrast Security",
5
5
  "keywords": [
6
6
  "security",