Skip to content

Commit e8188c3

Browse files
committed
Merge branch '0.17.3'
# Conflicts: # dist/index.browser.js # dist/index.cli.js # dist/index.js # package.json
2 parents 348cd78 + 984e08d commit e8188c3

16 files changed

+307
-74
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ v0.18.0
44
---
55
* **New option:** `reservedStrings` disables transformation of string literals, which being matched by passed RegExp patterns
66

7+
v0.17.3
8+
---
9+
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/303
10+
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/302
11+
712
v0.17.2
813
---
914
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/297

dist/index.browser.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.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.

index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { TInputOptions } from './src/types/options/TInputOptions';
22

3-
import { IObfuscationResult } from './src/interfaces/IObfuscationResult';
3+
import { IObfuscatedCode } from './src/interfaces/source-code/IObfuscatedCode';
44

55
export type ObfuscatorOptions = TInputOptions;
66

7-
export interface ObfuscationResult extends IObfuscationResult {}
7+
export interface ObfuscatedCode extends IObfuscatedCode {}
88

9-
export function obfuscate (sourceCode: string, inputOptions?: ObfuscatorOptions): ObfuscationResult;
9+
export function obfuscate (sourceCode: string, inputOptions?: ObfuscatorOptions): ObfuscatedCode;

src/node-transformers/control-flow-transformers/BlockStatementControlFlowTransformer.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,20 @@ export class BlockStatementControlFlowTransformer extends AbstractNodeTransforme
5252
}
5353

5454
/**
55-
* @param {BlockStatement} blockStatementNode
55+
* @param {Node} node
5656
* @returns {boolean}
5757
*/
58-
private static blockStatementHasProhibitedStatements (blockStatementNode: ESTree.BlockStatement): boolean {
59-
return blockStatementNode.body.some((statement: ESTree.Statement) => {
60-
const isBreakOrContinueStatement: boolean = NodeGuards.isBreakStatementNode(statement)
61-
|| NodeGuards.isContinueStatementNode(statement);
62-
const isVariableDeclarationWithLetOrConstKind: boolean = NodeGuards.isVariableDeclarationNode(statement)
63-
&& (statement.kind === 'const' || statement.kind === 'let');
64-
const isClassDeclaration: boolean = NodeGuards.isClassDeclarationNode(statement);
65-
66-
return NodeGuards.isFunctionDeclarationNode(statement)
67-
|| isBreakOrContinueStatement
68-
|| isVariableDeclarationWithLetOrConstKind
69-
|| isClassDeclaration;
70-
});
58+
private static isProhibitedStatementNode (node: ESTree.Node): boolean {
59+
const isBreakOrContinueStatement: boolean = NodeGuards.isBreakStatementNode(node)
60+
|| NodeGuards.isContinueStatementNode(node);
61+
const isVariableDeclarationWithLetOrConstKind: boolean = NodeGuards.isVariableDeclarationNode(node)
62+
&& (node.kind === 'const' || node.kind === 'let');
63+
const isClassDeclaration: boolean = NodeGuards.isClassDeclarationNode(node);
64+
65+
return NodeGuards.isFunctionDeclarationNode(node)
66+
|| isBreakOrContinueStatement
67+
|| isVariableDeclarationWithLetOrConstKind
68+
|| isClassDeclaration;
7169
}
7270

7371
/**
@@ -83,10 +81,7 @@ export class BlockStatementControlFlowTransformer extends AbstractNodeTransforme
8381
return estraverse.VisitorOption.Skip;
8482
}
8583

86-
if (
87-
NodeGuards.isBlockStatementNode(node)
88-
&& BlockStatementControlFlowTransformer.blockStatementHasProhibitedStatements(node)
89-
) {
84+
if (BlockStatementControlFlowTransformer.isProhibitedStatementNode(node)) {
9085
canTransform = false;
9186
}
9287
}

test/functional-tests/node-transformers/control-flow-transformers/block-statement-control-flow-transformer/BlockStatementControlFlowTransformer.spec.ts

Lines changed: 118 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,61 @@ describe('BlockStatementControlFlowTransformer', function () {
309309
});
310310
});
311311

312-
describe('Variant #8: block statement contain while statement with break statement', () => {
312+
describe('Variant #8: block statement contain break statement #3', () => {
313+
const statementRegExp: RegExp = /^\(function *\( *\) *\{ *while *\(!!\[\]\) *\{ *if *\(!!\[\]\) *break; *console\['log'\]\(0x1\);/;
314+
315+
let obfuscatedCode: string;
316+
317+
before(() => {
318+
const code: string = readFileAsString(__dirname + '/fixtures/break-statement-3.js');
319+
320+
obfuscatedCode = JavaScriptObfuscator.obfuscate(
321+
code,
322+
{
323+
...NO_ADDITIONAL_NODES_PRESET,
324+
controlFlowFlattening: true,
325+
controlFlowFlatteningThreshold: 1
326+
}
327+
).getObfuscatedCode();
328+
});
329+
330+
it('shouldn\'t transform block statement', () => {
331+
assert.match(obfuscatedCode, statementRegExp);
332+
});
333+
});
334+
335+
describe('Variant #9: block statement contain while statement with break statement', () => {
336+
const switchCaseRegExp: RegExp = /switch *\(_0x([a-f0-9]){4,6}\[_0x([a-f0-9]){4,6}\+\+\]\) *\{/;
337+
const switchCaseLengthRegExp: RegExp = /case *'[0-5]': *console\['log'\]\(0x[0-6]\);/g;
338+
const expectedSwitchCaseLength: number = 5;
339+
340+
let obfuscatedCode: string,
341+
switchCaseLength: number;
342+
343+
before(() => {
344+
const code: string = readFileAsString(__dirname + '/fixtures/break-statement-inside-while-statement-1.js');
345+
346+
obfuscatedCode = JavaScriptObfuscator.obfuscate(
347+
code,
348+
{
349+
...NO_ADDITIONAL_NODES_PRESET,
350+
controlFlowFlattening: true,
351+
controlFlowFlatteningThreshold: 1
352+
}
353+
).getObfuscatedCode();
354+
switchCaseLength = obfuscatedCode.match(switchCaseLengthRegExp)!.length;
355+
});
356+
357+
it('should wrap block statement statements in switch-case structure', () => {
358+
assert.match(obfuscatedCode, switchCaseRegExp);
359+
});
360+
361+
it('each statement should be wrapped by switch-case structure', () => {
362+
assert.equal(switchCaseLength, expectedSwitchCaseLength);
363+
});
364+
});
365+
366+
describe('Variant #10: block statement contain while statement with break statement', () => {
313367
const switchCaseRegExp: RegExp = /switch *\(_0x([a-f0-9]){4,6}\[_0x([a-f0-9]){4,6}\+\+\]\) *\{/;
314368
const switchCaseLengthRegExp: RegExp = /case *'[0-5]': *console\['log'\]\(0x[0-6]\);/g;
315369
const expectedSwitchCaseLength: number = 5;
@@ -318,7 +372,7 @@ describe('BlockStatementControlFlowTransformer', function () {
318372
switchCaseLength: number;
319373

320374
before(() => {
321-
const code: string = readFileAsString(__dirname + '/fixtures/break-statement-inside-while-statement.js');
375+
const code: string = readFileAsString(__dirname + '/fixtures/break-statement-inside-while-statement-2.js');
322376

323377
obfuscatedCode = JavaScriptObfuscator.obfuscate(
324378
code,
@@ -340,7 +394,7 @@ describe('BlockStatementControlFlowTransformer', function () {
340394
});
341395
});
342396

343-
describe('Variant #9: block statement contain continue statement #1', () => {
397+
describe('Variant #11: block statement contain continue statement #1', () => {
344398
const statementRegExp: RegExp = /^\(function *\( *\) *\{ *while *\(!!\[\]\) *\{ *continue; *console\['log'\]\(0x1\);/;
345399

346400
let obfuscatedCode: string;
@@ -363,7 +417,7 @@ describe('BlockStatementControlFlowTransformer', function () {
363417
});
364418
});
365419

366-
describe('Variant #10: block statement contain continue statement #2', () => {
420+
describe('Variant #12: block statement contain continue statement #2', () => {
367421
const statementRegExp: RegExp = /^\(function *\( *\) *\{ *while *\(!!\[\]\) *\{ *if *\(!!\[\]\) *\{ *continue; *\} *console\['log'\]\(0x1\);/;
368422

369423
let obfuscatedCode: string;
@@ -386,7 +440,61 @@ describe('BlockStatementControlFlowTransformer', function () {
386440
});
387441
});
388442

389-
describe('Variant #11: block statement contain while statement with continue statement', () => {
443+
describe('Variant #13: block statement contain continue statement #3', () => {
444+
const statementRegExp: RegExp = /^\(function *\( *\) *\{ *while *\(!!\[\]\) *\{ *if *\(!!\[\]\) *continue; *console\['log'\]\(0x1\);/;
445+
446+
let obfuscatedCode: string;
447+
448+
before(() => {
449+
const code: string = readFileAsString(__dirname + '/fixtures/continue-statement-3.js');
450+
451+
obfuscatedCode = JavaScriptObfuscator.obfuscate(
452+
code,
453+
{
454+
...NO_ADDITIONAL_NODES_PRESET,
455+
controlFlowFlattening: true,
456+
controlFlowFlatteningThreshold: 1
457+
}
458+
).getObfuscatedCode();
459+
});
460+
461+
it('shouldn\'t transform block statement', () => {
462+
assert.match(obfuscatedCode, statementRegExp);
463+
});
464+
});
465+
466+
describe('Variant #14: block statement contain while statement with continue statement', () => {
467+
const switchCaseRegExp: RegExp = /switch *\(_0x([a-f0-9]){4,6}\[_0x([a-f0-9]){4,6}\+\+\]\) *\{/;
468+
const switchCaseLengthRegExp: RegExp = /case *'[0-5]': *console\['log'\]\(0x[0-6]\);/g;
469+
const expectedSwitchCaseLength: number = 5;
470+
471+
let obfuscatedCode: string,
472+
switchCaseLength: number;
473+
474+
before(() => {
475+
const code: string = readFileAsString(__dirname + '/fixtures/continue-statement-inside-while-statement-1.js');
476+
477+
obfuscatedCode = JavaScriptObfuscator.obfuscate(
478+
code,
479+
{
480+
...NO_ADDITIONAL_NODES_PRESET,
481+
controlFlowFlattening: true,
482+
controlFlowFlatteningThreshold: 1
483+
}
484+
).getObfuscatedCode();
485+
switchCaseLength = obfuscatedCode.match(switchCaseLengthRegExp)!.length;
486+
});
487+
488+
it('should wrap block statement statements in switch-case structure', () => {
489+
assert.match(obfuscatedCode, switchCaseRegExp);
490+
});
491+
492+
it('each statement should be wrapped by switch-case structure', () => {
493+
assert.equal(switchCaseLength, expectedSwitchCaseLength);
494+
});
495+
});
496+
497+
describe('Variant #15: block statement contain continue statement #4', () => {
390498
const switchCaseRegExp: RegExp = /switch *\(_0x([a-f0-9]){4,6}\[_0x([a-f0-9]){4,6}\+\+\]\) *\{/;
391499
const switchCaseLengthRegExp: RegExp = /case *'[0-5]': *console\['log'\]\(0x[0-6]\);/g;
392500
const expectedSwitchCaseLength: number = 5;
@@ -395,7 +503,7 @@ describe('BlockStatementControlFlowTransformer', function () {
395503
switchCaseLength: number;
396504

397505
before(() => {
398-
const code: string = readFileAsString(__dirname + '/fixtures/continue-statement-inside-while-statement.js');
506+
const code: string = readFileAsString(__dirname + '/fixtures/continue-statement-inside-while-statement-2.js');
399507

400508
obfuscatedCode = JavaScriptObfuscator.obfuscate(
401509
code,
@@ -417,7 +525,7 @@ describe('BlockStatementControlFlowTransformer', function () {
417525
});
418526
});
419527

420-
describe('Variant #12: block statement contain function declaration', () => {
528+
describe('Variant #16: block statement contain function declaration', () => {
421529
const statementRegExp: RegExp = /^\(function *\( *\) *\{ *function *_0x([a-f0-9]){4,6} *\( *\) *\{ *\} *console\['log'\]\(0x1\);/
422530

423531
let obfuscatedCode: string;
@@ -440,7 +548,7 @@ describe('BlockStatementControlFlowTransformer', function () {
440548
});
441549
});
442550

443-
describe('Variant #13: block statement contain class declaration', () => {
551+
describe('Variant #17: block statement contain class declaration', () => {
444552
const statementRegExp: RegExp = /^\(function *\( *\) *{ * *class *_0x([a-f0-9]){4,6} *{.*?} *}.*class *_0x([a-f0-9]){4,6} *{.*?} *}.*class *_0x([a-f0-9]){4,6} *{.*?} *}/;
445553

446554
let obfuscatedCode: string;
@@ -463,7 +571,7 @@ describe('BlockStatementControlFlowTransformer', function () {
463571
});
464572
});
465573

466-
describe('Variant #14: `controlFlowFlatteningThreshold` chance', () => {
574+
describe('Variant #18: `controlFlowFlatteningThreshold` chance', () => {
467575
const samples: number = 1000;
468576
const delta: number = 0.1;
469577

@@ -507,7 +615,7 @@ describe('BlockStatementControlFlowTransformer', function () {
507615
});
508616
});
509617

510-
describe('Variant #15: No `unreachable code after return statement` warning', () => {
618+
describe('Variant #19: No `unreachable code after return statement` warning', () => {
511619
const switchCaseRegExp: RegExp = /switch *\(_0x([a-f0-9]){4,6}\[_0x([a-f0-9]){4,6}\+\+\]\) *\{/;
512620
const switchCaseLengthRegExp: RegExp = /case *'[0-5]': *console\['log'\]\(0x[0-6]\);/g;
513621
const returnStatementRegExp: RegExp = /case *'[0-5]': *return; *(case|})/;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(function () {
2+
while (true) {
3+
if (true)
4+
break;
5+
console.log(1);
6+
console.log(2);
7+
console.log(3);
8+
console.log(4);
9+
console.log(5);
10+
}
11+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(function () {
2+
while (true) {
3+
while (true)
4+
break;
5+
console.log(1);
6+
console.log(2);
7+
console.log(3);
8+
console.log(4);
9+
console.log(5);
10+
}
11+
})();

0 commit comments

Comments
 (0)