mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
String array calls transform fix (#1050)
* Fixed the wrong generation of code that has statements after `ReturnStatment` when `simplify` option is enabled * Fixed generation of reserved identifier names like `Map` or `Set` for `mangled` and `mangled-shuffled` identifier names generators
This commit is contained in:
+39
-10
@@ -1,26 +1,55 @@
|
||||
'use strict';
|
||||
|
||||
import { StringArrayWrappersType } from '../../src/enums/node-transformers/string-array-transformers/StringArrayWrappersType';
|
||||
|
||||
(function () {
|
||||
const JavaScriptObfuscator: any = require('../../index');
|
||||
|
||||
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
|
||||
`
|
||||
async function xyzzy(a,b)
|
||||
{
|
||||
if (a) {
|
||||
return await foo(a) ;
|
||||
console.log(a) ;
|
||||
} else {
|
||||
return await bar(b) ;
|
||||
console.log(b) ;
|
||||
function foo () {
|
||||
function bar() {
|
||||
var string1 = 'string1';
|
||||
var string2 = 'string2';
|
||||
var string3 = 'string3';
|
||||
var string4 = 'string4';
|
||||
var string5 = 'string5';
|
||||
var string6 = 'string6';
|
||||
|
||||
function bark () {
|
||||
var string1 = 'string1';
|
||||
var string2 = 'string2';
|
||||
var string3 = 'string3';
|
||||
var string4 = 'string4';
|
||||
var string5 = 'string5';
|
||||
var string6 = 'string6';
|
||||
}
|
||||
}
|
||||
|
||||
bar()
|
||||
}
|
||||
|
||||
console.log(foo());
|
||||
`,
|
||||
{
|
||||
identifierNamesGenerator: 'mangled',
|
||||
compact: false,
|
||||
simplify: true,
|
||||
stringArray: false
|
||||
controlFlowFlattening: false,
|
||||
controlFlowFlatteningThreshold: 1,
|
||||
simplify: false,
|
||||
stringArrayRotate: false,
|
||||
stringArray: true,
|
||||
stringArrayIndexesType: [
|
||||
'hexadecimal-number',
|
||||
'hexadecimal-numeric-string'
|
||||
],
|
||||
stringArrayThreshold: 1,
|
||||
stringArrayCallsTransform: true,
|
||||
stringArrayCallsTransformThreshold: 1,
|
||||
rotateStringArray: true,
|
||||
stringArrayWrappersType: StringArrayWrappersType.Function,
|
||||
transformObjectKeys: false,
|
||||
seed: 1
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
|
||||
|
||||
+38
-1
@@ -191,6 +191,43 @@ describe('StringArrayControlFlowTransformer', function () {
|
||||
assert.match(obfuscatedCode, regexp);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #5 - multiple `control flow storages` on the same block scope', () => {
|
||||
const regexp: RegExp = new RegExp(
|
||||
`var ${hexadecimalVariableMatch} *= *\\{` +
|
||||
`${hexadecimalVariableMatch} *: *0x0, *` +
|
||||
`${hexadecimalVariableMatch} *: *0x1 *` +
|
||||
`\\}; *` +
|
||||
`var ${hexadecimalVariableMatch} *= *\\{` +
|
||||
|
||||
`${hexadecimalVariableMatch} *: *0x2, *` +
|
||||
`${hexadecimalVariableMatch} *: *0x3 *` +
|
||||
`\\};`
|
||||
);
|
||||
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/multiple-storages-1.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
{
|
||||
...NO_ADDITIONAL_NODES_PRESET,
|
||||
stringArray: true,
|
||||
stringArrayThreshold: 1,
|
||||
stringArrayCallsTransform: true,
|
||||
stringArrayCallsTransformThreshold: 1
|
||||
}
|
||||
).getObfuscatedCode();
|
||||
|
||||
console.log(obfuscatedCode);
|
||||
});
|
||||
|
||||
it('should add `control flow storage` node with multiple items to the obfuscated code', () => {
|
||||
assert.match(obfuscatedCode, regexp);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #2 - negative cases', function () {
|
||||
@@ -320,7 +357,7 @@ describe('StringArrayControlFlowTransformer', function () {
|
||||
let obfuscatedCode: string;
|
||||
|
||||
before(() => {
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/multiple-storages.js');
|
||||
const code: string = readFileAsString(__dirname + '/fixtures/multiple-storages-2.js');
|
||||
|
||||
obfuscatedCode = JavaScriptObfuscator.obfuscate(
|
||||
code,
|
||||
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
(function () {
|
||||
var variable1 = 'foo' + 'bar';
|
||||
|
||||
function foo (arg) {
|
||||
var variable2 = 'baz' + 'bark';
|
||||
}
|
||||
})();
|
||||
+29
@@ -186,4 +186,33 @@ describe('MangledShuffledIdentifierNamesGenerator', () => {
|
||||
assert.isTrue(isSuccessComparison);
|
||||
});
|
||||
});
|
||||
|
||||
describe('isValidIdentifierName', () => {
|
||||
describe('Variant #1: reserved dom property name', () => {
|
||||
let identifierNamesGenerator: IIdentifierNamesGenerator,
|
||||
isValidName1: boolean,
|
||||
isValidName2: boolean,
|
||||
isValidName3: boolean;
|
||||
|
||||
beforeEach(() => {
|
||||
const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
|
||||
|
||||
inversifyContainerFacade.load('', '', {} );
|
||||
identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
|
||||
ServiceIdentifiers.IIdentifierNamesGenerator,
|
||||
IdentifierNamesGenerator.MangledShuffledIdentifierNamesGenerator
|
||||
);
|
||||
|
||||
isValidName1 = identifierNamesGenerator.isValidIdentifierName('Set');
|
||||
isValidName2 = identifierNamesGenerator.isValidIdentifierName('Array');
|
||||
isValidName3 = identifierNamesGenerator.isValidIdentifierName('WeakSet');
|
||||
});
|
||||
|
||||
it('should generate first identifier', () => {
|
||||
assert.isFalse(isValidName1);
|
||||
assert.isFalse(isValidName2);
|
||||
assert.isTrue(isValidName3);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
+27
@@ -312,5 +312,32 @@ describe('MangledIdentifierNamesGenerator', () => {
|
||||
assert.equal(secondMangledIdentifierName, expectedSecondIdentifier);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #3: reserved dom property name', () => {
|
||||
let identifierNamesGenerator: IIdentifierNamesGenerator,
|
||||
isValidName1: boolean,
|
||||
isValidName2: boolean,
|
||||
isValidName3: boolean;
|
||||
|
||||
beforeEach(() => {
|
||||
const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
|
||||
|
||||
inversifyContainerFacade.load('', '', {} );
|
||||
identifierNamesGenerator = inversifyContainerFacade.getNamed<IIdentifierNamesGenerator>(
|
||||
ServiceIdentifiers.IIdentifierNamesGenerator,
|
||||
IdentifierNamesGenerator.MangledIdentifierNamesGenerator
|
||||
);
|
||||
|
||||
isValidName1 = identifierNamesGenerator.isValidIdentifierName('Set');
|
||||
isValidName2 = identifierNamesGenerator.isValidIdentifierName('Array');
|
||||
isValidName3 = identifierNamesGenerator.isValidIdentifierName('WeakSet');
|
||||
});
|
||||
|
||||
it('should generate first identifier', () => {
|
||||
assert.isFalse(isValidName1);
|
||||
assert.isFalse(isValidName2);
|
||||
assert.isTrue(isValidName3);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user