Skip to content

fix(microservices): clone the kafka record before serializing - #18056

Merged
kamilmysliwiec merged 2 commits into
nestjs:masterfrom
Mihir-Rabari:fix/kafka-serializer-record-mutation
Oct 9, 2026
Merged

kamilmysliwiec merged 2 commits into
nestjs:masterfrom
Mihir-Rabari:fix/kafka-serializer-record-mutation

Conversation

@Mihir-Rabari

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix

What is the current behavior?

KafkaRequestSerializer.serialize() mutates the record the caller passes in: when the input is a Kafka message (key/value present), it encodes value/key in place on that object and returns the same reference. ClientKafka then writes kafka_correlationId, kafka_replyTopic and kafka_replyPartition into the headers of that same object when publishing the message.

So with client.send('topic', { key, value, headers }):

  • the caller's value is left as a JSON-encoded string,
  • the caller's headers object 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 metadata ClientKafka writes 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?

  • Yes
  • No

Other information

Added a serialize without mutating the caller record describe 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; targeted kafka-request.serializer.spec.ts 16/16; oxlint and prettier --check clean on the changed files.

Mihir-Rabari and others added 2 commits October 9, 2026 05:14
…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
kamilmysliwiec merged commit 8d6c9f4 into nestjs:master Oct 9, 2026
2 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants