Skip to content

Commit 7130b4c

Browse files
magnusnordlandernicolas-grekas
authored andcommitted
[HttpKernel] Add listener that checks when request has both Forwarded and X-Forwarded-For
1 parent 9bae2cb commit 7130b4c

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\EventListener;
13+
14+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
16+
use Symfony\Component\HttpKernel\KernelEvents;
17+
18+
/**
19+
* Validates that the headers and other information indicating the
20+
* client IP address of a request are consistent.
21+
*
22+
* @author Magnus Nordlander <magnus@fervo.se>
23+
*/
24+
class ValidateRequestListener implements EventSubscriberInterface
25+
{
26+
/**
27+
* Performs the validation.
28+
*
29+
* @param GetResponseEvent $event
30+
*/
31+
public function onKernelRequest(GetResponseEvent $event)
32+
{
33+
if (!$event->isMasterRequest()) {
34+
return;
35+
}
36+
$request = $event->getRequest();
37+
38+
if ($request::getTrustedProxies()) {
39+
// This will throw an exception if the headers are inconsistent.
40+
$request->getClientIps();
41+
}
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public static function getSubscribedEvents()
48+
{
49+
return array(
50+
KernelEvents::REQUEST => array(
51+
array('onKernelRequest', 256),
52+
),
53+
);
54+
}
55+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\EventListener;
13+
14+
use Symfony\Component\EventDispatcher\EventDispatcher;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpKernel\EventListener\ValidateRequestListener;
17+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
18+
use Symfony\Component\HttpKernel\HttpKernelInterface;
19+
use Symfony\Component\HttpKernel\KernelEvents;
20+
21+
class ValidateRequestListenerTest extends \PHPUnit_Framework_TestCase
22+
{
23+
/**
24+
* @expectedException Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException
25+
*/
26+
public function testListenerThrowsWhenMasterRequestHasInconsistentClientIps()
27+
{
28+
$dispatcher = new EventDispatcher();
29+
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
30+
31+
$request = new Request();
32+
$request->setTrustedProxies(array('1.1.1.1'));
33+
$request->server->set('REMOTE_ADDR', '1.1.1.1');
34+
$request->headers->set('FORWARDED', '2.2.2.2');
35+
$request->headers->set('X_FORWARDED_FOR', '3.3.3.3');
36+
37+
$dispatcher->addListener(KernelEvents::REQUEST, array(new ValidateRequestListener(), 'onKernelRequest'));
38+
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
39+
40+
$dispatcher->dispatch(KernelEvents::REQUEST, $event);
41+
}
42+
}

0 commit comments

Comments
 (0)