From dc0d2014d688828a8216d602fb9893008175d7f2 Mon Sep 17 00:00:00 2001 From: Adam Daley Date: Sat, 30 May 2026 10:33:33 +0100 Subject: [PATCH] Add pre-config proxy detection for forwarded headers (#3663) --- src/install/install.php | 10 +++-- .../FOSSBilling/Http/RequestFactory.php | 37 ++++++++++++++++ src/load.php | 2 +- .../FOSSBilling/RequestFactoryTest.php | 44 +++++++++++++++++++ 4 files changed, 89 insertions(+), 4 deletions(-) diff --git a/src/install/install.php b/src/install/install.php index 471c8d5b9..b77351eff 100644 --- a/src/install/install.php +++ b/src/install/install.php @@ -79,7 +79,8 @@ require Path::join(PATH_LIBRARY, 'FOSSBilling', 'Autoloader.php'); $loader = new FOSSBilling\AutoLoader(); $loader->register(); -$request = RequestFactory::createFromGlobals(); +$preConfigProxyConfig = RequestFactory::getPreConfigProxyConfig($_SERVER); +$request = RequestFactory::createFromGlobals($preConfigProxyConfig); $url = $request->getSchemeAndHttpHost() . $request->getRequestUri(); $current_url = Path::getDirectory($url); $root_url = str_replace('/install', '', $current_url) . '/'; @@ -90,7 +91,7 @@ const URL_ADMIN = SYSTEM_URL . 'admin'; // Load action and initialize the installer $action = $request->query->get('a', 'index'); $action = is_string($action) && $action !== '' ? $action : 'index'; -$installer = new FOSSBilling_Installer($request); +$installer = new FOSSBilling_Installer($request, $preConfigProxyConfig); // Run the installer only in non-CLI mode if (!Environment::isCLI()) { @@ -105,7 +106,7 @@ final class FOSSBilling_Installer private bool $isDebug = false; private readonly Filesystem $filesystem; - public function __construct(private readonly Request $request) + public function __construct(private readonly Request $request, private readonly array $preConfigProxyConfig = []) { require_once 'session.php'; $this->session = new Session(); @@ -471,6 +472,9 @@ final class FOSSBilling_Installer // Handle dynamic configs $data['security']['force_https'] = str_starts_with(SYSTEM_URL, 'https://'); + if (($this->preConfigProxyConfig['enabled'] ?? false) === true) { + $data['security']['trusted_proxies'] = $this->preConfigProxyConfig; + } $data['debug_and_monitoring']['report_errors'] = (bool) $this->session->get('error_reporting'); $data['debug_and_monitoring']['debug'] = $this->isDebug; $data['update_branch'] = $updateBranch; diff --git a/src/library/FOSSBilling/Http/RequestFactory.php b/src/library/FOSSBilling/Http/RequestFactory.php index eeed53e0a..abd8d6407 100644 --- a/src/library/FOSSBilling/Http/RequestFactory.php +++ b/src/library/FOSSBilling/Http/RequestFactory.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace FOSSBilling\Http; use FOSSBilling\Config; +use Symfony\Component\HttpFoundation\IpUtils; use Symfony\Component\HttpFoundation\Request; final class RequestFactory @@ -38,6 +39,20 @@ final class RequestFactory return self::configure($request, self::getProxyConfigFromAppConfig()); } + public static function getPreConfigProxyConfig(array $server): array + { + $remoteAddress = trim((string) ($server['REMOTE_ADDR'] ?? '')); + if ($remoteAddress === '' || !self::hasForwardedHeaders($server) || !self::isLocalNetworkAddress($remoteAddress)) { + return []; + } + + return [ + 'enabled' => true, + 'proxies' => [$remoteAddress], + 'headers' => isset($server['HTTP_FORWARDED']) ? 'forwarded' : 'x_forwarded', + ]; + } + public static function normalizeRoutePath(Request $request): string { $rawPath = $request->query->get('_url'); @@ -117,4 +132,26 @@ final class RequestFactory default => throw new \InvalidArgumentException('Invalid trusted proxy header configuration.'), }; } + + private static function hasForwardedHeaders(array $server): bool + { + return isset($server['HTTP_FORWARDED']) + || isset($server['HTTP_X_FORWARDED_FOR']) + || isset($server['HTTP_X_FORWARDED_HOST']) + || isset($server['HTTP_X_FORWARDED_PROTO']) + || isset($server['HTTP_X_FORWARDED_PORT']) + || isset($server['HTTP_X_FORWARDED_PREFIX']); + } + + private static function isLocalNetworkAddress(string $address): bool + { + return IpUtils::checkIp($address, [ + '127.0.0.0/8', + '10.0.0.0/8', + '172.16.0.0/12', + '192.168.0.0/16', + '::1/128', + 'fc00::/7', + ]); + } } diff --git a/src/load.php b/src/load.php index e3775c0d0..e512b3f3d 100644 --- a/src/load.php +++ b/src/load.php @@ -224,7 +224,7 @@ function init(): void // Initialize required Symfony components. global $filesystem, $request; $filesystem = new Filesystem(); - $request = RequestFactory::createFromGlobals(); + $request = RequestFactory::createFromGlobals(RequestFactory::getPreConfigProxyConfig($_SERVER)); RequestFactory::normalizeRoutePath($request); // Check config exists and is valid, redirecting to installer or throwing an exception if not. diff --git a/tests-legacy/library/FOSSBilling/RequestFactoryTest.php b/tests-legacy/library/FOSSBilling/RequestFactoryTest.php index c29f16a27..71613ce22 100644 --- a/tests-legacy/library/FOSSBilling/RequestFactoryTest.php +++ b/tests-legacy/library/FOSSBilling/RequestFactoryTest.php @@ -27,6 +27,50 @@ final class RequestFactoryTest extends PHPUnit\Framework\TestCase $this->assertFalse($request->isSecure()); } + public function testPreConfigProxyConfigTrustsForwardedHeadersFromLocalNetworkProxy(): void + { + $proxyConfig = RequestFactory::getPreConfigProxyConfig([ + 'REMOTE_ADDR' => '172.18.0.5', + 'HTTP_X_FORWARDED_PROTO' => 'https', + 'HTTP_X_FORWARDED_HOST' => 'billing.example.com', + ]); + $request = Request::create('http://internal.example/install/install.php', 'GET', [], [], [], [ + 'REMOTE_ADDR' => '172.18.0.5', + 'HTTP_X_FORWARDED_PROTO' => 'https', + 'HTTP_X_FORWARDED_HOST' => 'billing.example.com', + ]); + + RequestFactory::configure($request, $proxyConfig); + + $this->assertSame([ + 'enabled' => true, + 'proxies' => ['172.18.0.5'], + 'headers' => 'x_forwarded', + ], $proxyConfig); + $this->assertTrue($request->isSecure()); + $this->assertSame('billing.example.com', $request->getHost()); + } + + public function testPreConfigProxyConfigDoesNotTrustForwardedHeadersFromPublicAddress(): void + { + $proxyConfig = RequestFactory::getPreConfigProxyConfig([ + 'REMOTE_ADDR' => '198.51.100.10', + 'HTTP_X_FORWARDED_PROTO' => 'https', + 'HTTP_X_FORWARDED_HOST' => 'billing.example.com', + ]); + $request = Request::create('http://internal.example/install/install.php', 'GET', [], [], [], [ + 'REMOTE_ADDR' => '198.51.100.10', + 'HTTP_X_FORWARDED_PROTO' => 'https', + 'HTTP_X_FORWARDED_HOST' => 'billing.example.com', + ]); + + RequestFactory::configure($request, $proxyConfig); + + $this->assertSame([], $proxyConfig); + $this->assertFalse($request->isSecure()); + $this->assertSame('internal.example', $request->getHost()); + } + public function testConfigureTrustsForwardedProtoFromConfiguredTrustedProxy(): void { $request = Request::create('http://billing.example.com/admin', 'GET', [], [], [], [