8000 Merge pull request #642 from javascript-obfuscator/remove-conditional… · sec-js/javascript-obfuscator@dcec5f6 · GitHub
[go: up one dir, main page]

Skip to content

Commit dcec5f6

Browse files
authored
Merge pull request javascript-obfuscator#642 from javascript-obfuscator/remove-conditional-comments
Conditional comments will be removed from the code after obfuscation
2 parents bf7511b + 731d765 commit dcec5f6

File tree

10 files changed

+92
-56
lines changed

10 files changed

+92
-56
lines changed

.ncurc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"reject": []
2+
"reject": [
3+
"@types/estree"
4+
]
35
}

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Change Log
22

3+
v1.2.0
4+
---
5+
* Conditional comments will be removed from the code after obfuscation. https://github.com/javascript-obfuscator/javascript-obfuscator/issues/641
6+
37
v1.1.1
48
---
59
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/638

dist/index.browser.js

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.cli.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "javascript-obfuscator",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "JavaScript obfuscator",
55
"keywords": [
66
"obfuscator",
@@ -54,7 +54,7 @@
5454
"@types/mkdirp": "1.0.1",
5555
"@types/mocha": "7.0.2",
5656
"@types/multimatch": "4.0.0",
57-
"@types/node": "14.0.13",
57+
"@types/node": "14.0.14",
5858
"@types/rimraf": "3.0.0",
5959
"@types/sinon": "9.0.4",
6060
"@types/string-template": "1.0.2",
@@ -65,12 +65,12 @@
6565
"coveralls": "3.1.0",
6666
"eslint": "7.3.1",
6767
"eslint-plugin-import": "2.21.2",
68-
"eslint-plugin-jsdoc": "28.0.0",
68+
"eslint-plugin-jsdoc": "28.5.1",
6969
"eslint-plugin-no-null": "1.0.2",
7070
"eslint-plugin-prefer-arrow": "1.2.1",
7171
"eslint-plugin-unicorn": "20.1.0",
7272
"fork-ts-checker-notifier-webpack-plugin": "3.0.0",
73-
"fork-ts-checker-webpack-plugin": "5.0.4",
73+
"fork-ts-checker-webpack-plugin": "5.0.5",
7474
"mocha": "8.0.1",
7575
"nyc": "15.1.0",
7676
"pjson": "1.0.9",

src/node-transformers/initializing-transformers/CommentsTransformer.ts

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { IVisitor } from '../../interfaces/node-transformers/IVisitor';
1111
import { NodeTransformationStage } from '../../enums/node-transformers/NodeTransformationStage';
1212

1313
import { AbstractNodeTransformer } from '../AbstractNodeTransformer' 10000 ;
14-
import { ConditionalCommentObfuscatingGuard } from '../preparing-transformers/obfuscating-guards/ConditionalCommentObfuscatingGuard';
1514
import { NodeGuards } from '../../node/NodeGuards';
1615

1716
@injectable()
@@ -33,6 +32,8 @@ export class CommentsTransformer extends AbstractNodeTransformer {
3332
@inject(ServiceIdentifiers.IOptions) options: IOptions
3433
) {
3534
super(randomGenerator, options);
35+
36+
this.filterComment = this.filterComment.bind(this);
3637
}
3738

3839
/**
@@ -50,25 +51,29 @@ export class CommentsTransformer extends AbstractNodeTransformer {
5051
}
5152
};
5253

54+
case NodeTransformationStage.Finalizing:
55+
return {
56+
leave: (node: ESTree.Node): ESTree.Node | undefined => {
57+
if (NodeGuards.isProgramNode(node)) {
58+
return this.filterComments(node);
59+
}
60+
}
61+
};
62+
5363
default:
5464
return null;
5565
}
5666
}
5767

5868
/**
59-
* Removes all comments from node except comments that contain
60-
* `@license`, `@preserve` or `javascript-obfuscator` words
61-
* Move comments to their nodes
62-
*
63-
* @param {Node} rootNode
64-
* @returns {NodeGuards}
69+
* Moves comments to their nodes
6570
*/
6671
public transformNode (rootNode: ESTree.Program): ESTree.Node {
6772
if (!rootNode.comments || !rootNode.comments.length) {
6873
return rootNode;
6974
}
7075

71-
const comments: ESTree.Comment[] = this.transformComments(rootNode.comments);
76+
const comments: ESTree.Comment[] = rootNode.comments.reverse();
7277

7378
if (comments.length === 0) {
7479
return rootNode;
@@ -109,14 +114,33 @@ export class CommentsTransformer extends AbstractNodeTransformer {
109114
}
110115

111116
/**
112-
* @param {Comment[]} comments
113-
* @returns {Comment[]}
117+
* Removes all comments from node except comments that contain preserved words
118+
*
119+
* @param {Node} rootNode
120+
* @returns {NodeGuards}
121+
*/
122+
public filterComments (rootNode: ESTree.Program): ESTree.Node {
123+
estraverse.traverse(rootNode, {
124+
enter: (node: ESTree.Node): void => {
125+
if (node.leadingComments) {
126+
node.leadingComments = node.leadingComments?.filter(this.filterComment);
127+
}
128+
129+
if (node.trailingComments) {
130+
node.trailingComments = node.trailingComments?.filter(this.filterComment);
131+
}
132+
}
133+
});
134+
135+
return rootNode;
136+
}
137+
138+
/**
139+
* @param {ESTree.Comment} comment
140+
* @returns {boolean}
114141
*/
115-
private transformComments (comments: ESTree.Comment[]): ESTree.Comment[] {
116-
return comments.filter((comment: ESTree.Comment) =>
117-
CommentsTransformer.preservedWords
118-
.some((preservedWord: string) => comment.value.includes(preservedWord)) ||
119-
ConditionalCommentObfuscatingGuard.isConditionalComment(comment)
120-
).reverse();
142+
private filterComment (comment: ESTree.Comment): boolean {
143+
return CommentsTransformer.preservedWords
144+
.some((preservedWord: string) => comment.value.includes(preservedWord));
121145
}
122146
}

src/node-transformers/preparing-transformers/obfuscating-guards/ConditionalCommentObfuscatingGuard.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,6 @@ export class ConditionalCommentObfuscatingGuard implements IObfuscatingGuard {
2323
*/
2424
private obfuscationAllowed: boolean = true;
2525

26-
/**
27-
* @param {Comment} comment
28-
* @returns {boolean}
29-
*/
30-
public static isConditionalComment (comment: ESTree.Comment): boolean {
31-
return ConditionalCommentObfuscatingGuard.obfuscationEnableCommentRegExp.test(comment.value) ||
32-
ConditionalCommentObfuscatingGuard.obfuscationDisableCommentRegExp.test(comment.value);
33-
}
34-
3526
/**
3627
* @returns {boolean}
3728
* @param node

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { readFileAsString } from '../../../../../helpers/readFileAsString';
99
describe('ConditionalCommentObfuscatingGuard', () => {
1010
describe('check', () => {
1111
describe('Variant #1: `disable` conditional comment', () => {
12+
const disableConditionalCommentRegExp: RegExp = /\/\/ *javascript-obfuscator:disable/;
1213
const obfuscatedVariableDeclarationRegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *0x1;/;
1314
const ignoredVariableDeclarationRegExp: RegExp = /var bar *= *2;/;
1415
const consoleLogRegExp: RegExp = /console.log\(_0x([a-f0-9]){4,6}\);/;
@@ -26,20 +27,26 @@ describe('ConditionalCommentObfuscatingGuard', () => {
2627
).getObfuscatedCode();
2728
});
2829

29-
it('match #1: should obfuscate variable declaration before `disable` conditional comment', () => {
30+
it('match #1: should remove `disable` conditional comment from the code', () => {
31+
assert.notMatch(obfuscatedCode, disableConditionalCommentRegExp);
32+
});
33+
34+
it('match #2: should obfuscate variable declaration before `disable` conditional comment', () => {
3035
assert.match(obfuscatedCode, obfuscatedVariableDeclarationRegExp);
3136
});
3237

33-
it('match #2: should ignore variable declaration after `disable` conditional comment', () => {
38+
it('match #3: should ignore variable declaration after `disable` conditional comment', () => {
3439
assert.match(obfuscatedCode, ignoredVariableDeclarationRegExp);
3540
});
3641

37-
it('match #3: should obfuscate variable name in `console.log`', () => {
42+
it('match #4: should obfuscate variable name in `console.log`', () => {
3843
assert.match(obfuscatedCode, consoleLogRegExp);
3944
});
4045
});
4146

4247
describe('Variant #2: `disable` and `enable` conditional comments #1', () => {
48+
const disableConditionalCommentRegExp: RegExp = /\/\/ *javascript-obfuscator:disable/;
49+
const enableConditionalCommentRegExp: RegExp = /\/\/ *javascript-obfuscator:enable/;
4350
const obfuscatedVariableDeclaration1RegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *0x1;/;
4451
const obfuscatedVariableDeclaration2RegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *0x3;/;
4552
const ignoredVariableDeclarationRegExp: RegExp = /var bar *= *2;/;
@@ -57,15 +64,23 @@ describe('ConditionalCommentObfuscatingGuard', () => {
5764
).getObfuscatedCode();
5865
});
5966

60-
it('match #1: should obfuscate variable declaration before `disable` conditional comment', () => {
67+
it('match #1: should remove `disable` conditional comment from the code', () => {
68+
assert.notMatch(obfuscatedCode, disableConditionalCommentRegExp);
69+
});
70+
71+
it('match #2: should remove `enable` conditional comment from the code', () => {
72+
assert.notMatch(obfuscatedCode, enableConditionalCommentRegExp);
73+
});
74+
75+
it('match #3: should obfuscate variable declaration before `disable` conditional comment', () => {
6176
assert.match(obfuscatedCode, obfuscatedVariableDeclaration1RegExp);
6277
});
6378

64-
it('match #2: should ignore variable declaration after `disable` conditional comment', () => {
79+
it('match #4: should ignore variable declaration after `disable` conditional comment', () => {
6580
assert.match(obfuscatedCode, ignoredVariableDeclarationRegExp);
6681
});
6782

68-
it('match #3: should obfuscate variable declaration after `enable` conditional comment', () => {
83+
it('match #5: should obfuscate variable declaration after `enable` conditional comment', () => {
6984
assert.match(obfuscatedCode, obfuscatedVariableDeclaration2RegExp);
7085
});
7186
});

yarn.lock

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,10 @@
369369
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.9.3.tgz#6356df2647de9eac569f9a52eda3480fa9e70b4d"
370370
integrity sha512-01s+ac4qerwd6RHD+mVbOEsraDHSgUaefQlEdBbUolnQFjKwCr7luvAlEwW1RFojh67u0z4OUTjPn9LEl4zIkA==
371371

372-
"@types/node@14.0.13":
373-
version "14.0.13"
374-
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.13.tgz#ee1128e881b874c371374c1f72201893616417c9"
375-
integrity sha512-rouEWBImiRaSJsVA+ITTFM6ZxibuAlTuNOCyxVbwreu6k6+ujs7DfnU9o+PShFhET78pMBl3eH+AGSI5eOTkPA==
372+
"@types/node@14.0.14":
373+
version "14.0.14"
374+
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.14.tgz#24a0b5959f16ac141aeb0c5b3cd7a15b7c64cbce"
375+
integrity sha512-syUgf67ZQpaJj01/tRTknkMNoBBLWJOBODF0Zm4NrXmiSuxjymFrxnTu1QVYRubhVkRcZLYZG8STTwJRdVm/WQ==
376376

377377
"@types/normalize-package-data@^2.4.0":
378378
version "2.4.0"
@@ -1908,10 +1908,10 @@ eslint-plugin-import@2.21.2:
19081908
resolve "^1.17.0"
19091909
tsconfig-paths "^3.9.0"
19101910

1911-
eslint-plugin-jsdoc@28.0.0:
1912-
version "28.0.0"
1913-
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-28.0.0.tgz#e726a79b4a0e16e8602022d55e0e705e91dc0f50"
1914-
integrity sha512-Etne7B8AQcgXrnDPXdfV4x+szHVEP+JVZ2cYNfZGn+HoaZigIOzxxJGU+QUhymz6vuz0fpzFM6fTt64XOvw69w==
1911+
eslint-plugin-jsdoc@28.5.1:
1912+
version "28.5.1"
1913+
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-28.5.1.tgz#5a95ee8705af389ba2062a1036be6077b5e62f3f"
1914+
integrity sha512-1XSWu8UnGwqO8mX3XKGofffL83VRt00ptq0m5OrTLFDN3At4x+/pJ8YHJONKhGC35TtjskcS9/RR6F9pjQ8c+w==
19151915
dependencies:
19161916
comment-parser "^0.7.5"
19171917
debug "^4.1.1"
@@ -2339,10 +2339,10 @@ fork-ts-checker-notifier-webpack-plugin@3.0.0:
23392339
dependencies:
23402340
node-notifier "^6.0.0"
23412341

2342-
fork-ts-checker-webpack-plugin@5.0.4:
2343-
version "5.0.4"
2344-
resolved "https://registry.yarnpkg.com/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-5.0.4.tgz#92ae758e581239e1e37ddd5a6c9cb955764ef470"
2345-
integrity sha512-nSEqM3KhAjTf8VmuZym2k6WadIasvXybJExFegqMJDkTrOBOY8yGjsXG2FGFJls3DOHtXKzrr3Bv0ZD1LaM7cA==
2342+
fork-ts-checker-webpack-plugin@5.0.5:
2343+
version "5.0.5"
2344+
resolved "https://registry.yarnpkg.com/fork-ts-checker-webpack-plugin/-/fork-ts-checker-webpack-plugin-5.0.5.tgz#4cf77f925008464db4670750a6e9266a4d25a988"
2345+
integrity sha512-2vpP+irspLOpZ1pcH3K3yR76MWXVSOYKTGwhwFQGnWjOs5MxzPN+dyDh1tNoaD5DUFP5kr4lGQURE9Qk+DpiUQ==
23462346
dependencies:
23472347
"@babel/code-frame" "^7.8.3"
23482348
chalk "^2.4.1"

0 commit comments

Comments
 (0)
0