js-confuser 2.1.0 → 2.1.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.
@@ -304,6 +304,9 @@ var _default = exports["default"] = function _default(_ref) {
304
304
  newBody.unshift(t.expressionStatement(t.stringLiteral("Dispatcher: ".concat(name, " -> ").concat(newName))));
305
305
  }
306
306
  var functionExpression = t.functionExpression(null, [], t.blockStatement(newBody));
307
+
308
+ // Carry over original source loc info
309
+ t.inherits(functionExpression, originalFn);
307
310
  var _iterator5 = _createForOfIteratorHelper(Object.getOwnPropertySymbols(originalFn)),
308
311
  _step5;
309
312
  try {
@@ -142,6 +142,12 @@ var _default = exports["default"] = function _default(_ref) {
142
142
  if (!setterPropsNeeded.has(identifierName)) {
143
143
  // Only provide 'set' method if the variable is modified
144
144
  var isModification = (0, _astUtils.isModifiedIdentifier)(identifierPath);
145
+
146
+ // Important: Do not generate setters for "const" bindings
147
+ var binding = identifierPath.scope.getBinding(identifierName);
148
+ if ((binding === null || binding === void 0 ? void 0 : binding.kind) === "const") {
149
+ isModification = false; // Prevent 'setter' as while not a syntax error, causes problems for build tools
150
+ }
145
151
  if (isModification) {
146
152
  setterPropsNeeded.add(identifierName);
147
153
  }
@@ -214,14 +220,19 @@ var _default = exports["default"] = function _default(_ref) {
214
220
  } finally {
215
221
  _iterator3.f();
216
222
  }
217
- var flattenedFunctionDeclaration = t.functionDeclaration(t.identifier(newFnName), [t.arrayPattern(_toConsumableArray(fnPath.node.params)), t.identifier(flatObjectName)], t.blockStatement(_toConsumableArray(fnPath.node.body.body).concat()), false, fnPath.node.async);
223
+ var flattenedFunctionDeclaration = t.functionDeclaration(t.identifier(newFnName), [t.identifier(flatObjectName), t.arrayPattern(_toConsumableArray(fnPath.node.params))], t.blockStatement(_toConsumableArray(fnPath.node.body.body).concat()), false, fnPath.node.async);
224
+
225
+ // Carry over original source loc info
226
+ t.inherits(flattenedFunctionDeclaration, fnPath.node);
218
227
 
219
228
  // Create the flat object variable declaration
220
229
  var flatObjectDeclaration = t.variableDeclaration("var", [t.variableDeclarator(t.identifier(flatObjectName), t.objectExpression(flatObjectProperties))]);
221
230
  var argName = me.getPlaceholder() + "_args";
222
231
 
223
232
  // Replace original function body with a call to the flattened function
224
- fnPath.node.body = t.blockStatement([flatObjectDeclaration, t.returnStatement(t.callExpression(t.identifier(newFnName), [t.identifier(argName), t.identifier(flatObjectName)]))]);
233
+ fnPath.node.body = t.blockStatement([flatObjectDeclaration, t.returnStatement(t.callExpression(t.identifier(newFnName), [t.identifier(flatObjectName),
234
+ // Default params require flat object first
235
+ t.identifier(argName)]))]);
225
236
  var originalLength = (0, _functionUtils.computeFunctionLength)(fnPath);
226
237
  fnPath.node.params = [t.restElement(t.identifier(argName))];
227
238
 
@@ -248,7 +248,6 @@ var _default = exports["default"] = function _default(_ref) {
248
248
  // Do not rename exports
249
249
  if ((0, _astUtils.isExportedIdentifier)(binding)) return false;
250
250
  }
251
- if (name === me.obfuscator.getStringCompressionLibraryName()) return false;
252
251
 
253
252
  // Global variables are additionally checked against user option
254
253
  if (isGlobal) {
@@ -57,7 +57,8 @@ var _default = exports["default"] = function _default(_ref) {
57
57
 
58
58
  // Function is redefined, do not apply integrity
59
59
  if (!binding || binding.constantViolations.length > 0) return;
60
- var code = me.obfuscator.generateCode(newFunctionDeclaration);
60
+ var _me$obfuscator$genera = me.obfuscator.generateCode(newFunctionDeclaration),
61
+ code = _me$obfuscator$genera.code;
61
62
  var codeTrimmed = code.replace(me.globalState.lock.integrity.sensitivityRegex, "");
62
63
  var seed = (0, _randomUtils.getRandomInteger)(0, 10000000);
63
64
  var hashCode = (0, _integrityTemplate.HashFunction)(codeTrimmed, seed);
@@ -171,9 +171,10 @@ function pack(_ref) {
171
171
  if (lastStatement && t.isExpressionStatement(lastStatement)) {
172
172
  Object.assign(lastStatement, t.returnStatement(lastStatement.expression));
173
173
  }
174
- var _outputCode = _obfuscator["default"].generateCode(ast, _objectSpread(_objectSpread({}, me.obfuscator.options), {}, {
175
- compact: true
176
- }));
174
+ var _Obfuscator$generateC = _obfuscator["default"].generateCode(ast, _objectSpread(_objectSpread({}, me.obfuscator.options), {}, {
175
+ compact: true
176
+ })),
177
+ _outputCode = _Obfuscator$generateC.code;
177
178
  var newAST = new _template["default"]("\n {prependNodes}\n Function({objectName}, {outputCode})({objectExpression});\n ").file({
178
179
  objectName: function objectName() {
179
180
  return t.stringLiteral(_objectName);
@@ -47,7 +47,16 @@ var _default = exports["default"] = function _default(_ref) {
47
47
  visitor: {
48
48
  "ThisExpression|Super": {
49
49
  exit: function exit(path) {
50
- markFunctionUnsafe(path);
50
+ var _fnPath;
51
+ var fnPath = path.find(function (p) {
52
+ return p.isFunction();
53
+ });
54
+ if ((_fnPath = fnPath) !== null && _fnPath !== void 0 && _fnPath.isArrowFunctionExpression()) fnPath = path.find(function (p) {
55
+ return p.isFunction() && !p.isArrowFunctionExpression();
56
+ });
57
+ if (fnPath) {
58
+ fnPath.node[_constants.UNSAFE] = true;
59
+ }
51
60
  }
52
61
  },
53
62
  // @js-confuser-var "myVar" -> __JS_CONFUSER_VAR__(myVar)
@@ -149,7 +149,8 @@ var _default = exports["default"] = function _default(_ref) {
149
149
  return (pluginInstance.order == _order.Order.Preparation || !hasRan.has(pluginInstance.order)) && !disallowedTransforms.has(pluginInstance.order);
150
150
  });
151
151
  newObfuscator.obfuscateAST(evalFile);
152
- var generated = _obfuscator["default"].generateCode(evalFile);
152
+ var _Obfuscator$generateC = _obfuscator["default"].generateCode(evalFile),
153
+ generated = _Obfuscator$generateC.code;
153
154
  var functionExpression = t.callExpression(t.identifier(rgfEvalName), [t.stringLiteral(generated)]);
154
155
  var index = rgfArrayExpression.elements.length;
155
156
  rgfArrayExpression.elements.push(functionExpression);
@@ -10,12 +10,7 @@ var _randomUtils = require("../../utils/random-utils");
10
10
  var t = _interopRequireWildcard(require("@babel/types"));
11
11
  function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
12
12
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
13
- var hasAllEncodings = false;
14
13
  function createDefaultStringEncoding(encodingImplementations) {
15
- if (hasAllEncodings) {
16
- return null;
17
- }
18
-
19
14
  // Create base91 encoding
20
15
  var strTable = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~"';
21
16
 
@@ -25,7 +20,6 @@ function createDefaultStringEncoding(encodingImplementations) {
25
20
 
26
21
  // Check if the encoding already exists
27
22
  if (typeof encodingImplementations[identity] !== "undefined") {
28
- hasAllEncodings = true;
29
23
  return null;
30
24
  }
31
25
  var encodingImplementation = {
@@ -36,7 +36,6 @@ var _default = exports["default"] = function _default(_ref) {
36
36
  var blocks = [];
37
37
  var stringMap = new Map();
38
38
  var stringArrayName = me.getPlaceholder() + "_array";
39
- var stringArrayCacheName = me.getPlaceholder() + "_cache";
40
39
  var encodingImplementations = Object.create(null);
41
40
  var availableStringEncodings = me.options.customStringEncodings;
42
41
 
@@ -185,11 +184,6 @@ var _default = exports["default"] = function _default(_ref) {
185
184
  (0, _astUtils.prependProgram)(programPath, t.variableDeclaration("var", [t.variableDeclarator(t.identifier(stringArrayName), t.arrayExpression(Array.from(stringMap.keys()).map(function (x) {
186
185
  return t.stringLiteral(x);
187
186
  })))]));
188
-
189
- // Create the string cache
190
- (0, _astUtils.prependProgram)(programPath, new _template["default"]("\n var {stringArrayCacheName} = {};\n ").single({
191
- stringArrayCacheName: stringArrayCacheName
192
- }));
193
187
  for (var _i = 0, _blocks = blocks; _i < _blocks.length; _i++) {
194
188
  var block = _blocks[_i];
195
189
  var _ref2 = block.node[STRING_CONCEALING],
@@ -205,7 +199,7 @@ var _default = exports["default"] = function _default(_ref) {
205
199
  });
206
200
 
207
201
  // The main function to get the string value
208
- var retrieveFunctionDeclaration = new _template["default"]("\n function ".concat(fnName, "(index) {\n if (typeof ").concat(stringArrayCacheName, "[index] === 'undefined') {\n return ").concat(stringArrayCacheName, "[index] = ").concat(decodeFnName, "(").concat(stringArrayName, "[index]);\n }\n return ").concat(stringArrayCacheName, "[index];\n }\n ")).addSymbols(_constants.NO_REMOVE).single();
202
+ var retrieveFunctionDeclaration = new _template["default"]("\n function ".concat(fnName, "(index) {\n return ").concat(decodeFnName, "(").concat(stringArrayName, "[index]);\n }\n ")).addSymbols(_constants.NO_REMOVE).single();
209
203
  (0, _astUtils.prepend)(block, [].concat(_toConsumableArray(decoder), [retrieveFunctionDeclaration]));
210
204
  }
211
205
  }
@@ -482,9 +482,11 @@ function isModifiedIdentifier(identifierPath) {
482
482
  if (identifierPath.parentPath.isUpdateExpression()) {
483
483
  isModification = true;
484
484
  }
485
+
486
+ // Important: Member expressions (object.identifier = true) are NOT considered modified. Here, "identifier" is not a variable here.
485
487
  if (identifierPath.find(function (p) {
486
488
  var _p$parentPath4;
487
- return p.key === "left" && ((_p$parentPath4 = p.parentPath) === null || _p$parentPath4 === void 0 ? void 0 : _p$parentPath4.isAssignmentExpression());
489
+ return p.key === "left" && ((_p$parentPath4 = p.parentPath) === null || _p$parentPath4 === void 0 ? void 0 : _p$parentPath4.isAssignmentExpression()) && !p.isMemberExpression();
488
490
  })) {
489
491
  isModification = true;
490
492
  }
@@ -42,9 +42,16 @@ function chance(percentChance) {
42
42
  * @param array
43
43
  */
44
44
  function shuffle(array) {
45
- array.sort(function () {
46
- return Math.random() - 0.5;
47
- });
45
+ // Fisher-Yates shuffle
46
+ var currentIndex = array.length,
47
+ randomIndex;
48
+ while (currentIndex !== 0) {
49
+ randomIndex = Math.floor(Math.random() * currentIndex);
50
+ currentIndex--;
51
+ var _ref = [array[randomIndex], array[currentIndex]];
52
+ array[currentIndex] = _ref[0];
53
+ array[randomIndex] = _ref[1];
54
+ }
48
55
  return array;
49
56
  }
50
57
 
@@ -9,7 +9,7 @@ var _assert = require("assert");
9
9
  var _presets = _interopRequireDefault(require("./presets"));
10
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
11
11
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
12
- var validProperties = new Set(["preset", "target", "compact", "hexadecimalNumbers", "minify", "renameVariables", "renameGlobals", "renameLabels", "identifierGenerator", "controlFlowFlattening", "globalConcealing", "stringCompression", "stringConcealing", "stringEncoding", "stringSplitting", "duplicateLiteralsRemoval", "dispatcher", "rgf", "objectExtraction", "flatten", "deadCode", "calculator", "lock", "movedDeclarations", "opaquePredicates", "shuffle", "variableMasking", "verbose", "globalVariables", "debugComments", "preserveFunctionLength", "astScrambler", "customStringEncodings", "functionOutlining", "pack"]);
12
+ var validProperties = new Set(["preset", "target", "compact", "hexadecimalNumbers", "minify", "renameVariables", "renameGlobals", "renameLabels", "identifierGenerator", "controlFlowFlattening", "globalConcealing", "stringConcealing", "stringEncoding", "stringSplitting", "duplicateLiteralsRemoval", "dispatcher", "rgf", "objectExtraction", "flatten", "deadCode", "calculator", "lock", "movedDeclarations", "opaquePredicates", "variableMasking", "verbose", "globalVariables", "debugComments", "preserveFunctionLength", "astScrambler", "customStringEncodings", "pack", "sourceMap", "sourceFileName"]);
13
13
  var validLockProperties = new Set(["selfDefending", "antiDebug", "tamperProtection", "startDate", "endDate", "domainLock", "integrity", "countermeasures", "customLocks"]);
14
14
  function validateOptions(options) {
15
15
  if (!options || Object.keys(options).length <= 1) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-confuser",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "JavaScript Obfuscation Tool.",
5
5
  "main": "dist/index.js",
6
6
  "types": "index.d.ts",