Skip to content

Commit

Permalink
protect table names and guarded
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Aug 6, 2020
1 parent c6f9ae2 commit 9240404
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use JsonSerializable;
use LogicException;

abstract class Model implements Arrayable, ArrayAccess, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable
{
Expand Down Expand Up @@ -375,7 +376,16 @@ public function qualifyColumn($column)
*/
protected function removeTableFromKey($key)
{
return Str::contains($key, '.') ? last(explode('.', $key)) : $key;
if (strpos($key, '.') !== false) {
if (! empty($this->getGuarded()) &&
$this->getGuarded() !== ['*']) {
throw new LogicException("Mass assignment of Eloquent attributes including table names is unsafe when guarding attributes.");
}

return last(explode('.', $key));
}

return $key;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Integration/Database/EloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ public function testCantUpdateGuardedAttributeUsingJson()
$this->assertNull($model->id);
}

public function testCantMassFillAttributesWithTableNamesWhenUsingGuarded()
{
$this->expectException(\LogicException::class);

$model = new TestModel2;

$model->fill(['foo.bar' => 123]);
}

public function testUserCanUpdateNullableDate()
{
$user = TestModel1::create([
Expand Down

0 comments on commit 9240404

Please sign in to comment.