Fixed for-await-of construction add when deadCodeInjection option is enabled

This commit is contained in:
sanex
2020-10-01 20:59:33 +03:00
parent 252b901b69
commit 329b7a2543
9 changed files with 97 additions and 8 deletions
+4
View File
@@ -1,5 +1,9 @@
Change Log
v2.4.3
---
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/769
v2.4.2
---
* Fixed `URI-malformed` when `splitStrings` and `stringArrayEncoding` options are enabled. https://github.com/javascript-obfuscator/javascript-obfuscator/issues/530
+4 -4
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
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "2.4.2",
"version": "2.4.3",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
@@ -106,7 +106,8 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
|| NodeGuards.isContinueStatementNode(targetNode)
|| NodeGuards.isAwaitExpressionNode(targetNode)
|| NodeGuards.isYieldExpressionNode(targetNode)
|| NodeGuards.isSuperNode(targetNode);
|| NodeGuards.isSuperNode(targetNode)
|| (NodeGuards.isForOfStatementNode(targetNode) && targetNode.await);
}
/**
+8
View File
@@ -141,6 +141,14 @@ export class NodeGuards {
&& !('directive' in node);
}
/**
* @param {Node} node
* @returns {boolean}
*/
public static isForOfStatementNode (node: ESTree.Node): node is ESTree.ForOfStatement {
return node.type === NodeType.ForOfStatement;
}
/**
* @param {Node} node
* @returns {boolean}
@@ -438,6 +438,57 @@ describe('DeadCodeInjectionTransformer', () => {
assert.equal(superExpressionMatchesLength, expectedSuperExpressionMatchesLength);
});
});
describe('Variant #6 - for-await expression in block statement', () => {
const functionRegExp: RegExp = new RegExp(
`var ${variableMatch} *= *function *\\(\\) *\\{` +
`console\\[${variableMatch}\\('${hexMatch}'\\)\\]\\(${variableMatch}\\('${hexMatch}'\\)\\);` +
`\\};`,
'g'
);
const awaitExpressionRegExp: RegExp = new RegExp(
`for await *\\(const ${variableMatch} of *\\[]\\){}`,
'g'
);
const expectedFunctionMatchesLength: number = 4;
const expectedAwaitExpressionMatchesLength: number = 1;
let functionMatchesLength: number = 0,
awaitExpressionMatchesLength: number = 0;
before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/for-await-expression.js');
const obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
code,
{
...NO_ADDITIONAL_NODES_PRESET,
deadCodeInjection: true,
deadCodeInjectionThreshold: 1,
stringArray: true,
stringArrayThreshold: 1
}
).getObfuscatedCode();
const functionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(functionRegExp);
const awaitExpressionMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(awaitExpressionRegExp);
if (functionMatches) {
functionMatchesLength = functionMatches.length;
}
if (awaitExpressionMatches) {
awaitExpressionMatchesLength = awaitExpressionMatches.length;
}
});
it('match #1: shouldn\'t add dead code', () => {
assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
});
it('match #2: shouldn\'t add dead code', () => {
assert.equal(awaitExpressionMatchesLength, expectedAwaitExpressionMatchesLength);
});
});
});
describe('Variant #5 - chance of `IfStatement` variant', () => {
@@ -0,0 +1,25 @@
(async 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');
};
if (true) {
for await (const item of []) {}
}
foo();
bar();
baz();
bark();
}
})();