Fixed conditional comments in some rare cases

This commit is contained in:
sanex3339
2020-01-07 14:22:18 +03:00
parent 023c5b54a7
commit 6d3c2ae467
9 changed files with 87 additions and 36 deletions
+5
View File
@@ -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
+3 -3
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "0.21.0",
"version": "0.21.1",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
@@ -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;
}
const commentIdx: number = comments.findIndex((comment: ESTree.Comment) =>
comment.range && node.range && comment.range[0] < node.range[0]
);
if (commentIdx === -1) {
return;
}
node.leadingComments = comments.splice(commentIdx, comments.length - commentIdx).reverse();
}
});
if (comments.length > 0) {
programNode.trailingComments = comments.reverse();
}
public transformNode (rootNode: ESTree.Program): ESTree.Node {
if (!rootNode.comments || !rootNode.comments.length) {
return rootNode;
}
return programNode;
const comments: ESTree.Comment[] = this.transformComments(rootNode.comments);
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;
}
const commentIdx: number = comments.findIndex((comment: ESTree.Comment) =>
comment.range && node.range && comment.range[0] < node.range[0]
);
if (commentIdx === -1) {
return;
}
node.leadingComments = comments.splice(commentIdx, comments.length - commentIdx).reverse();
}
});
if (comments.length > 0) {
rootNode.trailingComments = comments.reverse();
}
return rootNode;
}
/**
@@ -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 +1,5 @@
// javascript-obfuscator:disable
var foo = 1;
// javascript-obfuscator:enable
var bar = 2;