Merge pull request #865 from javascript-obfuscator/if-statement-simplify-fix

Fixed `simplify` option regression after `2.10.2`
This commit is contained in:
Timofey Kachalov
2021-01-20 09:58:02 +03:00
committed by GitHub
10 changed files with 81 additions and 39 deletions
+4
View File
@@ -1,5 +1,9 @@
Change Log
v2.10.3
---
* Fixed `simplify` option regression after `2.10.2`. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/864
v2.10.2
---
* Fixed behavior of `simplify` options when a node with a single-statement `body` is inside simplified `IfStatement` node. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/860
+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
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "2.10.2",
"version": "2.10.3",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
@@ -286,7 +286,8 @@ export class IfStatementSimplifyTransformer extends AbstractStatementSimplifyTra
*/
return NodeGuards.isFunctionDeclarationNode(statement)
/**
* Ignore any nodes with a single statement as a `body`
* Have to ignore all `IfStatement` nodes
* Also have to ignore any nodes with a single statement as a `body`
* Without ignore it can break following code:
* Input:
* if (condition1) {
@@ -306,6 +307,7 @@ export class IfStatementSimplifyTransformer extends AbstractStatementSimplifyTra
*
* See issue: https://github.com/javascript-obfuscator/javascript-obfuscator/issues/860
*/
|| NodeGuards.isIfStatementNode(statement)
|| NodeGuards.isNodeWithSingleStatementBody(statement)
/**
+10 -15
View File
@@ -1,31 +1,26 @@
'use strict';
import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNodes';
import { StringArrayEncoding } from '../../src/enums/node-transformers/string-array-transformers/StringArrayEncoding';
(function () {
const JavaScriptObfuscator: any = require('../../index');
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
console.log('1');
console.log('22');
console.log('333');
console.log('4444');
console.log('55555');
console.log('666666');
console.log('7777777');
console.log('88888888');
console.log('999999999');
function foo () {
if (bar) {
if (baz) {
const a = aa()
}
} else {
bb()
}
}
`,
{
...NO_ADDITIONAL_NODES_PRESET,
compact: false,
stringArray: true,
stringArrayThreshold: 1,
stringArrayEncoding: [
StringArrayEncoding.Rc4
]
simplify: true
}
).getObfuscatedCode();
@@ -753,31 +753,63 @@ describe('IfStatementSimplifyTransformer', () => {
describe('Prohibited single statement', () => {
describe('Variant #1: `IfStatement` as prohibited single statement', () => {
const regExp: RegExp = new RegExp(
'if *\\(!!\\[]\\) *{ *' +
'if *\\(!\\[]\\) *' +
'var _0x([a-f0-9]){4,6} *= *baz\\(\\); *' +
'} *else *' +
'var _0x([a-f0-9]){4,6} *= *hawk\\(\\);'
);
describe('Variant #1: `IfStatement` with `var` variable inside`' , () => {
const regExp: RegExp = new RegExp(
'if *\\(!!\\[]\\) *{ *' +
'if *\\(!\\[]\\) *' +
'var _0x([a-f0-9]){4,6} *= *baz\\(\\); *' +
'} *else *' +
'var _0x([a-f0-9]){4,6} *= *hawk\\(\\);'
);
let obfuscatedCode: string;
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/if-statement-as-prohibited-single-statement.js');
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/if-statement-as-prohibited-single-statement-1.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
simplify: true
}
).getObfuscatedCode();
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
simplify: true
}
).getObfuscatedCode();
});
it('should not simplify if statement', () => {
assert.match(obfuscatedCode, regExp);
});
});
it('should not simplify if statement', () => {
assert.match(obfuscatedCode, regExp);
describe('Variant #2: `IfStatement` with `const` variable inside`' , () => {
const regExp: RegExp = new RegExp(
'if *\\(!!\\[]\\) *{ *' +
'if *\\(!\\[]\\) *{ *' +
'const _0x([a-f0-9]){4,6} *= *baz\\(\\); *' +
'} *' +
'} *else *' +
'var _0x([a-f0-9]){4,6} *= *hawk\\(\\);'
);
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/if-statement-as-prohibited-single-statement-2.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
simplify: true
}
).getObfuscatedCode();
});
it('should not simplify if statement', () => {
assert.match(obfuscatedCode, regExp);
});
});
});
@@ -0,0 +1,9 @@
function foo() {
if (true) {
if (false) {
const bar = baz();
}
} else {
var bark = hawk();
}
}