Skip to content

Commit f35a7b5

Browse files
committed
bug symfony#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 ------- 3e47c71 [HttpKernel] Turn bad hosts into 400 instead of 500
2 parents fad59b3 + 3e47c71 commit f35a7b5

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Symfony/Component/HttpKernel/EventListener/RouterListener.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1616
use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
1717
use Symfony\Component\HttpKernel\KernelEvents;
18+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1819
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
1920
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2021
use Symfony\Component\HttpFoundation\RequestStack;
@@ -96,7 +97,11 @@ public function setRequest(Request $request = null)
9697
private function setCurrentRequest(Request $request = null)
9798
{
9899
if (null !== $request && $this->request !== $request) {
99-
$this->context->fromRequest($request);
100+
try {
101+
$this->context->fromRequest($request);
102+
} catch (\UnexpectedValueException $e) {
103+
throw new BadRequestHttpException($e->getMessage(), $e, $e->getCode());
104+
}
100105
}
101106

102107
$this->request = $request;

src/Symfony/Component/HttpKernel/Tests/EventListener/RouterListenerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,19 @@ public function getLoggingParameterData()
155155
array(array(), 'Matched route "n/a".'),
156156
);
157157
}
158+
159+
/**
160+
* @expectedException \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
161+
*/
162+
public function testSubRequestWithBadHost()
163+
{
164+
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();
165+
$request = Request::create('http://bad host %22/');
166+
$event = new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST);
167+
168+
$requestMatcher = $this->getMockBuilder('Symfony\Component\Routing\Matcher\RequestMatcherInterface')->getMock();
169+
170+
$listener = new RouterListener($requestMatcher, new RequestContext(), null, $this->requestStack);
171+
$listener->onKernelRequest($event);
172+
}
158173
}

0 commit comments

Comments
 (0)