Просмотр исходного кода

Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/535

sanex3339 5 лет назад
Родитель
Сommit
fcbe2ee66e

+ 4 - 0
CHANGELOG.md

@@ -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

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/index.browser.js


Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/index.cli.js


Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/index.js


+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "javascript-obfuscator",
-  "version": "0.24.2",
+  "version": "0.24.3",
   "description": "JavaScript obfuscator",
   "keywords": [
     "obfuscator",

+ 7 - 2
src/analyzers/scope-analyzer/ScopeAnalyzer.ts

@@ -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');
 

+ 64 - 0
test/functional-tests/node-transformers/obfuscating-transformers/scope-identifiers-transformer/class-expression/ClassExpression.spec.ts

@@ -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);
+            });
+        });
+    });
+});

+ 9 - 0
test/functional-tests/node-transformers/obfuscating-transformers/scope-identifiers-transformer/class-expression/fixtures/base.js

@@ -0,0 +1,9 @@
+(function () {
+    var Foo = class {
+        getInstance () {
+            return new Foo();
+        }
+    };
+
+    new Foo();
+})();

+ 7 - 0
test/functional-tests/node-transformers/obfuscating-transformers/scope-identifiers-transformer/class-expression/fixtures/parent-block-scope-is-program-node.js

@@ -0,0 +1,7 @@
+var Foo = class {
+    getInstance () {
+        return new Foo();
+    }
+};
+
+new Foo();

+ 1 - 0
test/index.spec.ts

@@ -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';

Некоторые файлы не были показаны из-за большого количества измененных файлов