Преглед на файлове

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

sanex3339 преди 7 години
родител
ревизия
d9612d1bf4

+ 1 - 0
CHANGELOG.md

@@ -4,6 +4,7 @@ v0.12.0
 ---
 * Added ability to disable and enable obfuscation for specific parts of the code by adding conditional comments. 
 * Added obfuscation of `es2015` class names.
+* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/94
 
 v0.11.0
 ---

Файловите разлики са ограничени, защото са твърде много
+ 0 - 0
dist/index.js


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

@@ -4,10 +4,14 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
 import * as estraverse from 'estraverse';
 import * as ESTree from 'estree';
 
+import { TNodeWithBlockStatement } from '../../types/node/TNodeWithBlockStatement';
+
 import { IOptions } from '../../interfaces/options/IOptions';
 import { IRandomGenerator } from '../../interfaces/utils/IRandomGenerator';
 import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
 
+import { NodeType } from '../../enums/node/NodeType';
+
 import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
 import { NodeGuards } from '../../node/NodeGuards';
 import { Nodes } from '../../node/Nodes';
@@ -103,6 +107,13 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
             return blockStatementNode;
         }
 
+        const blockScopeOfBlockStatementNode: TNodeWithBlockStatement = NodeUtils
+            .getBlockScopesOfNode(blockStatementNode)[0];
+
+        if (blockScopeOfBlockStatementNode.type === NodeType.Program) {
+            return blockStatementNode;
+        }
+
         const minInteger: number = 0;
         const maxInteger: number = this.collectedBlockStatements.length - 1;
         const randomIndex: number = this.randomGenerator.getRandomInteger(minInteger, maxInteger);

+ 30 - 0
test/functional-tests/node-transformers/dead-code-injection-transformers/DeadCodeInjectionTransformer.spec.ts

@@ -279,5 +279,35 @@ describe('DeadCodeInjectionTransformer', () => {
                 assert.closeTo(distribution4, expectedDistribution, delta);
             });
         });
+
+        describe('variant #6 - block scope of block statement is `ProgramNode`', () => {
+            const regExp: RegExp = new RegExp(
+                `if *\\(!!\\[\\]\\) *{` +
+                    `console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
+                `\\}`
+            );
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/block-scope-is-program-node.js');
+                const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_CUSTOM_NODES_PRESET,
+                        stringArray: true,
+                        stringArrayThreshold: 1,
+                        deadCodeInjection: true,
+                        deadCodeInjectionThreshold: 1
+                    }
+                );
+
+                obfuscatedCode = obfuscationResult.getObfuscatedCode();
+            });
+
+            it('shouldn\'t add dead code in block statements with `ProgramNode` block scope', () => {
+                assert.match(obfuscatedCode, regExp);
+            });
+        });
     });
 });

+ 27 - 0
test/functional-tests/node-transformers/dead-code-injection-transformers/fixtures/block-scope-is-program-node.js

@@ -0,0 +1,27 @@
+if (true) {
+    console.log('foo');
+}
+
+(function(){
+    var foo = function () {
+        return true;
+    };
+    var bar = function () {
+        return true;
+    };
+    var baz = function () {
+        return true;
+    };
+    var bark = function () {
+        return true;
+    };
+    var hawk = function () {
+        return true;
+    };
+
+    foo();
+    bar();
+    baz();
+    bark();
+    hawk();
+})();

Някои файлове не бяха показани, защото твърде много файлове са промени