浏览代码

Fixed rare bug with runtime error with `identifiersNameGenarator`: `mangled`

sanex3339 7 年之前
父节点
当前提交
6f480a6f6f

文件差异内容过多而无法显示
+ 0 - 0
dist/index.js


+ 5 - 5
package.json

@@ -22,7 +22,7 @@
     "@babel/runtime": "7.0.0-beta.42",
     "chalk": "2.3.2",
     "chance": "1.0.13",
-    "class-validator": "0.8.4",
+    "class-validator": "0.8.5",
     "commander": "2.15.1",
     "escodegen-wallaby": "1.6.18",
     "esprima": "4.0.0",
@@ -52,9 +52,9 @@
     "@types/estree": "0.0.38",
     "@types/md5": "2.1.32",
     "@types/mkdirp": "0.5.2",
-    "@types/mocha": "2.2.48",
+    "@types/mocha": "5.0.0",
     "@types/multimatch": "2.1.2",
-    "@types/node": "9.4.7",
+    "@types/node": "9.6.0",
     "@types/rimraf": "2.0.2",
     "@types/sinon": "4.3.0",
     "@types/string-template": "1.0.2",
@@ -64,7 +64,7 @@
     "chai": "4.1.2",
     "coveralls": "3.0.0",
     "istanbul": "1.1.0-alpha.1",
-    "mocha": "5.0.4",
+    "mocha": "5.0.5",
     "pre-commit": "1.2.2",
     "rimraf": "2.6.2",
     "sinon": "4.4.8",
@@ -76,7 +76,7 @@
     "tslint-webpack-plugin": "1.2.2",
     "typescript": "2.8.0-rc",
     "webpack": "4.2.0",
-    "webpack-cli": "2.0.12",
+    "webpack-cli": "2.0.13",
     "webpack-node-externals": "1.6.0"
   },
   "repository": {

+ 1 - 0
src/node-transformers/obfuscating-transformers/ClassDeclarationTransformer.ts

@@ -135,6 +135,7 @@ export class ClassDeclarationTransformer extends AbstractNodeTransformer {
                 .replace(replaceableIdentifier.name, nodeIdentifier);
 
             replaceableIdentifier.name = newReplaceableIdentifier.name;
+            NodeMetadata.set(replaceableIdentifier, { renamedIdentifier: true });
         });
     }
 

+ 1 - 0
src/node-transformers/obfuscating-transformers/FunctionDeclarationTransformer.ts

@@ -156,6 +156,7 @@ export class FunctionDeclarationTransformer extends AbstractNodeTransformer {
                 .replace(replaceableIdentifier.name, nodeIdentifier);
 
             replaceableIdentifier.name = newReplaceableIdentifier.name;
+            NodeMetadata.set(replaceableIdentifier, { renamedIdentifier: true });
         }
     }
 

+ 1 - 0
src/node-transformers/obfuscating-transformers/VariableDeclarationTransformer.ts

@@ -174,6 +174,7 @@ export class VariableDeclarationTransformer extends AbstractNodeTransformer {
                     .replace(replaceableIdentifier.name, nodeIdentifier);
 
                 replaceableIdentifier.name = newReplaceableIdentifier.name;
+                NodeMetadata.set(replaceableIdentifier, { renamedIdentifier: true });
             }
         });
     }

+ 6 - 2
test/dev/dev.ts

@@ -8,8 +8,12 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
         `
         (function() {
             function test() {
-                class inner {}
-                let a, b, c, d;
+                var variable = 1;
+                function g () {}
+        
+                var func = function () {
+                    var e;
+                }
             }
         })();
         `,

+ 21 - 19
test/functional-tests/node-transformers/obfuscating-transformers/function-declaration-transformer/FunctionDeclarationTransformer.spec.ts

@@ -151,30 +151,32 @@ describe('FunctionDeclarationTransformer', () => {
         });
 
         describe('Variant #5: already renamed identifiers shouldn\'t be renamed twice', () => {
-            const functionDeclarationRegExp: RegExp = /function *d\(\) *{/;
-            const variableDeclarationsRegExp: RegExp = /let *e, *f, *g, *h;/;
+            describe('Variant #1', () => {
+                const functionDeclarationRegExp: RegExp = /function *d\(\) *{/;
+                const variableDeclarationsRegExp: RegExp = /let *e, *f, *g, *h;/;
 
-            let obfuscatedCode: string;
+                let obfuscatedCode: string;
 
-            before(() => {
-                const code: string = readFileAsString(__dirname + '/fixtures/prevent-renaming-of-renamed-identifiers.js');
-                const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
-                    code,
-                    {
-                        ...NO_ADDITIONAL_NODES_PRESET,
-                        identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
-                    }
-                );
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/prevent-renaming-of-renamed-identifiers-1.js');
+                    const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
+                        }
+                    );
 
-                obfuscatedCode = obfuscationResult.getObfuscatedCode();
-            });
+                    obfuscatedCode = obfuscationResult.getObfuscatedCode();
+                });
 
-            it('Match #1: shouldn\'t rename twice function declaration name', () => {
-                assert.match(obfuscatedCode, functionDeclarationRegExp);
-            });
+                it('Match #1: shouldn\'t rename twice function declaration name', () => {
+                    assert.match(obfuscatedCode, functionDeclarationRegExp);
+                });
 
-            it('Match #2: should correctly rename variable declarations', () => {
-                assert.match(obfuscatedCode, variableDeclarationsRegExp);
+                it('Match #2: should correctly rename variable declarations', () => {
+                    assert.match(obfuscatedCode, variableDeclarationsRegExp);
+                });
             });
         });
     });

+ 0 - 0
test/functional-tests/node-transformers/obfuscating-transformers/function-declaration-transformer/fixtures/prevent-renaming-of-renamed-identifiers.js → test/functional-tests/node-transformers/obfuscating-transformers/function-declaration-transformer/fixtures/prevent-renaming-of-renamed-identifiers-1.js


+ 70 - 30
test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/VariableDeclarationTransformer.spec.ts

@@ -439,45 +439,85 @@ describe('VariableDeclarationTransformer', () => {
     });
 
     describe('Variant #13: already renamed identifiers shouldn\'t be renamed twice', () => {
-        const variableDeclarationRegExp: RegExp = /var *d *= *0x1;/;
-        const functionDeclarationRegExp1: RegExp = /function *e *\(\) *{}/;
-        const functionDeclarationRegExp2: RegExp = /function *f *\(\) *{}/;
-        const functionDeclarationRegExp3: RegExp = /function *g *\(\) *{}/;
-        const functionDeclarationRegExp4: RegExp = /function *h *\(\) *{}/;
+        describe('Variant #1', () => {
+            const variableDeclarationRegExp: RegExp = /var *d *= *0x1;/;
+            const functionDeclarationRegExp1: RegExp = /function *e *\(\) *{}/;
+            const functionDeclarationRegExp2: RegExp = /function *f *\(\) *{}/;
+            const functionDeclarationRegExp3: RegExp = /function *g *\(\) *{}/;
+            const functionDeclarationRegExp4: RegExp = /function *h *\(\) *{}/;
 
-        let obfuscatedCode: string;
+            let obfuscatedCode: string;
 
-        before(() => {
-            const code: string = readFileAsString(__dirname + '/fixtures/prevent-renaming-of-renamed-identifiers.js');
-            const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
-                code,
-                {
-                    ...NO_ADDITIONAL_NODES_PRESET,
-                    identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
-                }
-            );
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/prevent-renaming-of-renamed-identifiers-1.js');
+                const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
+                    }
+                );
 
-            obfuscatedCode = obfuscationResult.getObfuscatedCode();
-        });
+                obfuscatedCode = obfuscationResult.getObfuscatedCode();
+            });
 
-        it('Match #1: shouldn\'t rename twice variable declaration name', () => {
-            assert.match(obfuscatedCode, variableDeclarationRegExp);
-        });
+            it('Match #1: shouldn\'t rename twice variable declaration name', () => {
+                assert.match(obfuscatedCode, variableDeclarationRegExp);
+            });
 
-        it('Match #2: should correctly rename function declaration name', () => {
-            assert.match(obfuscatedCode, functionDeclarationRegExp1);
-        });
+            it('Match #2: should correctly rename function declaration name', () => {
+                assert.match(obfuscatedCode, functionDeclarationRegExp1);
+            });
 
-        it('Match #2: should correctly rename function declaration name', () => {
-            assert.match(obfuscatedCode, functionDeclarationRegExp2);
-        });
+            it('Match #3: should correctly rename function declaration name', () => {
+                assert.match(obfuscatedCode, functionDeclarationRegExp2);
+            });
+
+            it('Match #4: should correctly rename function declaration name', () => {
+                assert.match(obfuscatedCode, functionDeclarationRegExp3);
+            });
 
-        it('Match #2: should correctly rename function declaration name', () => {
-            assert.match(obfuscatedCode, functionDeclarationRegExp3);
+            it('Match #5: should correctly rename function declaration name', () => {
+                assert.match(obfuscatedCode, functionDeclarationRegExp4);
+            });
         });
 
-        it('Match #2: should correctly rename function declaration name', () => {
-            assert.match(obfuscatedCode, functionDeclarationRegExp4);
+        describe('Variant #2', () => {
+            const variableDeclarationRegExp1: RegExp = /var *d *= *0x1;/;
+            const variableDeclarationRegExp2: RegExp = /var *e;/;
+            const functionDeclarationRegExp: RegExp = /function *f *\(\) *{/;
+            const variableDeclarationRegExp3: RegExp = /var *f *= *function *\(\) *{}/;
+
+            let obfuscatedCode: string;
+
+            before(() => {
+                const code: string = readFileAsString(__dirname + '/fixtures/prevent-renaming-of-renamed-identifiers-2.js');
+                const obfuscationResult: IObfuscationResult = JavaScriptObfuscator.obfuscate(
+                    code,
+                    {
+                        ...NO_ADDITIONAL_NODES_PRESET,
+                        identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator
+                    }
+                );
+
+                obfuscatedCode = obfuscationResult.getObfuscatedCode();
+            });
+
+            it('Match #1: shouldn\'t rename twice variable declaration name', () => {
+                assert.match(obfuscatedCode, variableDeclarationRegExp1);
+            });
+
+            it('Match #2: shouldn\'t rename twice variable declaration name', () => {
+                assert.match(obfuscatedCode, variableDeclarationRegExp2);
+            });
+
+            it('Match #3: should correctly rename function declaration name', () => {
+                assert.match(obfuscatedCode, functionDeclarationRegExp);
+            });
+
+            it('Match #4: should correctly rename variable declaration name', () => {
+                assert.match(obfuscatedCode, variableDeclarationRegExp3);
+            });
         });
     });
 });

+ 0 - 0
test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/fixtures/prevent-renaming-of-renamed-identifiers.js → test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/fixtures/prevent-renaming-of-renamed-identifiers-1.js


+ 12 - 0
test/functional-tests/node-transformers/obfuscating-transformers/variable-declaration-transformer/fixtures/prevent-renaming-of-renamed-identifiers-2.js

@@ -0,0 +1,12 @@
+(function() {
+    function test() {
+        var variable = 1;
+        var g;
+
+        function e () {
+            var e = function () {
+
+            }
+        }
+    }
+})();

+ 15 - 20
yarn.lock

@@ -593,9 +593,9 @@
   dependencies:
     "@types/node" "*"
 
-"@types/mocha@2.2.48":
-  version "2.2.48"
-  resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-2.2.48.tgz#3523b126a0b049482e1c3c11877460f76622ffab"
+"@types/mocha@5.0.0":
+  version "5.0.0"
+  resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-5.0.0.tgz#a3014921991066193f6c8e47290d4d598dfd19e6"
 
 "@types/[email protected]":
   version "2.1.2"
@@ -605,9 +605,9 @@
   version "8.0.53"
   resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.53.tgz#396b35af826fa66aad472c8cb7b8d5e277f4e6d8"
 
-"@types/node@9.4.7":
-  version "9.4.7"
-  resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.7.tgz#57d81cd98719df2c9de118f2d5f3b1120dcd7275"
+"@types/node@9.6.0":
+  version "9.6.0"
+  resolved "https://registry.yarnpkg.com/@types/node/-/node-9.6.0.tgz#d3480ee666df9784b1001a1872a2f6ccefb6c2d7"
 
 "@types/[email protected]":
   version "2.0.2"
@@ -709,10 +709,6 @@ ansi-styles@~1.0.0:
   version "1.0.0"
   resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-1.0.0.tgz#cb102df1c56f5123eab8b67cd7b98027a0279178"
 
[email protected]:
-  version "1.1.71"
-  resolved "https://registry.yarnpkg.com/ansicolor/-/ansicolor-1.1.71.tgz#38bbf97db282edb1462dcb23400112407a9486e3"
-
 any-observable@^0.2.0:
   version "0.2.0"
   resolved "https://registry.yarnpkg.com/any-observable/-/any-observable-0.2.0.tgz#c67870058003579009083f54ac0abafb5c33d242"
@@ -1896,11 +1892,10 @@ class-utils@^0.3.5:
     lazy-cache "^2.0.2"
     static-extend "^0.1.1"
 
[email protected].4:
-  version "0.8.4"
-  resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.8.4.tgz#f78d31f5aeabd27e05394be07e93f42e2041a0bb"
[email protected].5:
+  version "0.8.5"
+  resolved "https://registry.yarnpkg.com/class-validator/-/class-validator-0.8.5.tgz#484785acda98f68549c3a84dc1bb2f77b736dc58"
   dependencies:
-    ansicolor "1.1.71"
     validator "9.2.0"
 
 cli-cursor@^1.0.2:
@@ -4100,9 +4095,9 @@ [email protected], [email protected], "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdi
   dependencies:
     minimist "0.0.8"
 
[email protected].4:
-  version "5.0.4"
-  resolved "https://registry.yarnpkg.com/mocha/-/mocha-5.0.4.tgz#6b7aa328472da1088e69d47e75925fd3a3bb63c6"
[email protected].5:
+  version "5.0.5"
+  resolved "https://registry.yarnpkg.com/mocha/-/mocha-5.0.5.tgz#e228e3386b9387a4710007a641f127b00be44b52"
   dependencies:
     browser-stdout "1.3.1"
     commander "2.11.0"
@@ -5982,9 +5977,9 @@ webpack-addons@^1.1.5:
   dependencies:
     jscodeshift "^0.4.0"
 
[email protected]2:
-  version "2.0.12"
-  resolved "https://registry.yarnpkg.com/webpack-cli/-/webpack-cli-2.0.12.tgz#64db876d044f03d8d6544281854b71a3a3c77dd3"
[email protected]3:
+  version "2.0.13"
+  resolved "https://registry.yarnpkg.com/webpack-cli/-/webpack-cli-2.0.13.tgz#6e2bd9ef91345344737217e22e29001ad8537518"
   dependencies:
     chalk "^2.3.2"
     cross-spawn "^6.0.5"

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