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

Error on class static field #116

Open
fgeorges opened this issue Nov 23, 2023 · 5 comments
Open

Error on class static field #116

fgeorges opened this issue Nov 23, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@fgeorges
Copy link

Describe the bug:

When the code to confuse contains a class with a static field, an error is thrown and code generation does not complete.

Config and Small code sample

Repro:

require("js-confuser").obfuscate(`
  class C {
    static v = 42;
  }
`, {
  target: "node",
  preset: "low",
}).then(obfuscated => {
  console.log(obfuscated)
})

Expected behavior

This example should pass and generate the corresponding, confused code.

Actual behavior

It throws an error:

xxx/node_modules/escodegen/escodegen.js:2525
        result = this[type](expr, precedence, flags);
                           ^

TypeError: this[type] is not a function
    at CodeGenerator.generateExpression (xxx/node_modules/escodegen/escodegen.js:2525:28)
    at xxx/node_modules/escodegen/escodegen.js:1133:38
    at withIndent (xxx/node_modules/escodegen/escodegen.js:581:9)
    at CodeGenerator.ClassBody (xxx/node_modules/escodegen/escodegen.js:1128:13)
    at CodeGenerator.generateStatement (xxx/node_modules/escodegen/escodegen.js:2538:33)
    at CodeGenerator.ClassDeclaration (xxx/node_modules/escodegen/escodegen.js:1159:30)
    at CodeGenerator.generateStatement xxx/node_modules/escodegen/escodegen.js:2538:33)
    at CodeGenerator.Program (xxx/node_modules/escodegen/escodegen.js:1718:43)
    at CodeGenerator.generateStatement (xxx/node_modules/escodegen/escodegen.js:2538:33)
    at generateInternal (xxx/node_modules/escodegen/escodegen.js:2559:28)

Additional context

It seems that in CodeGenerator.prototype.generateExpression(), the expression this[type] is undefined when type is PropertyDefinition.

@fgeorges fgeorges added the bug Something isn't working label Nov 23, 2023
@j4k0xb
Copy link

j4k0xb commented Nov 27, 2023

you can work around it with the es5 option

estools/escodegen#443, estools/escodegen#438
but it would be a good idea to switch to a more maintained escodegen fork or different lib (maybe https://github.com/davidbonnet/astring)

@fgeorges
Copy link
Author

Thank you, @j4k0xb, using es5: true works!

For the suggestions about escodegen and/or astring, is it rather for @MichaelXF, or is it possible to change the generation lib by configuration also?

@j4k0xb
Copy link

j4k0xb commented Nov 29, 2023

for michael

@fgeorges
Copy link
Author

fgeorges commented Nov 30, 2023

Thank you! Just for the record, I have another issue, when I use es5. Repro code:

const code = `
function f() {}
() => { f(); };
`;

const options = {
    target: 'node',
    preset: 'low',
    es5: true,
};

require('js-confuser')
    .obfuscate(code, options)
    .then(t => console.log(t))

When executed, I get the following error (sometimes, not all the time, but I'd say above 50%):

node:internal/process/promises:288
            triggerUncaughtException(err, true / * fromPromise * /);
            ^

AssertionError [ERR_ASSERTION]: Should be a BlockStatement
    at /xxx/node_modules/js-confuser/dist/transforms/es5/es5.js:84:22
    at walk (/xxx/node_modules/js-confuser/dist/traverse.js:76:7)
    at walk (/xxx/node_modules/js-confuser/dist/traverse.js:63:15)
    at walk (/xxx/node_modules/js-confuser/dist/traverse.js:54:13)
    at walk (/xxx/node_modules/js-confuser/dist/traverse.js:63:15)
    at traverse (/xxx/node_modules/js-confuser/dist/traverse.js:97:3)
    at AntiArrowFunction.apply (/xxx/node_modules/js-confuser/dist/transforms/transform.js:136:27)
    at /xxx/node_modules/js-confuser/dist/transforms/transform.js:131:32
    at Array.forEach (<anonymous>)
    at ES5.apply (/xxx/node_modules/js-confuser/dist/transforms/transform.js:131:17) {
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: false,
  expected: true,
  operator: '=='
}

Node.js v18.18.2

When it fails, it is indeed not a BlockStatement, but instead a SequenceExpression.

If I change the test code from:

function f() {}
() => { f(); };

to:

function f() {}
() => {
    const f = f;
    f();
};

then it never fails anymore.

Just in case it can help if/when testing other libs.

@j4k0xb
Copy link

j4k0xb commented Nov 30, 2023

minify runs before es5 and theyre kinda incompatible 🤔

object.body = SequenceExpression([

ok(object.body.type == "BlockStatement", "Should be a BlockStatement");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants