Skip to content

Enforce size limit when paged searches are enabled - #445

Open
arianf wants to merge 2 commits into
ruby-ldap:masterfrom
arianf:fix-paged-search-size-limit
Open

arianf wants to merge 2 commits into
ruby-ldap:masterfrom
arianf:fix-paged-search-size-limit

Conversation

@arianf

@arianf arianf commented Jan 31, 2026 •

Copy link
Copy Markdown

Enforce size limit when paged searches are enabled

Fixes the :size option being ignored when paged searches are enabled and the requested size exceeds 126.

Problem

When paging is enabled and remaining entries needed exceeds 126, the code sets query_limit = 0 (no server-side limit) to avoid conflicts with the page size. However, the limit is never enforced client-side.

The existing code comment acknowledges this:

"(Because we pass zero as the sizelimit on search rounds when the remaining limit is larger than our max page size of 126. In these cases, I think the caller's search limit will be ignored!)"

Solution

Add a client-side check to break the paging loop when the requested size limit is reached:

break if size > 0 && n_results >= size

Related Issues

Testing

Tested against a production LDAP server with 5900+ entries matching the filter.
Before: all 5900+ returned.
After: exactly 3000 as requested.

Checklist

  • Fix applied to lib/net/ldap/connection.rb
  • Add unit test for size limit with paged searches
  • Update History.rdoc with change summary
  • Run full test suite (bundle exec rake)

arianf and others added 2 commits January 30, 2026 16:38
When paging is enabled and the requested size exceeds 126, the client now
correctly stops fetching additional pages once the size limit is reached.

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.

🟡 Changes recommended

Entries can still exceed the documented maximum because the limit is checked only after yielding a complete page.

3 open findings
What changed in this PR

Adds client-side termination for size-limited paged LDAP searches.

Changes:

  • Stops paging after reaching the requested size.
  • Adds a regression test.
  • Updates release history.
File Description
lib/​net/​ldap/​connection.rb Adds paging termination logic.
test/​test_ldap_connection.rb Tests size-limited paging.
History.rdoc Documents the fix.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

# 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
Comment on lines +375 to +392
# 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"
Comment on lines +514 to +515
# When paging is enabled, we set query_limit=0 (no server-side limit)
# for efficiency, so we must enforce the limit client-side here.
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.

3 participants