diff --git a/rector.php b/rector.php index 238f94307649..1532d33471a3 100644 --- a/rector.php +++ b/rector.php @@ -92,6 +92,8 @@ __DIR__ . '/system/Debug/Exceptions.php', // @TODO remove if deprecated $httpVerb is removed __DIR__ . '/system/Router/AutoRouterImproved.php', + // @TODO remove if deprecated $config is removed + __DIR__ . '/system/HTTP/Request.php', ], // check on constant compare diff --git a/system/HTTP/Request.php b/system/HTTP/Request.php index ebe602b18e38..26b7b8f460c8 100644 --- a/system/HTTP/Request.php +++ b/system/HTTP/Request.php @@ -26,7 +26,7 @@ class Request extends OutgoingRequest implements RequestInterface * * @var array * - * @deprecated Check the App config directly + * @deprecated 4.0.5 No longer used. Check the App config directly */ protected $proxyIPs; @@ -35,15 +35,10 @@ class Request extends OutgoingRequest implements RequestInterface * * @param App $config * - * @deprecated The $config is no longer needed and will be removed in a future version + * @deprecated 4.0.5 The $config is no longer needed and will be removed in a future version */ - public function __construct($config = null) + public function __construct($config = null) // @phpstan-ignore-line { - /** - * @deprecated $this->proxyIps property will be removed in the future - */ - $this->proxyIPs = $config->proxyIPs; - if (empty($this->method)) { $this->method = $this->getServer('REQUEST_METHOD') ?? 'GET'; } @@ -59,7 +54,7 @@ public function __construct($config = null) * @param string $ip IP Address * @param string $which IP protocol: 'ipv4' or 'ipv6' * - * @deprecated Use Validation instead + * @deprecated 4.0.5 Use Validation instead * * @codeCoverageIgnore */ @@ -73,7 +68,7 @@ public function isValidIP(?string $ip = null, ?string $which = null): bool * * @param bool $upper Whether to return in upper or lower case. * - * @deprecated The $upper functionality will be removed and this will revert to its PSR-7 equivalent + * @deprecated 4.0.5 The $upper functionality will be removed and this will revert to its PSR-7 equivalent * * @codeCoverageIgnore */ @@ -87,7 +82,7 @@ public function getMethod(bool $upper = false): string * * @return $this * - * @deprecated Use withMethod() instead for immutability + * @deprecated 4.0.5 Use withMethod() instead for immutability * * @codeCoverageIgnore */ diff --git a/system/HTTP/RequestTrait.php b/system/HTTP/RequestTrait.php index 57f97982dea4..32b0401bbbe9 100644 --- a/system/HTTP/RequestTrait.php +++ b/system/HTTP/RequestTrait.php @@ -60,17 +60,8 @@ public function getIPAddress(): string 'valid_ip', ]; - /** - * @deprecated $this->proxyIPs property will be removed in the future - */ - // @phpstan-ignore-next-line - $proxyIPs = $this->proxyIPs ?? config(App::class)->proxyIPs; - // @phpstan-ignore-next-line - - // Workaround for old Config\App file. App::$proxyIPs may be empty string. - if ($proxyIPs === '') { - $proxyIPs = []; - } + $proxyIPs = config(App::class)->proxyIPs; + if (! empty($proxyIPs) && (! is_array($proxyIPs) || is_int(array_key_first($proxyIPs)))) { throw new ConfigException( 'You must set an array with Proxy IP address key and HTTP header name value in Config\App::$proxyIPs.' diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index 2b61850ec579..2c1f7017a610 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -11,6 +11,7 @@ namespace CodeIgniter\HTTP; +use CodeIgniter\Config\Factories; use CodeIgniter\Exceptions\ConfigException; use CodeIgniter\HTTP\Exceptions\HTTPException; use CodeIgniter\HTTP\Files\UploadedFile; @@ -973,7 +974,8 @@ public function testGetIPAddressThruProxy() '10.0.1.200' => 'X-Forwarded-For', '192.168.5.0/24' => 'X-Forwarded-For', ]; - $this->request = new Request($config); + Factories::injectMock('config', App::class, $config); + $this->request = new Request(); $this->request->populateHeaders(); // we should see the original forwarded address @@ -990,7 +992,8 @@ public function testGetIPAddressThruProxyIPv6() $config->proxyIPs = [ '2001:db8::2:1' => 'X-Forwarded-For', ]; - $this->request = new Request($config); + Factories::injectMock('config', App::class, $config); + $this->request = new Request(); $this->request->populateHeaders(); // we should see the original forwarded address @@ -1075,7 +1078,8 @@ public function testGetIPAddressThruProxySubnet() $config = new App(); $config->proxyIPs = ['192.168.5.0/24' => 'X-Forwarded-For']; - $this->request = new Request($config); + Factories::injectMock('config', App::class, $config); + $this->request = new Request(); $this->request->populateHeaders(); // we should see the original forwarded address @@ -1090,7 +1094,8 @@ public function testGetIPAddressThruProxySubnetIPv6() $config = new App(); $config->proxyIPs = ['2001:db8:1234::/48' => 'X-Forwarded-For']; - $this->request = new Request($config); + Factories::injectMock('config', App::class, $config); + $this->request = new Request(); $this->request->populateHeaders(); // we should see the original forwarded address @@ -1166,7 +1171,8 @@ public function testGetIPAddressThruProxyInvalidConfigArray() $config = new App(); $config->proxyIPs = ['192.168.5.0/28']; - $this->request = new Request($config); + Factories::injectMock('config', App::class, $config); + $this->request = new Request(); $this->request->populateHeaders(); $this->request->getIPAddress(); diff --git a/tests/system/HTTP/RequestTest.php b/tests/system/HTTP/RequestTest.php index 17cf45078fe0..4b54577d18ec 100644 --- a/tests/system/HTTP/RequestTest.php +++ b/tests/system/HTTP/RequestTest.php @@ -11,6 +11,7 @@ namespace CodeIgniter\HTTP; +use CodeIgniter\Config\Factories; use CodeIgniter\Test\CIUnitTestCase; use Config\App; @@ -616,7 +617,8 @@ public function testGetIPAddressThruProxy() '10.0.1.200' => 'X-Forwarded-For', '192.168.5.0/24' => 'X-Forwarded-For', ]; - $this->request = new Request($config); + Factories::injectMock('config', App::class, $config); + $this->request = new Request(); $this->request->populateHeaders(); // we should see the original forwarded address @@ -667,7 +669,8 @@ public function testGetIPAddressThruProxySubnet() $config = new App(); $config->proxyIPs = ['192.168.5.0/24' => 'X-Forwarded-For']; - $this->request = new Request($config); + Factories::injectMock('config', App::class, $config); + $this->request = new Request(); $this->request->populateHeaders(); // we should see the original forwarded address diff --git a/user_guide_src/source/installation/upgrade_440.rst b/user_guide_src/source/installation/upgrade_440.rst index 9c765f24434b..586097a9cc30 100644 --- a/user_guide_src/source/installation/upgrade_440.rst +++ b/user_guide_src/source/installation/upgrade_440.rst @@ -72,6 +72,12 @@ Mandatory File Changes Config Files ============ +app/Config/App.php +------------------ + +- The property ``$proxyIPs`` must be an array. If you don't use proxy servers, + it must be ``public array $proxyIPs = [];``. + .. _upgrade-440-config-routing: app/Config/Routing.php