Skip to content

Commit 14b045b

Browse files
committed
fix: disallow Model::update() without WHERE clause
1 parent 2aa5afd commit 14b045b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

system/Model.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,12 @@ protected function doUpdate($id = null, $data = null): bool
386386
$builder->set($key, $val, $escape[$key] ?? null);
387387
}
388388

389+
if ($builder->getCompiledQBWhere() === []) {
390+
throw new DatabaseException(
391+
'Updates are not allowed unless they contain a "where" or "like" clause.'
392+
);
393+
}
394+
389395
return $builder->update();
390396
}
391397

tests/system/Models/UpdateModelTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace CodeIgniter\Models;
1313

14+
use CodeIgniter\Database\Exceptions\DatabaseException;
1415
use CodeIgniter\Database\Exceptions\DataException;
1516
use CodeIgniter\Entity\Entity;
17+
use Generator;
1618
use stdClass;
1719
use Tests\Support\Models\EventModel;
1820
use Tests\Support\Models\JobModel;
@@ -378,4 +380,30 @@ public function testUpdateWithSetAndEscape(): void
378380
'email' => '1+1',
379381
]);
380382
}
383+
384+
/**
385+
* @dataProvider provideInvalidIds
386+
*
387+
* @param false|null $id
388+
*/
389+
public function testUpdateThrowDatabaseExceptionWithoutWhereClause($id): void
390+
{
391+
$this->expectException(DatabaseException::class);
392+
$this->expectExceptionMessage(
393+
'Updates are not allowed unless they contain a "where" or "like" clause.'
394+
);
395+
396+
// $useSoftDeletes = false
397+
$this->createModel(JobModel::class);
398+
399+
$this->model->update($id, ['name' => 'Foo Bar']);
400+
}
401+
402+
public function provideInvalidIds(): Generator
403+
{
404+
yield from [
405+
[null],
406+
[false],
407+
];
408+
}
381409
}

0 commit comments

Comments
 (0)