Skip to content

Commit ebee42f

Browse files
dazumaclaude
andcommitted
fix: Reject non-V1 events in KafkaBinding#encode_event
Raise SpecVersionError when encode_event receives an event with a specversion that does not start with "1", ensuring V0.3 events cannot be encoded via the Kafka binding. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Daniel Azuma <dazuma@gmail.com>
1 parent 2e05ba1 commit ebee42f

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

lib/cloud_events/kafka_binding.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ def encode_event(event, structured_format: false, key_mapper: :NOT_SET, **format
209209
if event.is_a?(Event::Opaque)
210210
return encode_opaque_event(event)
211211
end
212+
unless event.spec_version.start_with?("1")
213+
raise(SpecVersionError, "Unrecognized specversion: #{event.spec_version}")
214+
end
212215
if structured_format
213216
encode_structured_event(event, structured_format, key_mapper, **format_args)
214217
else

test/test_kafka_binding.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,16 @@
493493
message = kafka_binding.encode_event(event, key_mapper: nil)
494494
assert_nil message[:key]
495495
end
496+
497+
it "raises SpecVersionError for a V0 event" do
498+
v0_event = CloudEvents::Event::V0.new(id: my_id,
499+
source: my_source_string,
500+
specversion: "0.3",
501+
type: my_type)
502+
assert_raises CloudEvents::SpecVersionError do
503+
kafka_binding.encode_event(v0_event, key_mapper: nil)
504+
end
505+
end
496506
end
497507

498508
describe "encode_event structured mode" do

0 commit comments

Comments
 (0)