Skip to content

Commit

Permalink
Fixed #309
Browse files Browse the repository at this point in the history
  • Loading branch information
sanex3339 committed Aug 8, 2018
1 parent df4ac27 commit b4ef1bf
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Change Log
v0.18.0
---
* **New option:** `reservedStrings` disables transformation of string literals, which being matched by passed RegExp patterns
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/309
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/307

v0.17.3
Expand Down
2 changes: 1 addition & 1 deletion 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.

Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,14 @@ export class FunctionTransformer extends AbstractNodeTransformer {
const replaceVisitor: estraverse.Visitor = {
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void | estraverse.VisitorOption => {
/**
* Should to process nested functions in different traverse loop to avoid wrong code generation
* Should skip function node itself
*/
if (node === functionNode) {
return;
}

/**
* Should process nested functions in different traverse loop to avoid wrong code generation
*/
if (NodeGuards.isFunctionNode(node)) {
this.replaceFunctionParams(node, lexicalScopeNode, new Set(ignoredIdentifierNamesSet));
Expand Down Expand Up @@ -194,8 +201,6 @@ export class FunctionTransformer extends AbstractNodeTransformer {
}
};

functionNode.params.forEach((paramsNode: ESTree.Node) => estraverse.replace(paramsNode, replaceVisitor));

estraverse.replace(functionNode.body, replaceVisitor);
estraverse.replace(functionNode, replaceVisitor)
}
}
10 changes: 5 additions & 5 deletions test/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo

let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
(function(){
function foo ({id}) {
function bar ({id: baz}) {
return id !== baz;
}
(function(foo){
function foo () {
}
return new foo();
})();
`,
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,63 @@ describe('FunctionTransformer', () => {
});
});

describe('function id name obfuscation', () => {
const functionExpressionParamIdentifierRegExp: RegExp = /\(function *\((_0x[a-f0-9]{4,6})\) *\{/;
const functionParamIdentifierRegExp: RegExp = /function *(_0x[a-f0-9]{4,6}) *\(\) *\{/;
const functionObjectIdentifierRegExp: RegExp = /return new (_0x[a-f0-9]{4,6}) *\(\);/;

let obfuscatedCode: string,
functionExpressionParamIdentifierName: string,
functionParamIdentifierName: string,
functionObjectIdentifierName: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/function-id-name.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET
}
).getObfuscatedCode();

const functionExpressionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
.match(functionExpressionParamIdentifierRegExp);
const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
.match(functionParamIdentifierRegExp);
const functionObjectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
.match(functionObjectIdentifierRegExp);

functionParamIdentifierName = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
functionExpressionParamIdentifierName = (<RegExpMatchArray>functionExpressionParamIdentifierMatch)[1];
functionObjectIdentifierName = (<RegExpMatchArray>functionObjectIdentifierMatch)[1];
});

it('should correctly transform function expression parameter identifier', () => {
assert.match(obfuscatedCode, functionExpressionParamIdentifierRegExp);
});

it('should correctly transform function parameter identifier', () => {
assert.match(obfuscatedCode, functionParamIdentifierRegExp);
});

it('should correctly transform function object parameter identifier', () => {
assert.match(obfuscatedCode, functionObjectIdentifierRegExp);
});

it('should generate same names for function parameter and function object identifiers', () => {
assert.equal(functionParamIdentifierName, functionObjectIdentifierName);
});

it('should generate same names for function parameter identifiers', () => {
assert.equal(functionExpressionParamIdentifierName, functionParamIdentifierName);
});

it('should generate same names for function expression parameter and function object identifiers', () => {
assert.equal(functionExpressionParamIdentifierName, functionObjectIdentifierName);
});
});

describe('object pattern as parameter', () => {
describe('Variant #1: simple', () => {
const functionParameterRegExp: RegExp = /function *\(\{ *bar *\}\) *\{/;
Expand Down Expand Up @@ -299,15 +356,15 @@ describe('FunctionTransformer', () => {
assert.match(obfuscatedCode, functionBodyRegExp);
});

it('equal #1: shouldn\'t keep same names variable declaration identifier and function parameters identifiers', () => {
it('equal #1: shouldn\'t keep same names for variable declaration identifier and function parameters identifiers', () => {
assert.notEqual(variableDeclarationIdentifierName, functionParameterIdentifierName);
});

it('equal #2: shouldn\'t keep same names variable declaration identifier and function parameters identifiers', () => {
it('equal #2: shouldn\'t keep same names for variable declaration identifier and function parameters identifiers', () => {
assert.notEqual(variableDeclarationIdentifierName, functionDefaultParameterIdentifierName1);
});

it('equal #3: shouldn\'t keep same names variable declaration identifier and function parameters identifiers', () => {
it('equal #3: shouldn\'t keep same names for variable declaration identifier and function parameters identifiers', () => {
assert.notEqual(variableDeclarationIdentifierName, functionDefaultParameterIdentifierName2);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(function(foo){
function foo () {

}

return new foo();
})();

0 comments on commit b4ef1bf

Please sign in to comment.