Skip to content

Commit 2ffbbbb

Browse files
committed
fix: do not allow bool as $id
1 parent 66395c5 commit 2ffbbbb

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

system/BaseModel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,10 @@ public function insertBatch(?array $set = null, ?bool $escape = null, int $batch
899899
*/
900900
public function update($id = null, $data = null): bool
901901
{
902+
if (is_bool($id)) {
903+
throw new InvalidArgumentException('$id should not be boolean.');
904+
}
905+
902906
if (is_numeric($id) || is_string($id)) {
903907
$id = [$id];
904908
}
@@ -1037,6 +1041,10 @@ public function updateBatch(?array $set = null, ?string $index = null, int $batc
10371041
*/
10381042
public function delete($id = null, bool $purge = false)
10391043
{
1044+
if (is_bool($id)) {
1045+
throw new InvalidArgumentException('$id should not be boolean.');
1046+
}
1047+
10401048
if ($id && (is_numeric($id) || is_string($id))) {
10411049
$id = [$id];
10421050
}

tests/system/Models/UpdateModelTest.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use CodeIgniter\Database\Exceptions\DataException;
1616
use CodeIgniter\Entity\Entity;
1717
use Generator;
18+
use InvalidArgumentException;
1819
use stdClass;
1920
use Tests\Support\Models\EventModel;
2021
use Tests\Support\Models\JobModel;
@@ -386,12 +387,10 @@ public function testUpdateWithSetAndEscape(): void
386387
*
387388
* @param false|null $id
388389
*/
389-
public function testUpdateThrowDatabaseExceptionWithoutWhereClause($id): void
390+
public function testUpdateThrowDatabaseExceptionWithoutWhereClause($id, string $exception, string $exceptionMessage): void
390391
{
391-
$this->expectException(DatabaseException::class);
392-
$this->expectExceptionMessage(
393-
'Updates are not allowed unless they contain a "where" or "like" clause.'
394-
);
392+
$this->expectException($exception);
393+
$this->expectExceptionMessage($exceptionMessage);
395394

396395
// $useSoftDeletes = false
397396
$this->createModel(JobModel::class);
@@ -402,8 +401,16 @@ public function testUpdateThrowDatabaseExceptionWithoutWhereClause($id): void
402401
public function provideInvalidIds(): Generator
403402
{
404403
yield from [
405-
[null],
406-
[false],
404+
[
405+
null,
406+
DatabaseException::class,
407+
'Updates are not allowed unless they contain a "where" or "like" clause.',
408+
],
409+
[
410+
false,
411+
InvalidArgumentException::class,
412+
'$id should not be boolean.',
413+
],
407414
];
408415
}
409416
}

0 commit comments

Comments
 (0)