Skip to content

THRIFT-6561: Report preset-dictionary Ruby Header ZLIB payloads as TransportException - #4194

Merged
kpumuk merged 1 commit into
apache:masterfrom
kpumuk:rb-zlib-need-dict
Nov 17, 2026
Merged

kpumuk merged 1 commit into
apache:masterfrom
kpumuk:rb-zlib-need-dict

Conversation

@kpumuk

@kpumuk kpumuk commented Nov 17, 2026

Copy link
Copy Markdown
Member

HeaderTransport converts Zlib::DataError and Zlib::BufError from the ZLIB transform into a Thrift::TransportException (THRIFT-6141), but not Zlib::NeedDict. A compressed frame payload whose ZLIB header requests a preset dictionary therefore raised a raw Zlib::NeedDict. Servers that handle transport errors per connection treated it as unexpected; a ThreadedServer client thread, for example, ended with an exception report. This change reports such payloads like other invalid ZLIB payloads: TransportException type UNKNOWN, "Invalid ZLIB payload".

The remaining Zlib::Error subclasses (StreamError, MemError, VersionError, and others) indicate API misuse, allocation failure or the gzip reader rather than malformed input, so they still propagate unchanged.

The new spec adds a preset-dictionary payload to the existing invalid ZLIB payload examples, read through HeaderTransport#read. It fails on master and passes with this change.

Reproduction

require "thrift"
require "zlib"

def header_frame(payload)
  header = [
    Thrift::HeaderSubprotocolID::BINARY,
    1,
    Thrift::HeaderTransformID::ZLIB,
    0
  ].pack("C*")
  frame_size = 10 + header.bytesize + payload.bytesize

  [
    frame_size,
    Thrift::HeaderTransport::HEADER_MAGIC,
    0,
    0,
    header.bytesize / 4
  ].pack("NnnNn") + header + payload
end

deflater = Zlib::Deflate.new
deflater.set_dictionary("dictionary")

{
  "invalid" => "not-zlib",
  "preset dictionary" => deflater.deflate("valid payload", Zlib::FINISH)
}.each do |name, payload|
  transport = Thrift::HeaderTransport.new(
    Thrift::MemoryBufferTransport.new(header_frame(payload))
  )

  begin
    transport.read(1)
  rescue => error
    puts "#{name}: #{error.class}: #{error.message}"
  end
end

ruby -I lib/rb/lib repro.rb with Ruby 4.0.9 on master (50242d7):

invalid: Thrift::TransportException: Invalid ZLIB payload
preset dictionary: Zlib::NeedDict: need dictionary

With this change:

invalid: Thrift::TransportException: Invalid ZLIB payload
preset dictionary: Thrift::TransportException: Invalid ZLIB payload
  • Did you create an Apache Jira ticket? (THRIFT-6561)
  • If a ticket exists: Does your pull request title follow the pattern "THRIFT-NNNN: describe my issue"?
  • Did you squash your changes to a single commit? (not required, but preferred)
  • Did you do your best to avoid breaking changes? If one was needed, did you label the Jira ticket with "Breaking-Change"?
  • CI required for this change.

…ansportException

Client: rb

Generated-By: amp/0.0.1794916837-g564d50 (model: anthropic/claude-opus-5-5; operator: kpumuk@apache.org)
Copilot AI balanced review requested due to automatic review settings November 17, 2026 15:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@mergeable mergeable Bot added the ruby Pull requests that update Ruby code label Nov 17, 2026

@slachiewicz slachiewicz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The new spec fails on master with Zlib::NeedDict: need dictionary and passes with the change; the full header_transport_spec.rb is green (73 examples).

NeedDict was the only gap: 20k random mutations of a valid deflate stream produced only DataError, BufError and NeedDict, all of which now surface as TransportException with the original Zlib error kept as #cause. Leaving StreamError/MemError/VersionError unmapped looks right to me.

@kpumuk
kpumuk merged commit 0fae59b into apache:master Nov 17, 2026
93 checks passed
@kpumuk
kpumuk deleted the rb-zlib-need-dict branch November 17, 2026 23:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ruby Pull requests that update Ruby code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants