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

1.13 Fix kafka metadata to HTTP headers conversion of invalid characters #3512

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions common/component/kafka/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package kafka
import (
"errors"
"fmt"
"net/url"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -205,14 +206,14 @@ func GetEventMetadata(message *sarama.ConsumerMessage) map[string]string {
if message != nil {
metadata := make(map[string]string, len(message.Headers)+5)
if message.Key != nil {
metadata[keyMetadataKey] = string(message.Key)
metadata[keyMetadataKey] = url.QueryEscape(string(message.Key))
}
metadata[offsetMetadataKey] = strconv.FormatInt(message.Offset, 10)
metadata[topicMetadataKey] = message.Topic
metadata[timestampMetadataKey] = strconv.FormatInt(message.Timestamp.UnixMilli(), 10)
metadata[partitionMetadataKey] = strconv.FormatInt(int64(message.Partition), 10)
for _, header := range message.Headers {
metadata[string(header.Key)] = string(header.Value)
metadata[string(header.Key)] = url.QueryEscape(string(header.Value))
}
return metadata
}
Expand Down
28 changes: 28 additions & 0 deletions common/component/kafka/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package kafka

import (
"fmt"
"net/url"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -456,4 +457,31 @@ func TestGetEventMetadata(t *testing.T) {
act := GetEventMetadata(nil)
require.Nil(t, act)
})

t.Run("key with invalid value escaped", func(t *testing.T) {
keyValue := "key1\xFF"
escapedKeyValue := url.QueryEscape(keyValue)

m := sarama.ConsumerMessage{
Headers: nil, Timestamp: ts, Key: []byte(keyValue), Value: []byte("MyValue"), Partition: 0, Offset: 123, Topic: "TestTopic",
}
act := GetEventMetadata(&m)
require.Equal(t, escapedKeyValue, act[keyMetadataKey])
})

t.Run("header with invalid value escaped", func(t *testing.T) {
headerKey := "key1"
headerValue := "value1\xFF"
escapedHeaderValue := url.QueryEscape(headerValue)

headers := []*sarama.RecordHeader{
{Key: []byte(headerKey), Value: []byte(headerValue)},
}
m := sarama.ConsumerMessage{
Headers: headers, Timestamp: ts, Key: []byte("MyKey"), Value: []byte("MyValue"), Partition: 0, Offset: 123, Topic: "TestTopic",
}
act := GetEventMetadata(&m)
require.Len(t, act, 6)
require.Equal(t, escapedHeaderValue, act[headerKey])
})
}
Loading