question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

transformObjectKeys=true causes obfuscator to crash with "TypeError: Cannot read property 'type' of undefined" if input code contains object spread operator

See original GitHub issue
var JavaScriptObfuscator = require('javascript-obfuscator');

var obfuscationResult = JavaScriptObfuscator.obfuscate(
	`
        var obj1={
		firstname:'John',
		lastname:'Smith'
	};
	var obj2={
		age:40,
		country:'USA'
	}
	var user={...obj1, ...obj2}
	console.log(user);

    `,
	{
		transformObjectKeys: true,
	}
);

console.log(obfuscationResult.getObfuscatedCode());

Expected Behavior

The normal ouput of this program is:

{firstname: "John", lastname: "Smith", age: 40, country: "USA"}

Current Behavior

The current behavior is:

/Users/vincent/dev/packager/node_modules/javascript-obfuscator/dist/webpack:/JavaScriptObfuscator/src/node/NodeGuards.ts:270
        return node.type === NodeType.ObjectPattern;
                    ^
TypeError: Cannot read property 'type' of undefined
    at Function.type (/Users/vincent/dev/packager/node_modules/javascript-obfuscator/dist/webpack:/JavaScriptObfuscator/src/node/NodeGuards.ts:270:21)
    at Function.isObjectPatternNode (/Users/vincent/dev/packager/node_modules/javascript-obfuscator/dist/webpack:/JavaScriptObfuscator/src/node-transformers/converting-transformers/properties-extractors/AbstractPropertiesExtractor.ts:86:27)
    at t.isProhibitedPattern (/Users/vincent/dev/packager/node_modules/javascript-obfuscator/dist/webpack:/JavaScriptObfuscator/src/node-transformers/converting-transformers/properties-extractors/AbstractPropertiesExtractor.ts:120:45)
    at t.extractPropertiesToExpressionStatements (/Users/vincent/dev/packager/node_modules/javascript-obfuscator/dist/webpack:/JavaScriptObfuscator/src/node-transformers/converting-transformers/properties-extractors/AbstractPropertiesExtractor.ts:187:14)
    at transformObjectExpressionNode (/Users/vincent/dev/packager/node_modules/javascript-obfuscator/dist/webpack:/JavaScriptObfuscator/src/node-transformers/converting-transformers/properties-extractors/VariableDeclaratorPropertiesExtractor.ts:42:21)
    at t.extract (/Users/vincent/dev/packager/node_modules/javascript-obfuscator/dist/webpack:/JavaScriptObfuscator/src/node-transformers/converting-transformers/ObjectExpressionKeysTransformer.ts:104:36)
    at transformNode (/Users/vincent/dev/packager/node_modules/javascript-obfuscator/dist/webpack:/JavaScriptObfuscator/src/node-transformers/converting-transformers/ObjectExpressionKeysTransformer.ts:67:33)
    at Controller.visitorFunction (/Users/vincent/dev/packager/node_modules/javascript-obfuscator/dist/webpack:/JavaScriptObfuscator/src/node-transformers/TransformersRunner.ts:111:55)
    at Controller.__execute (/Users/vincent/dev/packager/node_modules/estraverse/estraverse.js:397:31)
    at Controller.replace (/Users/vincent/dev/packager/node_modules/estraverse/estraverse.js:637:27)

Steps to Reproduce (for bugs)

  1. create a file with the above code in a npm project
  2. run the code
  3. bug occurs

Your Environment

No related to environment, even on https://obfuscator.io/ it happens as well, the website does not obfuscate the code and displays TypeError: Cannot read property 'type' of undefined

Stack trace

See ‘Current Behavior’ section

Minimal working example that will help to reproduce issue

See code sample at top of bug report

Other notes

array spread operator like […arr1, …arr2] does not create this problem, only {…obj1, …obj2} does.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
zamotkincommented, Nov 20, 2019

The bug is fixed by PR above.

1reaction
sanex3339commented, Nov 20, 2019

Thank you! I will look into this tomorrow. Expect release with fix tomorrow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript ES6 spread operator on undefined - Stack Overflow
When the spread operator is activated on undefined an error is raised, although when the undefined is inside an object, an empty object...
Read more >
A spread, a rest and empty values - DEV Community ‍ ‍
Uncaught TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)). Unlike object spread, the array spread doesn't ...
Read more >
You Can Safely Apply Object-Spread To Null And Undefined ...
Ben Nadel demonstrates that the Object-Spread operator in TypeScript 3.2.4 can be safely applied to both Null and Undefined values.
Read more >
Javascript obfuscation techniques by example - Trickster Dev
We will experiment with some online tools that obfuscate the JS code. Hexadecimal string encoding. If we go to the uncreatively named Javascript ......
Read more >
"Object spread" | Can I use... Support tables for HTML5, CSS3 ...
JavaScript operator: Spread syntax (...): Spread in object literals · Global · Chrome · Edge * · Safari · Firefox · Opera ·...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found