jdecool/scaleway-runtimepublic Fork
⑂ be2173f
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: drop Event headers with underscores, which could shadow X-Real-Ip and X-Forwarded-ProtoUnverified

Jérémy DECOOL committed 2026-11-14T12:08:34+01:00 Browse files
be2173f parent: 7b3548a
modified docs/adr/0007-trust-only-headers-the-platform-overwrites.md +1 -0
@@ -16,3 +16,4 @@ The forgeable headers still reach the Application as plain headers, and trusted
1616 - Absolute URLs (router, redirects to absolute URLs, signed URLs) are only correct once `host` is set; Terraform cannot pass a function its own `domain_name` (the reference would be a cycle), so set it from a custom domain or after a first deploy.
1717 - `X-Real-Ip` and `X-Forwarded-Proto` are undocumented platform behaviour, measured once; a change of the platform would need a new probe.
1818 - An application configuring `framework.trusted_proxies` against `REMOTE_ADDR` or private ranges trusts forgeable headers again.
19+- Headers whose name holds an underscore are dropped, as nginx and Apache do: `X_Real_Ip` would otherwise land on the same `$_SERVER` key as the platform's `X-Real-Ip`, and could win over it.
@@ -16,3 +16,4 @@ The forgeable headers still reach the Application as plain headers, and trusted
16 - Absolute URLs (router, redirects to absolute URLs, signed URLs) are only correct once `host` is set; Terraform cannot pass a function its own `domain_name` (the reference would be a cycle), so set it from a custom domain or after a first deploy.16 - Absolute URLs (router, redirects to absolute URLs, signed URLs) are only correct once `host` is set; Terraform cannot pass a function its own `domain_name` (the reference would be a cycle), so set it from a custom domain or after a first deploy.
17 - `X-Real-Ip` and `X-Forwarded-Proto` are undocumented platform behaviour, measured once; a change of the platform would need a new probe.17 - `X-Real-Ip` and `X-Forwarded-Proto` are undocumented platform behaviour, measured once; a change of the platform would need a new probe.
18 - An application configuring `framework.trusted_proxies` against `REMOTE_ADDR` or private ranges trusts forgeable headers again.18 - An application configuring `framework.trusted_proxies` against `REMOTE_ADDR` or private ranges trusts forgeable headers again.
19+- Headers whose name holds an underscore are dropped, as nginx and Apache do: `X_Real_Ip` would otherwise land on the same `$_SERVER` key as the platform's `X-Real-Ip`, and could win over it.
modified src/HttpConverter.php +5 -0
@@ -56,6 +56,11 @@ final class HttpConverter
5656 $contentType = '';
5757 $contentTypeHeader = '';
5858 foreach (self::stringMap($event['headers'] ?? null) as $name => $value) {
59+ // "X_Real_Ip" would land on the same key as the platform's X-Real-Ip, and may come after it:
60+ // dropped, as nginx and Apache drop such names for CGI.
61+ if (str_contains($name, '_')) {
62+ continue;
63+ }
5964 $key = strtoupper(str_replace('-', '_', $name));
6065 // PHP exposes these two without the HTTP_ prefix, and HeaderBag expects them that way.
6166 $server[\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'], true) ? $key : 'HTTP_'.$key] = $value;
@@ -56,6 +56,11 @@ final class HttpConverter
56 $contentType = '';56 $contentType = '';
57 $contentTypeHeader = '';57 $contentTypeHeader = '';
58 foreach (self::stringMap($event['headers'] ?? null) as $name => $value) {58 foreach (self::stringMap($event['headers'] ?? null) as $name => $value) {
59+ // "X_Real_Ip" would land on the same key as the platform's X-Real-Ip, and may come after it:
60+ // dropped, as nginx and Apache drop such names for CGI.
61+ if (str_contains($name, '_')) {
62+ continue;
63+ }
59 $key = strtoupper(str_replace('-', '_', $name));64 $key = strtoupper(str_replace('-', '_', $name));
60 // PHP exposes these two without the HTTP_ prefix, and HeaderBag expects them that way.65 // PHP exposes these two without the HTTP_ prefix, and HeaderBag expects them that way.
61 $server[\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'], true) ? $key : 'HTTP_'.$key] = $value;66 $server[\in_array($key, ['CONTENT_TYPE', 'CONTENT_LENGTH'], true) ? $key : 'HTTP_'.$key] = $value;
modified tests/HttpConverterTest.php +13 -0
@@ -311,6 +311,19 @@ final class HttpConverterTest extends TestCase
311311 self::assertSame([], Request::getTrustedProxies());
312312 }
313313
314+ public function testHeadersWithUnderscoresCannotShadowThePlatformOnes(): void
315+ {
316+ $request = HttpConverter::toRequest(['headers' => [
317+ 'X-Forwarded-Proto' => 'https',
318+ 'X-Real-Ip' => '2001:db8::1',
319+ 'X_forwarded_proto' => 'http',
320+ 'X_real_ip' => '203.0.113.66',
321+ ]]);
322+
323+ self::assertSame('2001:db8::1', $request->getClientIp());
324+ self::assertTrue($request->isSecure());
325+ }
326+
314327 public function testTrustedProxiesConfiguredByTheAppStillApply(): void
315328 {
316329 Request::setTrustedProxies(['2001:db8::1'], Request::HEADER_X_FORWARDED_HOST);
@@ -311,6 +311,19 @@ final class HttpConverterTest extends TestCase
311 self::assertSame([], Request::getTrustedProxies());311 self::assertSame([], Request::getTrustedProxies());
312 }312 }
313 313
314+ public function testHeadersWithUnderscoresCannotShadowThePlatformOnes(): void
315+ {
316+ $request = HttpConverter::toRequest(['headers' => [
317+ 'X-Forwarded-Proto' => 'https',
318+ 'X-Real-Ip' => '2001:db8::1',
319+ 'X_forwarded_proto' => 'http',
320+ 'X_real_ip' => '203.0.113.66',
321+ ]]);
322+
323+ self::assertSame('2001:db8::1', $request->getClientIp());
324+ self::assertTrue($request->isSecure());
325+ }
326+
314 public function testTrustedProxiesConfiguredByTheAppStillApply(): void327 public function testTrustedProxiesConfiguredByTheAppStillApply(): void
315 {328 {
316 Request::setTrustedProxies(['2001:db8::1'], Request::HEADER_X_FORWARDED_HOST);329 Request::setTrustedProxies(['2001:db8::1'], Request::HEADER_X_FORWARDED_HOST);