fix(repository): persist guarded columns passed to create()
Tests / PHP 8.4 (push) Failing after 5m9s
Tests / PHP 8.4 (push) Failing after 5m9s
create() built the model through hydrate(), which skips #[Guarded] properties on purpose: that is the right behaviour for an HTTP payload, where a client must not set password_hash or a token. But create() is called from trusted server code - a service, a seeder - and the guarded value was silently dropped, so an account was inserted with an empty password hash. The write succeeded and nothing failed loudly. create() now re-applies guarded values after hydration, so every column the caller passes is persisted. hydrate() keeps rejecting them, which is what protects mass assignment. Covered by CreateWithGuardedTest: one case for create() persisting the hash, one asserting hydrate() still ignores it.
This commit is contained in:
1 parent
4faad2c46e
commit
227325e4b4
2 files changed
+86
No files matched your search
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Camelia\Orm\Tests\Unit;
|
||||
|
||||
use Camelia\Orm\Attribute\AutoIncrement;
|
||||
use Camelia\Orm\Attribute\Guarded;
|
||||
use Camelia\Orm\Attribute\PrimaryKey;
|
||||
use Camelia\Orm\Attribute\TableName;
|
||||
use Camelia\Orm\Model;
|
||||
use Camelia\Orm\Orm;
|
||||
use Camelia\Orm\Tests\Support\FakeConnection;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
#[TableName('cuentas_prueba')]
|
||||
final class CuentaDePrueba extends Model
|
||||
{
|
||||
#[PrimaryKey, AutoIncrement]
|
||||
public ?int $id = null;
|
||||
|
||||
public string $correo = '';
|
||||
|
||||
#[Guarded]
|
||||
public string $passwordHash = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Regression: Repository::create() delegated to hydrate(), which skips #[Guarded] properties by
|
||||
* design, so a trusted caller passing a password hash silently persisted an empty string.
|
||||
*/
|
||||
final class CreateWithGuardedTest extends TestCase
|
||||
{
|
||||
#[Test]
|
||||
public function create_persists_a_guarded_column_from_trusted_values(): void
|
||||
{
|
||||
$connection = new FakeConnection();
|
||||
|
||||
$model = Orm::fromConnection($connection)
|
||||
->repository(CuentaDePrueba::class)
|
||||
->create([
|
||||
'correo' => 'a@x',
|
||||
'passwordHash' => 'ARGON2ID',
|
||||
]);
|
||||
|
||||
self::assertSame('ARGON2ID', $model->passwordHash);
|
||||
self::assertStringContainsString('`password_hash`', $connection->lastSql());
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function hydrate_still_rejects_a_guarded_column_from_a_payload(): void
|
||||
{
|
||||
$model = new CuentaDePrueba()->hydrate([
|
||||
'correo' => 'a@x',
|
||||
'passwordHash' => 'FROM-PAYLOAD',
|
||||
]);
|
||||
|
||||
self::assertSame('', $model->passwordHash);
|
||||
self::assertSame('a@x', $model->correo);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user