Skip to content

Commit

Permalink
[5.5] Security fixes (#33858)
Browse files Browse the repository at this point in the history
* fix casing issue with guarded

* block json mass assignment

* formatting

* add boolean

* protect table names and guarded

* Apply fixes from StyleCI (#33772)

* dont allow mass filling with table names

* Apply fixes from StyleCI (#33773)

* [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

* Apply fixes from StyleCI (#33857)

Co-authored-by: Taylor Otwell <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Aug 13, 2020
1 parent 97b9f72 commit b5eed48
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
33 changes: 32 additions & 1 deletion 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 @@ -152,6 +159,7 @@ public function isFillable($key)
}

return empty($this->getFillable()) &&
strpos($key, '.') === false &&
! Str::startsWith($key, '_');
}

Expand All @@ -163,7 +171,30 @@ public function isFillable($key)
*/
public function isGuarded($key)
{
return in_array($key, $this->getGuarded()) || $this->getGuarded() == ['*'];
if (empty($this->getGuarded())) {
return false;
}

return $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
2 changes: 1 addition & 1 deletion Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public function qualifyColumn($column)
*/
protected function removeTableFromKey($key)
{
return Str::contains($key, '.') ? last(explode('.', $key)) : $key;
return $key;
}

/**
Expand Down

0 comments on commit b5eed48

Please sign in to comment.