From 2ae08dcf660281707dc2ffa3ab4f45c2018b6a0f Mon Sep 17 00:00:00 2001 From: Arian Faurtosh Date: Fri, 30 Jan 2026 16:37:14 -0800 Subject: [PATCH 1/2] Enforce size limit when paged searches are enabled When paging is enabled and the requested size exceeds 126, the client now correctly stops fetching additional pages once the size limit is reached. --- History.rdoc | 5 +++++ lib/net/ldap/connection.rb | 5 +++++ test/test_ldap_connection.rb | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/History.rdoc b/History.rdoc index 919eaf67..d49cf6a1 100644 --- a/History.rdoc +++ b/History.rdoc @@ -1,3 +1,8 @@ +=== Net::LDAP (Unreleased) +* Enforce size limit when paged searches are enabled + When paging is enabled and the requested size exceeds 126, the client now + correctly stops fetching additional pages once the size limit is reached. + === Net::LDAP 0.20.0 * Update test.yml by @HarlemSquirrel in #433 * Add `ostruct` as a dependency to the gemspec by @Ivanov-Anton in #432 diff --git a/lib/net/ldap/connection.rb b/lib/net/ldap/connection.rb index f1a70b18..376cf848 100644 --- a/lib/net/ldap/connection.rb +++ b/lib/net/ldap/connection.rb @@ -510,6 +510,11 @@ def search(args = nil) end end + # Stop paging if we've reached the caller's requested size limit. + # When paging is enabled, we set query_limit=0 (no server-side limit) + # for efficiency, so we must enforce the limit client-side here. + break if size > 0 && n_results >= size + break unless more_pages end # loop diff --git a/test/test_ldap_connection.rb b/test/test_ldap_connection.rb index fdfa418c..74ab486c 100644 --- a/test/test_ldap_connection.rb +++ b/test/test_ldap_connection.rb @@ -353,6 +353,44 @@ def test_invalid_pdu_type Net::LDAP::PDU.new([0, ber]) end end + + # Test that size limit is enforced client-side when paging is enabled. + def test_search_size_limit_stops_paging_early + make_search_entry = lambda { |entry_dn| + ber = Net::BER::BerIdentifiedArray.new([1, [entry_dn, [["uid", ["user"]]]]]) + ber.ber_identifier = Net::LDAP::PDU::SearchReturnedData + [1, ber] + } + + make_search_result = lambda { |cookie = nil| + ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""]) + ber.ber_identifier = Net::LDAP::PDU::SearchResult + return [1, ber] if cookie.nil? + + paging_value = "\x30".b + (126.to_ber + cookie.to_ber).then { |s| s.length.chr + s } + controls = [[Net::LDAP::LDAPControls::PAGED_RESULTS, false, paging_value]] + [1, ber, controls] + } + + # Page 1: 5 entries + result with "more" cookie + # Page 2: 5 entries + result with empty cookie (shouldn't be reached) + page1 = 5.times.map { |i| make_search_entry.call("uid=user#{i}") } + [make_search_result.call("more")] + page2 = 5.times.map { |i| make_search_entry.call("uid=user#{i + 5}") } + [make_search_result.call] + + mock = flexmock("socket") + write_count = 0 + mock.should_receive(:write).and_return { write_count += 1 } + mock.should_receive(:read_ber).and_return(*(page1 + page2)) + + conn = Net::LDAP::Connection.new(socket: mock) + results = [] + conn.search(base: "dc=test", size: 3, paged_searches_supported: true) { |e| results << e } + + # With fix: 5 entries >= size 3, stops after page 1 + # Without fix: would fetch page 2 and get 10 entries + assert_equal 5, results.size + assert_equal 1, write_count, "Should stop paging when size limit reached" + end end class TestLDAPConnectionErrors < Test::Unit::TestCase From afd10f6a4cefb271824e4f451de0312c04f456f6 Mon Sep 17 00:00:00 2001 From: Arian Faurtosh Date: Tue, 17 Nov 2026 06:16:27 -0800 Subject: [PATCH 2/2] Stop returning entries past the size limit in paged searches Later page requests send a different sizelimit from the first one, which RFC 2696 says they must not, so a server can send a full page past the caller's :size. Also moves the per-page sizelimit calculation into a helper so search stays within the project's RuboCop metrics. - Moved the per-page sizelimit calculation into search_query_limit - Stopped returning entries once :size is reached, still reading the rest of the page - Replaced the test with page-boundary and over-full last page cases --- History.rdoc | 2 +- lib/net/ldap/connection.rb | 43 ++++++++++++++++-------- test/test_ldap_connection.rb | 65 ++++++++++++++++++++++-------------- 3 files changed, 71 insertions(+), 39 deletions(-) diff --git a/History.rdoc b/History.rdoc index d36e9d2f..4cacd274 100644 --- a/History.rdoc +++ b/History.rdoc @@ -1,4 +1,4 @@ -=== Net::LDAP (Unreleased) +=== Unreleased * Move readme to md in #452 * Enforce size limit when paged searches are enabled When paging is enabled and the requested size exceeds 126, the client now diff --git a/lib/net/ldap/connection.rb b/lib/net/ldap/connection.rb index 376cf848..4e6ffc7d 100644 --- a/lib/net/ldap/connection.rb +++ b/lib/net/ldap/connection.rb @@ -399,15 +399,7 @@ def search(args = nil) deref: deref, attributes: attrs do |payload| loop do - # should collect this into a private helper to clarify the structure - query_limit = 0 - if size > 0 - query_limit = if paged - (((size - n_results) < 126) ? (size - n_results) : 0) - else - size - end - end + query_limit = search_query_limit(size, n_results, paged) request = [ base.to_ber, @@ -449,6 +441,12 @@ def search(args = nil) while pdu = queued_read(message_id) case pdu.app_tag when Net::LDAP::PDU::SearchReturnedData + # Later page requests carry a different sizelimit from the first, + # which RFC 2696 says they must not, so a server may keep the first + # request's limit and send a full page. Read past the extra entries + # to keep the connection in sync, but don't return them. + next if size_limit_reached?(size, n_results) + n_results += 1 yield pdu.search_entry if block_given? when Net::LDAP::PDU::SearchResultReferral @@ -510,10 +508,10 @@ def search(args = nil) end end - # Stop paging if we've reached the caller's requested size limit. - # When paging is enabled, we set query_limit=0 (no server-side limit) - # for efficiency, so we must enforce the limit client-side here. - break if size > 0 && n_results >= size + # Stop once the caller's size limit is reached. When the limit ends on + # a page boundary nothing remains, and the query_limit computed for + # the next round would be 0, which the server reads as "no limit". + break if size_limit_reached?(size, n_results) break unless more_pages end # loop @@ -746,6 +744,25 @@ def socket private + # The sizelimit for one search request; 0 means "no limit" to the server. + # A paged request can't carry a sizelimit larger than its page size of 126, + # so it sends 0 until fewer than 126 entries remain. + def search_query_limit(size, n_results, paged) + return 0 if size.zero? + + if paged + remaining = size - n_results + remaining < 126 ? remaining : 0 + else + size + end + end + + # A size of 0 means the caller set no limit. + def size_limit_reached?(size, n_results) + size > 0 && n_results >= size + end + # Wrap around Socket.tcp to normalize with other Socket initializers class DefaultSocket def self.new(host, port, socket_opts = {}) diff --git a/test/test_ldap_connection.rb b/test/test_ldap_connection.rb index 74ab486c..1b6a4633 100644 --- a/test/test_ldap_connection.rb +++ b/test/test_ldap_connection.rb @@ -354,42 +354,57 @@ def test_invalid_pdu_type end end - # Test that size limit is enforced client-side when paging is enabled. - def test_search_size_limit_stops_paging_early - make_search_entry = lambda { |entry_dn| - ber = Net::BER::BerIdentifiedArray.new([1, [entry_dn, [["uid", ["user"]]]]]) + # A size limit that ends on a page boundary leaves 0 remaining, and a + # sizelimit of 0 means "no limit" to the server. + def test_search_size_limit_on_page_boundary_stops_paging + # The third page is what an unlimited request would return. + responses = paged_search_page(0, "more") + paged_search_page(1, "more") + paged_search_page(2, nil) + results, write_count = run_paged_search(responses, size: 252) + + assert_equal 252, results.size + assert_equal 2, write_count, "Should not request a page after reaching the size limit" + end + + # The second request asks for the 4 remaining entries, but this server keeps + # the first request's sizelimit of 0 and sends a full page. + def test_search_size_limit_drops_entries_past_the_limit + responses = paged_search_page(0, "more") + paged_search_page(1, nil) + results, write_count, result = run_paged_search(responses, size: 130) + + assert_equal 130, results.size + assert_equal 2, write_count + assert result.success?, "Should read the rest of the page through to its search result" + end + + def paged_search_page(page, cookie) + entries = 126.times.map do |index| + ber = Net::BER::BerIdentifiedArray.new([1, ["uid=user#{(page * 126) + index}", [["uid", ["user"]]]]]) ber.ber_identifier = Net::LDAP::PDU::SearchReturnedData [1, ber] - } - - make_search_result = lambda { |cookie = nil| - ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""]) - ber.ber_identifier = Net::LDAP::PDU::SearchResult - return [1, ber] if cookie.nil? + end + entries + [paged_search_result(cookie)] + end - paging_value = "\x30".b + (126.to_ber + cookie.to_ber).then { |s| s.length.chr + s } - controls = [[Net::LDAP::LDAPControls::PAGED_RESULTS, false, paging_value]] - [1, ber, controls] - } + def paged_search_result(cookie) + ber = Net::BER::BerIdentifiedArray.new([Net::LDAP::ResultCodeSuccess, "", ""]) + ber.ber_identifier = Net::LDAP::PDU::SearchResult + return [1, ber] if cookie.nil? - # Page 1: 5 entries + result with "more" cookie - # Page 2: 5 entries + result with empty cookie (shouldn't be reached) - page1 = 5.times.map { |i| make_search_entry.call("uid=user#{i}") } + [make_search_result.call("more")] - page2 = 5.times.map { |i| make_search_entry.call("uid=user#{i + 5}") } + [make_search_result.call] + paging_value = "\x30".b + (126.to_ber + cookie.to_ber).then { |s| s.length.chr + s } + controls = [[Net::LDAP::LDAPControls::PAGED_RESULTS, false, paging_value]] + [1, ber, controls] + end + def run_paged_search(responses, size:) mock = flexmock("socket") write_count = 0 mock.should_receive(:write).and_return { write_count += 1 } - mock.should_receive(:read_ber).and_return(*(page1 + page2)) + mock.should_receive(:read_ber).and_return(*responses) conn = Net::LDAP::Connection.new(socket: mock) results = [] - conn.search(base: "dc=test", size: 3, paged_searches_supported: true) { |e| results << e } - - # With fix: 5 entries >= size 3, stops after page 1 - # Without fix: would fetch page 2 and get 10 entries - assert_equal 5, results.size - assert_equal 1, write_count, "Should stop paging when size limit reached" + result = conn.search(base: "dc=test", size: size, paged_searches_supported: true) { |entry| results << entry } + [results, write_count, result] end end