Skip to content

Commit 04fa7d7

Browse files
fix(pagination): prevent integer overflow in typesense pagination (#924)
* fix(pagination): prevent integer overflow in typesense pagination - add test to verify pagination works correctly with overflow page numbers * chore: lint * Update TypesenseEngine.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 9a8eaec commit 04fa7d7

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Engines/TypesenseEngine.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,15 @@ public function search(Builder $builder)
228228
*/
229229
public function paginate(Builder $builder, $perPage, $page)
230230
{
231+
$maxInt = 4294967295;
232+
233+
$page = max(1, (int) $page);
234+
$perPage = max(1, (int) $perPage);
235+
236+
if ($page * $perPage > $maxInt) {
237+
$page = floor($maxInt / $perPage);
238+
}
239+
231240
return $this->performSearch(
232241
$builder,
233242
$this->buildSearchParameters($builder, $page, $perPage)

tests/Integration/TypesenseSearchableTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,21 @@ public function test_it_can_use_raw_cursor_search_with_after_raw_search_callback
223223
$this->assertArrayHasKey('search_time_ms', $rawResults);
224224
}
225225

226+
public function test_it_handles_pagination_with_max_int_overflow()
227+
{
228+
$maxInt = 4294967295;
229+
$perPage = 10;
230+
$overflowPage = 4294967296; // max int + 1
231+
$expectedPage = floor($maxInt / $perPage);
232+
233+
$results = SearchableUser::search('lar')
234+
->paginate($perPage, $overflowPage);
235+
236+
// Verify the page was adjusted correctly
237+
$this->assertEquals($expectedPage, $results->currentPage());
238+
$this->assertEquals($perPage, $results->perPage());
239+
}
240+
226241
protected static function scoutDriver(): string
227242
{
228243
return 'typesense';

0 commit comments

Comments
 (0)