-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from 9 commits
4c262d4
da4b6c5
f0c3d15
2eea0f1
c017290
99b072e
41d2603
ea691e7
a9f864d
66f19f1
d52d098
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?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\HttpFoundation\Exception; | ||
|
||
/** | ||
* The HTTP request contains headers with conflicting information. | ||
* | ||
* This exception should trigger an HTTP 400 response in your application code. | ||
* | ||
* @author Magnus Nordlander <magnus@fervo.se> | ||
*/ | ||
class ConflictingHeadersException extends \RuntimeException | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?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\HttpKernelInterface; | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about naming this class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have a generic There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I should think that |
||
{ | ||
/** | ||
* Performs the validation. | ||
* | ||
* @param GetResponseEvent $event | ||
*/ | ||
public function onKernelRequest(GetResponseEvent $event) | ||
{ | ||
if ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably use the |
||
} | ||
} | ||
} | ||
|
||
/** | ||
* {@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,67 @@ | ||
<?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 testListenerThrowsOnInconsistentMasterRequests() | ||
{ | ||
$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::MASTER_REQUEST); | ||
|
||
$this->setExpectedException('Symfony\Component\HttpKernel\Exception\HttpException'); | ||
$dispatcher->dispatch(KernelEvents::REQUEST, $event); | ||
} | ||
|
||
public function testListenerDoesNothingOnConsistentRequests() | ||
{ | ||
$dispatcher = new EventDispatcher(); | ||
$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface'); | ||
$listener = new ValidateRequestClientIpListener(); | ||
$request = $this->getMock('Symfony\Component\HttpFoundation\Request'); | ||
$request->method('getClientIps') | ||
->willReturn(array('127.0.0.1')); | ||
|
||
$dispatcher->addListener(KernelEvents::REQUEST, array($listener, 'onKernelRequest')); | ||
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); | ||
$dispatcher->dispatch(KernelEvents::REQUEST, $event); | ||
} | ||
|
||
public function testListenerDoesNothingOnSubrequests() | ||
{ | ||
$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); | ||
$dispatcher->dispatch(KernelEvents::REQUEST, $event); | ||
} | ||
} |
There was a problem hiding this comment.
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
).