瀏覽代碼

More correct fix of https://github.com/javascript-obfuscator/javascript-obfuscator/issues/297

sanex3339 6 年之前
父節點
當前提交
0296b13616

File diff suppressed because it is too large
+ 0 - 0
dist/index.browser.js


File diff suppressed because it is too large
+ 0 - 0
dist/index.cli.js


File diff suppressed because it is too large
+ 0 - 0
dist/index.js


+ 15 - 6
src/node-transformers/obfuscating-transformers/FunctionTransformer.ts

@@ -129,26 +129,35 @@ export class FunctionTransformer extends AbstractNodeTransformer {
     /**
      * @param {Function} functionNode
      * @param {TNodeWithBlockScope} blockScopeNode
+     * @param {Set<string>} ignoredIdentifierNamesSet
      */
-    private replaceFunctionParams (functionNode: ESTree.Function, blockScopeNode: TNodeWithBlockScope): void {
-        const ignoredIdentifiersSet: Set <string> = new Set();
-
+    private replaceFunctionParams (
+        functionNode: ESTree.Function,
+        blockScopeNode: TNodeWithBlockScope,
+        ignoredIdentifierNamesSet: Set <string> = new Set()
+    ): void {
         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
+                 */
                 if (NodeGuards.isFunctionNode(node)) {
-                    this.replaceFunctionParams(node, blockScopeNode);
+                    this.replaceFunctionParams(node, blockScopeNode, new Set(ignoredIdentifierNamesSet));
 
                     return estraverse.VisitorOption.Skip;
                 }
 
+                /**
+                 * Should to ignore all identifiers that related to shorthand properties
+                 */
                 if (FunctionTransformer.isProhibitedPropertyNode(node)) {
-                    ignoredIdentifiersSet.add(node.key.name);
+                    ignoredIdentifierNamesSet.add(node.key.name);
                 }
 
                 if (
                     parentNode
                     && NodeGuards.isReplaceableIdentifierNode(node, parentNode)
-                    && !ignoredIdentifiersSet.has(node.name)
+                    && !ignoredIdentifierNamesSet.has(node.name)
                 ) {
                     const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
                         .replace(node.name, blockScopeNode);

+ 9 - 3
test/dev/dev.ts

@@ -6,9 +6,15 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
 
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
-        function foo (data) {
-            return ({data}) => data + 1;
-        }	
+        function foo (data, options) {
+            function bar ({data, ...rest}) {
+                function baz ({options}) {
+                    return data + options + rest;
+                }
+            }
+    
+            return data;
+        }
         `,
         {
             ...NO_ADDITIONAL_NODES_PRESET,

+ 10 - 5
test/functional-tests/node-transformers/obfuscating-transformers/function-transformer/FunctionTransformer.spec.ts

@@ -114,8 +114,9 @@ describe('FunctionTransformer', () => {
         });
 
         describe('Variant #3: correct transformation when parent scope identifier conflicts with current scope object pattern identifier', () => {
-            const functionObjectPatternParameterRegExp: RegExp = /function _0x[a-f0-9]{4,6} *\({data, *\.\.\._0x[a-f0-9]{4,6}}\) *{/;
-            const returnRegExp1: RegExp = /return data *\+ *_0x[a-f0-9]{4,6};/;
+            const functionObjectPatternParameterRegExp1: RegExp = /function _0x[a-f0-9]{4,6} *\({data, *\.\.\._0x[a-f0-9]{4,6}}\) *{/;
+            const functionObjectPatternParameterRegExp2: RegExp = /function _0x[a-f0-9]{4,6} *\({options}\) *{/;
+            const returnRegExp1: RegExp = /return data *\+ *options *\+ *_0x[a-f0-9]{4,6};/;
             const returnRegExp2: RegExp = /return _0x[a-f0-9]{4,6};/;
 
             let obfuscatedCode: string;
@@ -133,14 +134,18 @@ describe('FunctionTransformer', () => {
             });
 
             it('match #1: should transform function parameter object pattern rest identifier', () => {
-                assert.match(obfuscatedCode, functionObjectPatternParameterRegExp);
+                assert.match(obfuscatedCode, functionObjectPatternParameterRegExp1);
             });
 
-            it('match #2: should transform identifier in `ReturnStatement` of inner function', () => {
+            it('match #2: should transform function parameter object pattern rest identifier', () => {
+                assert.match(obfuscatedCode, functionObjectPatternParameterRegExp2);
+            });
+
+            it('match #3: should transform identifier in `ReturnStatement` of inner function', () => {
                 assert.match(obfuscatedCode, returnRegExp1);
             });
 
-            it('match #2: should transform identifier in `ReturnStatement` of outer function', () => {
+            it('match #4: should transform identifier in `ReturnStatement` of outer function', () => {
                 assert.match(obfuscatedCode, returnRegExp2);
             });
         });

+ 4 - 2
test/functional-tests/node-transformers/obfuscating-transformers/function-transformer/fixtures/object-pattern-as-parameter-3.js

@@ -1,7 +1,9 @@
 (function(){
-    function foo (data) {
+    function foo (data, options) {
         function bar ({data, ...rest}) {
-            return data + rest;
+            function baz ({options}) {
+                return data + options + rest;
+            }
         }
 
         return data;

Some files were not shown because too many files changed in this diff