Skip to content

Commit adb5f4e

Browse files
committed
0.18.1 version
1 parent 5c6f59e commit adb5f4e

File tree

9 files changed

+148
-14
lines changed

9 files changed

+148
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
Change Log
22
===
3+
v0.18.1
4+
---
5+
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/317
6+
37
v0.18.0
48
---
59
* **New option:** `reservedStrings` disables transformation of string literals, which being matched by passed RegExp patterns

dist/index.browser.js

Lines changed: 3 additions & 3 deletions
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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "javascript-obfuscator",
3-
"version": "0.18.0",
3+
"version": "0.18.1",
44
"description": "JavaScript obfuscator",
55
"keywords": [
66
"obfuscator",

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
1919
import { NodeGuards } from '../../node/NodeGuards';
2020
import { NodeLexicalScopeUtils } from '../../node/NodeLexicalScopeUtils';
2121
import { NodeMetadata } from '../../node/NodeMetadata';
22+
import { NodeType } from '../../enums/node/NodeType';
2223

2324
/**
2425
* replaces:
@@ -118,6 +119,27 @@ export class FunctionTransformer extends AbstractNodeTransformer {
118119
return functionNode;
119120
}
120121

122+
/**
123+
* @param {Identifier} node
124+
* @param {Node} parentNode
125+
* @returns {boolean}
126+
*/
127+
private isGlobalFunctionDeclarationIdentifier (node: ESTree.Identifier, parentNode: ESTree.Node): boolean {
128+
if (!NodeGuards.isFunctionDeclarationNode(parentNode) || parentNode.id !== node) {
129+
return false
130+
}
131+
132+
const lexicalScopeNode: TNodeWithLexicalScope | undefined = NodeLexicalScopeUtils.getLexicalScopes(parentNode)[1];
133+
134+
if (!lexicalScopeNode) {
135+
return false;
136+
}
137+
138+
const isGlobalDeclaration: boolean = lexicalScopeNode.type === NodeType.Program;
139+
140+
return !this.options.renameGlobals && isGlobalDeclaration;
141+
}
142+
121143
/**
122144
* @param {Function} functionNode
123145
* @param {TNodeWithLexicalScope} lexicalScopeNode
@@ -184,6 +206,11 @@ export class FunctionTransformer extends AbstractNodeTransformer {
184206
&& !NodeMetadata.isRenamedIdentifier(node)
185207
&& !ignoredIdentifierNamesSet.has(node.name)
186208
) {
209+
// should ignore identifiers of global function declarations
210+
if (this.isGlobalFunctionDeclarationIdentifier(node, parentNode)) {
211+
return;
212+
}
213+
187214
const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
188215
.replace(node.name, lexicalScopeNode);
189216
const newIdentifierName: string = newIdentifier.name;

test/dev/dev.ts

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

77
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
88
`
9-
(function(foo){
10-
function foo () {
11-
12-
}
13-
14-
return new foo();
15-
})();
9+
function foo (foo) {}
10+
11+
new foo();
1612
1713
`,
1814
{

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

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,118 @@ describe('FunctionTransformer', () => {
140140
assert.match(obfuscatedCode, functionObjectIdentifierRegExp);
141141
});
142142

143-
it('should generate same names for function parameter and function object identifiers', () => {
143+
it('should generate same names for function id and function object identifiers', () => {
144144
assert.equal(functionIdentifierName, functionObjectIdentifierName);
145145
});
146146

147147
it('should generate same names for function id and parameter identifiers', () => {
148148
assert.equal(functionIdentifierName, functionParamIdentifierName);
149149
});
150150
});
151+
152+
describe('Variant #3: global function declaration identifier', () => {
153+
describe('Variant #1: `renameGlobals` option is disabled', () => {
154+
const functionIdentifiersRegExp: RegExp = /function *(foo) *\((_0x[a-f0-9]{4,6})\) *\{/;
155+
const functionObjectIdentifierRegExp: RegExp = /new (foo) *\(\);/;
156+
157+
let obfuscatedCode: string,
158+
functionIdentifierName: string,
159+
functionParamIdentifierName: string,
160+
functionObjectIdentifierName: string;
161+
162+
before(() => {
163+
const code: string = readFileAsString(__dirname + '/fixtures/function-id-name-3.js');
164+
165+
obfuscatedCode = JavaScriptObfuscator.obfuscate(
166+
code,
167+
{
168+
...NO_ADDITIONAL_NODES_PRESET,
169+
renameGlobals: false
170+
}
171+
).getObfuscatedCode();
172+
173+
const functionIdentifiersMatch: RegExpMatchArray|null = obfuscatedCode
174+
.match(functionIdentifiersRegExp);
175+
const functionObjectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
176+
.match(functionObjectIdentifierRegExp);
177+
178+
functionIdentifierName = (<RegExpMatchArray>functionIdentifiersMatch)[1];
179+
functionParamIdentifierName = (<RegExpMatchArray>functionIdentifiersMatch)[2];
180+
functionObjectIdentifierName = (<RegExpMatchArray>functionObjectIdentifierMatch)[1];
181+
});
182+
183+
it('should correctly transform function identifiers', () => {
184+
assert.match(obfuscatedCode, functionIdentifiersRegExp);
185+
});
186+
187+
it('should correctly transform function object parameter identifier', () => {
188+
assert.match(obfuscatedCode, functionObjectIdentifierRegExp);
189+
});
190+
191+
it('should generate same names for function id and function object identifiers', () => {
192+
assert.equal(functionIdentifierName, functionObjectIdentifierName);
193+
});
194+
195+
it('should generate different names for function parameter and function object identifiers', () => {
196+
assert.notEqual(functionParamIdentifierName, functionObjectIdentifierName);
197+
});
198+
199+
it('should generate different names for function id and parameter identifiers', () => {
200+
assert.notEqual(functionIdentifierName, functionParamIdentifierName);
201+
});
202+
});
203+
204+
describe('Variant #2: `renameGlobals` option is enabled', () => {
205+
const functionIdentifiersRegExp: RegExp = /function *(_0x[a-f0-9]{4,6}) *\((_0x[a-f0-9]{4,6})\) *\{/;
206+
const functionObjectIdentifierRegExp: RegExp = /new (_0x[a-f0-9]{4,6}) *\(\);/;
207+
208+
let obfuscatedCode: string,
209+
functionIdentifierName: string,
210+
functionParamIdentifierName: string,
211+
functionObjectIdentifierName: string;
212+
213+
before(() => {
214+
const code: string = readFileAsString(__dirname + '/fixtures/function-id-name-3.js');
215+
216+
obfuscatedCode = JavaScriptObfuscator.obfuscate(
217+
code,
218+
{
219+
...NO_ADDITIONAL_NODES_PRESET,
220+
renameGlobals: true
221+
}
222+
).getObfuscatedCode();
223+
224+
const functionIdentifiersMatch: RegExpMatchArray|null = obfuscatedCode
225+
.match(functionIdentifiersRegExp);
226+
const functionObjectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
227+
.match(functionObjectIdentifierRegExp);
228+
229+
functionIdentifierName = (<RegExpMatchArray>functionIdentifiersMatch)[1];
230+
functionParamIdentifierName = (<RegExpMatchArray>functionIdentifiersMatch)[2];
231+
functionObjectIdentifierName = (<RegExpMatchArray>functionObjectIdentifierMatch)[1];
232+
});
233+
234+
it('should correctly transform function identifiers', () => {
235+
assert.match(obfuscatedCode, functionIdentifiersRegExp);
236+
});
237+
238+
it('should correctly transform function object parameter identifier', () => {
239+
assert.match(obfuscatedCode, functionObjectIdentifierRegExp);
240+
});
241+
242+
it('should generate same names for function id and function object identifiers', () => {
243+
assert.equal(functionIdentifierName, functionObjectIdentifierName);
244+
});
245+
246+
it('should generate same names for function parameter and function object identifiers', () => {
247+
assert.equal(functionParamIdentifierName, functionObjectIdentifierName);
248+
});
249+
250+
it('should generate same names for function id and parameter identifiers', () => {
251+
assert.equal(functionIdentifierName, functionParamIdentifierName);
252+
});
253+
});
254+
});
151255
});
152256

153257
describe('object pattern as parameter', () => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function foo (foo) {}
2+
3+
new foo();

0 commit comments

Comments
 (0)