Skip to content

Commit

Permalink
fix: prevent ERR_OUT_OF_RANGE errors when reading messageSize from bu…
Browse files Browse the repository at this point in the history
…ffer (adelsz#571)
  • Loading branch information
nbarnett authored and zth committed Apr 27, 2024
1 parent 4adb2b5 commit fbe031e
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/wire/src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,16 @@ export const parseMessage = <Params extends object>(
messageOffset: number = 0,
): ParseResult<Params> => {
let bufferOffset = messageOffset;

// Check if we have enough data to read the indicator and message size
// The + 5 is made up of 1 byte for readInt8 and 4 bytes for readUInt32BE
if (bufferOffset + 5 > buf.length) {
return {
type: "IncompleteMessageError",
messageName: message.name,
};
}

const indicator = buf.readInt8(bufferOffset);
const expectedIndicator = message.indicator.charCodeAt(0);
const isUnexpectedErrorMessage =
Expand Down

0 comments on commit fbe031e

Please sign in to comment.