Browse Source

Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/195

sanex3339 7 năm trước cách đây
mục cha
commit
71f5d43b79

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/index.js


+ 35 - 11
src/node-transformers/dead-code-injection-transformers/DeadCodeInjectionTransformer.ts

@@ -179,6 +179,37 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
         return isValidBlockStatementNode;
     }
 
+    /**
+     * @param {BlockStatement} blockStatementNode
+     * @returns {boolean}
+     */
+    private static isValidWrappedBlockStatementNode (blockStatementNode: ESTree.BlockStatement): boolean {
+        if (!blockStatementNode.body.length) {
+            return false;
+        }
+
+        let isValidBlockStatementNode: boolean = true;
+
+        estraverse.traverse(blockStatementNode, {
+            enter: (node: ESTree.Node): estraverse.VisitorOption | void => {
+                if (DeadCodeInjectionTransformer.isScopeHoistingFunctionDeclaration(node)) {
+                    isValidBlockStatementNode = false;
+
+                    return estraverse.VisitorOption.Break;
+                }
+            }
+        });
+
+        if (!isValidBlockStatementNode) {
+            return false;
+        }
+
+        const blockScopeOfBlockStatementNode: TNodeWithBlockScope = NodeUtils
+            .getBlockScopesOfNode(blockStatementNode)[0];
+
+        return blockScopeOfBlockStatementNode.type !== NodeType.Program;
+    }
+
     /**
      * @param {TransformationStage} transformationStage
      * @returns {IVisitor | null}
@@ -269,17 +300,10 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
             return estraverse.VisitorOption.Break;
         }
 
-        const isInvalidBlockStatementNode: boolean = !blockStatementNode.body.length
-            || this.randomGenerator.getMathRandom() > this.options.deadCodeInjectionThreshold;
-
-        if (isInvalidBlockStatementNode) {
-            return blockStatementNode;
-        }
-
-        const blockScopeOfBlockStatementNode: TNodeWithBlockScope = NodeUtils
-            .getBlockScopesOfNode(blockStatementNode)[0];
-
-        if (blockScopeOfBlockStatementNode.type === NodeType.Program) {
+        if (
+            this.randomGenerator.getMathRandom() > this.options.deadCodeInjectionThreshold
+            || !DeadCodeInjectionTransformer.isValidWrappedBlockStatementNode(blockStatementNode)
+        ) {
             return blockStatementNode;
         }
 

+ 11 - 9
test/dev/dev.ts

@@ -8,24 +8,26 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
         `
         (function(){
             function foo () {
-                function inner1 () {}
+                var a = 1;
                 inner1();
+                var b = 2;
+                function inner1 () {}
+                var c = 3;
             }
             function bar () {
-                function inner2 () {}
-                inner2();
+                var a = 1;
             }
             function baz () {
-                function inner3 () {}
-                inner3();
+                var a = 1;
             }
             function bark () {
-                function inner4 () {}
-                inner4();
+                var a = 1;
             }
             function hawk () {
-                function inner5 () {}
-                inner5();
+                var a = 1;
+            }
+            function eagle () {
+                var a = 1;
             }
         })();
         `,

+ 63 - 25
test/functional-tests/node-transformers/dead-code-injection-transformers/DeadCodeInjectionTransformer.spec.ts

@@ -519,38 +519,76 @@ describe('DeadCodeInjectionTransformer', () => {
         });
 
         describe('variant #12 - block statement with scope-hoisting', () => {
-            const regExp: RegExp = new RegExp(
-                `${variableMatch} *\\(\\); *` +
-                `var *${variableMatch} *= *0x2; *` +
-                `function *${variableMatch} *\\(\\) *{ *} *`,
-                'g'
-            );
-            const expectedMatchesLength: number = 5;
+            describe('variant #1: collecting of block statements', () => {
+                const regExp: RegExp = new RegExp(
+                    `${variableMatch} *\\(\\); *` +
+                    `var *${variableMatch} *= *0x2; *` +
+                    `function *${variableMatch} *\\(\\) *{ *} *`,
+                    'g'
+                );
+                const expectedMatchesLength: number = 5;
 
-            let matchesLength: number = 0;
+                let matchesLength: number = 0;
 
-            before(() => {
-                const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting.js');
-                const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
-                    code,
-                    {
-                        ...NO_ADDITIONAL_NODES_PRESET,
-                        stringArray: true,
-                        stringArrayThreshold: 1,
-                        deadCodeInjection: true,
-                        deadCodeInjectionThreshold: 1
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-1.js');
+                    const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            stringArray: true,
+                            stringArrayThreshold: 1,
+                            deadCodeInjection: true,
+                            deadCodeInjectionThreshold: 1
+                        }
+                    );
+
+                    const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
+                    const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
+
+                    if (functionMatches) {
+                        matchesLength = functionMatches.length;
                     }
+                });
+
+                it('shouldn\'t collect block statements with scope-hoisting', () => {
+                    assert.equal(matchesLength, expectedMatchesLength);
+                });
+            });
+
+            describe('variant #2: wrapping of block statements in dead code conditions', () => {
+                const regExp: RegExp = new RegExp(
+                    `function *${variableMatch} *\\(\\) *{ *` +
+                        `var *${variableMatch} *= *0x1; *` +
+                        `${variableMatch} *\\(\\); *` +
+                        `var *${variableMatch} *= *0x2; *` +
+                        `function *${variableMatch} *\\(\\) *{ *} *` +
+                        `var *${variableMatch} *= *0x3; *` +
+                    `}`,
+                    'g'
                 );
 
-                const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
-                const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
+                let obfuscatedCode: string;
 
-                if (functionMatches) {
-                    matchesLength = functionMatches.length;
-                }            });
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/block-statement-with-scope-hoisting-2.js');
+                    const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            stringArray: true,
+                            stringArrayThreshold: 1,
+                            deadCodeInjection: true,
+                            deadCodeInjectionThreshold: 1
+                        }
+                    );
 
-            it('shouldn\'t collect block statements with scope-hoisting', () => {
-                assert.equal(matchesLength, expectedMatchesLength);
+                    obfuscatedCode = obfuscationResult.getObfuscatedCode();
+                });
+
+                it('shouldn\'t wrap block statements in dead code conditions', () => {
+                    assert.match(obfuscatedCode, regExp);
+                });
             });
         });
     });

+ 0 - 0
test/functional-tests/node-transformers/dead-code-injection-transformers/fixtures/block-statement-with-scope-hoisting.js → test/functional-tests/node-transformers/dead-code-injection-transformers/fixtures/block-statement-with-scope-hoisting-1.js


+ 24 - 0
test/functional-tests/node-transformers/dead-code-injection-transformers/fixtures/block-statement-with-scope-hoisting-2.js

@@ -0,0 +1,24 @@
+(function(){
+    function foo () {
+        var a = 1;
+        inner1();
+        var b = 2;
+        function inner1 () {}
+        var c = 3;
+    }
+    function bar () {
+        var a = 1;
+    }
+    function baz () {
+        var a = 1;
+    }
+    function bark () {
+        var a = 1;
+    }
+    function hawk () {
+        var a = 1;
+    }
+    function eagle () {
+        var a = 1;
+    }
+})();

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác