Skip to content

Commit

Permalink
Merge pull request javascript-obfuscator#537 from javascript-obfuscat…
Browse files Browse the repository at this point in the history
…or/issue-535

Fixed runtime error with class expressions
  • Loading branch information
sanex3339 authored Jan 29, 2020
2 parents d60ec7a + fcbe2ee commit b575fb3
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 8 deletions.
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.24.3
---
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/535

v0.24.2
---
* Reverted validation errors under `node` target for `sourceMap*` options
Expand Down
6 changes: 3 additions & 3 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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "0.24.2",
"version": "0.24.3",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down
9 changes: 7 additions & 2 deletions src/analyzers/scope-analyzer/ScopeAnalyzer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,15 @@ export class ScopeAnalyzer implements IScopeAnalyzer {
// fix of class scopes
// trying to move class scope references to the parent scope
if (childScope.type === 'class' && childScope.upper) {
if (!childScope.variables.length) {
return;
}

// class name variable is always first
const classNameVariable: eslintScope.Variable = childScope.variables[0];

const upperVariable: eslintScope.Variable | undefined = childScope.upper.variables
.find((variable: eslintScope.Variable) => {
// class name variable is always first
const classNameVariable: eslintScope.Variable = childScope.variables[0];
const isValidClassNameVariable: boolean = classNameVariable.defs
.some((definition: eslintScope.Definition) => definition.type === 'ClassName');

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { assert } from 'chai';

import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../../src/options/presets/NoCustomNodes';

import { getRegExpMatch } from '../../../../../helpers/getRegExpMatch';
import { readFileAsString } from '../../../../../helpers/readFileAsString';

import { JavaScriptObfuscator } from '../../../../../../src/JavaScriptObfuscatorFacade';

describe('ScopeIdentifiersTransformer ClassExpression identifiers', () => {
describe('transformation of ClassExpression identifiers', () => {
describe('Variant #1: `ClassExpression` parent block scope is not a `ProgramNode`', () => {
const classNameIdentifierRegExp: RegExp = /var (_0x[a-f0-9]{4,6}) *= *class *\{/;
const classCallIdentifierRegExp: RegExp = /new *(_0x[a-f0-9]{4,6}) *\( *\);/;

let obfuscatedCode: string,
classNameIdentifier: string,
classCallIdentifier: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/base.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET
}
).getObfuscatedCode();
classNameIdentifier = getRegExpMatch(obfuscatedCode, classNameIdentifierRegExp);
classCallIdentifier = getRegExpMatch(obfuscatedCode, classCallIdentifierRegExp);
});

it('should transform class variable name', () => {
assert.equal(classNameIdentifier, classCallIdentifier);
});
});

describe('Variant #2: `ClassExpression` parent block scope is a `ProgramNode`', () => {
const classNameIdentifierRegExp: RegExp = /var Foo *= *class *\{/;
const classCallIdentifierRegExp: RegExp = /new *Foo *\( *\);/;

let obfuscatedCode: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/parent-block-scope-is-program-node.js');

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

it('match #1: shouldn\'t transform class name', () => {
assert.match(obfuscatedCode, classNameIdentifierRegExp);
});

it('match #2: shouldn\'t transform class name', () => {
assert.match(obfuscatedCode, classCallIdentifierRegExp);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(function () {
var Foo = class {
getInstance () {
return new Foo();
}
};

new Foo();
})();
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var Foo = class {
getInstance () {
return new Foo();
}
};

new Foo();
1 change: 1 addition & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import './functional-tests/node-transformers/obfuscating-transformers/labeled-st
import './functional-tests/node-transformers/obfuscating-transformers/literal-transformer/LiteralTransformer.spec';
import './functional-tests/node-transformers/obfuscating-transformers/scope-identifiers-transformer/catch-clause/CatchClause.spec';
import './functional-tests/node-transformers/obfuscating-transformers/scope-identifiers-transformer/class-declaration/ClassDeclaration.spec';
import './functional-tests/node-transformers/obfuscating-transformers/scope-identifiers-transformer/class-expression/ClassExpression.spec';
import './functional-tests/node-transformers/obfuscating-transformers/scope-identifiers-transformer/function-declaration/FunctionDeclaration.spec';
import './functional-tests/node-transformers/obfuscating-transformers/scope-identifiers-transformer/function/Function.spec';
import './functional-tests/node-transformers/obfuscating-transformers/scope-identifiers-transformer/import-declaration/ImportDeclaration.spec';
Expand Down

0 comments on commit b575fb3

Please sign in to comment.