Bläddra i källkod

Added more fixes of identifier names conflicts when using `identifiersPrefix`

sanex3339 5 år sedan
förälder
incheckning
ac259dab1c

Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 0
dist/index.browser.js


Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 0
dist/index.cli.js


Filskillnaden har hållts tillbaka eftersom den är för stor
+ 0 - 0
dist/index.js


+ 4 - 0
src/generators/identifier-names-generators/DictionaryIdentifierNamesGenerator.ts

@@ -115,6 +115,10 @@ export class DictionaryIdentifierNamesGenerator extends AbstractIdentifierNamesG
         const identifierName: string = this.generate();
         const identifierName: string = this.generate();
         const identifierNameWithPrefix: string = `${prefix}${identifierName}`;
         const identifierNameWithPrefix: string = `${prefix}${identifierName}`;
 
 
+        if (!this.isValidIdentifierName(identifierNameWithPrefix)) {
+            return this.generateWithPrefix();
+        }
+
         this.preserveName(identifierNameWithPrefix);
         this.preserveName(identifierNameWithPrefix);
 
 
         return identifierNameWithPrefix;
         return identifierNameWithPrefix;

+ 4 - 0
src/generators/identifier-names-generators/MangledIdentifierNamesGenerator.ts

@@ -88,6 +88,10 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
         const identifierName: string = this.generate(nameLength);
         const identifierName: string = this.generate(nameLength);
         const identifierNameWithPrefix: string = `${prefix}${identifierName}`;
         const identifierNameWithPrefix: string = `${prefix}${identifierName}`;
 
 
+        if (!this.isValidIdentifierName(identifierNameWithPrefix)) {
+            return this.generateWithPrefix(nameLength);
+        }
+
         this.preserveName(identifierNameWithPrefix);
         this.preserveName(identifierNameWithPrefix);
 
 
         return identifierNameWithPrefix;
         return identifierNameWithPrefix;

+ 4 - 13
test/dev/dev.ts

@@ -7,23 +7,14 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
 
 
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
     let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
         `
         `
-            const a = {x: 'abc'};
-
-            function foo (b, c) {
-                return a;
-                
-                function bar () {
-                    var a = 0;
-                    return a;
-                }
-            }
+            const ab = 'abc';
         `,
         `,
         {
         {
             ...NO_ADDITIONAL_NODES_PRESET,
             ...NO_ADDITIONAL_NODES_PRESET,
             compact: false,
             compact: false,
-            // identifierNamesGenerator: 'dictionary',
-            // identifiersDictionary: ['a', 'b', 'c', 'd', 'e'],
-            identifierNamesGenerator: 'mangled',
+            identifierNamesGenerator: 'dictionary',
+            identifiersDictionary: ['a', 'b'],
+            identifiersPrefix: 'a',
             transformObjectKeys: true,
             transformObjectKeys: true,
             stringArray: true,
             stringArray: true,
             stringArrayThreshold: 1
             stringArrayThreshold: 1

+ 80 - 0
test/functional-tests/generators/identifier-names-generators/dictionary-identifier-names-generator/DictionaryIdentifierNamesGenerator.spec.ts

@@ -0,0 +1,80 @@
+import { assert } from 'chai';
+
+import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
+
+import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
+
+import { readFileAsString } from '../../../../helpers/readFileAsString';
+
+import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
+
+describe('DictionaryIdentifierNamesGenerator', () => {
+    describe('generateWithPrefix', () => {
+        describe('Variant #1: should not generate same name for string array as existing name in code', () => {
+            describe('Variant #1: `renameGlobals` option is disabled', () => {
+                const stringArrayStorageRegExp: RegExp = /const a[aB] *= *\['abc'];/;
+                const variableDeclarationIdentifierNameRegExp: RegExp = /const ab *= *a[abAB]\('0x0'\);/;
+
+                let obfuscatedCode: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/string-array-storage-name-conflict-1.js');
+
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
+                            identifiersDictionary: ['a', 'b'],
+                            identifiersPrefix: 'a',
+                            transformObjectKeys: true,
+                            stringArray: true,
+                            stringArrayThreshold: 1
+                        }
+                    ).getObfuscatedCode();
+                });
+
+                it('Match #1: should generate correct identifier for string array', () => {
+                    assert.match(obfuscatedCode, stringArrayStorageRegExp);
+                });
+
+                it('Match #2: should keep identifier name for last variable declaration', () => {
+                    assert.match(obfuscatedCode, variableDeclarationIdentifierNameRegExp);
+                });
+            });
+
+            describe('Variant #2: `renameGlobals` option is enabled', () => {
+                const stringArrayStorageRegExp: RegExp = /const a[aB] *= *\['abc'];/;
+                const lastVariableDeclarationIdentifierNameRegExp: RegExp = /const a[AB] *= *a[AB]\('0x0'\);/;
+
+                let obfuscatedCode: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/string-array-storage-name-conflict-1.js');
+
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            identifierNamesGenerator: IdentifierNamesGenerator.DictionaryIdentifierNamesGenerator,
+                            identifiersDictionary: ['a', 'b'],
+                            identifiersPrefix: 'a',
+                            renameGlobals: true,
+                            transformObjectKeys: true,
+                            stringArray: true,
+                            stringArrayThreshold: 1
+                        }
+                    ).getObfuscatedCode();
+                });
+
+                it('Match #1: should generate correct identifier for string array', () => {
+                    assert.match(obfuscatedCode, stringArrayStorageRegExp);
+                });
+
+                it('Match #2: should keep identifier name for last variable declaration', () => {
+                    assert.match(obfuscatedCode, lastVariableDeclarationIdentifierNameRegExp);
+                });
+            });
+        });
+    });
+});

+ 1 - 0
test/functional-tests/generators/identifier-names-generators/dictionary-identifier-names-generator/fixtures/string-array-storage-name-conflict-1.js

@@ -0,0 +1 @@
+const ab = 'abc';

+ 78 - 0
test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/MangledIdentifierNamesGenerator.spec.ts

@@ -0,0 +1,78 @@
+import { assert } from 'chai';
+
+import { IdentifierNamesGenerator } from '../../../../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
+
+import { NO_ADDITIONAL_NODES_PRESET } from '../../../../../src/options/presets/NoCustomNodes';
+
+import { readFileAsString } from '../../../../helpers/readFileAsString';
+
+import { JavaScriptObfuscator } from '../../../../../src/JavaScriptObfuscatorFacade';
+
+describe('MangledIdentifierNamesGenerator', () => {
+    describe('generateWithPrefix', () => {
+        describe('Variant #1: should not generate same name for string array as existing name in code', () => {
+            describe('Variant #1: `renameGlobals` option is disabled', () => {
+                const stringArrayStorageRegExp: RegExp = /const ab *= *\['abc'];/;
+                const lastVariableDeclarationIdentifierNameRegExp: RegExp = /const aa *= *ac\('0x0'\);/;
+
+                let obfuscatedCode: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/string-array-storage-name-conflict-1.js');
+
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
+                            identifiersPrefix: 'a',
+                            transformObjectKeys: true,
+                            stringArray: true,
+                            stringArrayThreshold: 1
+                        }
+                    ).getObfuscatedCode();
+                });
+
+                it('Match #1: should generate correct identifier for string array', () => {
+                    assert.match(obfuscatedCode, stringArrayStorageRegExp);
+                });
+
+                it('Match #2: should keep identifier name for last variable declaration', () => {
+                    assert.match(obfuscatedCode, lastVariableDeclarationIdentifierNameRegExp);
+                });
+            });
+
+            describe('Variant #2: `renameGlobals` option is enabled', () => {
+                const stringArrayStorageRegExp: RegExp = /const ab *= *\['abc'];/;
+                const lastVariableDeclarationIdentifierNameRegExp: RegExp = /const aB *= *ac\('0x0'\);/;
+
+                let obfuscatedCode: string;
+
+                before(() => {
+                    const code: string = readFileAsString(__dirname + '/fixtures/string-array-storage-name-conflict-1.js');
+
+                    obfuscatedCode = JavaScriptObfuscator.obfuscate(
+                        code,
+                        {
+                            ...NO_ADDITIONAL_NODES_PRESET,
+                            identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
+                            identifiersPrefix: 'a',
+                            renameGlobals: true,
+                            transformObjectKeys: true,
+                            stringArray: true,
+                            stringArrayThreshold: 1
+                        }
+                    ).getObfuscatedCode();
+                });
+
+                it('Match #1: should generate correct identifier for string array', () => {
+                    assert.match(obfuscatedCode, stringArrayStorageRegExp);
+                });
+
+                it('Match #2: should keep identifier name for last variable declaration', () => {
+                    assert.match(obfuscatedCode, lastVariableDeclarationIdentifierNameRegExp);
+                });
+            });
+        });
+    });
+});

+ 27 - 0
test/functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/fixtures/string-array-storage-name-conflict-1.js

@@ -0,0 +1,27 @@
+const testA = 'abc';
+const testB = 'abc';
+const testC = 'abc';
+const testD = 'abc';
+const testE = 'abc';
+const testF = 'abc';
+const testG = 'abc';
+const testH = 'abc';
+const testI = 'abc';
+const testJ = 'abc';
+const testK = 'abc';
+const testL = 'abc';
+const testM = 'abc';
+const testN = 'abc';
+const testO = 'abc';
+const testP = 'abc';
+const testQ = 'abc';
+const testR = 'abc';
+const testS = 'abc';
+const testT = 'abc';
+const testU = 'abc';
+const testV = 'abc';
+const testW = 'abc';
+const testX = 'abc';
+const testY = 'abc';
+const testZ = 'abc';
+const aa = 'abc';

+ 2 - 0
test/index.spec.ts

@@ -58,6 +58,8 @@ import './functional-tests/custom-nodes/domain-lock-nodes/DomainLockNode.spec';
 import './functional-tests/custom-nodes/string-array-nodes/StringArrayCallsWrapper.spec';
 import './functional-tests/custom-nodes/string-array-nodes/StringArrayCallsWrapper.spec';
 import './functional-tests/custom-nodes/string-array-nodes/StringArrayRotateFunctionNode.spec';
 import './functional-tests/custom-nodes/string-array-nodes/StringArrayRotateFunctionNode.spec';
 import './functional-tests/custom-nodes/string-array-nodes/StringArrayNode.spec';
 import './functional-tests/custom-nodes/string-array-nodes/StringArrayNode.spec';
+import './functional-tests/generators/identifier-names-generators/dictionary-identifier-names-generator/DictionaryIdentifierNamesGenerator.spec';
+import './functional-tests/generators/identifier-names-generators/mangled-identifier-names-generator/MangledIdentifierNamesGenerator.spec';
 import './functional-tests/issues/issue321.spec';
 import './functional-tests/issues/issue321.spec';
 import './functional-tests/issues/issue355.spec';
 import './functional-tests/issues/issue355.spec';
 import './functional-tests/issues/issue419.spec';
 import './functional-tests/issues/issue419.spec';

Vissa filer visades inte eftersom för många filer har ändrats