Skip to content

Commit 8d7fbae

Browse files
committed
fix(API): prevent TypeError and handle integer overflow in paginate()
When a huge page number (e.g. PHP_INT_MAX) is requested via ?page=, the offset calculation `($page - 1) * $perPage` overflows the int type and becomes a float. Passing that float to `BaseBuilder::limit(?int, ?int)` raises a TypeError, which `paginate()` reports as a 500 response ("Paginate Error"). Clamp the requested page to the available range, so the offset always stays within the int type: $page = max(1, min($page, max(1, $totalPages))); This also aligns the Query Builder path with the Pager used by the Model path, which already limits the current page to the page count.
1 parent 6f92d1f commit 8d7fbae

2 files changed

Lines changed: 83 additions & 4 deletions

File tree

‎system/API/ResponseTrait.php‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,15 +416,19 @@ protected function paginate(BaseBuilder|Model $resource, int $perPage = 20, ?str
416416
];
417417
} else {
418418
// Query Builder, we need to handle pagination manually
419-
$offset = ($page - 1) * $perPage;
420-
$total = (clone $resource)->countAllResults();
421-
$data = $resource->limit($perPage, $offset)->get()->getResultArray();
419+
// The page is clamped to the available range so that the offset
420+
// cannot overflow the int type when a huge page is requested.
421+
$total = (clone $resource)->countAllResults();
422+
$totalPages = (int) ceil($total / $perPage);
423+
$page = max(1, min($page, max(1, $totalPages)));
424+
$offset = ($page - 1) * $perPage;
425+
$data = $resource->limit($perPage, $offset)->get()->getResultArray();
422426

423427
$meta = [
424428
'page' => $page,
425429
'perPage' => $perPage,
426430
'total' => $total,
427-
'totalPages' => (int) ceil($total / $perPage),
431+
'totalPages' => $totalPages,
428432
];
429433
}
430434

‎tests/system/API/ResponseTraitTest.php‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,81 @@ public function testPaginateWithQueryBuilder(): void
799799
$this->assertSame(3, $responseBody['meta']['totalPages']);
800800
}
801801

802+
public function testPaginateWithQueryBuilderPageOverflow(): void
803+
{
804+
// Mock the database and builder
805+
$db = $this->createStub(BaseConnection::class);
806+
807+
$builder = $this->getMockBuilder(BaseBuilder::class)
808+
->setConstructorArgs(['test_table', $db])
809+
->onlyMethods(['countAllResults', 'limit', 'get'])
810+
->getMock();
811+
812+
$result = $this->createMock(BaseResult::class);
813+
814+
$data = [
815+
['id' => 1, 'name' => 'Item 1'],
816+
['id' => 2, 'name' => 'Item 2'],
817+
];
818+
819+
// Mock the query builder chain
820+
$builder->method('countAllResults')->willReturn(50);
821+
$builder->expects($this->once())
822+
->method('limit')
823+
->with(20, 40)
824+
->willReturnSelf();
825+
$builder->method('get')->willReturn($result);
826+
$result->method('getResultArray')->willReturn($data);
827+
828+
$controller = $this->makeController('/api/items?page=' . PHP_INT_MAX);
829+
$this->request->setGlobal('get', ['page' => (string) PHP_INT_MAX]);
830+
831+
$this->invoke($controller, 'paginate', [$builder, 20]);
832+
833+
$responseBody = json_decode($this->response->getBody(), true);
834+
835+
$this->assertSame(3, $responseBody['meta']['page']);
836+
$this->assertSame(20, $responseBody['meta']['perPage']);
837+
$this->assertSame(50, $responseBody['meta']['total']);
838+
$this->assertSame(3, $responseBody['meta']['totalPages']);
839+
$this->assertSame($data, $responseBody['data']);
840+
$this->assertStringContainsString('page=3', $responseBody['links']['self']);
841+
$this->assertNull($responseBody['links']['next']);
842+
}
843+
844+
public function testPaginateWithQueryBuilderZeroResultsPageOverflow(): void
845+
{
846+
// Mock the database and builder
847+
$db = $this->createStub(BaseConnection::class);
848+
849+
$builder = $this->getMockBuilder(BaseBuilder::class)
850+
->setConstructorArgs(['test_table', $db])
851+
->onlyMethods(['countAllResults', 'limit', 'get'])
852+
->getMock();
853+
854+
$result = $this->createMock(BaseResult::class);
855+
856+
// Mock the query builder chain
857+
$builder->method('countAllResults')->willReturn(0);
858+
$builder->expects($this->once())
859+
->method('limit')
860+
->with(20, 0)
861+
->willReturnSelf();
862+
$builder->method('get')->willReturn($result);
863+
$result->method('getResultArray')->willReturn([]);
864+
865+
$controller = $this->makeController('/api/items?page=' . PHP_INT_MAX);
866+
$this->request->setGlobal('get', ['page' => (string) PHP_INT_MAX]);
867+
868+
$this->invoke($controller, 'paginate', [$builder, 20]);
869+
870+
$responseBody = json_decode($this->response->getBody(), true);
871+
872+
$this->assertSame(1, $responseBody['meta']['page']);
873+
$this->assertSame(0, $responseBody['meta']['totalPages']);
874+
$this->assertSame([], $responseBody['data']);
875+
}
876+
802877
public function testPaginateWithCustomPerPage(): void
803878
{
804879
$data = [

0 commit comments

Comments
 (0)