Skip to content

Commit

Permalink
[6.x] Verify column names are actual columns when using guarded (#33777)
Browse files Browse the repository at this point in the history
* verify column names are actual columns when using guarded

* Apply fixes from StyleCI (#33778)

* remove json check
  • Loading branch information
taylorotwell authored and driesvints committed Aug 13, 2020
1 parent 79590e8 commit 97de42a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
31 changes: 28 additions & 3 deletions src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ trait GuardsAttributes
*/
protected static $unguarded = false;

/**
* The actual columns that exist on the database and can be guarded.
*
* @var array
*/
protected static $guardableColumns = [];

/**
* Get the fillable attributes for the model.
*
Expand Down Expand Up @@ -164,12 +171,30 @@ public function isFillable($key)
*/
public function isGuarded($key)
{
if (strpos($key, '->') !== false) {
$key = Str::before($key, '->');
if (empty($this->getGuarded())) {
return false;
}

return $this->getGuarded() == ['*'] ||
! empty(preg_grep('/^'.preg_quote($key).'$/i', $this->getGuarded()));
! empty(preg_grep('/^'.preg_quote($key).'$/i', $this->getGuarded())) ||
! $this->isGuardableColumn($key);
}

/**
* Determine if the given column is a valid, guardable column.
*
* @param string $key
* @return bool
*/
protected function isGuardableColumn($key)
{
if (! isset(static::$guardableColumns[get_class($this)])) {
static::$guardableColumns[get_class($this)] = $this->getConnection()
->getSchemaBuilder()
->getColumnListing($this->getTable());
}

return in_array($key, static::$guardableColumns[get_class($this)]);
}

/**
Expand Down
14 changes: 12 additions & 2 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -919,11 +919,21 @@ public function testUnderscorePropertiesAreNotFilled()
public function testGuarded()
{
$model = new EloquentModelStub;

EloquentModelStub::setConnectionResolver($resolver = m::mock('Illuminate\Database\ConnectionResolverInterface'));
$resolver->shouldReceive('connection')->andReturn($connection = m::mock(stdClass::class));
$connection->shouldReceive('getSchemaBuilder->getColumnListing')->andReturn(['name', 'age', 'foo']);

$model->guard(['name', 'age']);
$model->fill(['name' => 'foo', 'age' => 'bar', 'foo' => 'bar']);
$this->assertFalse(isset($model->name));
$this->assertFalse(isset($model->age));
$this->assertEquals('bar', $model->foo);
$this->assertSame('bar', $model->foo);

$model = new EloquentModelStub;
$model->guard(['name', 'age']);
$model->fill(['Foo' => 'bar']);
$this->assertFalse(isset($model->Foo));
}

public function testFillableOverridesGuarded()
Expand Down Expand Up @@ -1829,7 +1839,7 @@ public function getDates()
class EloquentModelSaveStub extends Model
{
protected $table = 'save_stub';
protected $guarded = ['id'];
protected $guarded = [];

public function save(array $options = [])
{
Expand Down

0 comments on commit 97de42a

Please sign in to comment.