Skip to content

Commit 3aa0266

Browse files
authored
Do not validate snappy xerial header version and compat fields (#2483)
1 parent 226810c commit 3aa0266

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

Diff for: kafka/codec.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,15 @@ def _detect_xerial_stream(payload):
193193
"""
194194

195195
if len(payload) > 16:
196-
header = struct.unpack('!' + _XERIAL_V1_FORMAT, bytes(payload)[:16])
197-
return header == _XERIAL_V1_HEADER
196+
magic = struct.unpack('!' + _XERIAL_V1_FORMAT[:8], bytes(payload)[:8])
197+
version, compat = struct.unpack('!' + _XERIAL_V1_FORMAT[8:], bytes(payload)[8:16])
198+
# Until there is more than one way to do xerial blocking, the version + compat
199+
# fields can be ignored. Also some producers (i.e., redpanda) are known to
200+
# incorrectly encode these as little-endian, and that causes us to fail decoding
201+
# when we otherwise would have succeeded.
202+
# See https://github.com/dpkp/kafka-python/issues/2414
203+
if magic == _XERIAL_V1_HEADER[:8]:
204+
return True
198205
return False
199206

200207

Diff for: test/test_codec.py

+2
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ def test_snappy_detect_xerial():
3939
_detect_xerial_stream = kafka1.codec._detect_xerial_stream
4040

4141
header = b'\x82SNAPPY\x00\x00\x00\x00\x01\x00\x00\x00\x01Some extra bytes'
42+
redpanda_header = b'\x82SNAPPY\x00\x01\x00\x00\x00\x01\x00\x00\x00Some extra bytes'
4243
false_header = b'\x01SNAPPY\x00\x00\x00\x01\x00\x00\x00\x01'
4344
default_snappy = snappy_encode(b'foobar' * 50)
4445
random_snappy = snappy_encode(b'SNAPPY' * 50, xerial_compatible=False)
4546
short_data = b'\x01\x02\x03\x04'
4647

4748
assert _detect_xerial_stream(header) is True
49+
assert _detect_xerial_stream(redpanda_header) is True
4850
assert _detect_xerial_stream(b'') is False
4951
assert _detect_xerial_stream(b'\x00') is False
5052
assert _detect_xerial_stream(false_header) is False

0 commit comments

Comments
 (0)