Prepare LaySite 1.4.6 proxy IP handling for direct CDN ingress

This commit is contained in:
2026-11-27 23:28:31 +08:00
parent 301ec8f0ea
commit 2117ba1944
4 changed files with 29 additions and 16 deletions
+17 -12
View File
@@ -8,24 +8,29 @@ use App\Config\Environment;
final class Request extends \support\Request
{
private const LOOPBACK = ['127.0.0.1', '::1'];
public function clientAddress(): string
{
$peer = $this->getRemoteIp();
$forwarded = $this->header('x-real-ip', '');
if (!$this->fromTrustedProxy() || !filter_var($forwarded, FILTER_VALIDATE_IP)) {
$peer = trim($this->getRemoteIp(), '[]');
$configured = Environment::get('LAYSITE_TRUSTED_PROXIES');
$trusted = match (true) {
$configured === false => self::isIntranetIp($peer),
trim($configured) === '*' => true,
default => in_array($peer, array_map('trim', explode(',', $configured)), true),
};
if (!$trusted) {
return $peer;
}
return inet_ntop(inet_pton($forwarded));
}
// Prefer a single address set by the ingress over a forwarded chain.
foreach (['x-real-ip', 'cf-connecting-ip'] as $name) {
$address = $this->header($name);
if (filter_var($address, FILTER_VALIDATE_IP)) {
return inet_ntop(inet_pton($address));
}
}
private function fromTrustedProxy(): bool
{
$configured = Environment::get('LAYSITE_TRUSTED_PROXIES');
$trusted = $configured === false ? self::LOOPBACK : array_map('trim', explode(',', $configured));
$address = $this->getRealIp(safeMode: false);
return in_array($this->getRemoteIp(), $trusted, true);
return filter_var($address, FILTER_VALIDATE_IP) ? inet_ntop(inet_pton($address)) : $peer;
}
}