Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
## Unreleased

### Fixes
- Fix `TopicPartition.metadata` reading past the end of committed metadata that has no
NUL terminator, as stored by clients such as kafka-python or the Java client. Reading
such offsets (`Consumer.committed()`, `AdminClient.list_consumer_group_offsets()`)
returned trailing heap bytes, raised `UnicodeDecodeError`, or crashed with SIGSEGV (#2430).
- Fix `AvroDeserializer` rejecting messages of 5 bytes or fewer when the schema GUID is
sent in a header (`header_schema_id_serializer`), for example `int` keys or short
strings. The length check now runs where the schema id framing is parsed, so it
Expand Down
17 changes: 12 additions & 5 deletions src/confluent_kafka/src/confluent_kafka.c
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,7 @@ static void TopicPartition_setup(TopicPartition *self,
long long offset,
int32_t leader_epoch,
const char *metadata,
size_t metadata_size,
rd_kafka_resp_err_t err) {
self->topic = strdup(topic);
self->partition = partition;
Expand All @@ -1483,7 +1484,11 @@ static void TopicPartition_setup(TopicPartition *self,
self->leader_epoch = leader_epoch;

if (metadata != NULL) {
self->metadata = strdup(metadata);
/* metadata is not guaranteed to be NUL-terminated:
* librdkafka passes it as a buffer plus metadata_size. */
self->metadata = malloc(metadata_size + 1);
memcpy(self->metadata, metadata, metadata_size);
self->metadata[metadata_size] = '\0';
} else {
self->metadata = NULL;
}
Expand Down Expand Up @@ -1519,7 +1524,8 @@ TopicPartition_init(PyObject *self, PyObject *args, PyObject *kwargs) {
}

TopicPartition_setup((TopicPartition *)self, topic, partition, offset,
leader_epoch, metadata, 0);
leader_epoch, metadata,
metadata ? strlen(metadata) : 0, 0);
return 0;
}

Expand Down Expand Up @@ -1731,14 +1737,15 @@ static PyObject *TopicPartition_new0(const char *topic,
long long offset,
int32_t leader_epoch,
const char *metadata,
size_t metadata_size,
rd_kafka_resp_err_t err) {
TopicPartition *self;

self = (TopicPartition *)TopicPartitionType.tp_new(&TopicPartitionType,
NULL, NULL);

TopicPartition_setup(self, topic, partition, offset, leader_epoch,
metadata, err);
metadata, metadata_size, err);

return (PyObject *)self;
}
Expand All @@ -1748,8 +1755,8 @@ static PyObject *TopicPartition_new0(const char *topic,
PyObject *c_part_to_py(const rd_kafka_topic_partition_t *rktpar) {
return TopicPartition_new0(
rktpar->topic, rktpar->partition, rktpar->offset,
rd_kafka_topic_partition_get_leader_epoch(rktpar), rktpar->metadata,
rktpar->err);
rd_kafka_topic_partition_get_leader_epoch(rktpar),
(const char *)rktpar->metadata, rktpar->metadata_size, rktpar->err);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import socket
import struct

from confluent_kafka import TopicPartition

_NOT_COORDINATOR = 16


def commit_and_check(consumer, topic, metadata):
if metadata is None:
Expand Down Expand Up @@ -61,3 +66,86 @@ def test_consumer_topicpartition_metadata(kafka_cluster):
assert 'embedded null character' in str(ve)

c.close()


def _kafka_string(value):
encoded = value.encode()
return struct.pack('>h', len(encoded)) + encoded


def _recv_exact(sock, size):
data = b''
while len(data) < size:
chunk = sock.recv(size - len(data))
if not chunk:
raise ConnectionError('broker closed the connection')
data += chunk
return data


def _offset_commit_v2(host, port, group, topic, partitions, offset, metadata):
"""
Commit offsets with a hand-built OffsetCommit v2 request.

confluent-kafka always commits metadata with its NUL terminator, so this
is the simplest way to store metadata of its exact length, as kafka-python
and the Java client do.

:returns: The set of per-partition error codes.
"""
request = struct.pack('>hhi', 8, 2, 1) + _kafka_string('test-raw-commit')
request += _kafka_string(group) + struct.pack('>i', -1) + _kafka_string('') + struct.pack('>q', -1)
request += struct.pack('>i', 1) + _kafka_string(topic) + struct.pack('>i', len(partitions))
for partition in partitions:
request += struct.pack('>iqh', partition, offset, len(metadata)) + metadata

with socket.create_connection((host, port), timeout=10) as sock:
sock.sendall(struct.pack('>i', len(request)) + request)
(size,) = struct.unpack('>i', _recv_exact(sock, 4))
response = _recv_exact(sock, size)

# Skip the correlation id, then read topics[].partitions[].error_code.
position = 4
errors = set()
(topic_count,) = struct.unpack_from('>i', response, position)
position += 4
for _ in range(topic_count):
(name_length,) = struct.unpack_from('>h', response, position)
position += 2 + name_length
(partition_count,) = struct.unpack_from('>i', response, position)
position += 4
for _ in range(partition_count):
_, error = struct.unpack_from('>ih', response, position)
position += 6
errors.add(error)
return errors


def _commit_to_coordinator(kafka_cluster, group, topic, partitions, offset, metadata):
brokers = kafka_cluster.admin().list_topics(timeout=10).brokers.values()
for broker in brokers:
errors = _offset_commit_v2(broker.host, broker.port, group, topic, partitions, offset, metadata)
if errors != {_NOT_COORDINATOR}:
return errors
raise AssertionError('no broker accepted the commit as group coordinator')


def test_consumer_committed_metadata_without_nul_terminator_returns_committed_bytes(kafka_cluster):
# 248 bytes: glibc rounds this allocation to a chunk whose usable size ends
# exactly at the next chunk's header, so an over-read past the buffer finds
# non-zero bytes and shows up deterministically instead of by chance.
metadata = 'm' * 248
partitions = list(range(8))
topic = kafka_cluster.create_topic_and_wait_propogation(
"test_topicpartition_unterminated", {'num_partitions': len(partitions)}
)
group = 'test_topicpartition_unterminated'

errors = _commit_to_coordinator(kafka_cluster, group, topic, partitions, 1, metadata.encode())
assert errors == {0}

c = kafka_cluster.consumer({'group.id': group})
offsets = c.committed([TopicPartition(topic, p) for p in partitions], timeout=100)
c.close()

assert [tp.metadata for tp in offsets] == [metadata] * len(partitions)