Skip to content

Commit 26475a4

Browse files
Grzegorz Rajchmantaylorotwell
authored andcommitted
Prevent accessing object properties using array access (#18403)
1 parent bca505c commit 26475a4

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ public function __set($key, $value)
12541254
*/
12551255
public function offsetExists($offset)
12561256
{
1257-
return isset($this->$offset);
1257+
return ! is_null($this->getAttribute($offset));
12581258
}
12591259

12601260
/**
@@ -1265,7 +1265,7 @@ public function offsetExists($offset)
12651265
*/
12661266
public function offsetGet($offset)
12671267
{
1268-
return $this->$offset;
1268+
return $this->getAttribute($offset);
12691269
}
12701270

12711271
/**
@@ -1277,7 +1277,7 @@ public function offsetGet($offset)
12771277
*/
12781278
public function offsetSet($offset, $value)
12791279
{
1280-
$this->$offset = $value;
1280+
$this->setAttribute($offset, $value);
12811281
}
12821282

12831283
/**
@@ -1288,7 +1288,7 @@ public function offsetSet($offset, $value)
12881288
*/
12891289
public function offsetUnset($offset)
12901290
{
1291-
unset($this->$offset);
1291+
unset($this->attributes[$offset], $this->relations[$offset]);
12921292
}
12931293

12941294
/**
@@ -1299,7 +1299,7 @@ public function offsetUnset($offset)
12991299
*/
13001300
public function __isset($key)
13011301
{
1302-
return ! is_null($this->getAttribute($key));
1302+
return $this->offsetExists($key);
13031303
}
13041304

13051305
/**
@@ -1310,7 +1310,7 @@ public function __isset($key)
13101310
*/
13111311
public function __unset($key)
13121312
{
1313-
unset($this->attributes[$key], $this->relations[$key]);
1313+
$this->offsetUnset($key);
13141314
}
13151315

13161316
/**

src/Illuminate/Support/Fluent.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function toJson($options = 0)
9494
*/
9595
public function offsetExists($offset)
9696
{
97-
return isset($this->{$offset});
97+
return isset($this->attributes[$offset]);
9898
}
9999

100100
/**
@@ -105,7 +105,7 @@ public function offsetExists($offset)
105105
*/
106106
public function offsetGet($offset)
107107
{
108-
return $this->{$offset};
108+
return $this->get($offset);
109109
}
110110

111111
/**
@@ -117,7 +117,7 @@ public function offsetGet($offset)
117117
*/
118118
public function offsetSet($offset, $value)
119119
{
120-
$this->{$offset} = $value;
120+
$this->attributes[$offset] = $value;
121121
}
122122

123123
/**
@@ -128,7 +128,7 @@ public function offsetSet($offset, $value)
128128
*/
129129
public function offsetUnset($offset)
130130
{
131-
unset($this->{$offset});
131+
unset($this->attributes[$offset]);
132132
}
133133

134134
/**
@@ -165,7 +165,7 @@ public function __get($key)
165165
*/
166166
public function __set($key, $value)
167167
{
168-
$this->attributes[$key] = $value;
168+
$this->offsetSet($key, $value);
169169
}
170170

171171
/**
@@ -176,7 +176,7 @@ public function __set($key, $value)
176176
*/
177177
public function __isset($key)
178178
{
179-
return isset($this->attributes[$key]);
179+
return $this->offsetExists($key);
180180
}
181181

182182
/**
@@ -187,6 +187,6 @@ public function __isset($key)
187187
*/
188188
public function __unset($key)
189189
{
190-
unset($this->attributes[$key]);
190+
$this->offsetUnset($key);
191191
}
192192
}

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ public function testCalculatedAttributes()
9898
$this->assertEquals($hash, $model->password_hash);
9999
}
100100

101+
public function testArrayAccessToAttributes()
102+
{
103+
$model = new EloquentModelStub(['attributes' => 1, 'connection' => 2, 'table' => 3]);
104+
unset($model['table']);
105+
106+
$this->assertTrue(isset($model['attributes']));
107+
$this->assertEquals($model['attributes'], 1);
108+
$this->assertTrue(isset($model['connection']));
109+
$this->assertEquals($model['connection'], 2);
110+
$this->assertFalse(isset($model['table']));
111+
$this->assertEquals($model['table'], null);
112+
$this->assertFalse(isset($model['with']));
113+
}
114+
101115
public function testNewInstanceReturnsNewInstanceWithAttributesSet()
102116
{
103117
$model = new EloquentModelStub;

tests/Support/SupportFluentTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ public function testGetMethodReturnsAttribute()
5858
$this->assertNull($fluent->foo);
5959
}
6060

61+
public function testArrayAccessToAttributes()
62+
{
63+
$fluent = new Fluent(['attributes' => '1']);
64+
65+
$this->assertTrue(isset($fluent['attributes']));
66+
$this->assertEquals($fluent['attributes'], 1);
67+
68+
$fluent->attributes();
69+
70+
$this->assertTrue($fluent['attributes']);
71+
}
72+
6173
public function testMagicMethodsCanBeUsedToSetAttributes()
6274
{
6375
$fluent = new Fluent;

0 commit comments

Comments
 (0)