fix(repository): persist guarded columns passed to create()
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:
dgarcia committed 2026-11-15 13:37:39 -03:00
1 parent 4faad2c46e
commit 227325e4b4
2 files changed
+86

No files matched your search

+24
View File
@@ -218,6 +218,13 @@ final class Repository
}
/**
* Builds a new model from $values and inserts it.
*
* `hydrate()` deliberately skips #[Guarded] properties, which is right for request payloads but
* wrong here: this method is called from trusted server code (a seeder, a service), and a guarded
* column such as a password hash would otherwise be silently dropped. Guarded values are applied
* after hydration, so `create()` can persist every column it is given.
*
* @param array<string, mixed> $values
* @return T
*/
@@ -226,6 +233,23 @@ final class Repository
/** @var T $model */
$model = new $this->class();
$model->hydrate($values);
$metadata = $this->metadata();
foreach ($values as $key => $value) {
$property = $metadata->resolveProperty((string) $key);
if ($property === null) {
continue;
}
$definition = $metadata->propertyMetadata($property);
if ($definition->guarded && !$definition->ignored && !$definition->primaryKey) {
$model->set($property, $model->castFor($definition, $value));
}
}
$this->persister()->insert($model);
return $model;
+62
View File
@@ -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);
}
}