Skip to content

fix: update aes.js temp variable so it is minified correctly#1134

Closed
kendhia wants to merge 1 commit intodigitalbazaar:mainfrom
kendhia:patch-1
Closed

fix: update aes.js temp variable so it is minified correctly#1134
kendhia wants to merge 1 commit intodigitalbazaar:mainfrom
kendhia:patch-1

Conversation

@kendhia
Copy link

@kendhia kendhia commented Jan 31, 2026

I am using node-forge in an nextjs app, and it works fine in development mode. But, the library was throwing an error only in the production minified version.
(b = c).length();typeError: b.length is not a function

The minified code looked like:

                    c = [];
                    var e = (b = c).length();
                    if (16 === e || 24 === e || 32 === e) {
                        e >>>= 2;
                        for (var d = 0; d < e; ++d)
                            c.push(b.getInt32())
                    }

Which was throwing the error also reported in this other issue. But, the fix suggested there causes another problem (since the minified code got b=c, b's length is always 0. So it will always fail!).

In current implementation, the key is indeed an object and not an array so var len = tmp.length(); is correct.
The fix that got the minified code to have correct implementation and worked in production mode as in development mode is only to remove the class level tmp variable and use a one that is defined in the scope of if clause.

// convert key byte buffer into 32-bit integer array
  if(!forge.util.isArray(key)) {
    const _tmp = key;
    key = [];

    // key lengths of 16, 24, 32 bytes allowed
    var len = _tmp.length();
    if(len === 16 || len === 24 || len === 32) {
      len = len >>> 2;
      for(var i = 0; i < len; ++i) {
        key.push(_tmp.getInt32());
      }
    }
  }

Now the minified version has _tmp variable initiated correctly and it does not fail.

@davidlehn
Copy link
Member

Interesting. What minifier is doing this? Seems like a bug there. Looks like the (old) webpack used to create min files here is doing the right thing, if i'm reading the js-beautify processed code correctly.

Is there a way to write a test for that? Seems kind of difficult since the code is probably correct?

Does it still work if you avoid the rename, drop the outer var tmp;, and use var tmp = key; in both inner blocks? Simplifies to a nice 3 liner change.

A better fix is to support and use new features like const and let. This library predates those! Could try and see, but this should stay in var land for now.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants