Skip to content

grpc-js: fix Buffer-only .copy() crash on Uint8Array messages in CompressionFilter#3066

Open
chatman-media wants to merge 1 commit into
grpc:masterfrom
chatman-media:fix-compression-filter-uint8array
Open

grpc-js: fix Buffer-only .copy() crash on Uint8Array messages in CompressionFilter#3066
chatman-media wants to merge 1 commit into
grpc:masterfrom
chatman-media:fix-compression-filter-uint8array

Conversation

@chatman-media

Copy link
Copy Markdown

CompressionFilter's writeMessage (both the shared base implementation and IdentityHandler's override) calls message.copy(output, 5) to frame the outgoing message. .copy() only exists on Buffer, but the message here comes straight from the caller's serialize function, which is only nominally typed as returning a Buffer — nothing enforces that at runtime. protobufjs in particular falls back to a plain Uint8Array from Writer.finish() when its optional Buffer util isn't available (some bundlers strip it out), so this can throw TypeError: message.copy is not a function deep inside grpc-js with basically no context for whoever hits it.

Buffer.prototype.set (inherited from Uint8Array) does the same job and works for both types, so this just swaps .copy() for .set() in both spots. Added a test that sends a Uint8Array payload through both the identity path and a non-identity handler with the NoCompress flag, since both call sites had the same bug.

Ref #3050.

… compression filter

CompressionFilter's writeMessage methods call message.copy(output, 5) to frame
outgoing messages. copy() only exists on Buffer, so if a request serializer
hands back a plain Uint8Array instead (protobufjs does this when its Buffer
util is unavailable, e.g. under some bundlers), this throws
'TypeError: message.copy is not a function' deep inside grpc-js with no
useful context. Buffer.prototype.set works for both types, so swapping to
that keeps the framing logic identical while fixing the crash. Added a test
that exercises both the identity and non-identity (NoCompress) code paths
with a Uint8Array payload.
@linux-foundation-easycla

Copy link
Copy Markdown

CLA Not Signed

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.

1 participant