Skip to content

Conversation

@bigdale123
Copy link

So I think I encountered a typo in the aes.js lib file. On line 203, the length of tmp is found using tmp.length(). But,
a) this results in a type error complaining that tmp.length() is not a function (See Issue #552
b) it is done the correct way (at least, as far as I can tell) not 10 lines earlier. I have provided an excerpt from aes.js:

183 |  if(typeof key === 'string' &&
184 |    (key.length === 16 || key.length === 24 || key.length === 32)) {
185 |    // convert key string into byte buffer
186 |    key = forge.util.createBuffer(key);
187 |  } else if(forge.util.isArray(key) &&
188 |    (key.length === 16 || key.length === 24 || key.length === 32)) {
189 |    // convert key integer array into byte buffer
190 |    tmp = key;
191 |    key = forge.util.createBuffer();
192 |    for(var i = 0; i < tmp.length; ++i) {
193 |      key.putByte(tmp[i]);
194 |    }
195 |  }
196 |
197 |  // convert key byte buffer into 32-bit integer array
198 |  if(!forge.util.isArray(key)) {
199 |    tmp = key;
200 |    key = [];
201 |
202 |    // key lengths of 16, 24, 32 bytes allowed
203 |    var len = tmp.length();
204 |    if(len === 16 || len === 24 || len === 32) {
205 |      len = len >>> 2;
206 |      for(var i = 0; i < len; ++i) {
207 |        key.push(tmp.getInt32());
208 |      }
209 |    }
210 |  }

So this pull request addresses that by chaning line 203 from var len = tmp.length(); to var len = tmp.length;
Hope i'm not wrong about it, but it seems like a no brainer.

Copy link

@cyber-hari cyber-hari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@kendhia
Copy link

kendhia commented Jan 31, 2026

I came across this solution as I had same error but only once node-forge being used in a nextjs app is minified/bundled for production.
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 reported in this issue. But, the fix suggested causes another problem.
Since the minified code got b=c, b's length is always 0. So it will always fail!

The key is indeed an object and not an array so var len = tmp.length(); is correct.

The fix that worked for me is only to use a new temp variable so the minified version of the code is also correct.

// 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());
      }
    }
  }

@kendhia
Copy link

kendhia commented Jan 31, 2026

This PR wasn't active for a while. So I created a new one #1134

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.

3 participants