Skip to content

Commit

Permalink
Prevented mutation of the name sequences of mangled identifier name…
Browse files Browse the repository at this point in the history
… generators
  • Loading branch information
sanex3339 committed Jul 10, 2020
1 parent b2f5a91 commit d074bbe
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Change Log

v1.5.2
---
* Prevented mutation of the name sequences of `mangled` identifier name generators

v1.5.1
---
* Fixed runtime error when `IfStatement` contains only single `let` or `const` variable declaration when `simlify` option enabled. https://github.com/javascript-obfuscator/javascript-obfuscator/issues/661
Expand Down
6 changes: 3 additions & 3 deletions dist/index.browser.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.cli.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "1.5.1",
"version": "1.5.2",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene

@postConstruct()
public initialize (): void {
if (!MangledIdentifierNamesGenerator.nameSequence) {
MangledIdentifierNamesGenerator.nameSequence = [
...`${numbersString}${alphabetString}${alphabetStringUppercase}`
];
}
this.initializeNameSequence([
...`${numbersString}${alphabetString}${alphabetStringUppercase}`
]);
}

/**
Expand Down Expand Up @@ -138,13 +136,29 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
&& !MangledIdentifierNamesGenerator.reservedNamesSet.has(mangledName);
}

/**
* @param {string[]} nameSequence
*/
protected initializeNameSequence (nameSequence: string[]): void {
if (!this.getNameSequence()) {
MangledIdentifierNamesGenerator.nameSequence = nameSequence;
}
}

/**
* @returns {string[]}
*/
protected getNameSequence (): string[] {
return MangledIdentifierNamesGenerator.nameSequence;
}

/**
* @param {string} previousMangledName
* @returns {string}
*/
private generateNewMangledName (previousMangledName: string): string {
protected generateNewMangledName (previousMangledName: string): string {
const generateNewMangledName: (name: string) => string = (name: string): string => {
const nameSequence: string[] = MangledIdentifierNamesGenerator.nameSequence;
const nameSequence: string[] = this.getNameSequence();
const nameSequenceLength: number = nameSequence.length;
const nameLength: number = name.length;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import { MangledIdentifierNamesGenerator } from './MangledIdentifierNamesGenerat

@injectable()
export class MangledShuffledIdentifierNamesGenerator extends MangledIdentifierNamesGenerator {
/**
* @type {string[]}
*/
protected static shuffledNameSequence: string[];

/**
* @type {IArrayUtils}
*/
Expand All @@ -35,11 +40,33 @@ export class MangledShuffledIdentifierNamesGenerator extends MangledIdentifierNa

@postConstruct()
public initialize (): void {
if (!MangledIdentifierNamesGenerator.nameSequence) {
MangledIdentifierNamesGenerator.nameSequence = [
...`${numbersString}`,
...this.arrayUtils.shuffle([...`${alphabetString}${alphabetStringUppercase}`])
];
this.initializeNameSequence([
...`${numbersString}`,
...this.arrayUtils.shuffle([...`${alphabetString}${alphabetStringUppercase}`])
]);
}

/**
* @param {string[]} nameSequence
*/
protected initializeNameSequence (nameSequence: string[]): void {
if (!this.getNameSequence()) {
MangledShuffledIdentifierNamesGenerator.shuffledNameSequence = nameSequence;
}
}

/**
* @returns {string[]}
*/
protected getNameSequence (): string[] {
return MangledShuffledIdentifierNamesGenerator.shuffledNameSequence;
}

/**
* @param {string} previousMangledName
* @returns {string}
*/
protected generateNewMangledName (previousMangledName: string): string {
return super.generateNewMangledName(previousMangledName);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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('MangledShuffledIdentifierNamesGenerator', () => {
describe('Variant #1: prevent name sequence mutation of base `mangled` generator', () => {
const functionsRegExp: RegExp = new RegExp(
'function foo *\\(a, *b\\) *{} *' +
'function foo *\\(a, *b\\) *{} *' +
'function foo *\\(a, *b\\) *{} *' +
'function foo *\\(a, *b\\) *{}'
);

let obfuscatedCode: string = '';

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/prevent-name-sequence-mutation.js');

for (let i = 0; i < 4; i++) {
const identifierNamesGenerator = i % 2 === 0
? IdentifierNamesGenerator.MangledIdentifierNamesGenerator
: IdentifierNamesGenerator.MangledShuffledIdentifierNamesGenerator;

obfuscatedCode += JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
identifierNamesGenerator
}
).getObfuscatedCode();
}
});

it('Should not mutate name sequences between mangled generators', () => {
assert.notMatch(obfuscatedCode, functionsRegExp);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function foo (bar, baz) {}
1 change: 1 addition & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import './functional-tests/custom-code-helpers/string-array/templates/string-arr
import './functional-tests/custom-code-helpers/string-array/templates/string-array-rotate-function-template/StringArrayRotateFunctionTemplate.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/generators/identifier-names-generators/mangled-shuffled-identifier-names-generator/MangledShuffledIdentifierNamesGenerator.spec';
import './functional-tests/issues/issue321.spec';
import './functional-tests/issues/issue355.spec';
import './functional-tests/issues/issue419.spec';
Expand Down

0 comments on commit d074bbe

Please sign in to comment.