Skip to content

Commit 9240404

Browse files
committed
protect table names and guarded
1 parent c6f9ae2 commit 9240404

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Illuminate/Database/Eloquent/Model.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Illuminate\Support\Str;
1818
use Illuminate\Support\Traits\ForwardsCalls;
1919
use JsonSerializable;
20+
use LogicException;
2021

2122
abstract class Model implements Arrayable, ArrayAccess, Jsonable, JsonSerializable, QueueableEntity, UrlRoutable
2223
{
@@ -375,7 +376,16 @@ public function qualifyColumn($column)
375376
*/
376377
protected function removeTableFromKey($key)
377378
{
378-
return Str::contains($key, '.') ? last(explode('.', $key)) : $key;
379+
if (strpos($key, '.') !== false) {
380+
if (! empty($this->getGuarded()) &&
381+
$this->getGuarded() !== ['*']) {
382+
throw new LogicException("Mass assignment of Eloquent attributes including table names is unsafe when guarding attributes.");
383+
}
384+
385+
return last(explode('.', $key));
386+
}
387+
388+
return $key;
379389
}
380390

381391
/**

tests/Integration/Database/EloquentModelTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public function testCantUpdateGuardedAttributeUsingJson()
4747
$this->assertNull($model->id);
4848
}
4949

50+
public function testCantMassFillAttributesWithTableNamesWhenUsingGuarded()
51+
{
52+
$this->expectException(\LogicException::class);
53+
54+
$model = new TestModel2;
55+
56+
$model->fill(['foo.bar' => 123]);
57+
}
58+
5059
public function testUserCanUpdateNullableDate()
5160
{
5261
$user = TestModel1::create([

0 commit comments

Comments
 (0)