ソースを参照

fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/29

sanex3339 8 年 前
コミット
5c1215801a

+ 9 - 14
dist/index.js

@@ -90,7 +90,7 @@ module.exports =
 /******/ 	__webpack_require__.p = "";
 /******/
 /******/ 	// Load entry module and return exports
-/******/ 	return __webpack_require__(__webpack_require__.s = 150);
+/******/ 	return __webpack_require__(__webpack_require__.s = 151);
 /******/ })
 /************************************************************************/
 /******/ ([
@@ -5747,10 +5747,8 @@ var inversify_1 = __webpack_require__(2);
 var ServiceIdentifiers_1 = __webpack_require__(4);
 var estraverse = __webpack_require__(18);
 var NodeObfuscationReplacers_1 = __webpack_require__(20);
-var NodeType_1 = __webpack_require__(15);
 var AbstractNodeTransformer_1 = __webpack_require__(17);
 var Node_1 = __webpack_require__(12);
-var NodeUtils_1 = __webpack_require__(8);
 var FunctionTransformer = function (_AbstractNodeTransfor) {
     (0, _inherits3.default)(FunctionTransformer, _AbstractNodeTransfor);
 
@@ -5777,11 +5775,9 @@ var FunctionTransformer = function (_AbstractNodeTransfor) {
             var _this2 = this;
 
             functionNode.params.forEach(function (paramsNode) {
-                NodeUtils_1.NodeUtils.typedTraverse(paramsNode, NodeType_1.NodeType.Identifier, {
-                    enter: function enter(node) {
-                        return _this2.identifierReplacer.storeNames(node.name, nodeIdentifier);
-                    }
-                });
+                if (Node_1.Node.isIdentifierNode(paramsNode)) {
+                    _this2.identifierReplacer.storeNames(paramsNode.name, nodeIdentifier);
+                }
             });
         }
     }, {
@@ -6426,11 +6422,9 @@ var VariableDeclarationTransformer = function (_AbstractNodeTransfor) {
             var _this2 = this;
 
             variableDeclarationNode.declarations.forEach(function (declarationNode) {
-                NodeUtils_1.NodeUtils.typedTraverse(declarationNode.id, NodeType_1.NodeType.Identifier, {
-                    enter: function enter(node) {
-                        return _this2.identifierReplacer.storeNames(node.name, nodeIdentifier);
-                    }
-                });
+                if (Node_1.Node.isIdentifierNode(declarationNode.id)) {
+                    _this2.identifierReplacer.storeNames(declarationNode.id.name, nodeIdentifier);
+                }
             });
         }
     }, {
@@ -8152,7 +8146,8 @@ module.exports = require("mkdirp");
 module.exports = require("reflect-metadata");
 
 /***/ },
-/* 150 */
+/* 150 */,
+/* 151 */
 /***/ function(module, exports, __webpack_require__) {
 
 "use strict";

+ 3 - 5
src/node-transformers/obfuscation-transformers/FunctionTransformer.ts

@@ -9,11 +9,9 @@ import { IObfuscationReplacer } from '../../interfaces/node-transformers/IObfusc
 import { IObfuscationReplacerWithStorage } from '../../interfaces/node-transformers/IObfuscationReplacerWithStorage';
 
 import { NodeObfuscatorsReplacers } from '../../enums/container/NodeObfuscationReplacers';
-import { NodeType } from '../../enums/NodeType';
 
 import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
 import { Node } from '../../node/Node';
-import { NodeUtils } from '../../node/NodeUtils';
 
 /**
  * replaces:
@@ -63,9 +61,9 @@ export class FunctionTransformer extends AbstractNodeTransformer {
     private storeFunctionParams (functionNode: ESTree.Function, nodeIdentifier: number): void {
         functionNode.params
             .forEach((paramsNode: ESTree.Node) => {
-                NodeUtils.typedTraverse(paramsNode, NodeType.Identifier, {
-                    enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name, nodeIdentifier)
-                });
+                if (Node.isIdentifierNode(paramsNode)) {
+                    this.identifierReplacer.storeNames(paramsNode.name, nodeIdentifier);
+                }
             });
     }
 

+ 3 - 3
src/node-transformers/obfuscation-transformers/VariableDeclarationTransformer.ts

@@ -83,9 +83,9 @@ export class VariableDeclarationTransformer extends AbstractNodeTransformer {
     private storeVariableNames (variableDeclarationNode: ESTree.VariableDeclaration, nodeIdentifier: number): void {
         variableDeclarationNode.declarations
             .forEach((declarationNode: ESTree.VariableDeclarator) => {
-                NodeUtils.typedTraverse(declarationNode.id, NodeType.Identifier, {
-                    enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name, nodeIdentifier)
-                });
+                if (Node.isIdentifierNode(declarationNode.id)) {
+                    this.identifierReplacer.storeNames(declarationNode.id.name, nodeIdentifier);
+                }
             });
     }
 

+ 18 - 0
test/functional-tests/node-transformers/obfuscation-transformers/function-transformer/FunctionTransformer.spec.ts

@@ -34,4 +34,22 @@ describe('FunctionTransformer', () => {
             assert.equal(/variable *= *0x6;/.test(obfuscatedCode), true);
         });
     });
+
+    describe('object pattern as argument', () => {
+        const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+            readFileAsString(__dirname + '/fixtures/object-pattern-as-argument.js'),
+            {
+                ...NO_CUSTOM_NODES_PRESET
+            }
+        );
+        const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
+
+        it('shouldn\'t transform function parameter object pattern identifier', () => {
+            const functionParameterMatch: RegExp = /function *\(\{ *bar *\}\) *\{/;
+            const functionBodyMatch: RegExp = /return *bar;/;
+
+            assert.match(obfuscatedCode, functionParameterMatch);
+            assert.match(obfuscatedCode, functionBodyMatch);
+        });
+    });
 });

+ 5 - 0
test/functional-tests/node-transformers/obfuscation-transformers/function-transformer/fixtures/object-pattern-as-argument.js

@@ -0,0 +1,5 @@
+(function () {
+    var test = function ({ bar }) {
+        return bar;
+    }
+})();

+ 18 - 0
test/functional-tests/node-transformers/obfuscation-transformers/variable-declaration-transformer/VariableDeclarationTransformer.spec.ts

@@ -146,4 +146,22 @@ describe('VariableDeclarationTransformer', () => {
             assert.match(obfuscationResult.getObfuscatedCode(),  /_0x([a-f0-9]){4,6}\['\\x74\\x65\\x73\\x74'\]/);
         });
     });
+
+    describe('object pattern as argument', () => {
+        const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+            readFileAsString(__dirname + '/fixtures/object-pattern.js'),
+            {
+                ...NO_CUSTOM_NODES_PRESET
+            }
+        );
+        const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
+
+        it('shouldn\'t transform object pattern variable declarator', () => {
+            const objectPatternVariableDeclarator: RegExp = /var *\{ *bar *\} *= *\{ *'\\x62\\x61\\x72' *: *'\\x66\\x6f\\x6f' *\};/;
+            const variableUsageMatch: RegExp = /console\['\\x6c\\x6f\\x67'\]\(bar\);/;
+
+            assert.match(obfuscatedCode, objectPatternVariableDeclarator);
+            assert.match(obfuscatedCode, variableUsageMatch);
+        });
+    });
 });

+ 4 - 0
test/functional-tests/node-transformers/obfuscation-transformers/variable-declaration-transformer/fixtures/object-pattern.js

@@ -0,0 +1,4 @@
+(function () {
+    var { bar } = { bar: 'foo' };
+    console.log(bar);
+})();