Skip to content

Commit 2e05ba1

Browse files
dazumaclaude
andcommitted
fix: Reject CloudEvents 0.3 in KafkaBinding structured mode decode
The binary decode path already raised SpecVersionError for non-1.x spec versions, but structured mode would accept and return a V0 event. Now decode_structured_content checks the decoded event's spec_version and raises SpecVersionError if it does not start with "1". Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Daniel Azuma <dazuma@gmail.com>
1 parent e0dfc22 commit 2e05ba1

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

lib/cloud_events/kafka_binding.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,13 @@ def decode_structured_content(message, content_type, allow_opaque, **format_args
281281
content_type: content_type,
282282
data_decoder: @data_decoders,
283283
**format_args)
284-
return result[:event] if result
284+
if result
285+
event = result[:event]
286+
if event && !event.spec_version.start_with?("1")
287+
raise(SpecVersionError, "Unrecognized specversion: #{event.spec_version}")
288+
end
289+
return event
290+
end
285291
return Event::Opaque.new(content, content_type) if allow_opaque
286292
raise(UnsupportedFormatError, "Unknown cloudevents content type: #{content_type}")
287293
end

test/test_kafka_binding.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,25 @@
350350
end
351351
end
352352

353+
it "raises SpecVersionError for structured event with specversion 0.3" do
354+
v03_hash = {
355+
"data" => "hello",
356+
"id" => my_id,
357+
"source" => my_source_string,
358+
"specversion" => "0.3",
359+
"type" => my_type,
360+
}
361+
v03_struct = JSON.dump(v03_hash)
362+
message = {
363+
key: nil,
364+
value: v03_struct,
365+
headers: { "content-type" => "application/cloudevents+json" },
366+
}
367+
assert_raises CloudEvents::SpecVersionError do
368+
kafka_binding.decode_event(message, reverse_key_mapper: nil)
369+
end
370+
end
371+
353372
it "raises FormatSyntaxError for malformed JSON" do
354373
message = {
355374
key: nil,

0 commit comments

Comments
 (0)