Browse Source

wip runtime error fix

sanex3339 8 years ago
parent
commit
3809f5445a

+ 8 - 8
dist/index.js

@@ -1256,7 +1256,7 @@ var Obfuscator = function () {
             var _this2 = this;
 
             estraverse.replace(node, {
-                leave: function leave(node, parentNode) {
+                enter: function enter(node, parentNode) {
                     _this2.initializeNodeObfuscators(node, parentNode);
                 }
             });
@@ -2536,7 +2536,7 @@ var CatchClauseObfuscator = function (_AbstractNodeObfuscat) {
             var _this2 = this;
 
             NodeUtils_1.NodeUtils.typedReplace(catchClauseNode.param, NodeType_1.NodeType.Identifier, {
-                leave: function leave(node) {
+                enter: function enter(node) {
                     return _this2.identifierReplacer.storeNames(node.name);
                 }
             });
@@ -2547,7 +2547,7 @@ var CatchClauseObfuscator = function (_AbstractNodeObfuscat) {
             var _this3 = this;
 
             estraverse.replace(catchClauseNode, {
-                leave: function leave(node, parentNode) {
+                enter: function enter(node, parentNode) {
                     if (Nodes_1.Nodes.isReplaceableIdentifierNode(node, parentNode)) {
                         node.name = _this3.identifierReplacer.replace(node.name);
                     }
@@ -2610,7 +2610,7 @@ var FunctionDeclarationObfuscator = function (_AbstractNodeObfuscat) {
             var _this2 = this;
 
             NodeUtils_1.NodeUtils.typedReplace(functionDeclarationNode.id, NodeType_1.NodeType.Identifier, {
-                leave: function leave(node) {
+                enter: function enter(node) {
                     return _this2.identifierReplacer.storeNames(node.name);
                 }
             });
@@ -2683,7 +2683,7 @@ var FunctionObfuscator = function (_AbstractNodeObfuscat) {
 
             functionNode.params.forEach(function (paramsNode) {
                 NodeUtils_1.NodeUtils.typedReplace(paramsNode, NodeType_1.NodeType.Identifier, {
-                    leave: function leave(node) {
+                    enter: function enter(node) {
                         return _this2.identifierReplacer.storeNames(node.name);
                     }
                 });
@@ -2819,7 +2819,7 @@ var MemberExpressionObfuscator = function (_AbstractNodeObfuscat) {
             var _this2 = this;
 
             estraverse.replace(memberExpressionNode.property, {
-                leave: function leave(node, parentNode) {
+                enter: function enter(node, parentNode) {
                     if (Nodes_1.Nodes.isLiteralNode(node)) {
                         _this2.obfuscateLiteralProperty(node);
                         return;
@@ -2911,7 +2911,7 @@ var MethodDefinitionObfuscator = function (_AbstractNodeObfuscat) {
             var _this2 = this;
 
             estraverse.replace(methodDefinitionNode.key, {
-                leave: function leave(node) {
+                enter: function enter(node) {
                     if (Nodes_1.Nodes.isIdentifierNode(node) && !Utils_1.Utils.arrayContains(_this2.ignoredNames, node.name) && methodDefinitionNode.computed === false) {
                         methodDefinitionNode.computed = true;
                         node.name = new StringLiteralReplacer_1.StringLiteralReplacer(_this2.nodes, _this2.options).replace(node.name);
@@ -3061,7 +3061,7 @@ var VariableDeclarationObfuscator = function (_AbstractNodeObfuscat) {
 
             variableDeclarationNode.declarations.forEach(function (declarationNode) {
                 NodeUtils_1.NodeUtils.typedReplace(declarationNode.id, NodeType_1.NodeType.Identifier, {
-                    leave: function leave(node) {
+                    enter: function enter(node) {
                         return _this2.identifierReplacer.storeNames(node.name);
                     }
                 });

+ 1 - 1
src/Obfuscator.ts

@@ -133,7 +133,7 @@ export class Obfuscator implements IObfuscator {
      */
     private obfuscate (node: ESTree.Node): void {
         estraverse.replace(node, {
-            leave: (node: ESTree.Node, parentNode: ESTree.Node): any => {
+            enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
                 this.initializeNodeObfuscators(node, parentNode);
             }
         });

+ 2 - 2
src/node-obfuscators/CatchClauseObfuscator.ts

@@ -48,7 +48,7 @@ export class CatchClauseObfuscator extends AbstractNodeObfuscator {
      */
     private storeCatchClauseParam (catchClauseNode: ESTree.CatchClause): void {
         NodeUtils.typedReplace(catchClauseNode.param, NodeType.Identifier, {
-            leave: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
+            enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
         });
     }
 
@@ -57,7 +57,7 @@ export class CatchClauseObfuscator extends AbstractNodeObfuscator {
      */
     private replaceCatchClauseParam (catchClauseNode: ESTree.CatchClause): void {
         estraverse.replace(catchClauseNode, {
-            leave: (node: ESTree.Node, parentNode: ESTree.Node): any => {
+            enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
                 if (Nodes.isReplaceableIdentifierNode(node, parentNode)) {
                     node.name = this.identifierReplacer.replace(node.name);
                 }

+ 1 - 1
src/node-obfuscators/FunctionDeclarationObfuscator.ts

@@ -54,7 +54,7 @@ export class FunctionDeclarationObfuscator extends AbstractNodeObfuscator {
      */
     private storeFunctionName (functionDeclarationNode: ESTree.FunctionDeclaration): void {
         NodeUtils.typedReplace(functionDeclarationNode.id, NodeType.Identifier, {
-            leave: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
+            enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
         });
     }
 

+ 1 - 1
src/node-obfuscators/FunctionObfuscator.ts

@@ -50,7 +50,7 @@ export class FunctionObfuscator extends AbstractNodeObfuscator {
         functionNode.params
             .forEach((paramsNode: ESTree.Node) => {
                 NodeUtils.typedReplace(paramsNode, NodeType.Identifier, {
-                    leave: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
+                    enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
                 });
             });
     }

+ 1 - 1
src/node-obfuscators/MemberExpressionObfuscator.ts

@@ -14,7 +14,7 @@ export class MemberExpressionObfuscator extends AbstractNodeObfuscator {
      */
     public obfuscateNode (memberExpressionNode: ESTree.MemberExpression): void {
         estraverse.replace(memberExpressionNode.property, {
-            leave: (node: ESTree.Node, parentNode: ESTree.Node): any => {
+            enter: (node: ESTree.Node, parentNode: ESTree.Node): any => {
                 if (Nodes.isLiteralNode(node)) {
                     this.obfuscateLiteralProperty(node);
 

+ 1 - 1
src/node-obfuscators/MethodDefinitionObfuscator.ts

@@ -32,7 +32,7 @@ export class MethodDefinitionObfuscator extends AbstractNodeObfuscator {
      */
     private replaceMethodName (methodDefinitionNode: ESTree.MethodDefinition): void {
         estraverse.replace(methodDefinitionNode.key, {
-            leave: (node: ESTree.Node): any => {
+            enter: (node: ESTree.Node): any => {
                 if (
                     Nodes.isIdentifierNode(node) &&
                     !Utils.arrayContains(this.ignoredNames, node.name) &&

+ 1 - 1
src/node-obfuscators/VariableDeclarationObfuscator.ts

@@ -57,7 +57,7 @@ export class VariableDeclarationObfuscator extends AbstractNodeObfuscator {
         variableDeclarationNode.declarations
             .forEach((declarationNode: ESTree.VariableDeclarator) => {
                 NodeUtils.typedReplace(declarationNode.id, NodeType.Identifier, {
-                    leave: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
+                    enter: (node: ESTree.Identifier) => this.identifierReplacer.storeNames(node.name)
                 });
             });
     }

+ 27 - 5
test/functional-tests/node-obfuscators/FunctionObfuscator.spec.ts

@@ -7,14 +7,36 @@ import { JavaScriptObfuscator } from "../../../src/JavaScriptObfuscator";
 const assert: Chai.AssertStatic = require('chai').assert;
 
 describe('FunctionObfuscator', () => {
-    describe('obfuscation of identifiers of FunctionDeclaration and FunctionExpression node body', () => {
-        it('should replace identifier name with obfuscated one', () => {
-            let obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
-                `var test = 'test';`,
+    describe('identifiers obfuscation inside `FunctionDeclaration` and `FunctionExpression` node body', () => {
+        it('should correct obfuscate both function parameter identifier and function body identifier with same name', () => {
+            const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                `
+                    (function () {
+                        var test = function (test) {
+                            console.log(test);
+                            
+                            if (true) {
+                                var test = 5
+                            }
+                            
+                            return test;
+                        }
+                    })();
+                `,
                 Object.assign({}, NO_CUSTOM_NODES_PRESET)
             );
 
-            assert.match(obfuscationResult.getObfuscatedCode(),  /^var *test *= *'\\x74\\x65\\x73\\x74';$/);
+            const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
+
+            const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
+                .match(/var _0x[a-z0-9]{5,6} *= *function *\((_0x[a-z0-9]{5,6})\) *\{/);
+            const functionBodyIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
+                .match(/console\['\\x6c\\x6f\\x67'\]\((_0x[a-z0-9]{5,6})\)/);
+
+            const functionParamIdentifierName: string = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
+            const functionBodyIdentifierName: string = (<RegExpMatchArray>functionBodyIdentifierMatch)[1];
+
+            assert.equal(functionParamIdentifierName, functionBodyIdentifierName);
         });
     });
 });

+ 3 - 3
test/functional-tests/node-obfuscators/VariableDeclarationObfuscator.spec.ts

@@ -114,7 +114,7 @@ describe('VariableDeclarationObfuscator', () => {
             );
         });
 
-        it('blablabla', () => {
+        it('should correct obfuscate variables inside function body', () => {
             const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
             const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
                 .match(/function *_0x[a-z0-9]{5,6} *\((_0x[a-z0-9]{5,6})\,(_0x[a-z0-9]{5,6})\) *\{/);
@@ -136,8 +136,8 @@ describe('VariableDeclarationObfuscator', () => {
             assert.notEqual(functionParamIdentifierName, constructorIdentifierName);
             assert.notEqual(functionParamIdentifierName, innerFunctionParamIdentifierName);
 
-            assert.notEqual(functionParamIdentifierName, objectIdentifierName);
-            assert.notEqual(functionParamIdentifierName, variableDeclarationIdentifierName);
+            assert.equal(functionParamIdentifierName, objectIdentifierName);
+            assert.equal(functionParamIdentifierName, variableDeclarationIdentifierName);
 
             assert.equal(innerFunctionParamIdentifierName, constructorIdentifierName);
             assert.equal(variableDeclarationIdentifierName, objectIdentifierName);

+ 1 - 0
test/index.spec.ts

@@ -29,6 +29,7 @@ import './functional-tests/custom-nodes/unicode-array-nodes/UnicodeArrayCallsWra
 import './functional-tests/custom-nodes/unicode-array-nodes/UnicodeArrayDecodeNode.spec';
 import './functional-tests/custom-nodes/unicode-array-nodes/UnicodeArrayRotateFunctionNode.spec';
 import './functional-tests/custom-nodes/unicode-array-nodes/UnicodeArrayNode.spec';
+import './functional-tests/node-obfuscators/FunctionObfuscator.spec';
 import './functional-tests/node-obfuscators/LiteralObfuscator.spec';
 import './functional-tests/node-obfuscators/MemberExpressionObfuscator.spec';
 import './functional-tests/node-obfuscators/MethodDefinitionObfuscator.spec';