Skip to content

Commit dec61b9

Browse files
committed
Fix use of data()
Fix a type error in getData() when request->data is invalid.
1 parent 46fc1d1 commit dec61b9

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/Controller/Component/SecurityComponent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ protected function _authRequired(Controller $controller)
265265
$requireAuth = $this->_config['requireAuth'];
266266

267267
if (in_array($request->getParam('action'), $requireAuth) || $requireAuth == ['*']) {
268-
if (!isset($request->data['_Token'])) {
268+
if ($request->getData('_Token') === null) {
269269
throw new AuthSecurityException('\'_Token\' was not found in request data.');
270270
}
271271

src/Http/ServerRequest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,6 +1489,9 @@ public function getData($name = null, $default = null)
14891489
if ($name === null) {
14901490
return $this->data;
14911491
}
1492+
if (!is_array($this->data) && $name) {
1493+
return $default;
1494+
}
14921495

14931496
return Hash::get($this->data, $name, $default);
14941497
}

tests/TestCase/Network/RequestTest.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2520,7 +2520,7 @@ public function testReadingParams()
25202520
*
25212521
* @return void
25222522
*/
2523-
public function testDataReading()
2523+
public function testGetData()
25242524
{
25252525
$post = [
25262526
'Model' => [
@@ -2541,6 +2541,19 @@ public function testDataReading()
25412541
$this->assertSame('default', $request->getData('Model.imaginary', 'default'));
25422542
}
25432543

2544+
/**
2545+
* Test that getData() doesn't fail on scalar data.
2546+
*
2547+
* @return void
2548+
*/
2549+
public function testGetDataOnStringData()
2550+
{
2551+
$post = 'strange, but could happen';
2552+
$request = new Request(compact('post'));
2553+
$this->assertNull($request->getData('Model'));
2554+
$this->assertNull($request->getData('Model.field'));
2555+
}
2556+
25442557
/**
25452558
* Test writing with data()
25462559
*

0 commit comments

Comments
 (0)