Skip to content

Raise BerError, not NoMethodError/RangeError, on truncated or empty BER input - #455

Open
viftode4 wants to merge 1 commit into
ruby-ldap:masterfrom
viftode4:ber-truncated
Open

viftode4 wants to merge 1 commit into
ruby-ldap:masterfrom
viftode4:ber-truncated

Conversation

@viftode4

@viftode4 viftode4 commented Dec 6, 2026

Copy link
Copy Markdown

A truncated LDAP response makes Net::LDAP#bind (and any search) raise NoMethodError instead of a BER/LDAP error. Reproduced end to end against a socket server that answers the bind with a cut-off PDU (net-ldap 0.21.0):

server sends before
30 NoMethodError: undefined method '<=' for nil (ber_parser.rb:138)
30 84 NoMethodError: undefined method 'each_byte' for nil (:146)
02 00 / 06 00 NoMethodError for nil in integer / OID parsing
long-form length that does not fit a long RangeError: bignum too big to convert into 'long'

read_ber already carries a TODO that it "can throw TypeErrors and other nasties" on short reads. This makes it raise Net::BER::BerError in those places: end of data inside the length, fewer content bytes than the length promises, empty INTEGER/OID content, and a content length above 2^31-1. Valid input is unchanged (full suite: 220 tests green; the new TestBERMalformedInput fails on master and passes here).

Found by mutating valid BER with a small fuzzer and watching for exception classes other than BerError.

Made by SLIME

…ER input

read_ber / read_ber_length / parse_ber_object assumed the stream always had
the bytes the length field promised. A server (or anything on the wire)
sending a truncated PDU made Net::LDAP#bind raise NoMethodError
(undefined method '<=' / 'each_byte' / 'unpack' for nil) or RangeError,
which callers that rescue Net::LDAP::Error or Net::BER::BerError never see.
Zero-length INTEGER and OBJECT IDENTIFIER content raised the same way.

All these cases now raise Net::BER::BerError. Adds tests.

Made by SLIME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant