@contrast/agentify 1.1.2 → 1.3.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.
@@ -15,20 +15,72 @@
15
15
 
16
16
  'use strict';
17
17
 
18
- module.exports = function(deps) {
18
+ module.exports = function(core) {
19
+ const { logger, patcher } = core;
20
+
21
+ const getOrig = (v) => patcher.unwrap(v);
22
+ const slice = patcher.unwrap(String.prototype.slice);
23
+ const toUntrackedString = (v) => slice.call(` ${v}`, 1);
24
+
19
25
  /**
20
26
  * Components e.g. Assess, Protect, can
21
27
  * 1) configure rewriter to add desired ContrastMethod functions to source code
22
28
  * 2) patch those functions on `contrastMethods.api` in order to add instrumentation
23
29
  */
24
- const contrastMethods = deps.contrastMethods = {
30
+ const contrastMethods = {
25
31
  // TODO: Assess will require add'l methods
26
32
  // TODO: ESM will require add'l methods
27
33
  api: {
28
34
  eval(v) {
29
35
  return v;
30
36
  },
31
- Function: deps.patcher.patch(global.Function, {
37
+ plus(left, right) {
38
+ const ret = getOrig(left) + getOrig(right);
39
+ return ret;
40
+ },
41
+ doubleEqual(left, right) {
42
+ left = getOrig(left);
43
+ right = getOrig(right);
44
+
45
+ return left == right;
46
+ },
47
+ notTripleEqual(left, right) {
48
+ left = getOrig(left);
49
+ right = getOrig(right);
50
+
51
+ return left !== right;
52
+ },
53
+ tripleEqual(left, right) {
54
+ left = getOrig(left);
55
+ right = getOrig(right);
56
+
57
+ return left === right;
58
+ },
59
+ notDoubleEqual(left, right) {
60
+ left = getOrig(left);
61
+ right = getOrig(right);
62
+
63
+ return left != right;
64
+ },
65
+ forceCopy(handle) {
66
+ // Used to force a copy of handle:
67
+ // https://stackoverflow.com/a/31733628
68
+ // This is a workaround for CONTRAST-30333, in which something we
69
+ // suspect is a v8 bug means that handle will otherwise be clobbered
70
+ // and if it was externalized, we will lose the data associated with it
71
+ if (typeof handle === 'string') {
72
+ handle = toUntrackedString(handle);
73
+ }
74
+ return handle;
75
+ },
76
+ cast: function __cast(val) {
77
+ return getOrig(val);
78
+ },
79
+ tag: function __contrastTag(strings) {
80
+ // XXX implemented in lib/assess/propagators/templates.js
81
+ },
82
+
83
+ Function: core.patcher.patch(global.Function, {
32
84
  name: 'global.Function',
33
85
  patchType: 'rewrite-injection'
34
86
  }),
@@ -56,9 +108,9 @@ module.exports = function(deps) {
56
108
  } catch (err) {
57
109
  // We should never expect this since the installation process is well
58
110
  // controlled, but still we should have the defensive code.
59
- deps.logger.error({ err }, 'Unable to install global.ContrastMethods');
111
+ logger.error({ err }, 'Unable to install global.ContrastMethods');
60
112
  }
61
113
  };
62
114
 
63
- return deps.contrastMethods;
115
+ return core.contrastMethods = contrastMethods;
64
116
  };
package/lib/index.js CHANGED
@@ -15,16 +15,21 @@
15
15
 
16
16
  'use strict';
17
17
 
18
+ const fs = require('fs');
19
+ const path = require('path');
18
20
  const Module = require('module');
21
+ const { env } = require('process');
19
22
 
20
23
  const defaultOpts = {
21
24
  install: true,
22
25
  svcList: [
23
26
  'reporter',
24
27
  'contrastMethods',
25
- 'instrumentationLocks',
28
+ 'deadzones',
26
29
  'scopes',
27
30
  'sources',
31
+ 'architectureComponents',
32
+ 'assess',
28
33
  'protect',
29
34
  'depHooks',
30
35
  'rewriteHooks',
@@ -32,14 +37,12 @@ const defaultOpts = {
32
37
  ]
33
38
  };
34
39
 
35
- module.exports = function(deps) {
40
+ module.exports = function(core) {
36
41
  // compose add'l local services
37
- require('./sources')(deps);
38
- require('./contrast-methods')(deps);
39
- require('./instrumentation-locks')(deps);
40
- require('./function-hooks')(deps);
41
- require('./rewrite-hooks')(deps);
42
- // TODO: NODE-2229
42
+ require('./sources')(core);
43
+ require('./contrast-methods')(core);
44
+ require('./function-hooks')(core);
45
+ require('./rewrite-hooks')(core);
43
46
 
44
47
  /**
45
48
  * The interface is a function, which when called, will hook runMain
@@ -57,10 +60,10 @@ module.exports = function(deps) {
57
60
  opts = { ...defaultOpts, ...opts };
58
61
  }
59
62
 
60
- return new Agent(deps, opts);
63
+ return new Agent(core, opts);
61
64
  }
62
65
 
63
- deps.agentify = agentify;
66
+ core.agentify = agentify;
64
67
 
65
68
  return agentify;
66
69
  };
@@ -68,16 +71,16 @@ module.exports = function(deps) {
68
71
  class Agent {
69
72
  /**
70
73
  *
71
- * @param {object} deps dependencies
74
+ * @param {object} core dependencies
72
75
  * @param {object} opts
73
76
  * @param {function} opts.preRunMain custom function for registering services
74
77
  * @param {boolean} opts.install whether to automatically install
75
78
  */
76
- constructor(deps, opts) {
79
+ constructor(core, opts) {
77
80
  const self = this;
78
- const { config, logger } = deps;
81
+ const { config, logger } = core;
79
82
 
80
- this.deps = deps;
83
+ this.core = core;
81
84
  this.opts = opts;
82
85
  this.runMain = Module.runMain;
83
86
 
@@ -90,7 +93,7 @@ class Agent {
90
93
  logger.info('Starting the Contrast agent');
91
94
  logger.debug({ config }, 'Agent configuration');
92
95
 
93
- const plugin = await opts.preRunMain(deps);
96
+ const plugin = await opts.preRunMain(core);
94
97
 
95
98
  if (opts.install) {
96
99
  await self.install();
@@ -102,6 +105,8 @@ class Agent {
102
105
  } catch (err) {
103
106
  await self.handleInstallFailure(err);
104
107
  }
108
+
109
+ self.logEffectiveConfig();
105
110
  return self.runMain.apply(this, args);
106
111
  };
107
112
  }
@@ -114,7 +119,7 @@ class Agent {
114
119
  * @param {error} err Error thrown during install
115
120
  */
116
121
  async handleInstallFailure(err) {
117
- this.deps.logger.error(
122
+ this.core.logger.error(
118
123
  { err },
119
124
  'A fatal agent installation error has occurred. The application will be run without instrumentation.'
120
125
  );
@@ -122,13 +127,34 @@ class Agent {
122
127
 
123
128
  async install() {
124
129
  for (const svcName of this.opts.svcList) {
125
- this.deps.logger.trace('installing service: %s', svcName);
126
- const svc = this.deps[svcName];
130
+ const svc = this.core[svcName];
127
131
  if (svc && svc.install) {
132
+ this.core.logger.trace('installing service: %s', svcName);
128
133
  await svc.install();
129
134
  }
130
135
  }
131
136
  }
137
+
138
+ logEffectiveConfig() {
139
+ const { config, getEffectiveConfig } = this.core;
140
+
141
+ if (env.CONTRAST__AGENT__DIAGNOSTICS__ENABLE !== 'false') {
142
+ const content = JSON.stringify(getEffectiveConfig(), null, 2).concat('\n\n');
143
+
144
+ if (env.CONTRAST__AGENT__DIAGNOSTICS__QUIET !== 'true') {
145
+ fs.writeFileSync(1, content, 'utf8');
146
+ }
147
+
148
+ let outputDir = env.CONTRAST__AGENT__DIAGNOSTICS__REPORT_PATH;
149
+ if (!outputDir && config.agent.logger.path) {
150
+ outputDir = path.join(config.agent.logger.path, '../contrast_effective_config.json');
151
+ } else {
152
+ outputDir = path.join(__dirname, 'contrast_effective_config.json');
153
+ }
154
+
155
+ fs.writeFileSync(outputDir, content, 'utf-8');
156
+ }
157
+ }
132
158
  }
133
159
 
134
160
  module.exports.Agent = Agent;
@@ -28,6 +28,7 @@ module.exports = function(deps) {
28
28
  Module.prototype._compile = function(content, filename) {
29
29
  let compiled;
30
30
  const { code } = deps.rewriter.rewrite(content, { filename, inject: true, wrap: true });
31
+
31
32
  try {
32
33
  compiled = origCompile.call(this, code, filename);
33
34
  } catch (err) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/agentify",
3
- "version": "1.1.2",
3
+ "version": "1.3.0",
4
4
  "description": "Configures Contrast agent services and instrumentation within an application",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
@@ -16,4 +16,4 @@
16
16
  "scripts": {
17
17
  "test": "../scripts/test.sh"
18
18
  }
19
- }
19
+ }
@@ -1,51 +0,0 @@
1
- /*
2
- * Copyright: 2022 Contrast Security, Inc
3
- * Contact: support@contrastsecurity.com
4
- * License: Commercial
5
-
6
- * NOTICE: This Software and the patented inventions embodied within may only be
7
- * used as part of Contrast Security’s commercial offerings. Even though it is
8
- * made available through public repositories, use of this Software is subject to
9
- * the applicable End User Licensing Agreement found at
10
- * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
- * between Contrast Security and the End User. The Software may not be reverse
12
- * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
- * way not consistent with the End User License Agreement.
14
- */
15
-
16
- 'use strict';
17
-
18
- const Module = require('module');
19
-
20
- module.exports = function(deps) {
21
- const {
22
- patcher: { patch },
23
- scopes: { instrumentation }
24
- } = deps;
25
-
26
- deps.instrumentationLocks = {
27
- install() {
28
- const name = 'Module.prototype.require';
29
- const store = {
30
- name: `${name}`,
31
- lock: true,
32
- };
33
-
34
- // `require` has sinks and propagators that run
35
- patch(Module.prototype, 'require', {
36
- name,
37
- patchType: 'instrumentation-lock',
38
- around(next, data) {
39
- if (!instrumentation.isLocked()) {
40
- return instrumentation.run(store, next);
41
- }
42
- return next();
43
- }
44
- });
45
-
46
- // likely more to come e.g. v4 deadzones
47
- }
48
- };
49
-
50
- return deps.instrumentationLocks;
51
- };