Skip to content

Commit 97de42a

Browse files
taylorotwelldriesvints
authored andcommitted
[6.x] Verify column names are actual columns when using guarded (#33777)
* verify column names are actual columns when using guarded * Apply fixes from StyleCI (#33778) * remove json check
1 parent 79590e8 commit 97de42a

File tree

2 files changed

+40
-5
lines changed

2 files changed

+40
-5
lines changed

src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ trait GuardsAttributes
2727
*/
2828
protected static $unguarded = false;
2929

30+
/**
31+
* The actual columns that exist on the database and can be guarded.
32+
*
33+
* @var array
34+
*/
35+
protected static $guardableColumns = [];
36+
3037
/**
3138
* Get the fillable attributes for the model.
3239
*
@@ -164,12 +171,30 @@ public function isFillable($key)
164171
*/
165172
public function isGuarded($key)
166173
{
167-
if (strpos($key, '->') !== false) {
168-
$key = Str::before($key, '->');
174+
if (empty($this->getGuarded())) {
175+
return false;
169176
}
170177

171178
return $this->getGuarded() == ['*'] ||
172-
! empty(preg_grep('/^'.preg_quote($key).'$/i', $this->getGuarded()));
179+
! empty(preg_grep('/^'.preg_quote($key).'$/i', $this->getGuarded())) ||
180+
! $this->isGuardableColumn($key);
181+
}
182+
183+
/**
184+
* Determine if the given column is a valid, guardable column.
185+
*
186+
* @param string $key
187+
* @return bool
188+
*/
189+
protected function isGuardableColumn($key)
190+
{
191+
if (! isset(static::$guardableColumns[get_class($this)])) {
192+
static::$guardableColumns[get_class($this)] = $this->getConnection()
193+
->getSchemaBuilder()
194+
->getColumnListing($this->getTable());
195+
}
196+
197+
return in_array($key, static::$guardableColumns[get_class($this)]);
173198
}
174199

175200
/**

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -919,11 +919,21 @@ public function testUnderscorePropertiesAreNotFilled()
919919
public function testGuarded()
920920
{
921921
$model = new EloquentModelStub;
922+
923+
EloquentModelStub::setConnectionResolver($resolver = m::mock('Illuminate\Database\ConnectionResolverInterface'));
924+
$resolver->shouldReceive('connection')->andReturn($connection = m::mock(stdClass::class));
925+
$connection->shouldReceive('getSchemaBuilder->getColumnListing')->andReturn(['name', 'age', 'foo']);
926+
922927
$model->guard(['name', 'age']);
923928
$model->fill(['name' => 'foo', 'age' => 'bar', 'foo' => 'bar']);
924929
$this->assertFalse(isset($model->name));
925930
$this->assertFalse(isset($model->age));
926-
$this->assertEquals('bar', $model->foo);
931+
$this->assertSame('bar', $model->foo);
932+
933+
$model = new EloquentModelStub;
934+
$model->guard(['name', 'age']);
935+
$model->fill(['Foo' => 'bar']);
936+
$this->assertFalse(isset($model->Foo));
927937
}
928938

929939
public function testFillableOverridesGuarded()
@@ -1829,7 +1839,7 @@ public function getDates()
18291839
class EloquentModelSaveStub extends Model
18301840
{
18311841
protected $table = 'save_stub';
1832-
protected $guarded = ['id'];
1842+
protected $guarded = [];
18331843

18341844
public function save(array $options = [])
18351845
{

0 commit comments

Comments
 (0)