Skip to content

Commit

Permalink
Fixed wrong generation of code that has statements after `ReturnStatm…
Browse files Browse the repository at this point in the history
…ent` when `simplify` option is enabled
  • Loading branch information
sanex3339 committed Feb 6, 2022
1 parent 40eec61 commit 77f64bf
Show file tree
Hide file tree
Showing 17 changed files with 311 additions and 49 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Change Log

v3.2.2
---
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1039

v3.2.1
---
* Updated copyright
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "3.2.1",
"version": "3.2.2",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down Expand Up @@ -53,7 +53,7 @@
"@types/eslint-scope": "3.7.3",
"@types/eslint-visitor-keys": "1.0.0",
"@types/estraverse": "5.1.1",
"@types/estree": "0.0.50",
"@types/estree": "0.0.51",
"@types/js-beautify": "1.13.3",
"@types/js-string-escape": "1.0.1",
"@types/md5": "2.3.1",
Expand Down
4 changes: 0 additions & 4 deletions src/declarations/ESTree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,4 @@ declare module 'estree' {
metadata?: LiteralNodeMetadata;
'x-verbatim-property'?: escodegen.XVerbatimProperty;
}

interface StaticBlock extends Omit<BlockStatement, 'type'> {
type: 'StaticBlock'
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ export interface IIteratedStatementsSimplifyData {
* @type {boolean}
*/
hasReturnStatement: boolean;

/**
* @type {boolean}
*/
hasStatementsAfterReturnStatement: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,19 @@ export abstract class AbstractStatementSimplifyTransformer extends AbstractNodeT
const {
startIndex,
unwrappedExpressions,
hasReturnStatement
hasReturnStatement,
hasStatementsAfterReturnStatement
} = this.collectIteratedStatementsSimplifyData(statementNode);

if (hasStatementsAfterReturnStatement) {
return {
leadingStatements: statementNode.body,
trailingStatement: null,
hasReturnStatement: false,
hasSingleExpression: false
};
}

const leadingStatements: ESTree.Statement[] = this.getLeadingStatements(statementNode, startIndex);

if (!unwrappedExpressions.length) {
Expand Down Expand Up @@ -111,6 +121,7 @@ export abstract class AbstractStatementSimplifyTransformer extends AbstractNodeT
const unwrappedExpressions: ESTree.Expression[] = [];

let hasReturnStatement: boolean = false;
let hasStatementsAfterReturnStatement: boolean = false;
let startIndex: number | null = null;

for (let i = statementNodeBodyLength - 1; i >= 0; i--) {
Expand All @@ -133,6 +144,7 @@ export abstract class AbstractStatementSimplifyTransformer extends AbstractNodeT
) {
unwrappedExpressions.unshift(statementBodyStatementNode.argument);
hasReturnStatement = true;
hasStatementsAfterReturnStatement = i !== statementNodeBodyLength - 1;
startIndex = i;
continue;
}
Expand All @@ -143,7 +155,8 @@ export abstract class AbstractStatementSimplifyTransformer extends AbstractNodeT
return {
startIndex,
unwrappedExpressions,
hasReturnStatement
hasReturnStatement,
hasStatementsAfterReturnStatement
};
}

Expand Down
6 changes: 2 additions & 4 deletions src/node/NodeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,8 @@ export class NodeGuards {
* @param {Node} node
* @returns {boolean}
*/
// TODO: add type guard after @types/estree update
public static isStaticBlockNode (node: ESTree.Node): boolean {
// TODO: Update @types/estree
return (<any>node).type === NodeType.StaticBlock;
public static isStaticBlockNode (node: ESTree.Node): node is ESTree.StaticBlock {
return node.type === NodeType.StaticBlock;
}

/**
Expand Down
37 changes: 10 additions & 27 deletions test/dev/dev.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,26 @@
'use strict';

import { StringArrayWrappersType } from '../../src/enums/node-transformers/string-array-transformers/StringArrayWrappersType';

(function () {
const JavaScriptObfuscator: any = require('../../index');

let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
function foo () {
var bar = 'bar';
function baz () {
var baz = 'baz';
return baz;
async function xyzzy(a,b)
{
if (a) {
return await foo(a) ;
console.log(a) ;
} else {
return await bar(b) ;
console.log(b) ;
}
return bar + baz();
}
console.log(foo());
`,
{
identifierNamesGenerator: 'mangled',
compact: false,
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 1,
simplify: false,
stringArray: true,
stringArrayIndexesType: [
'hexadecimal-number',
'hexadecimal-numeric-string'
],
stringArrayThreshold: 1,
stringArrayCallsTransform: true,
stringArrayCallsTransformThreshold: 1,
rotateStringArray: true,
stringArrayWrappersType: StringArrayWrappersType.Function,
transformObjectKeys: true
simplify: true,
stringArray: false
}
).getObfuscatedCode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ describe('StringArrayCodeHelperGroup', () => {
`function *(\\w) *\\(\\w, *\\w\\) *{.*return \\w;}.*`
);

describe('StringArrayCallsWrapper code helper names', () => {
describe('StringArrayCallsWrapper code helper names', function () {
this.timeout(10000);

const stringArrayCallsWrapperNames: Set<string> = new Set();
const samplesCount: number = 30;
let obfuscatedCode: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ describe('ConditionalCommentObfuscatingGuard', () => {
describe('check', () => {
describe('Variant #1: `disable` conditional comment', () => {
const disableConditionalCommentRegExp: RegExp = /\/\/ *javascript-obfuscator:disable/;
const obfuscatedVariableDeclarationRegExp: RegExp = /var _0x([a-f0-9]){5,6} *= *0x1;/;
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]){5,6}\);/;
const consoleLogRegExp: RegExp = /console.log\(_0x([a-f0-9]){4,6}\);/;

let obfuscatedCode: string;

Expand Down Expand Up @@ -47,8 +47,8 @@ describe('ConditionalCommentObfuscatingGuard', () => {
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]){5,6} *= *0x1;/;
const obfuscatedVariableDeclaration2RegExp: RegExp = /var _0x([a-f0-9]){5,6} *= *0x3;/;
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;/;

let obfuscatedCode: string;
Expand Down Expand Up @@ -87,7 +87,7 @@ describe('ConditionalCommentObfuscatingGuard', () => {

describe('Variant #3: `disable` and `enable` conditional comments #2', () => {
const ignoredVariableDeclarationRegExp: RegExp = /var foo *= *1;/;
const obfuscatedVariableDeclarationRegExp: RegExp = /var _0x([a-f0-9]){5,6} *= *0x2;/;
const obfuscatedVariableDeclarationRegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *0x2;/;

let obfuscatedCode: string;

Expand Down Expand Up @@ -139,7 +139,7 @@ describe('ConditionalCommentObfuscatingGuard', () => {
});

describe('Variant #5: `disable` and `enable` conditional comments with dead code injection', () => {
const obfuscatedFunctionExpressionRegExp: RegExp = /var _0x([a-f0-9]){5,6} *= *function *\(_0x([a-f0-9]){5,6}, *_0x([a-f0-9]){5,6}, *_0x([a-f0-9]){5,6}\) *{/g;
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;

const ignoredFunctionExpression1RegExp: RegExp = /var bar *= *function *\(a, *b, *c\) *{/;
Expand All @@ -166,7 +166,7 @@ describe('ConditionalCommentObfuscatingGuard', () => {
deadCodeInjectionThreshold: 1
}
).getObfuscatedCode();

const obfuscatedFunctionExpressionMatches: RegExpMatchArray | null = obfuscatedCode.match(
obfuscatedFunctionExpressionRegExp
);
Expand Down Expand Up @@ -209,7 +209,7 @@ describe('ConditionalCommentObfuscatingGuard', () => {
});

describe('Variant #6: `disable` and `enable` conditional comments with control flow flattening', () => {
const obfuscatedVariableDeclarationRegExp: RegExp = /var _0x([a-f0-9]){5,6} *= *_0x([a-f0-9]){5,6}\['\w{5}'];/;
const obfuscatedVariableDeclarationRegExp: RegExp = /var _0x([a-f0-9]){4,6} *= *_0x([a-f0-9]){4,6}\['\w{5}'];/;
const ignoredVariableDeclarationRegExp: RegExp = /var bar *= *'bar';/;

let obfuscatedCode: string;
Expand Down

0 comments on commit 77f64bf

Please sign in to comment.