Fixed stringArrayCallsWrapper option with mangled names generator

This commit is contained in:
sanex
2020-09-12 00:05:47 +03:00
parent 253b192bf5
commit 9b5eba5363
10 changed files with 92 additions and 31 deletions
+2 -2
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
+2 -2
View File
@@ -57,7 +57,7 @@
"@types/mkdirp": "1.0.1",
"@types/mocha": "8.0.3",
"@types/multimatch": "4.0.0",
"@types/node": "14.6.4",
"@types/node": "14.10.0",
"@types/rimraf": "3.0.0",
"@types/sinon": "9.0.5",
"@types/string-template": "1.0.2",
@@ -69,7 +69,7 @@
"coveralls": "3.1.0",
"eslint": "7.8.1",
"eslint-plugin-import": "2.22.0",
"eslint-plugin-jsdoc": "30.3.3",
"eslint-plugin-jsdoc": "30.4.0",
"eslint-plugin-no-null": "1.0.2",
"eslint-plugin-prefer-arrow": "1.2.2",
"eslint-plugin-unicorn": "21.0.0",
@@ -60,14 +60,16 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
}
/**
* We can only ignore limited nameLength, it has no sense here
* Generates next name based on a global previous mangled name
* We can ignore nameLength parameter here, it hasn't sense with this generator
*
* @param {number} nameLength
* @returns {string}
*/
public generateNext (nameLength?: number): string {
const identifierName: string = this.generateNewMangledName(this.previousMangledName);
this.previousMangledName = identifierName;
this.updatePreviousMangledName(identifierName);
this.preserveName(identifierName);
return identifierName;
@@ -84,7 +86,7 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
const identifierName: string = this.generateNewMangledName(this.previousMangledName);
const identifierNameWithPrefix: string = `${prefix}${identifierName}`;
this.previousMangledName = identifierName;
this.updatePreviousMangledName(identifierName);
if (!this.isValidIdentifierName(identifierNameWithPrefix)) {
return this.generateForGlobalScope(nameLength);
@@ -116,6 +118,7 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
MangledIdentifierNamesGenerator.lastMangledNameInScopeMap.set(lexicalScopeNode, identifierName);
this.updatePreviousMangledName(identifierName);
this.preserveNameForLexicalScope(identifierName, lexicalScopeNode);
return identifierName;
@@ -137,6 +140,13 @@ export class MangledIdentifierNamesGenerator extends AbstractIdentifierNamesGene
return MangledIdentifierNamesGenerator.nameSequence;
}
/**
* @param {string} name
*/
protected updatePreviousMangledName (name: string): void {
this.previousMangledName = name;
}
/**
* @param {string} previousMangledName
* @returns {string}
@@ -268,7 +268,7 @@ export class StringArrayTransformer extends AbstractNodeTransformer {
const isFilledScopeCallsWrapperNamesList: boolean = stringArrayScopeCallsWrapperNames.length === this.options.stringArrayWrappersCount;
if (!isFilledScopeCallsWrapperNamesList) {
const nextScopeCallsWrapperName: string = this.identifierNamesGenerator.generateForLexicalScope(currentLexicalScopeNode);
const nextScopeCallsWrapperName: string = this.identifierNamesGenerator.generateNext();
stringArrayScopeCallsWrapperNames.push(nextScopeCallsWrapperName);
stringArrayScopeCallsWrapperDataByEncoding[encoding] = {
+19 -11
View File
@@ -1,28 +1,36 @@
'use strict';
import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNodes';
import { IdentifierNamesGenerator } from '../../src/enums/generators/identifier-names-generators/IdentifierNamesGenerator';
(function () {
const JavaScriptObfuscator: any = require('../../index');
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
const foo = 'foo';
const bar = 'bar';
function test () {
const baz = 'baz';
const bark = 'bark';
function test1() {
const hawk = 'hawk';
const eagle = 'eagle';
}
const foo = 'aaa';
function test (a, b) {
const bar = 'bbb';
return a + b;
}
function test1 (a, b) {
const bar = 'bbb';
return a + b;
}
test();
var baz = 5;
`,
{
...NO_ADDITIONAL_NODES_PRESET,
compact: false,
identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
renameGlobals: true,
stringArray: true,
stringArrayThreshold: 1,
stringArrayWrappersChainedCalls: true,
@@ -256,13 +256,56 @@ describe('StringArrayTransformer', function () {
});
describe('Variant #5: `stringArrayWrappersChainedCalls` option is enabled', () => {
describe('Variant #1: correct evaluation of the string array wrappers chained calls', () => {
describe('Variant #1: correct chained calls', () => {
describe('Variant #1: base', () => {
const stringArrayCallRegExp: RegExp = new RegExp(
'const h *= *b;' +
'const foo *= *h\\(\'0x0\'\\);' +
'function test\\( *\\) *{' +
'const i *= *h;' +
'const c *= *i\\(\'0x1\'\\);' +
'const d *= *i\\(\'0x2\'\\);' +
'function e\\( *\\) *{' +
'const j *= *i;' +
'const f *= *j\\(\'0x3\'\\);' +
'const g *= *j\\(\'0x4\'\\);' +
'return f *\\+ *g;' +
'}' +
'return c *\\+ *d *\\+ *e\\(\\);' +
'}'
);
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/string-array-wrappers-chained-calls.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
identifierNamesGenerator: IdentifierNamesGenerator.MangledIdentifierNamesGenerator,
stringArray: true,
stringArrayThreshold: 1,
stringArrayWrappersChainedCalls: true,
stringArrayWrappersCount: 1
}
).getObfuscatedCode();
});
it('should add correct scope calls wrappers', () => {
assert.match(obfuscatedCode, stringArrayCallRegExp);
});
});
});
describe('Variant #2: correct evaluation of the string array wrappers chained calls', () => {
const samplesCount: number = 50;
const expectedEvaluationResult: string = 'aaabbbcccdddeee';
let isEvaluationSuccessful: boolean = true;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/string-array-wrappers-chained-calls-eval.js');
const code: string = readFileAsString(__dirname + '/fixtures/string-array-wrappers-chained-calls.js');
for (let i = 0; i < samplesCount; i++) {
const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
+8 -8
View File
@@ -415,10 +415,10 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-13.9.3.tgz#6356df2647de9eac569f9a52eda3480fa9e70b4d"
integrity sha512-01s+ac4qerwd6RHD+mVbOEsraDHSgUaefQlEdBbUolnQFjKwCr7luvAlEwW1RFojh67u0z4OUTjPn9LEl4zIkA==
"@types/node@14.6.4":
version "14.6.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.6.4.tgz#a145cc0bb14ef9c4777361b7bbafa5cf8e3acb5a"
integrity sha512-Wk7nG1JSaMfMpoMJDKUsWYugliB2Vy55pdjLpmLixeyMi7HizW2I/9QoxsPCkXl3dO+ZOVqPumKaDUv5zJu2uQ==
"@types/node@14.10.0":
version "14.10.0"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.10.0.tgz#15815dff82c8dc30827f6b1286f865902945095a"
integrity sha512-SOIyrdADB4cq6eY1F+9iU48iIomFAPltu11LCvA9PKcyEwHadjCFzNVPotAR+oEJA0bCP4Xvvgy+vwu1ZjVh8g==
"@types/normalize-package-data@^2.4.0":
version "2.4.0"
@@ -2035,10 +2035,10 @@ eslint-plugin-import@2.22.0:
resolve "^1.17.0"
tsconfig-paths "^3.9.0"
eslint-plugin-jsdoc@30.3.3:
version "30.3.3"
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-30.3.3.tgz#25ddf2e59164a6182c5a9e12e6560d69e252013d"
integrity sha512-u2z0LUTm00KLrWB2h7y+hkzYcTIDIi87efXRnv36cFvs23hIOuT77FdZNDBxbB4dxquHBNsyxLpyEuERYUVNEA==
eslint-plugin-jsdoc@30.4.0:
version "30.4.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-30.4.0.tgz#3c84f59b6a04eaef2b636f75eeee905d16b7f2f9"
integrity sha512-eb22QADWcISPQJwFJ+rUAl1NXdyOq3qy0Cp0+MZzpwlqFgJ+eJ7Fd/jYTfwDuN8QyFWumuyzSpwQBnF4PfM9Wg==
dependencies:
comment-parser "^0.7.6"
debug "^4.1.1"