Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turnary Operator Executes wrong/both paths with "--transform-object-keys true" #948

Closed
rodhoward opened this issue Jul 6, 2021 · 4 comments

Comments

@rodhoward
Copy link

Using the "?" Turnary operator a function in the "dead" branch is being executed when it shouldn't be.

Expected Behavior

Expected only the one (correct) path to be executed.

Current Behavior

const isTrue = something => !!(something?.bob || something?.sally);
const throwsError = () => {
  throw new Error("Should not be here!");
};
const myFunction = () => {
  return isTrue() ? { my: "object", anotherParam: throwsError() } : "The only place we should be";
};

console.log(myFunction());

Throws error!

Steps to Reproduce

  1. save above code. "test.js"
  2. npx javascript-obfuscator test.js --transform-object-keys true
  3. node test-obfuscated.js

Expected string "the only place we should be" but getting exception "Should not be here!".

FYI: Without the transform-object-keys option it seems to be working fine.

Your Environment

  • Obfuscator version used: "2.12.0"
  • Node version used: v14.15.3
@sanex3339
Copy link
Member

Hmm, this is a hard case to track.
Seems to find these cases we have to traverse up to all parent nodes of each ObjectExpression node until we found the host node of ObjectExpression. If we found ConditionalExpression or LogicalExpression node, then we have to traverse over ObjectExpression node and if we found CallExpression node, then this is our case.

But this is too many additional operations just to cover this case.

@sanex3339
Copy link
Member

sanex3339 commented Jul 6, 2021

Also, we may add more simple logic - if ObjectExpression contains CallExpression node in any place - ignore transformation of this ObjectExpression. But this will ignore a huge amount of ObjectExpression nodes.

@sanex3339
Copy link
Member

PR:
#951

@sanex3339
Copy link
Member

Released as javascript-obfuscator@2.15.6. For more performance, i used the following logic:

If ObjectExpression contains CallExpression or NewExpression nodes at any place - transformation of this ObjectExpression will be completely ignored

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants