From 9215b905c837faf0124ca114154f632a1d8e9119 Mon Sep 17 00:00:00 2001 From: n0nag0n Date: Sat, 21 Jun 2025 12:39:02 -0600 Subject: [PATCH] feat(request): Add servername property to Request class --- flight/net/Request.php | 12 ++++++++++++ tests/RequestTest.php | 2 ++ 2 files changed, 14 insertions(+) diff --git a/flight/net/Request.php b/flight/net/Request.php index be6f3f9..6ca6b72 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -33,6 +33,8 @@ use flight\util\Collection; * - **secure** - Connection is secure * - **accept** - HTTP accept parameters * - **proxy_ip** - Proxy IP address of the client + * - **host** - The hostname from the request. + * - **servername** - The server's hostname. See `$_SERVER['SERVER_NAME']`. */ class Request { @@ -126,6 +128,15 @@ class Request */ public string $host; + /** + * Server name + * + * CAUTION: Note: Under Apache 2, UseCanonicalName = On and ServerName must be set. + * Otherwise, this value reflects the hostname supplied by the client, which can be spoofed. + * It is not safe to rely on this value in security-dependent contexts. + */ + public string $servername; + /** * Stream path for where to pull the request body from */ @@ -164,6 +175,7 @@ class Request 'accept' => self::getVar('HTTP_ACCEPT'), 'proxy_ip' => self::getProxyIpAddress(), 'host' => self::getVar('HTTP_HOST'), + 'servername' => self::getVar('SERVER_NAME', ''), ]; } diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 172e212..1b7b484 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -23,6 +23,7 @@ class RequestTest extends TestCase $_SERVER['REMOTE_ADDR'] = '8.8.8.8'; $_SERVER['HTTP_X_FORWARDED_FOR'] = '32.32.32.32'; $_SERVER['HTTP_HOST'] = 'example.com'; + $_SERVER['SERVER_NAME'] = 'test.com'; $_SERVER['CONTENT_TYPE'] = ''; $_GET = []; @@ -52,6 +53,7 @@ class RequestTest extends TestCase $this->assertFalse($this->request->secure); $this->assertEquals('', $this->request->accept); $this->assertEquals('example.com', $this->request->host); + $this->assertEquals('test.com', $this->request->servername); } public function testIpAddress(): void