Skip to content

Commit b6f81b8

Browse files
divinefish3046
andcommitted
Fix guarded to return always true
Fix for laravel/framework#33858 Co-Authored-By: Karl Pierce <[email protected]>
1 parent 5454ac9 commit b6f81b8

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

Diff for: README.md

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Table of contents
1111
* [Upgrading](#upgrading)
1212
* [Configuration](#configuration)
1313
* [Eloquent](#eloquent)
14+
* [Guarding attributes](#guarding-attributes)
1415
* [Optional: Alias](#optional-alias)
1516
* [Query Builder](#query-builder)
1617
* [Schema](#schema)
@@ -41,6 +42,7 @@ composer require jenssegers/mongodb
4142
5.2.x | 2.3.x or 3.0.x
4243
5.3.x | 3.1.x or 3.2.x
4344
5.4.x | 3.2.x
45+
5.5.x | 3.3.x
4446

4547
And add the service provider in `config/app.php`:
4648

@@ -192,6 +194,13 @@ class MyModel extends Eloquent {
192194

193195
Everything else (should) work just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent
194196

197+
### Guarding attributes
198+
199+
When choosing between guarding attributes or marking some as fillable, Taylor Otwell prefers the fillable route.
200+
This is in light of [recent security issues described here](https://blog.laravel.com/security-release-laravel-61835-7240).
201+
202+
Keep in mind guarding still works, but you may experience unexpected behavior.
203+
195204
### Optional: Alias
196205

197206
You may also register an alias for the MongoDB model by adding the following to the alias array in `config/app.php`:

Diff for: src/Jenssegers/Mongodb/Eloquent/Model.php

+11
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,17 @@ protected function removeTableFromKey($key)
420420
return $key;
421421
}
422422

423+
/**
424+
* Checks if column exists on a table. As this is a document model, just return true. This also
425+
* prevents calls to non-existent function Grammar::compileColumnListing()
426+
* @param string $key
427+
* @return bool
428+
*/
429+
protected function isGuardableColumn($key)
430+
{
431+
return true;
432+
}
433+
423434
/**
424435
* @inheritdoc
425436
*/

Diff for: src/Jenssegers/Mongodb/Schema/Builder.php

-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77

88
class Builder extends \Illuminate\Database\Schema\Builder
99
{
10-
/**
11-
* @inheritdoc
12-
*/
13-
public function __construct(Connection $connection)
14-
{
15-
$this->connection = $connection;
16-
}
17-
1810
/**
1911
* @inheritdoc
2012
*/

Diff for: tests/ModelTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public function tearDown()
1414
Soft::truncate();
1515
Book::truncate();
1616
Item::truncate();
17+
Guarded::truncate();
1718
}
1819

1920
public function testNewModel()
@@ -548,4 +549,27 @@ public function testChunkById()
548549

549550
$this->assertEquals(3, $count);
550551
}
552+
553+
public function testGuardedModel()
554+
{
555+
$model = new Guarded();
556+
557+
// foobar is properly guarded
558+
$model->fill(['foobar' => 'ignored', 'name' => 'John Doe']);
559+
$this->assertFalse(isset($model->foobar));
560+
$this->assertSame('John Doe', $model->name);
561+
562+
// foobar is guarded to any level
563+
$model->fill(['foobar->level2' => 'v2']);
564+
$this->assertNull($model->getAttribute('foobar->level2'));
565+
566+
// multi level statement also guarded
567+
$model->fill(['level1->level2' => 'v1']);
568+
$this->assertNull($model->getAttribute('level1->level2'));
569+
570+
// level1 is still writable
571+
$dataValues = ['array', 'of', 'values'];
572+
$model->fill(['level1' => $dataValues]);
573+
$this->assertEquals($dataValues, $model->getAttribute('level1'));
574+
}
551575
}

Diff for: tests/models/Guarded.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
5+
6+
class Guarded extends Eloquent
7+
{
8+
protected $connection = 'mongodb';
9+
protected $collection = 'guarded';
10+
protected $guarded = ['foobar', 'level1->level2'];
11+
}

0 commit comments

Comments
 (0)