Skip to content

Commit db84101

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

File tree

4 files changed

+103
-2
lines changed

4 files changed

+103
-2
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,9 @@
4646
<argument type="service" id="request_stack" />
4747
<tag name="kernel.event_subscriber" />
4848
</service>
49+
50+
<service id="validate_request_listener" class="Symfony\Component\HttpKernel\EventListener\ValidateRequestListener">
51+
<tag name="kernel.event_subscriber" />
52+
</service>
4953
</services>
5054
</container>

src/Symfony/Bundle/FrameworkBundle/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"symfony/config": "~2.4",
2323
"symfony/event-dispatcher": "~2.5",
2424
"symfony/finder": "~2.0,>=2.0.5",
25-
"symfony/http-foundation": "~2.4.9|~2.5,>=2.5.4",
26-
"symfony/http-kernel": "~2.7",
25+
"symfony/http-foundation": "~2.7",
26+
"symfony/http-kernel": "~2.7.15|~2.8.8",
2727
"symfony/filesystem": "~2.3",
2828
"symfony/routing": "~2.6,>2.6.4",
2929
"symfony/security-core": "~2.6.13|~2.7.9|~2.8",
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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
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)