mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Added more fixes of identifier names conflicts when using identifiersPrefix
This commit is contained in:
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -115,6 +115,10 @@ export class DictionaryIdentifierNamesGenerator extends AbstractIdentifierNamesG
|
||||
const identifierName: string = this.generate();
|
||||
const identifierNameWithPrefix: string = `${prefix}${identifierName}`;
|
||||
|
||||
if (!this.isValidIdentifierName(identifierNameWithPrefix)) {
|
||||
return this.generateWithPrefix();
|
||||
}
|
||||
|
||||
this.preserveName(identifierNameWithPrefix);
|
||||
|
||||
return identifierNameWithPrefix;
|
||||
|
||||
@@ -88,6 +88,10 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
|
||||
const identifierName: string = this.generate(nameLength);
|
||||
const identifierNameWithPrefix: string = `${prefix}${identifierName}`;
|
||||
|
||||
if (!this.isValidIdentifierName(identifierNameWithPrefix)) {
|
||||
return this.generateWithPrefix(nameLength);
|
||||
}
|
||||
|
||||
this.preserveName(identifierNameWithPrefix);
|
||||
|
||||
return identifierNameWithPrefix;
|
||||
|
||||
+4
-13
@@ -7,23 +7,14 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
|
||||
|
||||
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,
|
||||
compact: false,
|
||||
// identifierNamesGenerator: 'dictionary',
|
||||
// identifiersDictionary: ['a', 'b', 'c', 'd', 'e'],
|
||||
identifierNamesGenerator: 'mangled',
|
||||
identifierNamesGenerator: 'dictionary',
|
||||
identifiersDictionary: ['a', 'b'],
|
||||
identifiersPrefix: 'a',
|
||||
transformObjectKeys: true,
|
||||
stringArray: true,
|
||||
stringArrayThreshold: 1
|
||||
|
||||
+80
@@ -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,0 +1 @@
|
||||
const ab = 'abc';
|
||||
+78
@@ -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,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';
|
||||
@@ -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/StringArrayRotateFunctionNode.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/issue355.spec';
|
||||
import './functional-tests/issues/issue419.spec';
|
||||
|
||||
Reference in New Issue
Block a user