10000 [HttpFoundation] Warning when request has both Forwarded and X-Forwarded-For by magnusnordlander · Pull Request #18688 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpFoundation] Warning when request has both Forwarded and X-Forwarded-For #18688

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

Closed
wants to merge 11 commits into from
Closed
Prev Previous commit
Next Next commit
Added a listener to validate requests
  • Loading branch information
magnusnordlander committed Jun 25, 2016
commit c0172900e5a069d6b26b4f498fbdeb3055853bb1
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@
<argument type="service" id="request_stack" />
<tag name="kernel.event_subscriber" />
</service>

<service id="validate_request_client_ip_listener" class="Symfony\Component\HttpKernel\EventListener\ValidateRequestClientIpListener">
Copy link
Member

Choose a reason for hiding this comment

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

If we rename the class as I suggest, this should be renamed accordingly (validate_request_listener).

<tag name="kernel.event_subscriber" />
</service>
</services>
</container>
8 changes: 5 additions & 3 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,9 @@ public function getClientIps()

if ($hasTrustedForwardedHeader && $hasTrustedClientIpHeader && $forwardedClientIps !== $xForwardedForClientIps) {
throw new ConflictingHeadersException('The request has both a trusted Forwarded header and a trusted Client IP header, conflicting with each other with regards to the originating IP addresses of the request. This is the result of a misconfiguration. You should either configure your proxy only to send one of these headers, or configure Symfony to distrust one of them.');
} elseif (!$hasTrustedForwardedHeader && !$hasTrustedClientIpHeader) {
}

if (!$hasTrustedForwardedHeader && !$hasTrustedClientIpHeader) {
return $this->normalizeAndFilterClientIps(array(), $ip);
}

Expand Down Expand Up @@ -1923,7 +1925,7 @@ private function isFromTrustedProxy()
return self::$trustedProxies && IpUtils::checkIp($this->server->get('REMOTE_ADDR'), self::$trustedProxies);
}

private function normalizeAndFilterClientIps($clientIps, $ip)
private function normalizeAndFilterClientIps(array $clientIps, $ip)
{
$clientIps[] = $ip; // Complete the IP chain with the IP the request actually came from
$firstTrustedIp = null;
Expand All @@ -1944,7 +1946,7 @@ private function normalizeAndFilterClientIps($clientIps, $ip)
unset($clientIps[$key]);

// Fallback to this when the client IP falls into the range of trusted proxies
if (null === $firstTrustedIp) {
if (null === $firstTrustedIp) {
8000 $firstTrustedIp = $clientIp;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* Validates that the headers and other information indicating the
* client IP address of a request are consistent.
*
* @author Magnus Nordlander <magnus@fervo.se>
*/
class ValidateRequestClientIpListener implements EventSubscriberInterface
Copy link
Member

Choose a reason for hiding this comment

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

What about naming this class ValidateRequestListener to allow adding more checks in the future?

Copy link
Member

Choose a reason for hiding this comment

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

We already have a generic response_listener, so naming it request_listener might be an option as well (not sure about possible conflicts with existing end-user listeners though).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I should think that validate_request_listener has less potential for conflict

{
/**
* Performs the validation
*
* @param GetResponseEvent $event
*/
public function onKernelRequest(GetResponseEvent $event)
{
try {
// This will throw an exception if the headers are inconsistent.
$event->getRequest()->getClientIps();
} catch (ConflictingHeadersException $e) {
throw new HttpException(400, "The request headers contain conflicting information regarding the origin of this request.", $e);
}
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return array(
KernelEvents::REQUEST => array(
array('onKernelRequest', 256),
),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpKernel\Tests\EventListener;

use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\EventListener\ValidateRequestClientIpListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\KernelEvents;

class ValidateRequestClientIpTest extends \PHPUnit_Framework_TestCase
{
public function testListenerThrowsOnInconsistentRequests()
{
$dispatcher = new EventDispatcher();
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$listener = new ValidateRequestClientIpListener();
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request->method('getClientIps')
->will($this->throwException(new ConflictingHeadersException));

$dispatcher->addListener(KernelEvents::REQUEST, array($listener, 'onKernelRequest'));
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST);

$this->setExpectedException('Symfony\Component\HttpKernel\Exception\HttpException');
$dispatcher->dispatch(KernelEvents::REQUEST, $event);
}

public function testListenerDoesNothingOnConsistenRequests()
{
$dispatcher = new EventDispatcher();
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$listener = new ValidateRequestClientIpListener();
$request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$request->method('getClientIps')
->willReturn(['127.0.0.1']);

$dispatcher->addListener(KernelEvents::REQUEST, array($listener, 'onKernelRequest'));
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::SUB_REQUEST);
$dispatcher->dispatch(KernelEvents::REQUEST, $event);
}
}
0