Browse Source

DeadCodeInjection: testing for `block` and `continue` statements in `block statement`

sanex3339 8 years ago
parent
commit
ea4108269d

+ 27 - 20
test/dev/dev.ts

@@ -7,27 +7,34 @@ import { NO_CUSTOM_NODES_PRESET } from '../../src/options/presets/NoCustomNodes'
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
             (function(){
-                if (true) {
-                    var foo = function () {
-                        console.log('abc');
-                    };
-                    var bar = function () {
-                        console.log('def');
-                    };
-                    var baz = function () {
-                        console.log('ghi');
-                    };
-                    var bark = function () {
-                        console.log('jkl');
-                    };
-                  
+    if (true) {
+        var foo = function () {
+            console.log('abc');
+        };
+        var bar = function () {
+            console.log('def');
+        };
+        var baz = function () {
+            console.log('ghi');
+        };
+        var bark = function () {
+            console.log('jkl');
+        };
 
-                    foo();
-                    bar();
-                    baz();
-                    bark();
-                }
-            })();
+        for (var i = 0; i < 1; i++) {
+            continue;
+        }
+
+        for (var i = 0; i < 1; i++) {
+            break;
+        }
+
+        foo();
+        bar();
+        baz();
+        bark();
+    }
+})();
         `,
         {
             ...NO_CUSTOM_NODES_PRESET,

+ 45 - 5
test/functional-tests/node-transformers/dead-code-injection-transformers/DeadCodeInjectionTransformer.spec.ts

@@ -10,6 +10,7 @@ import { JavaScriptObfuscator } from '../../../../src/JavaScriptObfuscator';
 
 describe('DeadCodeInjectionTransformer', () => {
     const variableMatch: string = '_0x([a-f0-9]){4,6}';
+    const hexMatch: string = '0x[a-f0-9]';
 
     describe('transformNode (programNode: ESTree.Program, parentNode: ESTree.Node): ESTree.Node', () => {
         describe('variant #1 - 5 simple block statements', () => {
@@ -25,10 +26,10 @@ describe('DeadCodeInjectionTransformer', () => {
             );
             const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
             const deadCodeMatch: string = `` +
-                `if *\\(${variableMatch}\\('0x[a-z0-9]'\\) *[=|!]== *${variableMatch}\\('0x[a-z0-9]'\\)\\) *\\{`+
-                    `console\\[${variableMatch}\\('0x[a-z0-9]'\\)\\]\\(${variableMatch}\\('0x[a-z0-9]'\\)\\);` +
+                `if *\\(${variableMatch}\\('${hexMatch}'\\) *[=|!]== *${variableMatch}\\('${hexMatch}'\\)\\) *\\{`+
+                    `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
                 `\\} *else *\\{`+
-                    `console\\[${variableMatch}\\('0x[a-z0-9]'\\)\\]\\(${variableMatch}\\('0x[a-z0-9]'\\)\\);` +
+                    `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
                 `\\}` +
             ``;
             const regexp: RegExp = new RegExp(deadCodeMatch, 'g');
@@ -54,7 +55,7 @@ describe('DeadCodeInjectionTransformer', () => {
             const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
             const codeMatch: string = `` +
                 `var *${variableMatch} *= *function *\\(\\) *\\{` +
-                    `console\\[${variableMatch}\\('0x[a-z0-9]'\\)\\]\\(${variableMatch}\\('0x[a-z0-9]'\\)\\);` +
+                    `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
                 `\\};` +
             ``;
             const regexp: RegExp = new RegExp(codeMatch, 'g');
@@ -80,7 +81,7 @@ describe('DeadCodeInjectionTransformer', () => {
             const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
             const codeMatch: string = `` +
                 `var *${variableMatch} *= *function *\\(\\) *\\{` +
-                    `console\\[${variableMatch}\\('0x[a-z0-9]'\\)\\]\\(${variableMatch}\\('0x[a-z0-9]'\\)\\);` +
+                    `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
                 `\\};` +
             ``;
             const regexp: RegExp = new RegExp(codeMatch, 'g');
@@ -91,5 +92,44 @@ describe('DeadCodeInjectionTransformer', () => {
                 assert.equal(matches.length, 5);
             });
         });
+
+        describe('variant #4 - break or continue statement in block statement', () => {
+            const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                readFileAsString(__dirname + '/fixtures/break-continue-statement.js'),
+                {
+                    ...NO_CUSTOM_NODES_PRESET,
+                    deadCodeInjection: true,
+                    deadCodeInjectionThreshold: 1,
+                    stringArray: true,
+                    stringArrayThreshold: 1
+                }
+            );
+            const obfuscatedCode: string = obfuscationResult.getObfuscatedCode();
+
+            const functionMatch: string = `` +
+                `var *${variableMatch} *= *function *\\(\\) *\\{` +
+                    `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
+                `\\};` +
+            ``;
+            const loopMatch: string = `` +
+                `for *\\(var *${variableMatch} *= *${hexMatch}; *${variableMatch} *< *${hexMatch}; *${variableMatch}\\+\\+\\) *\\{` +
+                    `(?:continue|break);` +
+                `\\}` +
+            ``;
+
+            const functionRegExp: RegExp = new RegExp(functionMatch, 'g');
+            const loopRegExp: RegExp = new RegExp(loopMatch, 'g');
+
+            const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
+            const loopMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(loopRegExp);
+
+            it('shouldn\'t add dead code if block statement contains `continue` or `break` statements', () => {
+                assert.isNotNull(functionMatches);
+                assert.isNotNull(loopMatches);
+
+                assert.equal(functionMatches.length, 4);
+                assert.equal(loopMatches.length, 2);
+            });
+        });
     });
 });

+ 29 - 0
test/functional-tests/node-transformers/dead-code-injection-transformers/fixtures/break-continue-statement.js

@@ -0,0 +1,29 @@
+(function(){
+    if (true) {
+        var foo = function () {
+            console.log('abc');
+        };
+        var bar = function () {
+            console.log('def');
+        };
+        var baz = function () {
+            console.log('ghi');
+        };
+        var bark = function () {
+            console.log('jkl');
+        };
+
+        for (var i = 0; i < 1; i++) {
+            continue;
+        }
+
+        for (var i = 0; i < 1; i++) {
+            break;
+        }
+
+        foo();
+        bar();
+        baz();
+        bark();
+    }
+})();