Forráskód Böngészése

Merge pull request #311 from javascript-obfuscator/function-obfuscation-bugs

Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/…
Timofey Kachalov 6 éve
szülő
commit
6eeb1cbfa5

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@ Change Log
 v0.18.0
 ---
 * **New option:** `reservedStrings` disables transformation of string literals, which being matched by passed RegExp patterns
+* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/309
 * Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/307
 
 v0.17.3

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 0 - 0
dist/index.browser.js


A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 0 - 0
dist/index.cli.js


A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 0 - 0
dist/index.js


+ 9 - 4
src/node-transformers/obfuscating-transformers/FunctionTransformer.ts

@@ -160,7 +160,14 @@ export class FunctionTransformer extends AbstractNodeTransformer {
         const replaceVisitor: estraverse.Visitor = {
             enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void | estraverse.VisitorOption => {
                 /**
-                 * Should to process nested functions in different traverse loop to avoid wrong code generation
+                 * Should skip function node itself
+                 */
+                if (node === functionNode) {
+                    return;
+                }
+
+                /**
+                 * Should process nested functions in different traverse loop to avoid wrong code generation
                  */
                 if (NodeGuards.isFunctionNode(node)) {
                     this.replaceFunctionParams(node, lexicalScopeNode, new Set(ignoredIdentifierNamesSet));
@@ -194,8 +201,6 @@ export class FunctionTransformer extends AbstractNodeTransformer {
             }
         };
 
-        functionNode.params.forEach((paramsNode: ESTree.Node) => estraverse.replace(paramsNode, replaceVisitor));
-
-        estraverse.replace(functionNode.body, replaceVisitor);
+        estraverse.replace(functionNode, replaceVisitor)
     }
 }

+ 5 - 5
test/dev/dev.ts

@@ -6,12 +6,12 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
 
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
-        (function(){
-            function foo ({id}) {
-                function bar ({id: baz}) {
-                    return id !== baz;
-                }
+        (function(foo){
+            function foo () {
+            
             }
+  
+            return new foo();
         })();
         `,
         {

+ 60 - 3
test/functional-tests/node-transformers/obfuscating-transformers/function-transformer/FunctionTransformer.spec.ts

@@ -45,6 +45,63 @@ describe('FunctionTransformer', () => {
         });
     });
 
+    describe('function id name obfuscation', () => {
+        const functionExpressionParamIdentifierRegExp: RegExp = /\(function *\((_0x[a-f0-9]{4,6})\) *\{/;
+        const functionParamIdentifierRegExp: RegExp = /function *(_0x[a-f0-9]{4,6}) *\(\) *\{/;
+        const functionObjectIdentifierRegExp: RegExp = /return new (_0x[a-f0-9]{4,6}) *\(\);/;
+
+        let obfuscatedCode: string,
+            functionExpressionParamIdentifierName: string,
+            functionParamIdentifierName: string,
+            functionObjectIdentifierName: string;
+
+        before(() => {
+            const code: string = readFileAsString(__dirname + '/fixtures/function-id-name.js');
+
+            obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                code,
+                {
+                    ...NO_ADDITIONAL_NODES_PRESET
+                }
+            ).getObfuscatedCode();
+
+            const functionExpressionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
+                .match(functionExpressionParamIdentifierRegExp);
+            const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
+                .match(functionParamIdentifierRegExp);
+            const functionObjectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
+                .match(functionObjectIdentifierRegExp);
+
+            functionParamIdentifierName = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
+            functionExpressionParamIdentifierName = (<RegExpMatchArray>functionExpressionParamIdentifierMatch)[1];
+            functionObjectIdentifierName = (<RegExpMatchArray>functionObjectIdentifierMatch)[1];
+        });
+
+        it('should correctly transform function expression parameter identifier', () => {
+            assert.match(obfuscatedCode, functionExpressionParamIdentifierRegExp);
+        });
+
+        it('should correctly transform function parameter identifier', () => {
+            assert.match(obfuscatedCode, functionParamIdentifierRegExp);
+        });
+
+        it('should correctly transform function object parameter identifier', () => {
+            assert.match(obfuscatedCode, functionObjectIdentifierRegExp);
+        });
+
+        it('should generate same names for function parameter and function object identifiers', () => {
+            assert.equal(functionParamIdentifierName, functionObjectIdentifierName);
+        });
+
+        it('should generate same names for function parameter identifiers', () => {
+            assert.equal(functionExpressionParamIdentifierName, functionParamIdentifierName);
+        });
+
+        it('should generate same names for function expression parameter and function object identifiers', () => {
+            assert.equal(functionExpressionParamIdentifierName, functionObjectIdentifierName);
+        });
+    });
+
     describe('object pattern as parameter', () => {
         describe('Variant #1: simple', () => {
             const functionParameterRegExp: RegExp = /function *\(\{ *bar *\}\) *\{/;
@@ -299,15 +356,15 @@ describe('FunctionTransformer', () => {
                 assert.match(obfuscatedCode, functionBodyRegExp);
             });
 
-            it('equal #1: shouldn\'t keep same names variable declaration identifier and function parameters identifiers', () => {
+            it('equal #1: shouldn\'t keep same names for variable declaration identifier and function parameters identifiers', () => {
                 assert.notEqual(variableDeclarationIdentifierName, functionParameterIdentifierName);
             });
 
-            it('equal #2: shouldn\'t keep same names variable declaration identifier and function parameters identifiers', () => {
+            it('equal #2: shouldn\'t keep same names for variable declaration identifier and function parameters identifiers', () => {
                 assert.notEqual(variableDeclarationIdentifierName, functionDefaultParameterIdentifierName1);
             });
 
-            it('equal #3: shouldn\'t keep same names variable declaration identifier and function parameters identifiers', () => {
+            it('equal #3: shouldn\'t keep same names for variable declaration identifier and function parameters identifiers', () => {
                 assert.notEqual(variableDeclarationIdentifierName, functionDefaultParameterIdentifierName2);
             });
 

+ 7 - 0
test/functional-tests/node-transformers/obfuscating-transformers/function-transformer/fixtures/function-id-name.js

@@ -0,0 +1,7 @@
+(function(foo){
+    function foo () {
+
+    }
+
+    return new foo();
+})();

Nem az összes módosított fájl került megjelenítésre, mert túl sok fájl változott