From ce0221ee63924bc19fc47054d916c4b95a767e54 Mon Sep 17 00:00:00 2001
From: Yassine Doghri <yassine@doghri.fr>
Date: Wed, 16 Oct 2024 11:42:06 +0000
Subject: [PATCH] fix: allow passing json to app.proxyIPs config to set it

---
 app/Config/App.php | 38 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 36 insertions(+), 2 deletions(-)

diff --git a/app/Config/App.php b/app/Config/App.php
index 9dd39d536a..404ff5a00c 100644
--- a/app/Config/App.php
+++ b/app/Config/App.php
@@ -5,6 +5,7 @@ declare(strict_types=1);
 namespace Config;
 
 use CodeIgniter\Config\BaseConfig;
+use Override;
 
 class App extends BaseConfig
 {
@@ -192,9 +193,9 @@ class App extends BaseConfig
      *         '192.168.5.0/24' => 'X-Real-IP',
      *     ]
      *
-     * @var array<string, string>
+     * @var array<string, string>|string
      */
-    public array $proxyIPs = [];
+    public $proxyIPs = [];
 
     /**
      * --------------------------------------------------------------------------
@@ -249,4 +250,37 @@ class App extends BaseConfig
     public ?int $bandwidthLimit = null;
 
     public ?string $legalNoticeURL = null;
+
+    /**
+     * AuthToken Config Constructor
+     */
+    public function __construct()
+    {
+        parent::__construct();
+
+        if (is_string($this->proxyIPs)) {
+            $array = json_decode($this->proxyIPs, true);
+            if (is_array($array)) {
+                $this->proxyIPs = $array;
+            }
+        }
+    }
+
+    /**
+     * Override parent initEnvValue() to allow for direct setting to array properties values from ENV
+     *
+     * In order to set array properties via ENV vars we need to set the property to a string value first.
+     *
+     * @param mixed $property
+     */
+    #[Override]
+    protected function initEnvValue(&$property, string $name, string $prefix, string $shortPrefix): void
+    {
+        // if attempting to set property from ENV, first set to empty string
+        if ($name === 'proxyIPs' && $this->getEnvValue($name, $prefix, $shortPrefix) !== null) {
+            $property = '';
+        }
+
+        parent::initEnvValue($property, $name, $prefix, $shortPrefix);
+    }
 }
-- 
GitLab