Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Regression: Spread operator with conditional object fails again in v5.1.0 (similar to #437) #1372

@NotYoojun

Description

@NotYoojun

This looks like a regression of issue #437. That issue was marked as fixed, but the problem seems to have resurfaced in the latest release (5.1.0). It also appears not to be limited to transformObjectKeys.

Steps to Reproduce

Try to obfuscate the following script

function test(hasColor)
{
  const accent = hasColor ? "primary" : undefined;
  const theme = { palette: { primary: { main: "ok" } }};
  
  const obj = 
  {
    bgcolor: "test",
    ...(accent && { color: theme.palette[accent].main })
  }
  
  console.log(obj)
}

test(true);
test(false);

using the following config:

compact: true,
controlFlowFlattening: true,
transformObjectKeys: true, // Seems like either true or false can cause this problem
stringArray: true,

Expected Behavior

Running the obfuscated code should consistently produce:

{ bgcolor: 'test', color: 'ok' }
{ bgcolor: 'test' }

Actual Behavior

Execution is non-deterministic. Sometimes it works as expected, but other times it throws:

{ bgcolor: 'test', color: 'ok' }
TypeError: Cannot read properties of undefined (reading 'main')
    at test (eval at <anonymous> (D:\tmp\test-obfuscator.js:33:13), <anonymous>:1:1187)
    at eval (eval at <anonymous> (D:\tmp\test-obfuscator.js:33:13), <anonymous>:1:1540)
    at Object.<anonymous> (D:\tmp\test-obfuscator.js:33:13)
    at Module._compile (node:internal/modules/cjs/loader:1706:14)
    at Object..js (node:internal/modules/cjs/loader:1839:10)
    at Module.load (node:internal/modules/cjs/loader:1441:32)
    at Function._load (node:internal/modules/cjs/loader:1263:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:237:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5)
    ...

JavaScript Obfuscator Edition

  • JavaScript Obfuscator Open Source

Your Environment

  • Obfuscator version used: 5.1.0
  • Node version used: 22.19.0

Minimal Working Example

Run this a few times, very simple, then you will get the error.

const obfuscator = require('javascript-obfuscator');

const code = `

function test(hasColor)
{
  const accent = hasColor ? "primary" : undefined;
  const theme = { palette: { primary: { main: "ok" } }};
  
  const obj = 
  {
    bgcolor: "test",
    ...(accent && { color: theme.palette[accent].main })
  }
  
  console.log(obj)
}

test(true);
test(false);

`;

const obfuscatedCode = obfuscator.obfuscate(code, {
  compact: true,
  controlFlowFlattening: true,
  transformObjectKeys: true,
  stringArray: true,
});

console.log(eval(obfuscatedCode.getObfuscatedCode()));

If it doesn’t throw immediately, try running multiple times - the error appears when accent is undefined.
The spread operator with conditional object seems to be incorrectly transformed, leading to theme.palette[undefined].

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions