mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-12-03 05:41:52 +09:00
Add pre-config proxy detection for forwarded headers (#3663)
This commit is contained in:
1 parent
c622b4c45b
commit
dc0d2014d6
4 files changed
+89
-4
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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',
|
||||
]);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -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.
|
||||
|
||||
@@ -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', [], [], [], [
|
||||
|
||||
Reference in new issue
Block a user