@contrast/rewriter 1.2.0 → 1.3.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.
Files changed (2) hide show
  1. package/lib/index.js +71 -13
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -22,13 +22,13 @@ const { default: generate } = require('@babel/generator');
22
22
  const t = require('@babel/types');
23
23
  const { expression, statement } = require('@babel/template');
24
24
 
25
+
25
26
  /**
26
27
  * factory
27
28
  */
28
29
  module.exports = function(core) {
29
30
  const rewriter = new Rewriter(core);
30
- core.rewriter = rewriter;
31
- return rewriter;
31
+ return core.rewriter = rewriter;
32
32
  };
33
33
 
34
34
 
@@ -54,6 +54,10 @@ class Rewriter {
54
54
  const self = this;
55
55
  this.logger = deps.logger;
56
56
  this.visitors = [];
57
+ this.installedModes = [];
58
+ this.tokens = [];
59
+ this.injections = [];
60
+ this.methodLookups = {};
57
61
  this.rewriteTransforms = {
58
62
  enter(...args) {
59
63
  for (const v of self.visitors) {
@@ -62,7 +66,6 @@ class Rewriter {
62
66
  },
63
67
  Program: function Program(path, state) {
64
68
  if (state.wrap) {
65
-
66
69
  let [prefix, suffix] = Module.wrapper;
67
70
  prefix = prefix.trim();
68
71
  suffix = suffix.trim().replace(/;$/, '.apply(this, arguments);');
@@ -75,23 +78,29 @@ class Rewriter {
75
78
  }
76
79
 
77
80
  if (state.inject) {
78
- path.unshiftContainer('body', [
79
- statement(
80
- 'const ContrastMethods = global.ContrastMethods || (() => { throw new SyntaxError(\'ContrastMethods undefined during compilation\'); })();'
81
- )(),
82
- statement(
83
- 'var Function = global.ContrastMethods.Function || Function;'
84
- )(),
85
- ]);
81
+ path.unshiftContainer('body', self.injections);
86
82
  }
87
83
  },
88
- CallExpression(path, state) {
84
+ CallExpression(path) {
89
85
  if (path.node.callee.name === 'eval') {
90
86
  path.node.arguments = [
91
87
  t.callExpression(expression('global.ContrastMethods.eval')(), path.node.arguments)
92
88
  ];
93
89
  }
94
90
  },
91
+ BinaryExpression: function BinaryExpression(path) {
92
+ const method = self.methodLookups[path.node.operator];
93
+ if (method) {
94
+ path.replaceWith(
95
+ t.callExpression(
96
+ expression('ContrastMethods.%%method%%')({ method }), [
97
+ path.node.left,
98
+ path.node.right
99
+ ]
100
+ )
101
+ );
102
+ }
103
+ }
95
104
  };
96
105
  this.unwriteTransforms = {
97
106
  CallExpression(path) {
@@ -101,6 +110,55 @@ class Rewriter {
101
110
  }
102
111
  }
103
112
  };
113
+ this.install = function(mode) {
114
+ self.installedModes.push(mode);
115
+ !self.methodLookups.eval && (self.methodLookups = {
116
+ eval: 'eval'
117
+ });
118
+
119
+ !(self.injections.length === 2) && self.injections.push(
120
+ statement(
121
+ 'const %%id%% = global.%%id%% || (() => { throw new SyntaxError(%%errMessage%%); })();'
122
+ )({
123
+ id: 'ContrastMethods',
124
+ errMessage: t.stringLiteral(
125
+ 'ContrastMethods undefined during compilation'
126
+ )
127
+ }),
128
+ statement(
129
+ 'var %%name%% = global.ContrastMethods.%%id%% || %%name%%;'
130
+ )({ name: 'Function', id: 'Function' }),
131
+ );
132
+
133
+ if (self.installedModes.includes('protect')) {
134
+ // Protect doesn't have anything specific
135
+ // for rewriting that should not be rewritten
136
+ // in Assess (at least for now)
137
+ }
138
+ if (self.installedModes.includes('assess')) {
139
+ Object.assign(self.methodLookups, {
140
+ '+': 'plus',
141
+ '===': 'tripleEqual',
142
+ '!==': 'notTripleEqual',
143
+ '==': 'doubleEqual',
144
+ '!=': 'notDoubleEqual'
145
+ });
146
+ Object.assign(self.rewriteTransforms, {
147
+ AssignmentExpression(path) {
148
+ if (path.node.operator !== '+=') return;
149
+ path.replaceWith(
150
+ t.assignmentExpression(
151
+ '=',
152
+ path.node.left,
153
+ t.callExpression(expression('global.ContrastMethods.plus')(), [path.node.left, path.node.right])
154
+ )
155
+ );
156
+ }
157
+ });
158
+ }
159
+
160
+ self.tokens = Object.keys(self.methodLookups);
161
+ };
104
162
  }
105
163
 
106
164
  /**
@@ -127,7 +185,7 @@ class Rewriter {
127
185
  ...opts
128
186
  };
129
187
 
130
- if (state.orig.indexOf('eval') === -1 && state.orig.indexOf('Function') === -1) {
188
+ if (this.tokens.every((token) => state.orig.indexOf(token) === -1)) {
131
189
  return { code: state.orig };
132
190
  }
133
191
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/rewriter",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "A transpilation tool mainly used for instrumentation",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",