jdecool/scaleway-runtimepublic Fork
⑂ 9ef9df2
Commits
⬇ Clone ▾
git clone https://git.rickub.com/jdecool/scaleway-runtime.git
git clone ssh://git@rickub.com/jdecool/scaleway-runtime.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP

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.
Jérémy DECOOL committed 2026-11-13T22:50:58+01:00 Browse files
9ef9df2 parent: a8217b7
modified src/Runner/RequestHandlerKernel.php +14 -7
@@ -7,6 +7,7 @@ namespace JDecool\ScalewayRuntime\Runner;
77 use Psr\Http\Server\RequestHandlerInterface;
88 use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
99 use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
10+use Symfony\Component\HttpFoundation\Exception\SuspiciousOperationException;
1011 use Symfony\Component\HttpFoundation\Request;
1112 use Symfony\Component\HttpFoundation\Response;
1213 use Symfony\Component\HttpKernel\HttpKernelInterface;
@@ -45,13 +46,19 @@ final class RequestHandlerKernel implements HttpKernelInterface
4546
4647 // PSR-15 apps read the client from the server params and do not know the trusted proxies, so they get
4748 // 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+ }
5562
5663 $response = $this->handler->handle($this->psrFactory->createRequest($request->duplicate(server: $server)));
5764
@@ -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 get47 // 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
6161 self::assertSame('https://localhost/', $this->echo(['path' => '/', 'headers' => ['X-Forwarded-Proto' => 'https']])['uri']);
6262 }
6363
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+
6472 public function testBodiesAndUploadsReachTheHandler(): void
6573 {
6674 $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(): void72 public function testBodiesAndUploadsReachTheHandler(): void
65 {73 {
66 $echo = $this->echo([74 $echo = $this->echo([