8000 [Form] Moved POST_MAX_SIZE validation from FormValidator to request handler by webmozart · Pull Request #11924 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Moved POST_MAX_SIZE validation from FormValidator to request handler #11924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 24, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[Form] Add a form error if post_max_size has been reached.
  • Loading branch information
rpg600 authored and webmozart committed Sep 23, 2014
commit 47802105d3ab82c08303900c0ded5e977c4889d1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\Form\Extension\HttpFoundation;

use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Validator\Util\ServerParams;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\RequestHandlerInterface;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -24,6 +26,19 @@
*/
class HttpFoundationRequestHandler implements RequestHandlerInterface
{
/**
* @var ServerParams
*/
private $serverParams;

/**
* {@inheritdoc}
*/
public function __construct(ServerParams $params = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to update FrameworkBundle to inject the ServerParam explicitly there, to use the RequestStack rather than global variables

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RequestStack didn't exist in 2.3.

{
$this->serverParams = $params ?: new ServerParams();
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -61,6 +76,10 @@ public function handleRequest(FormInterface $form, $request = null)
$params = $request->request->get($name, $default);
$files = $request->files->get($name, $default);
} else {
if ($this->serverParams->getContentLength() > $this->serverParams->getPostMaxSize()) {
$form->addError(new FormError('Max post size exceeded.'));
}

// Don't submit the form if it is not present in the request
return;
}
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/Form/NativeRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Form;

use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Validator\Util\ServerParams;

/**
* A request handler using PHP's super globals $_GET, $_POST and $_SERVER.
Expand All @@ -20,6 +21,19 @@
*/
class NativeRequestHandler implements RequestHandlerInterface
{
/**
* @var ServerParams
*/
private $serverParams;

/**
* {@inheritdoc}
*/
public function __construct(ServerParams $params = null)
{
$this->serverParams = $params ?: new ServerParams();
}

/**
* The allowed keys of the $_FILES array.
*
Expand Down Expand Up @@ -75,6 +89,10 @@ public function handleRequest(FormInterface $form, $request = null)
$params = array_key_exists($name, $_POST) ? $_POST[$name] : $default;
$files = array_key_exists($name, $fixedFiles) ? $fixedFiles[$name] : $default;
} else {
if ($this->serverParams->getContentLength() > $this->serverParams->getPostMaxSize()) {
$form->addError(new FormError('Max post size exceeded.'));
}

// Don't submit the form if it is not present in the request
return;
}
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Form\Tests;

use Symfony\Component\Form\Forms;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
Expand All @@ -21,11 +23,17 @@ abstract class AbstractRequestHandlerTest extends \PHPUnit_Framework_TestCase
*/
protected $requestHandler;

/**
* @var \Symfony\Component\Form\FormFactory
*/
protected $factory;

protected $request;

protected function setUp()
{
$this->requestHandler = $this->getRequestHandler();
$this->factory = Forms::createFormFactoryBuilder()->getFormFactory();
$this->request = null;
}

Expand Down Expand Up @@ -249,6 +257,19 @@ public function testSubmitFileIfNoParam($method)
$this->requestHandler->handleRequest($form, $this->request);
}

public function testAddFormErrorIfPostMaxSizeExceeded()
{
$form = $this->factory->createNamed('name', 'text');
$this->setRequestData('POST', array(), array());
$_SERVER['CONTENT_LENGTH'] = 1000000000;

$this->requestHandler->handleRequest($form, $this->request);

$this->assertEquals("ERROR: Max post size exceeded.\n", $form->getErrorsAsString());

unset($_SERVER['CONTENT_LENGTH']);
}

abstract protected function setRequestData($method, $data, $files = array());

abstract protected function getRequestHandler();
Expand Down
0