@dacely/toildefender 0.1.6 → 0.1.8

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.
@@ -72,11 +72,29 @@ function isClassMethodScope(scope) {
72
72
  if (node.type == "MethodDefinition" || node.type == "ClassBody") {
73
73
  return true;
74
74
  }
75
- node = node.veilmark$parent;
75
+ node = node.toildefender$parent;
76
76
  }
77
77
  return false;
78
78
  }
79
79
 
80
+ function isNumericVmInternalNode(node) {
81
+ while (node) {
82
+ if (node.toildefender$numericVmInternal === true) {
83
+ return true;
84
+ }
85
+ node = node.toildefender$parent;
86
+ }
87
+ return false;
88
+ }
89
+
90
+ function isNumericVmInternalScope(scope) {
91
+ return isNumericVmInternalNode(scope && scope.block);
92
+ }
93
+
94
+ function isNumericVmInternalVariable(variable) {
95
+ return variable.defs.some(def => isNumericVmInternalNode(def.node));
96
+ }
97
+
80
98
  module.exports = class Variables {
81
99
 
82
100
  constructor (logger) {
@@ -93,6 +111,9 @@ module.exports = class Variables {
93
111
  */
94
112
  removeFunctionExpressionIds (ast) {
95
113
  return traverser.traverse(ast, [], (node, stack) => {
114
+ if (isNumericVmInternalNode(node)) {
115
+ return node;
116
+ }
96
117
  if (node.type == "FunctionExpression" && node.id && !functionExpressionUsesOwnName(node)) {
97
118
  node.id = null;
98
119
  }
@@ -114,7 +135,7 @@ module.exports = class Variables {
114
135
  this.esutils.setParentsRecursive(ast);
115
136
 
116
137
  scopeManager.scopes.forEach(scope => {
117
- if (!this.esutils.canInsertIntoScope(scope) || isClassMethodScope(scope)) {
138
+ if (!this.esutils.canInsertIntoScope(scope) || isClassMethodScope(scope) || isNumericVmInternalScope(scope)) {
118
139
  return;
119
140
  }
120
141
  scope.variables.forEach(variable => {
@@ -127,7 +148,7 @@ module.exports = class Variables {
127
148
  * as a FunctionExpression with an id, which are then
128
149
  * mistakingly replaced with EmptyStatements.
129
150
  */
130
- if (estest.isStatement(def.node)) {
151
+ if (estest.isStatement(def.node) && !isNumericVmInternalNode(def.node)) {
131
152
  this.esutils.replaceNode(ast, def.node, { type: "EmptyStatement" });
132
153
  this.esutils.insertIntoScope(scope, {
133
154
  type: "VariableDeclaration",
@@ -142,7 +163,8 @@ module.exports = class Variables {
142
163
  body: def.node.body,
143
164
  generator: def.node.generator === true,
144
165
  expression: def.node.expression === true,
145
- async: def.node.async === true
166
+ async: def.node.async === true,
167
+ toildefender$numericVmInternal: def.node.toildefender$numericVmInternal === true
146
168
  }
147
169
  }
148
170
  ]
@@ -163,8 +185,22 @@ module.exports = class Variables {
163
185
  * @param {ScopeManager} scopeManager Scope manager
164
186
  */
165
187
  obfuscateIdentifiers (ast, scopeManager) {
188
+ var usedNames = new Set();
189
+
190
+ function uniqueName(variable) {
191
+ var base = "$$var$" + utils.hash(variable);
192
+ var name = base + "$" + variable.name;
193
+ var counter = 0;
194
+ while (usedNames.has(name)) {
195
+ counter += 1;
196
+ name = base + counter.toString(36) + "$" + variable.name;
197
+ }
198
+ usedNames.add(name);
199
+ return name;
200
+ }
201
+
166
202
  scopeManager.scopes.forEach(scope => {
167
- if (isClassMethodScope(scope)) {
203
+ if (isClassMethodScope(scope) || isNumericVmInternalScope(scope)) {
168
204
  return;
169
205
  }
170
206
  if (scope.isStatic()) {
@@ -179,7 +215,11 @@ module.exports = class Variables {
179
215
  });
180
216
 
181
217
  for (let variable of scope.variables) {
182
- var name = "$$var$" + utils.hash(variable) + "$" + variable.name;
218
+ if (isNumericVmInternalVariable(variable)) {
219
+ continue;
220
+ }
221
+
222
+ var name = uniqueName(variable);
183
223
 
184
224
  if (variable.defs.some(def => def.type == "ClassName")) {
185
225
  continue;
@@ -199,7 +239,7 @@ module.exports = class Variables {
199
239
  def.name = name;
200
240
  }
201
241
 
202
- for (let ref of variable.references) {
242
+ for (let ref of variable.references.filter(ref => ref.resolved === variable)) {
203
243
  // change reference's name
204
244
  ref.identifier.name = name;
205
245
  }
@@ -236,10 +276,13 @@ module.exports = class Variables {
236
276
  var rng = new utils.UniqueRandomAlpha(3);
237
277
 
238
278
  scopeManager.scopes.forEach(scope => {
239
- if (!this.esutils.canInsertIntoScope(scope) || isClassMethodScope(scope)) {
279
+ if (!this.esutils.canInsertIntoScope(scope) || isClassMethodScope(scope) || isNumericVmInternalScope(scope)) {
240
280
  return;
241
281
  }
242
282
  scope.variables.forEach(variable => {
283
+ if (isNumericVmInternalVariable(variable)) {
284
+ return;
285
+ }
243
286
  variable.defs.forEach(def => {
244
287
  if (def.type == "Parameter") {
245
288
  assert(def.name.type == "Identifier");