bug #25750 [HttpKernel] Turn bad hosts into 400 instead of 500 (nicolas-grekas)
This PR was merged into the 2.7 branch.
Discussion
----------
[HttpKernel] Turn bad hosts into 400 instead of 500
| Q | A
| ------------- | ---
| Branch? | 2.7
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
Commits
-------
3e47c71028
[HttpKernel] Turn bad hosts into 400 instead of 500
This commit is contained in:
commit
f35a7b5a7a
|
@ -15,6 +15,7 @@ use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||||
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
|
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
|
||||||
use Symfony\Component\HttpKernel\KernelEvents;
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||||
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
|
||||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
use Symfony\Component\HttpFoundation\RequestStack;
|
use Symfony\Component\HttpFoundation\RequestStack;
|
||||||
|
@ -96,7 +97,11 @@ class RouterListener implements EventSubscriberInterface
|
||||||
private function setCurrentRequest(Request $request = null)
|
private function setCurrentRequest(Request $request = null)
|
||||||
{
|
{
|
||||||
if (null !== $request && $this->request !== $request) {
|
if (null !== $request && $this->request !== $request) {
|
||||||
$this->context->fromRequest($request);
|
try {
|
||||||
|
$this->context->fromRequest($request);
|
||||||
|
} catch (\UnexpectedValueException $e) {
|
||||||
|
throw new BadRequestHttpException($e->getMessage(), $e, $e->getCode());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
|
|
|
@ -155,4 +155,19 @@ class RouterListenerTest extends TestCase
|
||||||
array(array(), 'Matched route "n/a".'),
|
array(array(), 'Matched route "n/a".'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
||||||
|
*/
|
||||||
|
public function testSubRequestWithBadHost()
|
||||||
|
{
|
||||||
|
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
|
||||||
|
$request = Request::create('http://bad host %22/');
|
||||||
|
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
|
||||||
|
|
||||||
|
$requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
|
||||||
|
|
||||||
|
$listener = new RouterListener($requestMatcher, new RequestContext(), null, $this->requestStack);
|
||||||
|
$listener->onKernelRequest($event);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue