Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Change Log

v0.28.2
---
* Fixed change of kinds of variables for dead code with `deadCodeInjection` option

v0.28.1
---
* Removed `acorn-import-meta` package
Expand Down
14 changes: 7 additions & 7 deletions dist/index.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

32 changes: 18 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "0.28.1",
"version": "0.28.2",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand All @@ -25,13 +25,13 @@
"@nuxtjs/opencollective": "0.2.2",
"acorn": "7.2.0",
"chalk": "4.0.0",
"chance": "1.1.4",
"chance": "1.1.5",
"class-validator": "0.12.2",
"commander": "5.1.0",
"escodegen": "1.14.1",
"eslint-scope": "5.0.0",
"estraverse": "5.1.0",
"eventemitter3": "4.0.0",
"eventemitter3": "4.0.4",
"fast-deep-equal": "3.1.1",
"inversify": "5.0.1",
"js-string-escape": "1.0.1",
Expand All @@ -41,11 +41,11 @@
"reflect-metadata": "0.1.13",
"source-map-support": "0.5.19",
"string-template": "1.0.0",
"tslib": "1.11.2"
"tslib": "2.0.0"
},
"devDependencies": {
"@types/chai": "4.2.11",
"@types/chance": "1.0.10",
"@types/chance": "1.1.0",
"@types/escodegen": "0.0.6",
"@types/eslint-scope": "3.7.0",
"@types/estraverse": "0.0.6",
Expand All @@ -54,33 +54,33 @@
"@types/mkdirp": "1.0.0",
"@types/mocha": "7.0.2",
"@types/multimatch": "4.0.0",
"@types/node": "13.13.5",
"@types/node": "14.0.4",
"@types/rimraf": "3.0.0",
"@types/sinon": "9.0.0",
"@types/sinon": "9.0.3",
"@types/string-template": "1.0.2",
"@types/webpack-env": "1.15.2",
"@typescript-eslint/eslint-plugin": "2.31.0",
"@typescript-eslint/parser": "2.31.0",
"@typescript-eslint/eslint-plugin": "2.34.0",
"@typescript-eslint/parser": "2.34.0",
"chai": "4.2.0",
"coveralls": "3.1.0",
"eslint": "7.0.0",
"eslint-plugin-import": "2.20.2",
"eslint-plugin-jsdoc": "25.0.1",
"eslint-plugin-jsdoc": "25.4.2",
"eslint-plugin-no-null": "1.0.2",
"eslint-plugin-prefer-arrow": "1.2.1",
"eslint-plugin-unicorn": "19.0.1",
"eslint-plugin-unicorn": "20.0.0",
"fork-ts-checker-notifier-webpack-plugin": "2.0.0",
"fork-ts-checker-webpack-plugin": "4.1.3",
"fork-ts-checker-webpack-plugin": "4.1.4",
"mocha": "7.1.2",
"nyc": "15.0.1",
"pjson": "1.0.9",
"pre-commit": "1.2.2",
"rimraf": "3.0.2",
"sinon": "9.0.2",
"threads": "1.4.1",
"ts-loader": "7.0.3",
"ts-loader": "7.0.4",
"ts-node": "6.1.0",
"typescript": "3.8.3",
"typescript": "3.9.3",
"webpack": "4.43.0",
"webpack-cli": "3.3.11",
"webpack-node-externals": "1.7.2"
Expand Down Expand Up @@ -123,6 +123,10 @@
"Dmitry Zamotkin (https://github.com/zamotkin)"
],
"license": "BSD-2-Clause",
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/javascript-obfuscator"
},
"collective": {
"url": "https://opencollective.com/javascript-obfuscator"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ export class FunctionExpressionCalleeDataExtractor extends AbstractCalleeDataExt
* @param {Identifier} callee
* @returns {ICalleeData}
*/
public extract (blockScopeBody: ESTree.Node[], callee: ESTree.Identifier): ICalleeData | null {
public extract (blockScopeBody: ESTree.Node[], callee: ESTree.Identifier | ESTree.FunctionExpression): ICalleeData | null {
let calleeName: string | null = null;
let calleeBlockStatement: ESTree.BlockStatement | null = null;

if (NodeGuards.isIdentifierNode(callee)) {
calleeName = callee.name;
calleeBlockStatement = this.getCalleeBlockStatement(
NodeStatementUtils.getParentNodeWithStatements(blockScopeBody[0]),
callee.name
);
}

if (NodeGuards.isFunctionExpressionNode(callee)) {
} else if (NodeGuards.isFunctionExpressionNode(callee)) {
calleeName = null;
calleeBlockStatement = callee.body;
}

Expand All @@ -36,7 +37,7 @@ export class FunctionExpressionCalleeDataExtractor extends AbstractCalleeDataExt

return {
callee: calleeBlockStatement,
name: callee.name ?? null
name: calleeName
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ export class BlockStatementDeadCodeInjectionNode extends AbstractCustomNode {
this.deadCodeInjectionRootAstHostNode = deadCodeInjectionRootAstHostNode;
}

/**
* Have to override parent method to prevent a change of kinds of variables
*
* @returns {TStatement[]}
*/
public getNode (): TStatement[] {
return this.getNodeStructure();
}

/**
* @returns {TStatement[]}
*/
Expand Down
8 changes: 4 additions & 4 deletions test/functional-tests/cli/JavaScriptObfuscatorCLI.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe('JavaScriptObfuscatorCLI', function (): void {
const outputFileName1: string = 'foo-obfuscated.js';
const outputFileName2: string = 'bar-obfuscated.js';
const outputFileName3: string = 'baz-obfuscated.js';
const readFileEncoding: string = 'utf8';
const readFileEncoding = 'utf8';
const regExp1: RegExp = /^var a1_0x(\w){4,6} *= *0x1;$/;
const regExp2: RegExp = /^var a0_0x(\w){4,6} *= *0x2;$/;

Expand Down Expand Up @@ -249,7 +249,7 @@ describe('JavaScriptObfuscatorCLI', function (): void {
const identifiersPrefix: string = 'foo';
const outputFileName1: string = 'foo-obfuscated.js';
const outputFileName2: string = 'bar-obfuscated.js';
const readFileEncoding: string = 'utf8';
const readFileEncoding = 'utf8';
const regExp1: RegExp = /^var foo1_0x(\w){4,6} *= *0x1;$/;
const regExp2: RegExp = /^var foo0_0x(\w){4,6} *= *0x2;$/;

Expand Down Expand Up @@ -371,7 +371,7 @@ describe('JavaScriptObfuscatorCLI', function (): void {
const outputFileName1: string = 'foo-obfuscated.js';
const outputFileName2: string = 'bar-obfuscated.js';
const outputFileName3: string = 'baz-obfuscated.js';
const readFileEncoding: string = 'utf8';
const readFileEncoding = 'utf8';
const regExp1: RegExp = /^var a1_0x(\w){4,6} *= *0x1;$/;
const regExp2: RegExp = /^var a0_0x(\w){4,6} *= *0x2;$/;

Expand Down Expand Up @@ -438,7 +438,7 @@ describe('JavaScriptObfuscatorCLI', function (): void {
const outputFileName1: string = 'foo-obfuscated.js';
const outputFileName2: string = 'bar-obfuscated.js';
const outputFileName3: string = 'baz-obfuscated.js';
const readFileEncoding: string = 'utf8';
const readFileEncoding = 'utf8';
const regExp1: RegExp = /^var a0_0x(\w){4,6} *= *0x2;$/;

let outputFixturesFilePath1: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -787,5 +787,60 @@ describe('DeadCodeInjectionTransformer', () => {
});
});
});

describe('Variant #13 - prevailing kind of variables of inserted code', () => {
describe('Variant #1: base', () => {
const variableDeclarationsRegExp: RegExp = new RegExp(
`const ${variableMatch} *= *\\[\\]; *` +
`var ${variableMatch} *= *\\[\\]; *`,
'g'
);
const invalidVariableDeclarationsRegExp: RegExp = new RegExp(
`var ${variableMatch} *= *\\[\\]; *` +
`var ${variableMatch} *= *\\[\\]; *`,
'g'
);

const forLoopRegExp: RegExp = new RegExp(
`for *\\(const ${variableMatch} of ${variableMatch}\\) *{`,
'g'
);
const invalidForLoopRegExp: RegExp = new RegExp(
`for *\\(var ${variableMatch} of ${variableMatch}\\) *{`,
'g'
);

let obfuscatedCode: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevailing-kind-of-variables-1.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1
}
).getObfuscatedCode();
});

it('Match #1: shouldn\'t replace kinds of variables of inserted original code', () => {
assert.match(obfuscatedCode, variableDeclarationsRegExp);
});

it('Match #2: shouldn\'t replace kinds of variables of inserted original code', () => {
assert.notMatch(obfuscatedCode, invalidVariableDeclarationsRegExp);
});

it('Match #3: shouldn\'t replace kinds of variables of inserted original code', () => {
assert.match(obfuscatedCode, forLoopRegExp);
});

it('Match #4: shouldn\'t replace kinds of variables of inserted original code', () => {
assert.notMatch(obfuscatedCode, invalidForLoopRegExp);
});
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function foo () {
function bar () {
const a = [];
var b = [];
while (true) {
for (const a of b) {}
}
return a;
}

function baz () {
var a = 1;
}

function bark () {
var a = 1;
}

function hawk () {
var a = 1;
}
}
4 changes: 2 additions & 2 deletions test/runtime-tests/JavaScriptObfuscatorRuntime.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,8 @@ describe('JavaScriptObfuscator runtime eval', function () {

evaluationResult = result;
})
.catch((error) => {
evaluationResult = error.message;
.catch((error: Error) => {
evaluationResult = `${error.message}. ${error.stack}`;
});
});

Expand Down
Loading