Dead code injection unwrap stage now runs before string array transformers

This commit is contained in:
sanex
2021-06-16 09:43:56 +03:00
parent 0f0eb279a9
commit 899456f553
9 changed files with 152 additions and 80 deletions
+32 -12
View File
@@ -5,22 +5,42 @@
let obfuscationResult = JavaScriptObfuscator.obfuscate(
`
function foo() {
global.baz = 3;
}
(function(){
if (true) {
var foo = function () {
console.log('abc');
};
var bar = function () {
console.log('def');
};
var baz = function () {
console.log('ghi');
};
var bark = function () {
console.log('jkl');
};
var hawk = function () {
console.log('mno');
};
function bar(...args) {
console.log(2);
}
foo();
bar();
baz();
bark();
hawk();
}
})();
`,
{
compact: false,
identifierNamesCache: {
globalIdentifiers: {},
propertyIdentifiers: {}
},
renameGlobals: true,
renameProperties: true
simplify: false,
stringArray: true,
stringArrayThreshold: 1,
stringArrayWrappersChainedCalls: true,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
identifierNamesGenerator: 'mangled',
seed: 1
}
);
@@ -998,5 +998,46 @@ describe('DeadCodeInjectionTransformer', () => {
});
});
});
describe('Variant #12 - correct integration with `stringArrayWrappersChainedCalls` option', () => {
const regExp: RegExp = new RegExp(
`var ${variableMatch} *= *${variableMatch}; *` +
`if *\\(${variableMatch}\\(${hexMatch}\\) *[=|!]== *${variableMatch}\\(${hexMatch}\\)\\) *\\{`+
`(?:console|${variableMatch})\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
`\\} *else *\\{`+
`(?:console|${variableMatch})\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
`\\}`,
'g'
);
const expectedMatchesLength: number = 5;
let matchesLength: number = 0;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/input-1.js');
const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
stringArray: true,
stringArrayThreshold: 1,
stringArrayWrappersCount: 1,
stringArrayWrappersChainedCalls: true
}
).getObfuscatedCode();
const matches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(regExp);
if (matches) {
matchesLength = matches.length;
}
});
it('should unwrap dead code injection root AST host node before the string array transformer', () => {
assert.equal(matchesLength, expectedMatchesLength);
});
});
});
});