Skip to content

MINOR: [ruby] reject out-of-range enum and union index in binary decoder - #3944

Open
arib06 wants to merge 1 commit into
apache:mainfrom
arib06:ruby-enum-union-index-bounds
Open

MINOR: [ruby] reject out-of-range enum and union index in binary decoder#3944
arib06 wants to merge 1 commit into
apache:mainfrom
arib06:ruby-enum-union-index-bounds

Conversation

@arib06

@arib06 arib06 commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

What is the purpose of the change

read_enum, read_union and skip_union in lang/ruby/lib/avro/io.rb take the enum symbol index and the union branch index straight from untrusted serialized data and index writers_schema.symbols / writers_schema.schemas with no range check. Because Ruby arrays treat a negative subscript as an offset from the end, a crafted negative index (the zigzag byte 0x01 decodes to -1) wraps to a valid but wrong symbol or branch and is decoded silently, which is a type confusion / stream desync rather than a decode failure. A positive out-of-range index yields nil and later raises NoMethodError. The Python decoder already rejects these (AVRO-4296); this brings the Ruby SDK in line by validating 0 <= index < size at the branch point and raising Avro::AvroError.

Verifying this change

This change added tests and can be verified as follows:

  • Added test_enum_index_out_of_range and test_union_index_out_of_range to test/test_io.rb, which feed the decoder crafted bytes with a negative index (-1, wraps to the last element) and a positive out-of-range index, asserting Avro::AvroError is raised. Both tests fail on the unpatched decoder (the enum returns the wrong symbol, the union decodes the wrong branch) and pass with the fix.

Documentation

  • Does this pull request introduce a new feature? no

@github-actions github-actions Bot added the Ruby label Aug 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant