Repository navigation
fix(microservices): clone the kafka record before serializing - #18056
Merged
kamilmysliwiec merged 2 commits intoOct 9, 2026
Merged
kamilmysliwiec merged 2 commits into
kamilmysliwiec merged 2 commits into
Conversation
…ture Cast the record once instead of narrowing it, which tsc rejected, and keep serialize() typed as returning any so that subclasses with an async serialize() still type-check.
kamilmysliwiec
added a commit
that referenced
this pull request
Oct 16, 2026
KafkaRequestSerializer treats an object with a value or key as a ready-made Kafka record and, since #18056, copies its headers with a spread whatever they are. A string spreads into one header per character, so a request of about 16 kB (gzip-compressed) to a @MessagePattern handler that returns the received object made its reply carry 16 million headers, which kafkajs encodes at about 1 kB each until the heap is exhausted. Headers that are not an object, or that are an array, are now sent as no headers, as missing headers already are. Class instances are still copied. Behaviour change: a record whose headers are an array used to be sent with headers named after its indexes; it is now sent without headers. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-SGX8D2U5.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
KafkaRequestSerializer.serialize()mutates the record the caller passes in: when the input is a Kafka message (key/valuepresent), it encodesvalue/keyin place on that object and returns the same reference.ClientKafkathen writeskafka_correlationId,kafka_replyTopicandkafka_replyPartitioninto the headers of that same object when publishing the message.So with
client.send('topic', { key, value, headers }):valueis left as a JSON-encoded string,headersobject ends up carrying the correlation metadata of the first send — a record that is reused or retried goes out with a stale correlation id, so the reply gets misrouted or dropped.Issue Number: N/A
What is the new behavior?
The serializer now clones the record before encoding: a shallow copy of the message with a fresh copy of
headers(empty object when unset), encoding applied on the copy, and the copy returned. The caller's record and headers are left untouched, and the correlation metadataClientKafkawrites lands on the copy that is actually published. The output shape is unchanged for every previously covered input.Same class of fix as #17925 (a serializer/helper mutating a caller-owned object).
Does this PR introduce a breaking change?
Other information
Added a
serialize without mutating the caller recorddescribe block with regression specs covering: record with headers, record without headers, and headers being safely extensible by the client after serialization.Verification:
npx vitest run packages/microservices/test→ 68 test files / 1082 tests green; targetedkafka-request.serializer.spec.ts16/16;oxlintandprettier --checkclean on the changed files.