浏览代码

0.18.1 version

sanex3339 6 年之前
父节点
当前提交
adb5f4ee25

+ 4 - 0
CHANGELOG.md

@@ -1,5 +1,9 @@
 Change Log
 ===
+v0.18.1
+---
+* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/317
+
 v0.18.0
 ---
 * **New option:** `reservedStrings` disables transformation of string literals, which being matched by passed RegExp patterns

文件差异内容过多而无法显示
+ 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.18.0",
+  "version": "0.18.1",
   "description": "JavaScript obfuscator",
   "keywords": [
     "obfuscator",

+ 27 - 0
src/node-transformers/obfuscating-transformers/FunctionTransformer.ts

@@ -19,6 +19,7 @@ import { AbstractNodeTransformer } from '../AbstractNodeTransformer';
 import { NodeGuards } from '../../node/NodeGuards';
 import { NodeLexicalScopeUtils } from '../../node/NodeLexicalScopeUtils';
 import { NodeMetadata } from '../../node/NodeMetadata';
+import { NodeType } from '../../enums/node/NodeType';
 
 /**
  * replaces:
@@ -118,6 +119,27 @@ export class FunctionTransformer extends AbstractNodeTransformer {
         return functionNode;
     }
 
+    /**
+     * @param {Identifier} node
+     * @param {Node} parentNode
+     * @returns {boolean}
+     */
+    private isGlobalFunctionDeclarationIdentifier (node: ESTree.Identifier, parentNode: ESTree.Node): boolean {
+        if (!NodeGuards.isFunctionDeclarationNode(parentNode) || parentNode.id !== node) {
+            return false
+        }
+
+        const lexicalScopeNode: TNodeWithLexicalScope | undefined = NodeLexicalScopeUtils.getLexicalScopes(parentNode)[1];
+
+        if (!lexicalScopeNode) {
+            return false;
+        }
+
+        const isGlobalDeclaration: boolean = lexicalScopeNode.type === NodeType.Program;
+
+        return !this.options.renameGlobals && isGlobalDeclaration;
+    }
+
     /**
      * @param {Function} functionNode
      * @param {TNodeWithLexicalScope} lexicalScopeNode
@@ -184,6 +206,11 @@ export class FunctionTransformer extends AbstractNodeTransformer {
                     && !NodeMetadata.isRenamedIdentifier(node)
                     && !ignoredIdentifierNamesSet.has(node.name)
                 ) {
+                    // should ignore identifiers of global function declarations
+                    if (this.isGlobalFunctionDeclarationIdentifier(node, parentNode)) {
+                        return;
+                    }
+
                     const newIdentifier: ESTree.Identifier = this.identifierObfuscatingReplacer
                         .replace(node.name, lexicalScopeNode);
                     const newIdentifierName: string = newIdentifier.name;

+ 3 - 7
test/dev/dev.ts

@@ -6,13 +6,9 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
 
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
-        (function(foo){
-            function foo () {
-        
-            }
-        
-            return new foo();
-        })();
+        function foo (foo) {}
+
+        new foo();
 
         `,
         {

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

@@ -140,7 +140,7 @@ describe('FunctionTransformer', () => {
                 assert.match(obfuscatedCode, functionObjectIdentifierRegExp);
             });
 
-            it('should generate same names for function parameter and function object identifiers', () => {
+            it('should generate same names for function id and function object identifiers', () => {
                 assert.equal(functionIdentifierName, functionObjectIdentifierName);
             });
 
@@ -148,6 +148,110 @@ describe('FunctionTransformer', () => {
                 assert.equal(functionIdentifierName, functionParamIdentifierName);
             });
         });
+
+        describe('Variant #3: global function declaration identifier', () => {
+            describe('Variant #1: `renameGlobals` option is disabled', () => {
+                const functionIdentifiersRegExp: RegExp = /function *(foo) *\((_0x[a-f0-9]{4,6})\) *\{/;
+                const functionObjectIdentifierRegExp: RegExp = /new (foo) *\(\);/;
+
+                let obfuscatedCode: string,
+                    functionIdentifierName: string,
+                    functionParamIdentifierName: string,
+                    functionObjectIdentifierName: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/function-id-name-3.js');
+
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            renameGlobals: false
+                        }
+                    ).getObfuscatedCode();
+
+                    const functionIdentifiersMatch: RegExpMatchArray|null = obfuscatedCode
+                        .match(functionIdentifiersRegExp);
+                    const functionObjectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
+                        .match(functionObjectIdentifierRegExp);
+
+                    functionIdentifierName = (<RegExpMatchArray>functionIdentifiersMatch)[1];
+                    functionParamIdentifierName = (<RegExpMatchArray>functionIdentifiersMatch)[2];
+                    functionObjectIdentifierName = (<RegExpMatchArray>functionObjectIdentifierMatch)[1];
+                });
+
+                it('should correctly transform function identifiers', () => {
+                    assert.match(obfuscatedCode, functionIdentifiersRegExp);
+                });
+
+                it('should correctly transform function object parameter identifier', () => {
+                    assert.match(obfuscatedCode, functionObjectIdentifierRegExp);
+                });
+
+                it('should generate same names for function id and function object identifiers', () => {
+                    assert.equal(functionIdentifierName, functionObjectIdentifierName);
+                });
+
+                it('should generate different names for function parameter and function object identifiers', () => {
+                    assert.notEqual(functionParamIdentifierName, functionObjectIdentifierName);
+                });
+
+                it('should generate different names for function id and parameter identifiers', () => {
+                    assert.notEqual(functionIdentifierName, functionParamIdentifierName);
+                });
+            });
+
+            describe('Variant #2: `renameGlobals` option is enabled', () => {
+                const functionIdentifiersRegExp: RegExp = /function *(_0x[a-f0-9]{4,6}) *\((_0x[a-f0-9]{4,6})\) *\{/;
+                const functionObjectIdentifierRegExp: RegExp = /new (_0x[a-f0-9]{4,6}) *\(\);/;
+
+                let obfuscatedCode: string,
+                    functionIdentifierName: string,
+                    functionParamIdentifierName: string,
+                    functionObjectIdentifierName: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/function-id-name-3.js');
+
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            renameGlobals: true
+                        }
+                    ).getObfuscatedCode();
+
+                    const functionIdentifiersMatch: RegExpMatchArray|null = obfuscatedCode
+                        .match(functionIdentifiersRegExp);
+                    const functionObjectIdentifierMatch: RegExpMatchArray|null = obfuscatedCode
+                        .match(functionObjectIdentifierRegExp);
+
+                    functionIdentifierName = (<RegExpMatchArray>functionIdentifiersMatch)[1];
+                    functionParamIdentifierName = (<RegExpMatchArray>functionIdentifiersMatch)[2];
+                    functionObjectIdentifierName = (<RegExpMatchArray>functionObjectIdentifierMatch)[1];
+                });
+
+                it('should correctly transform function identifiers', () => {
+                    assert.match(obfuscatedCode, functionIdentifiersRegExp);
+                });
+
+                it('should correctly transform function object parameter identifier', () => {
+                    assert.match(obfuscatedCode, functionObjectIdentifierRegExp);
+                });
+
+                it('should generate same names for function id and function object identifiers', () => {
+                    assert.equal(functionIdentifierName, functionObjectIdentifierName);
+                });
+
+                it('should generate same names for function parameter and function object identifiers', () => {
+                    assert.equal(functionParamIdentifierName, functionObjectIdentifierName);
+                });
+
+                it('should generate same names for function id and parameter identifiers', () => {
+                    assert.equal(functionIdentifierName, functionParamIdentifierName);
+                });
+            });
+        });
     });
 
     describe('object pattern as parameter', () => {

+ 3 - 0
test/functional-tests/node-transformers/obfuscating-transformers/function-transformer/fixtures/function-id-name-3.js

@@ -0,0 +1,3 @@
+function foo (foo) {}
+
+new foo();

部分文件因为文件数量过多而无法显示