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;