Skip to content

Commit adfe3df

Browse files
authored
Merge pull request #308 from zhanghao0806/fix/kafka-ignore-unrelated-binary-headers
fix(kafka): ignore unrelated binary header values
2 parents 24842fd + b5f5c40 commit adfe3df

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎src/cloudevents/core/bindings/kafka.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,17 @@ def from_binary(
165165
attributes: dict[str, Any] = {}
166166

167167
for header_name, header_value_bytes in message.headers.items():
168-
header_value = header_value_bytes.decode("utf-8")
169-
170168
normalized_name = header_name.lower()
171169

172170
if normalized_name.startswith(CE_PREFIX):
171+
header_value = header_value_bytes.decode("utf-8")
173172
attr_name = normalized_name[len(CE_PREFIX) :]
174173
if attr_name == TIME_ATTR:
175174
attributes[attr_name] = isoparse(header_value)
176175
else:
177176
attributes[attr_name] = header_value
178177
elif normalized_name == CONTENT_TYPE_HEADER:
179-
attributes[DATACONTENTTYPE_ATTR] = header_value
178+
attributes[DATACONTENTTYPE_ATTR] = header_value_bytes.decode("utf-8")
180179

181180
# If message has a key, add it as partitionkey extension attribute
182181
if message.key is not None:

‎tests/test_core/test_bindings/test_kafka.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,3 +811,40 @@ def test_from_structured_with_key_auto_detect_v03() -> None:
811811
assert event.get_type() == "com.example.test"
812812
assert event.get_extension("partitionkey") == "partition-key-456"
813813
assert event.get_attributes()["specversion"] == "0.3"
814+
815+
816+
@pytest.mark.parametrize("reader", [from_binary, from_kafka])
817+
def test_binary_ignores_non_cloudevent_binary_headers(reader: Any) -> None:
818+
message = KafkaMessage(
819+
headers={
820+
"ce_specversion": b"1.0",
821+
"ce_id": b"123",
822+
"ce_type": b"example.created",
823+
"ce_source": b"/example",
824+
"content-type": b"application/json",
825+
"correlation-bin": b"\xff\x00\xfe",
826+
},
827+
key=None,
828+
value=b'{"message":"hello"}',
829+
)
830+
event = reader(message, JSONFormat())
831+
assert event.get_attributes()["id"] == "123"
832+
assert event.get_data() == {"message": "hello"}
833+
assert "correlation-bin" not in event.get_attributes()
834+
835+
836+
@pytest.mark.parametrize("header", ["ce_subject", "content-type"])
837+
def test_binary_rejects_invalid_utf8_in_cloudevent_headers(header: str) -> None:
838+
message = KafkaMessage(
839+
headers={
840+
"ce_specversion": b"1.0",
841+
"ce_id": b"123",
842+
"ce_type": b"example.created",
843+
"ce_source": b"/example",
844+
header: b"\xff",
845+
},
846+
key=None,
847+
value=b"",
848+
)
849+
with pytest.raises(UnicodeDecodeError):
850+
from_binary(message, JSONFormat())

0 commit comments

Comments
 (0)