[HttpKernel] Move conflicting origin IPs handling to catch block

This commit is contained in:
Nicolas Grekas 2016-06-29 19:50:09 +02:00
parent 3ef96b9458
commit 1f00b55373
2 changed files with 10 additions and 20 deletions

View File

@ -63,6 +63,9 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
try { try {
return $this->handleRaw($request, $type); return $this->handleRaw($request, $type);
} catch (\Exception $e) { } catch (\Exception $e) {
if ($e instanceof ConflictingHeadersException) {
$e = new BadRequestHttpException('The request headers contain conflicting information regarding the origin of this request.', $e);
}
if (false === $catch) { if (false === $catch) {
$this->finishRequest($request, $type); $this->finishRequest($request, $type);
@ -115,13 +118,6 @@ class HttpKernel implements HttpKernelInterface, TerminableInterface
*/ */
private function handleRaw(Request $request, $type = self::MASTER_REQUEST) private function handleRaw(Request $request, $type = self::MASTER_REQUEST)
{ {
if (self::MASTER_REQUEST === $type && $request::getTrustedProxies()) {
try {
$request->getClientIps();
} catch (ConflictingHeadersException $e) {
throw new BadRequestHttpException('The request headers contain conflicting information regarding the origin of this request.', $e);
}
}
$this->requestStack->push($request); $this->requestStack->push($request);
// request // request

View File

@ -276,7 +276,13 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
*/ */
public function testInconsistentClientIpsOnMasterRequests() public function testInconsistentClientIpsOnMasterRequests()
{ {
$kernel = new HttpKernel(new EventDispatcher(), $this->getResolver()); $dispatcher = new EventDispatcher();
$dispatcher->addListener(KernelEvents::REQUEST, function ($event) {
$event->getRequest()->getClientIp();
});
$kernel = new HttpKernel($dispatcher, $this->getResolver());
$request = new Request(); $request = new Request();
$request->setTrustedProxies(array('1.1.1.1')); $request->setTrustedProxies(array('1.1.1.1'));
$request->server->set('REMOTE_ADDR', '1.1.1.1'); $request->server->set('REMOTE_ADDR', '1.1.1.1');
@ -286,18 +292,6 @@ class HttpKernelTest extends \PHPUnit_Framework_TestCase
$kernel->handle($request, $kernel::MASTER_REQUEST, false); $kernel->handle($request, $kernel::MASTER_REQUEST, false);
} }
public function testInconsistentClientIpsOnSubRequests()
{
$kernel = new HttpKernel(new EventDispatcher(), $this->getResolver());
$request = new Request();
$request->setTrustedProxies(array('1.1.1.1'));
$request->server->set('REMOTE_ADDR', '1.1.1.1');
$request->headers->set('FORWARDED', '2.2.2.2');
$request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
$this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $kernel->handle($request, $kernel::SUB_REQUEST, false));
}
protected function getResolver($controller = null) protected function getResolver($controller = null)
{ {
if (null === $controller) { if (null === $controller) {