Skip to content

Commit 6eeb1cb

Browse files
authored
Merge pull request javascript-obfuscator#311 from javascript-obfuscator/function-obfuscation-bugs
Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/…
2 parents 985d5e9 + 2501f82 commit 6eeb1cb

File tree

8 files changed

+85
-15
lines changed

8 files changed

+85
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Change Log
33
v0.18.0
44
---
55
* **New option:** `reservedStrings` disables transformation of string literals, which being matched by passed RegExp patterns
6+
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/309
67
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/307
78

89
v0.17.3

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.

src/node-transformers/obfuscating-transformers/FunctionTransformer.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,14 @@ export class FunctionTransformer extends AbstractNodeTransformer {
160160
const replaceVisitor: estraverse.Visitor = {
161161
enter: (node: ESTree.Node, parentNode: ESTree.Node | null): void | estraverse.VisitorOption => {
162162
/**
163-
* Should to process nested functions in different traverse loop to avoid wrong code generation
163+
* Should skip function node itself
164+
*/
165+
if (node === functionNode) {
166+
return;
167+
}
168+
169+
/**
170+
* Should process nested functions in different traverse loop to avoid wrong code generation
164171
*/
165172
if (NodeGuards.isFunctionNode(node)) {
166173
this.replaceFunctionParams(node, lexicalScopeNode, new Set(ignoredIdentifierNamesSet));
@@ -194,8 +201,6 @@ export class FunctionTransformer extends AbstractNodeTransformer {
194201
}
195202
};
196203

197-
functionNode.params.forEach((paramsNode: ESTree.Node) => estraverse.replace(paramsNode, replaceVisitor));
198-
199-
estraverse.replace(functionNode.body, replaceVisitor);
204+
estraverse.replace(functionNode, replaceVisitor)
200205
}
201206
}

test/dev/dev.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
66

77
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
88
`
9-
(function(){
10-
function foo ({id}) {
11-
function bar ({id: baz}) {
12-
return id !== baz;
13-
}
9+
(function(foo){
10+
function foo () {
11+
1412
}
13+
14+
return new foo();
1515
})();
1616
`,
1717
{

test/functional-tests/node-transformers/obfuscating-transformers/function-transformer/FunctionTransformer.spec.ts

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,63 @@ describe('FunctionTransformer', () => {
4545
});
4646
});
4747

48+
describe('function id name obfuscation', () => {
49+
const functionExpressionParamIdentifierRegExp: RegExp = /\(function *\((_0x[a-f0-9]{4,6})\) *\{/;
50+
const functionParamIdentifierRegExp: RegExp = /function *(_0x[a-f0-9]{4,6}) *\(\) *\{/;
51+
const functionObjectIdentifierRegExp: RegExp = /return new (_0x[a-f0-9]{4,6}) *\(\);/;
52+
53+
let obfuscatedCode: string,
54+
functionExpressionParamIdentifierName: string,
55+
functionParamIdentifierName: string,
56+
functionObjectIdentifierName: string;
57+
58+
before(() => {
59+
const code: string = readFileAsString(__dirname + '/fixtures/function-id-name.js');
60+
61+
obfuscatedCode = JavaScriptObfuscator.obfuscate(
62+
code,
63+
{
64+
...NO_ADDITIONAL_NODES_PRESET
65+
}
66+
).getObfuscatedCode();
67+
68+
const functionExpressionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
69+
.match(functionExpressionParamIdentifierRegExp);
70+
const functionParamIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
71+
.match(functionParamIdentifierRegExp);
72+
const functionObjectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
73+
.match(functionObjectIdentifierRegExp);
74+
75+
functionParamIdentifierName = (<RegExpMatchArray>functionParamIdentifierMatch)[1];
76+
functionExpressionParamIdentifierName = (<RegExpMatchArray>functionExpressionParamIdentifierMatch)[1];
77+
functionObjectIdentifierName = (<RegExpMatchArray>functionObjectIdentifierMatch)[1];
78+
});
79+
80+
it('should correctly transform function expression parameter identifier', () => {
81+
assert.match(obfuscatedCode, functionExpressionParamIdentifierRegExp);
82+
});
83+
84+
it('should correctly transform function parameter identifier', () => {
85+
assert.match(obfuscatedCode, functionParamIdentifierRegExp);
86+
});
87+
88+
it('should correctly transform function object parameter identifier', () => {
89+
assert.match(obfuscatedCode, functionObjectIdentifierRegExp);
90+
});
91+
92+
it('should generate same names for function parameter and function object identifiers', () => {
93+
assert.equal(functionParamIdentifierName, functionObjectIdentifierName);
94+
});
95+
96+
it('should generate same names for function parameter identifiers', () => {
97+
assert.equal(functionExpressionParamIdentifierName, functionParamIdentifierName);
98+
});
99+
100+
it('should generate same names for function expression parameter and function object identifiers', () => {
101+
assert.equal(functionExpressionParamIdentifierName, functionObjectIdentifierName);
102+
});
103+
});
104+
48105
describe('object pattern as parameter', () => {
49106
describe('Variant #1: simple', () => {
50107
const functionParameterRegExp: RegExp = /function *\(\{ *bar *\}\) *\{/;
@@ -299,15 +356,15 @@ describe('FunctionTransformer', () => {
299356
assert.match(obfuscatedCode, functionBodyRegExp);
300357
});
301358

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

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

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

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(function(foo){
2+
function foo () {
3+
4+
}
5+
6+
return new foo();
7+
})();

0 commit comments

Comments
 (0)