ソースを参照

Conditional comments will be removed from the code after obfuscation. https://github.com/javascript-obfuscator/javascript-obfuscator/issues/641

sanex3339 4 年 前
コミット
733fe12d23

+ 3 - 1
.ncurc

@@ -1,3 +1,5 @@
 {
-  "reject": []
+  "reject": [
+    "@types/estree"
+  ]
 }

+ 4 - 0
CHANGELOG.md

@@ -1,5 +1,9 @@
 Change Log
 
+v1.2.0
+---
+* Conditional comments will be removed from the code after obfuscation. https://github.com/javascript-obfuscator/javascript-obfuscator/issues/641
+
 v1.1.1
 ---
 * Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/638

ファイルの差分が大きいため隠しています
+ 0 - 0
dist/index.browser.js


ファイルの差分が大きいため隠しています
+ 0 - 0
dist/index.cli.js


ファイルの差分が大きいため隠しています
+ 0 - 0
dist/index.js


+ 4 - 4
package.json

@@ -1,6 +1,6 @@
 {
   "name": "javascript-obfuscator",
-  "version": "1.1.1",
+  "version": "1.2.0",
   "description": "JavaScript obfuscator",
   "keywords": [
     "obfuscator",
@@ -54,7 +54,7 @@
     "@types/mkdirp": "1.0.1",
     "@types/mocha": "7.0.2",
     "@types/multimatch": "4.0.0",
-    "@types/node": "14.0.13",
+    "@types/node": "14.0.14",
     "@types/rimraf": "3.0.0",
     "@types/sinon": "9.0.4",
     "@types/string-template": "1.0.2",
@@ -65,12 +65,12 @@
     "coveralls": "3.1.0",
     "eslint": "7.3.1",
     "eslint-plugin-import": "2.21.2",
-    "eslint-plugin-jsdoc": "28.0.0",
+    "eslint-plugin-jsdoc": "28.5.1",
     "eslint-plugin-no-null": "1.0.2",
     "eslint-plugin-prefer-arrow": "1.2.1",
     "eslint-plugin-unicorn": "20.1.0",
     "fork-ts-checker-notifier-webpack-plugin": "3.0.0",
-    "fork-ts-checker-webpack-plugin": "5.0.4",
+    "fork-ts-checker-webpack-plugin": "5.0.5",
     "mocha": "8.0.1",
     "nyc": "15.1.0",
     "pjson": "1.0.9",

+ 40 - 16
src/node-transformers/initializing-transformers/CommentsTransformer.ts

@@ -11,7 +11,6 @@ import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
 import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';
 
 import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
-import { ConditionalCommentObfuscatingGuard } from '../preparing-transformers/obfuscating-guards/ConditionalCommentObfuscatingGuard';
 import { NodeGuards } from '../../node/NodeGuards';
 
 @injectable()
@@ -33,6 +32,8 @@ export class CommentsTransformer extends AbstractNodeTransformer {
         @inject(ServiceIdentifiers.IOptions) options: IOptions
     ) {
         super(randomGenerator, options);
+
+        this.filterComment = this.filterComment.bind(this);
     }
 
     /**
@@ -50,25 +51,29 @@ export class CommentsTransformer extends AbstractNodeTransformer {
                     }
                 };
 
+            case NodeTransformationStage.Finalizing:
+                return {
+                    leave: (node: ESTree.Node): ESTree.Node | undefined => {
+                        if (NodeGuards.isProgramNode(node)) {
+                            return this.filterComments(node);
+                        }
+                    }
+                };
+
             default:
                 return null;
         }
     }
 
     /**
-     * Removes all comments from node except comments that contain
-     * `@license`, `@preserve` or `javascript-obfuscator` words
-     * Move comments to their nodes
-     *
-     * @param {Node} rootNode
-     * @returns {NodeGuards}
+     * Moves comments to their nodes
      */
     public transformNode (rootNode: ESTree.Program): ESTree.Node {
         if (!rootNode.comments || !rootNode.comments.length) {
             return rootNode;
         }
 
-        const comments: ESTree.Comment[] = this.transformComments(rootNode.comments);
+        const comments: ESTree.Comment[] = rootNode.comments.reverse();
 
         if (comments.length === 0) {
             return rootNode;
@@ -109,14 +114,33 @@ export class CommentsTransformer extends AbstractNodeTransformer {
     }
 
     /**
-     * @param {Comment[]} comments
-     * @returns {Comment[]}
+     * Removes all comments from node except comments that contain preserved words
+     *
+     * @param {Node} rootNode
+     * @returns {NodeGuards}
+     */
+    public filterComments (rootNode: ESTree.Program): ESTree.Node {
+        estraverse.traverse(rootNode, {
+            enter: (node: ESTree.Node): void => {
+                if (node.leadingComments) {
+                    node.leadingComments = node.leadingComments?.filter(this.filterComment);
+                }
+
+                if (node.trailingComments) {
+                    node.trailingComments = node.trailingComments?.filter(this.filterComment);
+                }
+            }
+        });
+
+        return rootNode;
+    }
+
+    /**
+     * @param {ESTree.Comment} comment
+     * @returns {boolean}
      */
-    private transformComments (comments: ESTree.Comment[]): ESTree.Comment[] {
-        return comments.filter((comment: ESTree.Comment) =>
-            CommentsTransformer.preservedWords
-                .some((preservedWord: string) => comment.value.includes(preservedWord)) ||
-            ConditionalCommentObfuscatingGuard.isConditionalComment(comment)
-        ).reverse();
+    private filterComment (comment: ESTree.Comment): boolean {
+        return CommentsTransformer.preservedWords
+            .some((preservedWord: string) => comment.value.includes(preservedWord));
     }
 }

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

@@ -9,6 +9,7 @@ import { readFileAsString } from '../../../../../helpers/readFileAsString';
 describe('ConditionalCommentObfuscatingGuard', () => {
     describe('check', () => {
         describe('Variant #1: `disable` conditional comment', () => {
+            const disableConditionalCommentRegExp: RegExp = /\/\/ *javascript-obfuscator:disable/;
             const obfuscatedVariableDeclarationRegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *0x1;/;
             const ignoredVariableDeclarationRegExp: RegExp = /var bar *= *2;/;
             const consoleLogRegExp: RegExp = /console.log\(_0x([a-f0-9]){4,6}\);/;
@@ -26,20 +27,26 @@ describe('ConditionalCommentObfuscatingGuard', () => {
                 ).getObfuscatedCode();
             });
 
-            it('match #1: should obfuscate variable declaration before `disable` conditional comment', () => {
+            it('match #1: should remove `disable` conditional comment from the code', () => {
+                assert.notMatch(obfuscatedCode, disableConditionalCommentRegExp);
+            });
+
+            it('match #2: should obfuscate variable declaration before `disable` conditional comment', () => {
                 assert.match(obfuscatedCode, obfuscatedVariableDeclarationRegExp);
             });
 
-            it('match #2: should ignore variable declaration after `disable` conditional comment', () => {
+            it('match #3: should ignore variable declaration after `disable` conditional comment', () => {
                 assert.match(obfuscatedCode, ignoredVariableDeclarationRegExp);
             });
 
-            it('match #3: should obfuscate variable name in `console.log`', () => {
+            it('match #4: should obfuscate variable name in `console.log`', () => {
                 assert.match(obfuscatedCode, consoleLogRegExp);
             });
         });
 
         describe('Variant #2: `disable` and `enable` conditional comments #1', () => {
+            const disableConditionalCommentRegExp: RegExp = /\/\/ *javascript-obfuscator:disable/;
+            const enableConditionalCommentRegExp: RegExp = /\/\/ *javascript-obfuscator:enable/;
             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;/;
@@ -57,15 +64,23 @@ describe('ConditionalCommentObfuscatingGuard', () => {
                 ).getObfuscatedCode();
             });
 
-            it('match #1: should obfuscate variable declaration before `disable` conditional comment', () => {
+            it('match #1: should remove `disable` conditional comment from the code', () => {
+                assert.notMatch(obfuscatedCode, disableConditionalCommentRegExp);
+            });
+
+            it('match #2: should remove `enable` conditional comment from the code', () => {
+                assert.notMatch(obfuscatedCode, enableConditionalCommentRegExp);
+            });
+
+            it('match #3: should obfuscate variable declaration before `disable` conditional comment', () => {
                 assert.match(obfuscatedCode, obfuscatedVariableDeclaration1RegExp);
             });
 
-            it('match #2: should ignore variable declaration after `disable` conditional comment', () => {
+            it('match #4: should ignore variable declaration after `disable` conditional comment', () => {
                 assert.match(obfuscatedCode, ignoredVariableDeclarationRegExp);
             });
 
-            it('match #3: should obfuscate variable declaration after `enable` conditional comment', () => {
+            it('match #5: should obfuscate variable declaration after `enable` conditional comment', () => {
                 assert.match(obfuscatedCode, obfuscatedVariableDeclaration2RegExp);
             });
         });

+ 12 - 12
yarn.lock

@@ -369,10 +369,10 @@
   resolved "https://registry.yarnpkg.com/@types/node/-/node-13.9.3.tgz#6356df2647de9eac569f9a52eda3480fa9e70b4d"
   integrity sha512-01s+ac4qerwd6RHD+mVbOEsraDHSgUaefQlEdBbUolnQFjKwCr7luvAlEwW1RFojh67u0z4OUTjPn9LEl4zIkA==
 
-"@types/[email protected]3":
-  version "14.0.13"
-  resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.13.tgz#ee1128e881b874c371374c1f72201893616417c9"
-  integrity sha512-rouEWBImiRaSJsVA+ITTFM6ZxibuAlTuNOCyxVbwreu6k6+ujs7DfnU9o+PShFhET78pMBl3eH+AGSI5eOTkPA==
+"@types/[email protected]4":
+  version "14.0.14"
+  resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.14.tgz#24a0b5959f16ac141aeb0c5b3cd7a15b7c64cbce"
+  integrity sha512-syUgf67ZQpaJj01/tRTknkMNoBBLWJOBODF0Zm4NrXmiSuxjymFrxnTu1QVYRubhVkRcZLYZG8STTwJRdVm/WQ==
 
 "@types/normalize-package-data@^2.4.0":
   version "2.4.0"
@@ -1908,10 +1908,10 @@ [email protected]:
     resolve "^1.17.0"
     tsconfig-paths "^3.9.0"
 
-eslint-plugin-jsdoc@28.0.0:
-  version "28.0.0"
-  resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-28.0.0.tgz#e726a79b4a0e16e8602022d55e0e705e91dc0f50"
-  integrity sha512-Etne7B8AQcgXrnDPXdfV4x+szHVEP+JVZ2cYNfZGn+HoaZigIOzxxJGU+QUhymz6vuz0fpzFM6fTt64XOvw69w==
+eslint-plugin-jsdoc@28.5.1:
+  version "28.5.1"
+  resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-28.5.1.tgz#5a95ee8705af389ba2062a1036be6077b5e62f3f"
+  integrity sha512-1XSWu8UnGwqO8mX3XKGofffL83VRt00ptq0m5OrTLFDN3At4x+/pJ8YHJONKhGC35TtjskcS9/RR6F9pjQ8c+w==
   dependencies:
     comment-parser "^0.7.5"
     debug "^4.1.1"
@@ -2339,10 +2339,10 @@ [email protected]:
   dependencies:
     node-notifier "^6.0.0"
 
[email protected].4:
-  version "5.0.4"
-  resolved "https://registry.yarnpkg.com/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-5.0.4.tgz#92ae758e581239e1e37ddd5a6c9cb955764ef470"
-  integrity sha512-nSEqM3KhAjTf8VmuZym2k6WadIasvXybJExFegqMJDkTrOBOY8yGjsXG2FGFJls3DOHtXKzrr3Bv0ZD1LaM7cA==
[email protected].5:
+  version "5.0.5"
+  resolved "https://registry.yarnpkg.com/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-5.0.5.tgz#4cf77f925008464db4670750a6e9266a4d25a988"
+  integrity sha512-2vpP+irspLOpZ1pcH3K3yR76MWXVSOYKTGwhwFQGnWjOs5MxzPN+dyDh1tNoaD5DUFP5kr4lGQURE9Qk+DpiUQ==
   dependencies:
     "@babel/code-frame" "^7.8.3"
     chalk "^2.4.1"

この差分においてかなりの量のファイルが変更されているため、一部のファイルを表示していません