js-confuser icon indicating copy to clipboard operation
js-confuser copied to clipboard

object extraction fails with `const`

Open j4k0xb opened this issue 2 years ago • 3 comments

Describe the bug:

object extraction of a const variable errors at runtime

Config and Small code sample

Config:

{
  compact: true,
  identifierGenerator: 'randomized',
  objectExtraction: true,
  target: 'browser',
}

Code:

const obj = {prop: 0};
obj.prop = 1;
console.log(obj.prop);

Expected behavior

The program should output 1

Actual behavior

TypeError: Assignment to constant variable.

obfuscated code:

const obj_prop=0;!(obj_prop=1,console['log'](obj_prop))

Additional context

an easy fix would probably be to convert the declaration to let or var

j4k0xb avatar Jul 15 '23 19:07 j4k0xb

Great find, will fix this for next release.

MichaelXF avatar Jul 16 '23 01:07 MichaelXF

just found an edge case: const can only be safely converted to let instead of var because of closures:

for (let i = 0; i < 3; i++) {
  const obj = { prop: i };

  setTimeout(() => {
    console.log(obj.prop); // outputs 0, 1, 2
  }, 100);
}

after obfuscation: 2, 2, 2

j4k0xb avatar Sep 14 '23 17:09 j4k0xb

I will get to work on this

MichaelXF avatar Sep 14 '23 20:09 MichaelXF