Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(microservices): prevent kafka parser from modifying received message #9996

Merged
merged 2 commits into from Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions packages/microservices/helpers/kafka-parser.ts
Expand Up @@ -9,22 +9,28 @@ export class KafkaParser {
}

public parse<T = any>(data: any): T {
// Duplicate the object to not modify the original one (would break KafkaJS retries)
kamilmysliwiec marked this conversation as resolved.
Show resolved Hide resolved
const result = {
...data,
headers: { ...data.headers },
};

if (!this.keepBinary) {
data.value = this.decode(data.value);
result.value = this.decode(data.value);
}

if (!isNil(data.key)) {
data.key = this.decode(data.key);
result.key = this.decode(data.key);
}
if (!isNil(data.headers)) {
const decodeHeaderByKey = (key: string) => {
data.headers[key] = this.decode(data.headers[key]);
result.headers[key] = this.decode(data.headers[key]);
};
Object.keys(data.headers).forEach(decodeHeaderByKey);
} else {
data.headers = {};
result.headers = {};
}
return data;
return result;
}

public decode(value: Buffer): object | string | null | Buffer {
Expand Down
4 changes: 2 additions & 2 deletions packages/microservices/test/client/client-kafka.spec.ts
Expand Up @@ -76,7 +76,7 @@ describe('ClientKafka', () => {
message,
{
size: 0,
value: { test: true },
value: Buffer.from(JSON.stringify({ test: true })),
},
),
heartbeat,
Expand Down Expand Up @@ -488,7 +488,7 @@ describe('ClientKafka', () => {
expect(
callback.calledWith({
isDisposed: true,
response: payloadDisposed.message.value,
response: deserializedPayloadDisposed.message.value,
err: undefined,
}),
).to.be.true;
Expand Down
30 changes: 30 additions & 0 deletions packages/microservices/test/helpers/kafka-parser.spec.ts
Expand Up @@ -126,5 +126,35 @@ describe('KafkaParser', () => {
},
});
});

it('parse message multiple times (simulate retry)', () => {
const message = {
headers: {
[KafkaHeaders.CORRELATION_ID]: Buffer.from('correlation-id'),
},
value: Buffer.from(JSON.stringify({ prop: 'value' })),
key: Buffer.from('1'),
};
const expectedParsedMessage = {
key: '1',
value: {
prop: 'value',
},
headers: {
[KafkaHeaders.CORRELATION_ID]: 'correlation-id',
},
};
expect(kafkaParser.parse(message)).to.deep.eq(expectedParsedMessage);
// Parse message again and verify it still works correctly
expect(kafkaParser.parse(message)).to.deep.eq(expectedParsedMessage);
// Verify message was not modified
expect(message).to.deep.eq({
headers: {
[KafkaHeaders.CORRELATION_ID]: Buffer.from('correlation-id'),
},
value: Buffer.from(JSON.stringify({ prop: 'value' })),
key: Buffer.from('1'),
});
});
});
});