Skip to content

Commit

Permalink
Fix _validatePost returns true when empty form is submitted
Browse files Browse the repository at this point in the history
  • Loading branch information
chinpei215 committed May 5, 2017
1 parent 1e20350 commit c792290
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/Controller/Component/SecurityComponent.php
Expand Up @@ -102,7 +102,7 @@ public function startup(Event $event)
$controller = $event->getSubject();
$this->session = $controller->request->getSession();
$this->_action = $controller->request->getParam('action');
$hasData = (bool)$controller->request->getData();
$hasData = ($controller->request->getData() || $controller->request->is(['put', 'post', 'delete', 'patch']));
try {
$this->_secureRequired($controller);
$this->_authRequired($controller);
Expand Down Expand Up @@ -312,9 +312,6 @@ protected function _authRequired(Controller $controller)
*/
protected function _validatePost(Controller $controller)
{
if (!$controller->request->getData()) {
return true;
}
$token = $this->_validToken($controller);
$hashParts = $this->_hashParts($controller);
$check = Security::hash(implode('', $hashParts), 'sha1');
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase/Controller/Component/SecurityComponentTest.php
Expand Up @@ -554,6 +554,25 @@ public function testValidatePostFormHacking()
$this->assertFalse($result, 'validatePost passed when fields were missing. %s');
}

/**
* testValidatePostEmptyForm method
*
* Test that validatePost fails if empty form is submitted.
*
* @return void
* @triggers Controller.startup $this->Controller
*/
public function testValidatePostEmptyForm()
{
$this->Controller->request = $this->Controller->request
->withEnv('REQUEST_METHOD', 'POST')
->withParsedBody([]);
$event = new Event('Controller.startup', $this->Controller);
$this->Security->startup($event);
$result = $this->validatePost('AuthSecurityException', '\'_Token\' was not found in request data.');
$this->assertFalse($result, 'validatePost passed when empty form is submitted');
}

/**
* testValidatePostObjectDeserialize
*
Expand Down

0 comments on commit c792290

Please sign in to comment.