fix: answer a malformed Host with a 400 before reaching a PSR-15 ApplicationUnverified
Request::getHost() throws on a malformed Host or X-Forwarded-Host, which escaped as a logged 500 any client could trigger.
9ef9df2 parent: a8217b7 modified
src/Runner/RequestHandlerKernel.php +14 -7 | @@ -7,6 +7,7 @@ namespace JDecool\ScalewayRuntime\Runner; | ||
| 7 | 7 | use Psr\Http\Server\RequestHandlerInterface; |
| 8 | 8 | use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
| 9 | 9 | use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; |
| 10 | +use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException; | |
| 10 | 11 | use Symfony\Component\HttpFoundation\Request; |
| 11 | 12 | use Symfony\Component\HttpFoundation\Response; |
| 12 | 13 | use Symfony\Component\HttpKernel\HttpKernelInterface; |
| @@ -45,13 +46,19 @@ final class RequestHandlerKernel implements HttpKernelInterface | ||
| 45 | 46 | |
| 46 | 47 | // PSR-15 apps read the client from the server params and do not know the trusted proxies, so they get |
| 47 | 48 | // the request as the platform proxy received it, the way mod_remoteip presents it to PHP. |
| 48 | - $server = [ | |
| 49 | - 'REMOTE_ADDR' => $request->getClientIp(), | |
| 50 | - // PSR-7 needs a parsable URI; an Event without Host gets the default of Request::create(). | |
| 51 | - 'HTTP_HOST' => '' === $request->getHost() ? 'localhost' : $request->getHttpHost(), | |
| 52 | - 'SERVER_PORT' => $request->getPort() ?? ($request->isSecure() ? 443 : 80), | |
| 53 | - 'HTTPS' => $request->isSecure() ? 'on' : 'off', | |
| 54 | - ] + $request->server->all(); | |
| 49 | + try { | |
| 50 | + $server = [ | |
| 51 | + 'REMOTE_ADDR' => $request->getClientIp(), | |
| 52 | + // PSR-7 needs a parsable URI; an Event without Host gets the default of Request::create(). | |
| 53 | + 'HTTP_HOST' => '' === $request->getHost() ? 'localhost' : $request->getHttpHost(), | |
| 54 | + 'SERVER_PORT' => $request->getPort() ?? ($request->isSecure() ? 443 : 80), | |
| 55 | + 'HTTPS' => $request->isSecure() ? 'on' : 'off', | |
| 56 | + ] + $request->server->all(); | |
| 57 | + } catch (SuspiciousOperationException) { | |
| 58 | + // A malformed (X-Forwarded-)Host is the client's fault and no PSR-7 URI can carry it; FrameworkBundle | |
| 59 | + // answers Kernels the same 400 instead of letting anyone fill the error log. | |
| 60 | + return new Response('Bad Request', Response::HTTP_BAD_REQUEST); | |
| 61 | + } | |
| 55 | 62 | |
| 56 | 63 | $response = $this->handler->handle($this->psrFactory->createRequest($request->duplicate(server: $server))); |
| 57 | 64 | |
| @@ -7,6 +7,7 @@ namespace JDecool\ScalewayRuntime\Runner; | |||
| 7 | use Psr\Http\Server\RequestHandlerInterface; | 7 | use Psr\Http\Server\RequestHandlerInterface; |
| 8 | use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; | 8 | use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
| 9 | use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; | 9 | use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; |
| 10 | +use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException; | ||
| 10 | use Symfony\Component\HttpFoundation\Request; | 11 | use Symfony\Component\HttpFoundation\Request; |
| 11 | use Symfony\Component\HttpFoundation\Response; | 12 | use Symfony\Component\HttpFoundation\Response; |
| 12 | use Symfony\Component\HttpKernel\HttpKernelInterface; | 13 | use Symfony\Component\HttpKernel\HttpKernelInterface; |
| @@ -45,13 +46,19 @@ final class RequestHandlerKernel implements HttpKernelInterface | |||
| 45 | 46 | ||
| 46 | // PSR-15 apps read the client from the server params and do not know the trusted proxies, so they get | 47 | // PSR-15 apps read the client from the server params and do not know the trusted proxies, so they get |
| 47 | // the request as the platform proxy received it, the way mod_remoteip presents it to PHP. | 48 | // the request as the platform proxy received it, the way mod_remoteip presents it to PHP. |
| 48 | - $server = [ | 49 | + try { |
| 49 | - 'REMOTE_ADDR' => $request->getClientIp(), | 50 | + $server = [ |
| 50 | - // PSR-7 needs a parsable URI; an Event without Host gets the default of Request::create(). | 51 | + 'REMOTE_ADDR' => $request->getClientIp(), |
| 51 | - 'HTTP_HOST' => '' === $request->getHost() ? 'localhost' : $request->getHttpHost(), | 52 | + // PSR-7 needs a parsable URI; an Event without Host gets the default of Request::create(). |
| 52 | - 'SERVER_PORT' => $request->getPort() ?? ($request->isSecure() ? 443 : 80), | 53 | + 'HTTP_HOST' => '' === $request->getHost() ? 'localhost' : $request->getHttpHost(), |
| 53 | - 'HTTPS' => $request->isSecure() ? 'on' : 'off', | 54 | + 'SERVER_PORT' => $request->getPort() ?? ($request->isSecure() ? 443 : 80), |
| 54 | - ] + $request->server->all(); | 55 | + 'HTTPS' => $request->isSecure() ? 'on' : 'off', |
| 56 | + ] + $request->server->all(); | ||
| 57 | + } catch (SuspiciousOperationException) { | ||
| 58 | + // A malformed (X-Forwarded-)Host is the client's fault and no PSR-7 URI can carry it; FrameworkBundle | ||
| 59 | + // answers Kernels the same 400 instead of letting anyone fill the error log. | ||
| 60 | + return new Response('Bad Request', Response::HTTP_BAD_REQUEST); | ||
| 61 | + } | ||
| 55 | 62 | ||
| 56 | $response = $this->handler->handle($this->psrFactory->createRequest($request->duplicate(server: $server))); | 63 | $response = $this->handler->handle($this->psrFactory->createRequest($request->duplicate(server: $server))); |
| 57 | 64 | ||
modified
tests/Runner/RequestHandlerKernelTest.php +8 -0 | @@ -61,6 +61,14 @@ final class RequestHandlerKernelTest extends TestCase | ||
| 61 | 61 | self::assertSame('https://localhost/', $this->echo(['path' => '/', 'headers' => ['X-Forwarded-Proto' => 'https']])['uri']); |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | + public function testMalformedHostIsABadRequestThatSkipsTheHandler(): void | |
| 65 | + { | |
| 66 | + $functionResponse = $this->runner->invoke(['path' => '/', 'headers' => ['Host' => 'internal.local', 'X-Forwarded-Host' => 'bad host!']], []); | |
| 67 | + | |
| 68 | + self::assertSame(400, $functionResponse['statusCode']); | |
| 69 | + self::assertSame(0, $this->handler->handled); | |
| 70 | + } | |
| 71 | + | |
| 64 | 72 | public function testBodiesAndUploadsReachTheHandler(): void |
| 65 | 73 | { |
| 66 | 74 | $echo = $this->echo([ |
| @@ -61,6 +61,14 @@ final class RequestHandlerKernelTest extends TestCase | |||
| 61 | self::assertSame('https://localhost/', $this->echo(['path' => '/', 'headers' => ['X-Forwarded-Proto' => 'https']])['uri']); | 61 | self::assertSame('https://localhost/', $this->echo(['path' => '/', 'headers' => ['X-Forwarded-Proto' => 'https']])['uri']); |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | + public function testMalformedHostIsABadRequestThatSkipsTheHandler(): void | ||
| 65 | + { | ||
| 66 | + $functionResponse = $this->runner->invoke(['path' => '/', 'headers' => ['Host' => 'internal.local', 'X-Forwarded-Host' => 'bad host!']], []); | ||
| 67 | + | ||
| 68 | + self::assertSame(400, $functionResponse['statusCode']); | ||
| 69 | + self::assertSame(0, $this->handler->handled); | ||
| 70 | + } | ||
| 71 | + | ||
| 64 | public function testBodiesAndUploadsReachTheHandler(): void | 72 | public function testBodiesAndUploadsReachTheHandler(): void |
| 65 | { | 73 | { |
| 66 | $echo = $this->echo([ | 74 | $echo = $this->echo([ |