From 6f7200d4af19cc44ccffd6a189e21621f7198edb Mon Sep 17 00:00:00 2001 From: Cayman Date: Wed, 8 Jan 2025 11:20:18 -0500 Subject: [PATCH] chore: separate code paths for ChunkTypes --- .../sszSnappy/snappyFrames/uncompress.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/reqresp/src/encodingStrategies/sszSnappy/snappyFrames/uncompress.ts b/packages/reqresp/src/encodingStrategies/sszSnappy/snappyFrames/uncompress.ts index 1617eb8d500..5ecb4a40aac 100644 --- a/packages/reqresp/src/encodingStrategies/sszSnappy/snappyFrames/uncompress.ts +++ b/packages/reqresp/src/encodingStrategies/sszSnappy/snappyFrames/uncompress.ts @@ -46,12 +46,21 @@ export class SnappyFramesUncompress { case ChunkType.PADDING: case ChunkType.SKIPPABLE: continue; - case ChunkType.COMPRESSED: - case ChunkType.UNCOMPRESSED: { + case ChunkType.COMPRESSED: { const checksum = frame.subarray(0, 4); const data = frame.subarray(4); - const uncompressed = type === ChunkType.COMPRESSED ? uncompress(data, UNCOMPRESSED_CHUNK_SIZE) : data; + const uncompressed = uncompress(data, UNCOMPRESSED_CHUNK_SIZE); + if (crc(uncompressed).compare(checksum) !== 0) { + throw "malformed input: bad checksum"; + } + result.append(uncompressed); + break; + } + case ChunkType.UNCOMPRESSED: { + const checksum = frame.subarray(0, 4); + const uncompressed = frame.subarray(4); + if (uncompressed.length > UNCOMPRESSED_CHUNK_SIZE) { throw "malformed input: too large"; } @@ -59,6 +68,7 @@ export class SnappyFramesUncompress { throw "malformed input: bad checksum"; } result.append(uncompressed); + break; } } }