Преглед изворни кода

Fixed conditional comments in some rare cases

sanex3339 пре 5 година
родитељ
комит
6d3c2ae467

+ 5 - 0
CHANGELOG.md

@@ -1,5 +1,10 @@
 Change Log
 
+v0.21.1
+---
+* Fixed conditional comments in some rare cases
+
+
 v0.21.0
 ---
 * Improved `transformObjectKeys` transformation to cover more cases

Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
dist/index.browser.js


Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
dist/index.cli.js


Разлика између датотеке није приказан због своје велике величине
+ 0 - 0
dist/index.js


+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "javascript-obfuscator",
-  "version": "0.21.0",
+  "version": "0.21.1",
   "description": "JavaScript obfuscator",
   "keywords": [
     "obfuscator",

+ 34 - 20
src/node-transformers/preparing-transformers/CommentsTransformer.ts

@@ -60,35 +60,49 @@ export class CommentsTransformer extends AbstractNodeTransformer {
      * `@license`, `@preserve` or `javascript-obfuscator` words
      * Move comments to their nodes
      *
-     * @param {Node} programNode
+     * @param {Node} rootNode
      * @returns {NodeGuards}
      */
-    public transformNode (programNode: ESTree.Program): ESTree.Node {
-        if (programNode.comments) {
-            const comments: ESTree.Comment[] = this.transformComments(programNode.comments);
-            estraverse.traverse(programNode, {
-                enter: (node: ESTree.Node): void => {
-                    if (comments.length === 0) {
-                        return;
-                    }
+    public transformNode (rootNode: ESTree.Program): ESTree.Node {
+        if (!rootNode.comments || !rootNode.comments.length) {
+            return rootNode;
+        }
 
-                    const commentIdx: number = comments.findIndex((comment: ESTree.Comment) =>
-                        comment.range && node.range && comment.range[0] < node.range[0]
-                    );
+        const comments: ESTree.Comment[] = this.transformComments(rootNode.comments);
 
-                    if (commentIdx === -1) {
-                        return;
-                    }
+        if (comments.length === 0) {
+            return rootNode;
+        }
+
+        if (!rootNode.body.length) {
+            rootNode.leadingComments = comments;
+
+            return rootNode;
+        }
+
+        estraverse.traverse(rootNode, {
+            enter: (node: ESTree.Node): void => {
+                if (node === rootNode) {
+                    return;
+                }
 
-                    node.leadingComments = comments.splice(commentIdx, comments.length - commentIdx).reverse();
+                const commentIdx: number = comments.findIndex((comment: ESTree.Comment) =>
+                    comment.range && node.range && comment.range[0] < node.range[0]
+                );
+
+                if (commentIdx === -1) {
+                    return;
                 }
-            });
-            if (comments.length > 0) {
-                programNode.trailingComments = comments.reverse();
+
+                node.leadingComments = comments.splice(commentIdx, comments.length - commentIdx).reverse();
             }
+        });
+
+        if (comments.length > 0) {
+            rootNode.trailingComments = comments.reverse();
         }
 
-        return programNode;
+        return rootNode;
     }
 
     /**

+ 32 - 5
test/functional-tests/node-transformers/preparing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/ConditionalCommentObfuscatingGuard.spec.ts

@@ -39,7 +39,7 @@ describe('ConditionalCommentObfuscatingGuard', () => {
             });
         });
 
-        describe('Variant #2: `disable` and `enable` conditional comments', () => {
+        describe('Variant #2: `disable` and `enable` conditional comments #1', () => {
             const obfuscatedVariableDeclaration1RegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *0x1;/;
             const obfuscatedVariableDeclaration2RegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *0x3;/;
             const ignoredVariableDeclarationRegExp: RegExp = /var *bar *= *2;/;
@@ -47,7 +47,7 @@ describe('ConditionalCommentObfuscatingGuard', () => {
             let obfuscatedCode: string;
 
             beforeEach(() => {
-                const code: string = readFileAsString(__dirname + '/fixtures/disable-and-enable-comments.js');
+                const code: string = readFileAsString(__dirname + '/fixtures/disable-and-enable-comments-1.js');
 
                 obfuscatedCode = JavaScriptObfuscator.obfuscate(
                     code,
@@ -70,7 +70,34 @@ describe('ConditionalCommentObfuscatingGuard', () => {
             });
         });
 
-        describe('Variant #3: `disable` conditional comment from beginning of the code', () => {
+        describe('Variant #3: `disable` and `enable` conditional comments #2', () => {
+            const ignoredVariableDeclarationRegExp: RegExp = /var *foo *= *1;/;
+            const obfuscatedVariableDeclarationRegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *0x2;/;
+
+            let obfuscatedCode: string;
+
+            beforeEach(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/disable-and-enable-comments-2.js');
+
+                obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        renameGlobals: true
+                    }
+                ).getObfuscatedCode();
+            });
+
+            it('match #1: should ignore variable declaration after `disable` conditional comment', () => {
+                assert.match(obfuscatedCode, ignoredVariableDeclarationRegExp);
+            });
+
+            it('match #2: should obfuscate variable declaration before `disable` conditional comment', () => {
+                assert.match(obfuscatedCode, obfuscatedVariableDeclarationRegExp);
+            });
+        });
+
+        describe('Variant #4: `disable` conditional comment from beginning of the code', () => {
             const ignoredVariableDeclaration1RegExp: RegExp = /var *foo *= *1;/;
             const ignoredVariableDeclaration2RegExp: RegExp = /var *bar *= *2;/;
 
@@ -96,7 +123,7 @@ describe('ConditionalCommentObfuscatingGuard', () => {
             });
         });
 
-        describe('Variant #4: `disable` and `enable` conditional comments with dead code injection', () => {
+        describe('Variant #5: `disable` and `enable` conditional comments with dead code injection', () => {
             const obfuscatedFunctionExpressionRegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *function *\(_0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}, *_0x([a-f0-9]){4,6}\) *{/g;
             const expectedObfuscatedFunctionExpressionLength: number = 3;
 
@@ -166,7 +193,7 @@ describe('ConditionalCommentObfuscatingGuard', () => {
             });
         });
 
-        describe('Variant #5: `disable` and `enable` conditional comments with control flow flattening', () => {
+        describe('Variant #6: `disable` and `enable` conditional comments with control flow flattening', () => {
             const obfuscatedVariableDeclarationRegExp: RegExp = /var *_0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\['[a-zA-Z0-9]{1,5}'];/;
             const ignoredVariableDeclarationRegExp: RegExp = /var *bar *= *'bar';/;
 

+ 0 - 0
test/functional-tests/node-transformers/preparing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/fixtures/disable-and-enable-comments.js → test/functional-tests/node-transformers/preparing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/fixtures/disable-and-enable-comments-1.js


+ 5 - 0
test/functional-tests/node-transformers/preparing-transformers/obfuscating-guards/conditional-comment-obfuscating-guard/fixtures/disable-and-enable-comments-2.js

@@ -0,0 +1,5 @@
+// javascript-obfuscator:disable
+var foo = 1;
+
+// javascript-obfuscator:enable
+var bar = 2;

Неке датотеке нису приказане због велике количине промена