Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
Change Log

v5.9.1
---
* Fixed `splitStrings`/`stringArray` breaking a quoted `constructor` class key. Fixed https://github.com/javascript-obfuscator/javascript-obfuscator/issues/1464

v5.9.0
---
* Pro API: added `getProQuota()`, which returns the usage left on the API key's plan.
Expand Down
8 changes: 7 additions & 1 deletion src/node/NodeLiteralUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ export class NodeLiteralUtils {
* @returns {boolean}
*/
public static isProhibitedLiteralNode(literalNode: ESTree.Literal, parentNode: ESTree.Node): boolean {
if (NodeGuards.isPropertyNode(parentNode) && !parentNode.computed && parentNode.key === literalNode) {
if (
(NodeGuards.isPropertyNode(parentNode) ||
NodeGuards.isMethodDefinitionNode(parentNode) ||
NodeGuards.isPropertyDefinitionNode(parentNode)) &&
!parentNode.computed &&
parentNode.key === literalNode
) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ describe('ClassFieldTransformer', () => {
});

describe('Variant #2: `stringArray` option is enabled', () => {
const stringArrayRegExp: RegExp = getStringArrayRegExp(['property', 'constructor', 'bar']);
const stringArrayCallRegExp: RegExp = /\[_0x([a-f0-9]){4}\(0x2\)\]\(\)\{\}/;
const stringArrayRegExp: RegExp = getStringArrayRegExp(['property', 'bar']);
const stringArrayCallRegExp: RegExp = /\[_0x([a-f0-9]){4}\(0x1\)\]\(\)\{\}/;

let obfuscatedCode: string;

Expand Down Expand Up @@ -133,6 +133,56 @@ describe('ClassFieldTransformer', () => {
assert.match(obfuscatedCode, regExp);
});
});

describe('Variant #4: `constructor` key with `splitStrings` option', () => {
const constructorRegExp: RegExp = /'constructor'\(\)\{\}/;
const barRegExp: RegExp = /\['ba'\+'r'\]\(\)\{\}/;

let obfuscatedCode: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/literal-key.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(code, {
...NO_ADDITIONAL_NODES_PRESET,
splitStrings: true,
splitStringsChunkLength: 2
}).getObfuscatedCode();
});

it("shouldn't split method definition node with `constructor` key", () => {
assert.match(obfuscatedCode, constructorRegExp);
});

it('should still split other literal keys into a computed key', () => {
assert.match(obfuscatedCode, barRegExp);
});
});

describe('Variant #5: `constructor` key with `stringArray` option', () => {
const constructorRegExp: RegExp = /'constructor'\(\)\{\}/;
const barRegExp: RegExp = /\[_0x([a-f0-9]){4}\(0x1\)\]\(\)\{\}/;

let obfuscatedCode: string;

before(() => {
const code: string = readFileAsString(__dirname + '/fixtures/literal-key.js');

obfuscatedCode = JavaScriptObfuscator.obfuscate(code, {
...NO_ADDITIONAL_NODES_PRESET,
stringArray: true,
stringArrayThreshold: 1
}).getObfuscatedCode();
});

it("shouldn't add method definition node with `constructor` key to string array", () => {
assert.match(obfuscatedCode, constructorRegExp);
});

it('should still move other literal keys to the string array as a computed key', () => {
assert.match(obfuscatedCode, barRegExp);
});
});
});

describe('Variant #3: async `get()` method', () => {
Expand Down Expand Up @@ -225,7 +275,7 @@ describe('ClassFieldTransformer', () => {
});

describe('Variant #2: `stringArray` option is enabled', () => {
const stringArrayRegExp: RegExp = getStringArrayRegExp(['property', 'constructor', 'bar']);
const stringArrayRegExp: RegExp = getStringArrayRegExp(['property', 'bar']);
const stringArrayCallRegExp: RegExp = /\[_0x([a-f0-9]){4}\(0x0\)\] *= *0x1;/;

let obfuscatedCode: string;
Expand Down
103 changes: 103 additions & 0 deletions test/unit-tests/node/node-literal-utils/NodeLiteralUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,109 @@ describe('NodeLiteralUtils', () => {
});
});
});

describe('Variant #6: method definition node', () => {
describe('Variant #1: method definition key literal node', () => {
const literalNode: ESTree.Literal = NodeFactory.literalNode('foo');

let methodDefinitionNode: ESTree.MethodDefinition;

before(() => {
methodDefinitionNode = {
type: 'MethodDefinition',
key: literalNode,
value: NodeFactory.functionExpressionNode([], NodeFactory.blockStatementNode()),
kind: 'method',
computed: false,
static: false
};

literalNode.parentNode = methodDefinitionNode;
});

it('should return true for method definition key literal node', () => {
assert.equal(NodeLiteralUtils.isProhibitedLiteralNode(literalNode, methodDefinitionNode), true);
});
});

describe('Variant #2: computed method definition key literal node', () => {
const literalNode: ESTree.Literal = NodeFactory.literalNode('foo');

let methodDefinitionNode: ESTree.MethodDefinition;

before(() => {
methodDefinitionNode = {
type: 'MethodDefinition',
key: literalNode,
value: NodeFactory.functionExpressionNode([], NodeFactory.blockStatementNode()),
kind: 'method',
computed: true,
static: false
};

literalNode.parentNode = methodDefinitionNode;
});

it('should return false for computed method definition key literal node', () => {
assert.equal(
NodeLiteralUtils.isProhibitedLiteralNode(literalNode, methodDefinitionNode),
false
);
});
});
});

describe('Variant #7: property definition node', () => {
describe('Variant #1: property definition key literal node', () => {
const literalNode: ESTree.Literal = NodeFactory.literalNode('foo');

let propertyDefinitionNode: ESTree.PropertyDefinition;

before(() => {
propertyDefinitionNode = {
type: 'PropertyDefinition',
key: literalNode,
value: NodeFactory.literalNode(1),
computed: false,
static: false
};

literalNode.parentNode = propertyDefinitionNode;
});

it('should return true for property definition key literal node', () => {
assert.equal(
NodeLiteralUtils.isProhibitedLiteralNode(literalNode, propertyDefinitionNode),
true
);
});
});

describe('Variant #2: computed property definition key literal node', () => {
const literalNode: ESTree.Literal = NodeFactory.literalNode('foo');

let propertyDefinitionNode: ESTree.PropertyDefinition;

before(() => {
propertyDefinitionNode = {
type: 'PropertyDefinition',
key: literalNode,
value: NodeFactory.literalNode(1),
computed: true,
static: false
};

literalNode.parentNode = propertyDefinitionNode;
});

it('should return false for computed property definition key literal node', () => {
assert.equal(
NodeLiteralUtils.isProhibitedLiteralNode(literalNode, propertyDefinitionNode),
false
);
});
});
});
});

describe('Number literal node', () => {
Expand Down
Loading