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.14.2
---
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/181

v0.14.1
---
* Temporary fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/181
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "0.14.1",
"version": "0.14.2",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down Expand Up @@ -47,7 +47,7 @@
"@types/md5": "2.1.32",
"@types/mkdirp": "0.5.2",
"@types/mocha": "2.2.48",
"@types/node": "9.4.5",
"@types/node": "9.4.6",
"@types/rimraf": "2.0.2",
"@types/sinon": "4.1.3",
"@types/string-template": "1.0.2",
Expand All @@ -60,17 +60,17 @@
"chai": "4.1.2",
"coveralls": "3.0.0",
"istanbul": "1.1.0-alpha.1",
"mocha": "5.0.0",
"mocha": "5.0.1",
"pre-commit": "1.2.2",
"rimraf": "2.6.2",
"sinon": "4.3.0",
"threads": "0.10.1",
"ts-node": "4.1.0",
"tslint": "5.9.1",
"tslint-eslint-rules": "4.1.1",
"tslint-eslint-rules": "5.0.0",
"tslint-language-service": "0.9.8",
"tslint-webpack-plugin": "1.1.1",
"typescript": "2.7.1",
"typescript": "2.7.2",
"webpack": "3.11.0",
"webpack-node-externals": "1.6.0"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ServiceIdentifiers } from '../../container/ServiceIdentifiers';
import * as estraverse from 'estraverse';
import * as ESTree from 'estree';

import { TNodeWithScope } from '../../types/node/TNodeWithScope';
import { TStatement } from '../../types/node/TStatement';

import { IOptions } from '../../interfaces/options/IOptions';
Expand Down Expand Up @@ -51,13 +52,11 @@ export class TemplateLiteralTransformer extends AbstractNodeTransformer {
case TransformationStage.Converting:
return {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
if (parentNode
&& NodeGuards.isExpressionStatementNode(node)
&& NodeGuards.isTemplateLiteralNode(node.expression)
) {
return this.fixEsprimaReturnStatementTemplateLiteralNode(node, node.expression);
if (parentNode && NodeGuards.isReturnStatementNode(node) && node.argument === null) {
return this.fixEsprimaReturnStatementTemplateLiteralNode(node);
}

},
leave: (node: ESTree.Node, parentNode: ESTree.Node | null) => {
if (parentNode && NodeGuards.isTemplateLiteralNode(node)) {
return this.transformNode(node, parentNode);
}
Expand All @@ -77,7 +76,7 @@ export class TemplateLiteralTransformer extends AbstractNodeTransformer {
public transformNode (templateLiteralNode: ESTree.TemplateLiteral, parentNode: ESTree.Node): ESTree.Node {
const templateLiteralExpressions: ESTree.Expression[] = templateLiteralNode.expressions;

let nodes: (ESTree.Literal | ESTree.Expression)[] = [];
let nodes: ESTree.Expression[] = [];

templateLiteralNode.quasis.forEach((templateElement: ESTree.TemplateElement) => {
nodes.push(Nodes.getLiteralNode(templateElement.value.cooked));
Expand Down Expand Up @@ -122,33 +121,54 @@ export class TemplateLiteralTransformer extends AbstractNodeTransformer {
}

/**
* @param {ExpressionStatement} expressionStatementNode
* @param {TemplateLiteral} templateLiteralNode
* @param {ReturnStatement} returnStatementNode
* @returns {Node | VisitorOption}
*/
private fixEsprimaReturnStatementTemplateLiteralNode (
expressionStatementNode: ESTree.ExpressionStatement,
templateLiteralNode: ESTree.TemplateLiteral
): ESTree.Node | estraverse.VisitorOption {
const previousSiblingStatementNode: TStatement | null = NodeUtils
.getPreviousSiblingStatementNode(expressionStatementNode);
private fixEsprimaReturnStatementTemplateLiteralNode (returnStatementNode: ESTree.ReturnStatement): ESTree.Node | void {
const scopeNode: TNodeWithScope = NodeUtils.getScopeOfNode(returnStatementNode);
const scopeBody: TStatement[] = !NodeGuards.isSwitchCaseNode(scopeNode)
? scopeNode.body
: scopeNode.consequent;
const indexInScope: number = scopeBody.indexOf(returnStatementNode);

// in incorrect AST-tree return statement node should be penultimate
if (indexInScope !== scopeBody.length - 2) {
return;
}

if (
!previousSiblingStatementNode
|| !templateLiteralNode.parentNode
|| !NodeGuards.isReturnStatementNode(previousSiblingStatementNode)
|| previousSiblingStatementNode.argument !== null
) {
return expressionStatementNode;
const nextSiblingStatementNode: TStatement | null = scopeBody[indexInScope + 1];

if (!nextSiblingStatementNode || !NodeGuards.isExpressionStatementNode(nextSiblingStatementNode)) {
return;
}

previousSiblingStatementNode.argument = <ESTree.Expression>this.transformNode(
templateLiteralNode,
templateLiteralNode.parentNode
);
let isSiblingStatementHasTemplateLiteralNode: boolean = false;

estraverse.traverse(nextSiblingStatementNode, {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void | estraverse.VisitorOption => {
if (!NodeGuards.isTemplateLiteralNode(node)) {
return;
}

isSiblingStatementHasTemplateLiteralNode = true;

NodeUtils.parentizeNode(templateLiteralNode, previousSiblingStatementNode);
return estraverse.VisitorOption.Break;
}
});

if (!isSiblingStatementHasTemplateLiteralNode) {
return;
}

returnStatementNode.argument = nextSiblingStatementNode.expression;
scopeBody.pop();

if (!NodeGuards.isSwitchCaseNode(scopeNode)) {
scopeNode.body = [...scopeBody];
} else {
scopeNode.consequent = <ESTree.Statement[]>[...scopeBody];
}

return estraverse.VisitorOption.Remove;
return returnStatementNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,32 @@ describe('TemplateLiteralTransformer', () => {

assert.match(obfuscationResult.getObfuscatedCode(), /case *!!\[] *: *return *'foo\\x0abar'; *} *}$/);
});

it('variant #4: should transform es6 multiline template literal inside binary expression inside return statement', () => {
const code: string = readFileAsString(__dirname + '/fixtures/multiline-template-literal-binary-expression-return-statement-1.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
unicodeEscapeSequence: false
}
);

assert.match(obfuscationResult.getObfuscatedCode(), /{ *return *'foo\\x0abar' *\+ *0x1; *}$/);
});

it('variant #5: should transform es6 multiline template literal inside binary expression inside return statement', () => {
const code: string = readFileAsString(__dirname + '/fixtures/multiline-template-literal-binary-expression-return-statement-2.js');
const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
unicodeEscapeSequence: false
}
);

assert.match(obfuscationResult.getObfuscatedCode(), /case *!!\[] *: *return *'foo\\x0abar' *\+ *0x1; *} *}$/);
});
});

describe('variant #3: simple template literal with expression only', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function foo() {
return `foo
bar` + 1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function hi() {
switch (true) {
case true:
return `foo
bar` + 1;
}
}
42 changes: 22 additions & 20 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@
version "8.0.53"
resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.53.tgz#396b35af826fa66aad472c8cb7b8d5e277f4e6d8"

"@types/[email protected].5":
version "9.4.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.5.tgz#d2a90c634208173d1b1a0a6ba9f1df3de62edcf5"
"@types/[email protected].6":
version "9.4.6"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.6.tgz#d8176d864ee48753d053783e4e463aec86b8d82e"

"@types/[email protected]":
version "2.0.2"
Expand Down Expand Up @@ -1338,7 +1338,7 @@ diffie-hellman@^5.0.0:
miller-rabin "^4.0.0"
randombytes "^2.0.0"

doctrine@^0.7.2:
[email protected]:
version "0.7.2"
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-0.7.2.tgz#7cb860359ba3be90e040b26b729ce4bfa654c523"
dependencies:
Expand Down Expand Up @@ -2587,9 +2587,9 @@ [email protected], [email protected], "mkdirp@>=0.5 0", mkdirp@^0.5.1, mkdirp@~0.5.0:
dependencies:
minimist "0.0.8"

[email protected].0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-5.0.0.tgz#cccac988b0bc5477119cba0e43de7af6d6ad8f4e"
[email protected].1:
version "5.0.1"
resolved "https://registry.yarnpkg.com/mocha/-/mocha-5.0.1.tgz#759b62c836b0732382a62b6b1fb245ec1bc943ac"
dependencies:
browser-stdout "1.3.0"
commander "2.11.0"
Expand Down Expand Up @@ -3740,17 +3740,17 @@ [email protected]:
version "1.9.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.9.0.tgz#e37a86fda8cbbaf23a057f473c9f4dc64e5fc2e8"

tslib@^1.0.0, tslib@^1.7.1, tslib@^1.8.0:
tslib@^1.7.1, tslib@^1.8.0:
version "1.8.1"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.8.1.tgz#6946af2d1d651a7b1863b531d6e5afa41aa44eac"

tslint-eslint-rules@4.1.1:
version "4.1.1"
resolved "https://registry.yarnpkg.com/tslint-eslint-rules/-/tslint-eslint-rules-4.1.1.tgz#7c30e7882f26bc276bff91d2384975c69daf88ba"
tslint-eslint-rules@5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/tslint-eslint-rules/-/tslint-eslint-rules-5.0.0.tgz#ba46e32168137b7b6f73c2121a29d57abea032a6"
dependencies:
doctrine "^0.7.2"
tslib "^1.0.0"
tsutils "^1.4.0"
doctrine "0.7.2"
tslib "1.9.0"
tsutils "2.8.0"

[email protected]:
version "0.9.8"
Expand Down Expand Up @@ -3779,9 +3779,11 @@ [email protected]:
tslib "^1.8.0"
tsutils "^2.12.1"

tsutils@^1.4.0:
version "1.9.1"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-1.9.1.tgz#b9f9ab44e55af9681831d5f28d0aeeaf5c750cb0"
[email protected]:
version "2.8.0"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-2.8.0.tgz#0160173729b3bf138628dd14a1537e00851d814a"
dependencies:
tslib "^1.7.1"

tsutils@^2.12.1:
version "2.12.2"
Expand Down Expand Up @@ -3821,9 +3823,9 @@ typedarray@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"

[email protected].1:
version "2.7.1"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.7.1.tgz#bb3682c2c791ac90e7c6210b26478a8da085c359"
[email protected].2:
version "2.7.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.7.2.tgz#2d615a1ef4aee4f574425cdff7026edf81919836"

uglify-js@^2.6, uglify-js@^2.8.29:
version "2.8.29"
Expand Down