From 7708fa86bdb2e28c2f8d7f3dec061f5eb9e6ca9a Mon Sep 17 00:00:00 2001 From: FreeScout Date: Sun, 22 Oct 2023 23:34:01 -0700 Subject: [PATCH] Allow to set APP_CLOUDFLARE_IS_USED=true for proper client IP detection when CloudFlare is used - closes #3467 --- config/app.php | 7 +++++++ overrides/symfony/http-foundation/Request.php | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/config/app.php b/config/app.php index 04125e154..a5b06a8b4 100644 --- a/config/app.php +++ b/config/app.php @@ -462,6 +462,13 @@ 'csp_enabled' => env('APP_CSP_ENABLED', true), 'csp_script_src' => env('APP_CSP_SCRIPT_SRC', ''), + /* + |-------------------------------------------------------------------------- + | Let the application know that CloudFlare is used (for proper client IP detection). + |------------------------------------------------------------------------- + */ + 'cloudflare_is_used' => env('APP_CLOUDFLARE_IS_USED', false), + /* |-------------------------------------------------------------------------- | Autoloaded Service Providers diff --git a/overrides/symfony/http-foundation/Request.php b/overrides/symfony/http-foundation/Request.php index f675d1720..acecb6f20 100644 --- a/overrides/symfony/http-foundation/Request.php +++ b/overrides/symfony/http-foundation/Request.php @@ -890,6 +890,15 @@ public function setSession(SessionInterface $session) */ public function getClientIps() { + // Fix for CloudFlare. + if (isset($_SERVER["HTTP_CF_CONNECTING_IP"]) + && $_SERVER['REMOTE_ADDR'] != $_SERVER["HTTP_CF_CONNECTING_IP"] + && config('app.cloudflare_is_used') + ) { + $_SERVER['REMOTE_ADDR'] = $_SERVER["HTTP_CF_CONNECTING_IP"]; + $this->server->set('REMOTE_ADDR', $_SERVER["HTTP_CF_CONNECTING_IP"]); + } + $ip = $this->server->get('REMOTE_ADDR'); if (!$this->isFromTrustedProxy()) {