Skip to content

ERR_BUFFER_OUT_OF_BOUNDS when buffer is less than 4 bytes #280

@joaosenna19

Description

@joaosenna19

Node version: 22
Lib version: 0.6.2
We encountered an issue affecting several applications that consume messages from our RabbitMQ stream. All affected apps produced the following error:

2025-10-22T13:55:01.066416198Z node:internal/buffer:86
2025-10-22T13:55:01.066458598Z     throw new ERR_BUFFER_OUT_OF_BOUNDS();
2025-10-22T13:55:01.066465598Z     ^
2025-10-22T13:55:01.066469798Z 
2025-10-22T13:55:01.066473898Z RangeError [ERR_BUFFER_OUT_OF_BOUNDS]: Attempt to access memory outside buffer bounds
2025-10-22T13:55:01.066477698Z     at boundsError (node:internal/buffer:86:11)
2025-10-22T13:55:01.066481998Z     at Buffer.readUInt32BE (node:internal/buffer:311:5)
2025-10-22T13:55:01.066486598Z     at Connection.received (/app/node_modules/rabbitmq-stream-js-client/dist/connection.js:284:61)
2025-10-22T13:55:01.066492698Z     at TLSSocket.<anonymous> (/app/node_modules/rabbitmq-stream-js-client/dist/connection.js:126:22)
2025-10-22T13:55:01.066496898Z     at TLSSocket.emit (node:events:519:28)
2025-10-22T13:55:01.066500898Z     at addChunk (node:internal/streams/readable:561:12)
2025-10-22T13:55:01.066505198Z     at readableAddChunkPushByteMode (node:internal/streams/readable:512:3)
2025-10-22T13:55:01.066537198Z     at Readable.push (node:internal/streams/readable:392:5)
2025-10-22T13:55:01.066543398Z     at TLSWrap.onStreamRead (node:internal/stream_base_commons:189:23) {
2025-10-22T13:55:01.066547098Z   code: 'ERR_BUFFER_OUT_OF_BOUNDS'
2025-10-22T13:55:01.066550898Z }

After some investigation, we noticed that line 284 of the connection.ts file was throwing:

this.logger.debug(`Receiving ${data.length} (${data.readUInt32BE()}) bytes ... ${(0, util_1.inspect)(data)}`);

More specifically, the function readUInt32BE(). It throws, because the offset must satisfy: 0 <= offset <= buf.length - 4
We added some logging to log the buffer and that is what was coming (attaching a file, bc the buffer is long):

log.txt

Once it reached the 2 bytes chunk, it would throw. From what we understand, this function is attempting to read the frame size according to the RabbitMQ Streams Protocol Reference

Our temporary workaround was to comment out the line and patch the library.
We attempted to reproduce this with the C# client, and it does not throw. for the exact same message. This might suggest that the issue is related to how the lib handles TCP stream framing and small chunks.

Happy to provide more details if needed!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions