Skip to content

Fix server-side sort control encoding - #454

Open
wtn wants to merge 1 commit into
ruby-ldap:masterfrom
wtn:sort
Open

wtn wants to merge 1 commit into
ruby-ldap:masterfrom
wtn:sort

Conversation

@wtn

@wtn wtn commented Oct 31, 2026 •

Copy link
Copy Markdown

Since 4c24b4e (January 2012), encode_sort_controls builds the SortKey with assignments ending in commas. Ruby parses them as one multiple assignment, so the rule and reverse flag are sent twice:

control = Net::LDAP::Connection.new.encode_sort_controls(["cn"])
control.read_ber[2].read_ber  #=> [["cn", "", false, "", false]]

The rule and reverse flag also use universal tags. RFC 2891 tags them [0] and [1]. Because the control isn't critical, a server that can't decode it may ignore it and return unsorted results.

Fix

The rule is now encoded as 0x80 and left out when empty. The reverse flag is encoded as 0x81 and left out when false. Sorting should take effect on servers that ignored the malformed control, and the reverse flag should give descending order.

The new tests decode the control and compare the encoded value for a key with a rule and reverse flag. Without the change they fail with <[["cn"]]> expected but was <[["cn", "", false, "", false]]>.

encode_sort_controls built each SortKey with three assignments, the
first two ending in commas, which Ruby parses as one multiple
assignment. control[0] received the array of all three encoded fields,
so after join every SortKey carried five elements: type, rule, reverse,
rule, reverse.

The fields also used universal tags. RFC 2891 defines orderingRule as
[0] and reverseOrder as [1] BOOLEAN DEFAULT FALSE.

Build each SortKey explicitly, encode the optional fields as
context-specific 0x80 and 0x81, omit orderingRule when none is given
and reverseOrder when false, and cover the produced bytes with tests.

Co-authored-by: Claude Mythos 5.1 <noreply@anthropic.com>
@wtn
wtn force-pushed the sort branch 2 times, most recently from 722f27a to 6260ef9 Compare October 31, 2026 05:39
@wtn
wtn marked this pull request as ready for review October 31, 2026 05:42
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