Fixed cases when dead code is added to the inner code of eval expressions (#1062)

Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1053
This commit is contained in:
Timofey Kachalov
2022-02-14 14:34:39 +03:00
committed by GitHub
parent c4dae419eb
commit 30ba697c55
9 changed files with 135 additions and 36 deletions
@@ -1090,5 +1090,34 @@ describe('DeadCodeInjectionTransformer', () => {
assert.equal(matchesLength, expectedMatchesLength);
});
});
describe('Variant #13 - correct integration with `EvalCallExpressionTransformer`', () => {
const evalWithDeadCodeRegExp: RegExp = new RegExp(
`eval\\(\'if *\\(${variableMatch}`,
'g'
);
let obfuscatedCode: string;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/eval-call-expression-transformer-integration.js');
obfuscatedCode = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
stringArray: true,
stringArrayThreshold: 1
}
).getObfuscatedCode();
console.log(obfuscatedCode);
});
it('match #1: shouldn\'t add dead code to the eval call expression', () => {
assert.notMatch(obfuscatedCode, evalWithDeadCodeRegExp);
});
});
});
});
@@ -0,0 +1,25 @@
(function(){
if (true) {
var foo = function () {
return true;
};
var bar = function () {
return true;
};
var baz = function () {
return true;
};
var bark = function () {
return true;
};
if (true) {
eval('const eval = 1');
}
foo();
bar();
baz();
bark();
}
})();
@@ -50,6 +50,24 @@ describe('NodeMetadata', () => {
});
});
describe('isEvalHostNode', () => {
const expectedValue: boolean = true;
let node: ESTree.FunctionExpression,
value: boolean | undefined;
before(() => {
node = NodeFactory.functionExpressionNode([], NodeFactory.blockStatementNode([]));
node.metadata = {};
node.metadata.evalHostNode = true;
value = NodeMetadata.isEvalHostNode(node);
});
it('should return metadata value', () => {
assert.equal(value, expectedValue);
});
});
describe('isForceTransformNode', () => {
const expectedValue: boolean = true;