diff --git a/History.rdoc b/History.rdoc index cc0ef248..4cacd274 100644 --- a/History.rdoc +++ b/History.rdoc @@ -1,5 +1,8 @@ === 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 + correctly stops fetching additional pages once the size limit is reached. === Net::LDAP 0.21.0 * Patch bind success by @HarlemSquirrel in #447 diff --git a/lib/net/ldap/connection.rb b/lib/net/ldap/connection.rb index 4fd55da4..d2cf0fe8 100644 --- a/lib/net/ldap/connection.rb +++ b/lib/net/ldap/connection.rb @@ -402,15 +402,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, @@ -452,6 +444,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 @@ -513,6 +511,11 @@ def search(args = nil) end end + # 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 @@ -744,6 +747,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 d5925a2a..4f35b4a7 100644 --- a/test/test_ldap_connection.rb +++ b/test/test_ldap_connection.rb @@ -353,6 +353,59 @@ def test_invalid_pdu_type Net::LDAP::PDU.new([0, ber]) end end + + # 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] + end + entries + [paged_search_result(cookie)] + end + + 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? + + 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(*responses) + + conn = Net::LDAP::Connection.new(socket: mock) + results = [] + result = conn.search(base: "dc=test", size: size, paged_searches_supported: true) { |entry| results << entry } + [results, write_count, result] + end end class TestLDAPConnectionErrors < Test::Unit::TestCase