Skip to content

Commit

Permalink
Dead code injection transformer ignores PrivateIdentifier nodes (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanex3339 committed Feb 9, 2022
1 parent edc2a22 commit 2095d47
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Change Log

v3.2.5
---
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1056

v3.2.4
---
* Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1052
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "javascript-obfuscator",
"version": "3.2.4",
"version": "3.2.5",
"description": "JavaScript obfuscator",
"keywords": [
"obfuscator",
Expand Down
1 change: 1 addition & 0 deletions src/enums/node/NodeType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export enum NodeType {
NewExpression = 'NewExpression',
ObjectExpression = 'ObjectExpression',
ObjectPattern = 'ObjectPattern',
PrivateIdentifier = 'PrivateIdentifier',
Program = 'Program',
Property = 'Property',
PropertyDefinition = 'PropertyDefinition',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ export class DeadCodeInjectionTransformer extends AbstractNodeTransformer {
|| NodeGuards.isAwaitExpressionNode(targetNode)
|| NodeGuards.isYieldExpressionNode(targetNode)
|| NodeGuards.isSuperNode(targetNode)
|| (NodeGuards.isForOfStatementNode(targetNode) && targetNode.await);
|| (NodeGuards.isForOfStatementNode(targetNode) && targetNode.await)
|| NodeGuards.isPrivateIdentifierNode(targetNode);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions src/node/NodeGuards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,14 @@ export class NodeGuards {
return node.type === NodeType.ObjectExpression;
}

/**
* @param {Node} node
* @returns {boolean}
*/
public static isPrivateIdentifierNode (node: ESTree.Node): node is ESTree.PrivateIdentifier {
return node.type === NodeType.PrivateIdentifier;
}

/**
* @param {Node} node
* @returns {boolean}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,57 @@ describe('DeadCodeInjectionTransformer', () => {
assert.equal(awaitExpressionMatchesLength, expectedAwaitExpressionMatchesLength);
});
});

describe('Variant #7 - private identifier in block statement', () => {
const functionRegExp: RegExp = new RegExp(
`var ${variableMatch} *= *function *\\(\\) *\\{` +
`console\\[${variableMatch}\\(${hexMatch}\\)\\]\\(${variableMatch}\\(${hexMatch}\\)\\);` +
`\\};`,
'g'
);
const privateIdentifierRegExp: RegExp = new RegExp(
`this\.#private *= *0x1;`,
'g'
);
const expectedFunctionMatchesLength: number = 4;
const expectedPrivateIdentifierMatchesLength: number = 1;

let functionMatchesLength: number = 0,
privateIdentifierMatchesLength: number = 0;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/private-identifier.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 privateIdentifierMatches: RegExpMatchArray = <RegExpMatchArray>obfuscatedCode.match(privateIdentifierRegExp);

if (functionMatches) {
functionMatchesLength = functionMatches.length;
}

if (privateIdentifierMatches) {
privateIdentifierMatchesLength = privateIdentifierMatches.length;
}
});

it('match #1: shouldn\'t add dead code', () => {
assert.equal(functionMatchesLength, expectedFunctionMatchesLength);
});

it('match #2: shouldn\'t add dead code', () => {
assert.equal(privateIdentifierMatchesLength, expectedPrivateIdentifierMatchesLength);
});
});
});

describe('Variant #5 - chance of `IfStatement` variant', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Foo {
#private;

constructor(props) {
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) {
this.#private = 1;
}

foo();
bar();
baz();
bark();
}
}
}

0 comments on commit 2095d47

Please sign in to comment.