Sfoglia il codice sorgente

correct transformation of array pattern in variable declarator

sanex3339 8 anni fa
parent
commit
ae924d1a6a

+ 7 - 2
dist/index.js

@@ -6420,9 +6420,14 @@ var VariableDeclarationTransformer = function (_AbstractNodeTransfor) {
             var _this2 = this;
 
             variableDeclarationNode.declarations.forEach(function (declarationNode) {
-                if (Node_1.Node.isIdentifierNode(declarationNode.id)) {
-                    _this2.identifierReplacer.storeNames(declarationNode.id.name, nodeIdentifier);
+                if (Node_1.Node.isObjectPatternNode(declarationNode.id)) {
+                    return estraverse.VisitorOption.Skip;
                 }
+                NodeUtils_1.NodeUtils.typedTraverse(declarationNode.id, NodeType_1.NodeType.Identifier, {
+                    enter: function enter(node) {
+                        return _this2.identifierReplacer.storeNames(node.name, nodeIdentifier);
+                    }
+                });
             });
         }
     }, {

+ 6 - 2
src/node-transformers/obfuscating-transformers/VariableDeclarationTransformer.ts

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

+ 77 - 4
test/dev/dev.ts

@@ -11,10 +11,83 @@ if (!(<any>global)._babelPolyfill) {
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
             (function(){
-                 var foo = 1;
-                 var test = function (foo, abc = foo) {
-                    return foo, abc;
-                 }
+                var result = 1,
+                    term1 = 0,
+                    term2 = 1,
+                    i = 1;
+                while(i < 10)
+                {
+                    var test = 10;
+                    result = term1 + term2;
+                    console.log(result);
+                    term1 = term2;
+                    term2 = result;
+                    i++;
+                }
+        
+                console.log(test);
+                
+                var test = function (test) {
+                    console.log(test);
+                    
+                    if (true) {
+                        var test = 5
+                    }
+                    
+                    return test;
+                }
+                
+                console.log(test(1));
+                
+                function test2 (abc) {
+                    function test1 () {
+                      console.log('inside', abc.item);
+                    }
+                    
+                    console.log('тест', abc);
+                    
+                    var abc = {};
+                    
+                    return abc.item = 15, test1();
+                };
+                
+                var regexptest = /version\\/(\\d+)/i;
+                console.log(regexptest);
+                
+                test2(22);
+                console.log(105.4);
+                console.log(true, false);
+                
+                var sA = 'shorthand1';
+                var sB = 'shorthand2';
+                
+                console.log({sA, sB});
+                
+                try {
+                } catch (error) {
+                    console.log(error);
+                }
+                
+                function foo () {
+                    return function () {
+                        var sum1 = 10 + 20;
+                        var sum2 = 20 + 30;
+                        var sum3 = 30 + 50;
+                        var sub = sum3 - sum2;
+                        
+                        return sum1 + sub;
+                    }
+                }
+                
+                console.log(foo()());
+                
+                if (true) {
+                    console.log(\`1\`);
+                    console.log(\`2\`);
+                    console.log(\`3\`);
+                    console.log(\`4\`);
+                    console.log(\`5\`);
+                }
             })();
         `,
         {

+ 30 - 2
test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/VariableDeclarationTransformer.spec.ts

@@ -200,11 +200,39 @@ describe('VariableDeclarationTransformer', () => {
         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 objectPatternVariableDeclaratorMatch: RegExp = /var *\{ *bar *\} *= *\{ *'\\x62\\x61\\x72' *: *'\\x66\\x6f\\x6f' *\};/;
             const variableUsageMatch: RegExp = /console\['\\x6c\\x6f\\x67'\]\(bar\);/;
 
-            assert.match(obfuscatedCode, objectPatternVariableDeclarator);
+            assert.match(obfuscatedCode, objectPatternVariableDeclaratorMatch);
             assert.match(obfuscatedCode, variableUsageMatch);
         });
     });
+
+    describe('array pattern as variable declarator', () => {
+        const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+            readFileAsString(__dirname + '/fixtures/array-pattern.js'),
+            {
+                ...NO_CUSTOM_NODES_PRESET
+            }
+        );
+        const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
+
+        const objectPatternVariableDeclaratorMatch: RegExp = /var *\[ *(_0x([a-f0-9]){4,6}), *(_0x([a-f0-9]){4,6}) *\] *= *\[0x1, *0x2\];/;
+        const variableUsageMatch: RegExp = /console\['\\x6c\\x6f\\x67'\]\((_0x([a-f0-9]){4,6}), *(_0x([a-f0-9]){4,6})\);/;
+
+        const objectPatternIdentifierName1: string = obfuscatedCode.match(objectPatternVariableDeclaratorMatch)![1];
+        const objectPatternIdentifierName2: string = obfuscatedCode.match(objectPatternVariableDeclaratorMatch)![2];
+        const identifierName1: string = obfuscatedCode.match(variableUsageMatch)![1];
+        const identifierName2: string = obfuscatedCode.match(variableUsageMatch)![2];
+
+        it('should transform array pattern variable declarator', () => {
+            assert.match(obfuscatedCode, objectPatternVariableDeclaratorMatch);
+            assert.match(obfuscatedCode, variableUsageMatch);
+        });
+
+        it('should keep same identifier names same for identifiers in variable declaration and after variable declaration', () => {
+            assert.equal(objectPatternIdentifierName1, identifierName1);
+            assert.equal(objectPatternIdentifierName2, identifierName2);
+        });
+    });
 });

+ 5 - 0
test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/fixtures/array-pattern.js

@@ -0,0 +1,5 @@
+function foo () {
+    var [bar, baz] = [1, 2];
+
+    console.log(bar, baz);
+}

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

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