Skip to content

Commit 40409b3

Browse files
authored
Merge pull request #7621 from kenjis/remove-Request-proxyIPs
refactor: drop support for `Config\App::$proxyIPs = ''`
2 parents d19d034 + e9d9026 commit 40409b3

File tree

6 files changed

+32
-29
lines changed

6 files changed

+32
-29
lines changed

rector.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@
9292
__DIR__ . '/system/Debug/Exceptions.php',
9393
// @TODO remove if deprecated $httpVerb is removed
9494
__DIR__ . '/system/Router/AutoRouterImproved.php',
95+
// @TODO remove if deprecated $config is removed
96+
__DIR__ . '/system/HTTP/Request.php',
9597
],
9698

9799
// check on constant compare

system/HTTP/Request.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Request extends OutgoingRequest implements RequestInterface
2626
*
2727
* @var array<string, string>
2828
*
29-
* @deprecated Check the App config directly
29+
* @deprecated 4.0.5 No longer used. Check the App config directly
3030
*/
3131
protected $proxyIPs;
3232

@@ -35,15 +35,10 @@ class Request extends OutgoingRequest implements RequestInterface
3535
*
3636
* @param App $config
3737
*
38-
* @deprecated The $config is no longer needed and will be removed in a future version
38+
* @deprecated 4.0.5 The $config is no longer needed and will be removed in a future version
3939
*/
40-
public function __construct($config = null)
40+
public function __construct($config = null) // @phpstan-ignore-line
4141
{
42-
/**
43-
* @deprecated $this->proxyIps property will be removed in the future
44-
*/
45-
$this->proxyIPs = $config->proxyIPs;
46-
4742
if (empty($this->method)) {
4843
$this->method = $this->getServer('REQUEST_METHOD') ?? 'GET';
4944
}
@@ -59,7 +54,7 @@ public function __construct($config = null)
5954
* @param string $ip IP Address
6055
* @param string $which IP protocol: 'ipv4' or 'ipv6'
6156
*
62-
* @deprecated Use Validation instead
57+
* @deprecated 4.0.5 Use Validation instead
6358
*
6459
* @codeCoverageIgnore
6560
*/
@@ -73,7 +68,7 @@ public function isValidIP(?string $ip = null, ?string $which = null): bool
7368
*
7469
* @param bool $upper Whether to return in upper or lower case.
7570
*
76-
* @deprecated The $upper functionality will be removed and this will revert to its PSR-7 equivalent
71+
* @deprecated 4.0.5 The $upper functionality will be removed and this will revert to its PSR-7 equivalent
7772
*
7873
* @codeCoverageIgnore
7974
*/
@@ -87,7 +82,7 @@ public function getMethod(bool $upper = false): string
8782
*
8883
* @return $this
8984
*
90-
* @deprecated Use withMethod() instead for immutability
85+
* @deprecated 4.0.5 Use withMethod() instead for immutability
9186
*
9287
* @codeCoverageIgnore
9388
*/

system/HTTP/RequestTrait.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,8 @@ public function getIPAddress(): string
6060
'valid_ip',
6161
];
6262

63-
/**
64-
* @deprecated $this->proxyIPs property will be removed in the future
65-
*/
66-
// @phpstan-ignore-next-line
67-
$proxyIPs = $this->proxyIPs ?? config(App::class)->proxyIPs;
68-
// @phpstan-ignore-next-line
69-
70-
// Workaround for old Config\App file. App::$proxyIPs may be empty string.
71-
if ($proxyIPs === '') {
72-
$proxyIPs = [];
73-
}
63+
$proxyIPs = config(App::class)->proxyIPs;
64+
7465
if (! empty($proxyIPs) && (! is_array($proxyIPs) || is_int(array_key_first($proxyIPs)))) {
7566
throw new ConfigException(
7667
'You must set an array with Proxy IP address key and HTTP header name value in Config\App::$proxyIPs.'

tests/system/HTTP/IncomingRequestTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\HTTP;
1313

14+
use CodeIgniter\Config\Factories;
1415
use CodeIgniter\Exceptions\ConfigException;
1516
use CodeIgniter\HTTP\Exceptions\HTTPException;
1617
use CodeIgniter\HTTP\Files\UploadedFile;
@@ -973,7 +974,8 @@ public function testGetIPAddressThruProxy()
973974
'10.0.1.200' => 'X-Forwarded-For',
974975
'192.168.5.0/24' => 'X-Forwarded-For',
975976
];
976-
$this->request = new Request($config);
977+
Factories::injectMock('config', App::class, $config);
978+
$this->request = new Request();
977979
$this->request->populateHeaders();
978980

979981
// we should see the original forwarded address
@@ -990,7 +992,8 @@ public function testGetIPAddressThruProxyIPv6()
990992
$config->proxyIPs = [
991993
'2001:db8::2:1' => 'X-Forwarded-For',
992994
];
993-
$this->request = new Request($config);
995+
Factories::injectMock('config', App::class, $config);
996+
$this->request = new Request();
994997
$this->request->populateHeaders();
995998

996999
// we should see the original forwarded address
@@ -1075,7 +1078,8 @@ public function testGetIPAddressThruProxySubnet()
10751078

10761079
$config = new App();
10771080
$config->proxyIPs = ['192.168.5.0/24' => 'X-Forwarded-For'];
1078-
$this->request = new Request($config);
1081+
Factories::injectMock('config', App::class, $config);
1082+
$this->request = new Request();
10791083
$this->request->populateHeaders();
10801084

10811085
// we should see the original forwarded address
@@ -1090,7 +1094,8 @@ public function testGetIPAddressThruProxySubnetIPv6()
10901094

10911095
$config = new App();
10921096
$config->proxyIPs = ['2001:db8:1234::/48' => 'X-Forwarded-For'];
1093-
$this->request = new Request($config);
1097+
Factories::injectMock('config', App::class, $config);
1098+
$this->request = new Request();
10941099
$this->request->populateHeaders();
10951100

10961101
// we should see the original forwarded address
@@ -1166,7 +1171,8 @@ public function testGetIPAddressThruProxyInvalidConfigArray()
11661171

11671172
$config = new App();
11681173
$config->proxyIPs = ['192.168.5.0/28'];
1169-
$this->request = new Request($config);
1174+
Factories::injectMock('config', App::class, $config);
1175+
$this->request = new Request();
11701176
$this->request->populateHeaders();
11711177

11721178
$this->request->getIPAddress();

tests/system/HTTP/RequestTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\HTTP;
1313

14+
use CodeIgniter\Config\Factories;
1415
use CodeIgniter\Test\CIUnitTestCase;
1516
use Config\App;
1617

@@ -616,7 +617,8 @@ public function testGetIPAddressThruProxy()
616617
'10.0.1.200' => 'X-Forwarded-For',
617618
'192.168.5.0/24' => 'X-Forwarded-For',
618619
];
619-
$this->request = new Request($config);
620+
Factories::injectMock('config', App::class, $config);
621+
$this->request = new Request();
620622
$this->request->populateHeaders();
621623

622624
// we should see the original forwarded address
@@ -667,7 +669,8 @@ public function testGetIPAddressThruProxySubnet()
667669

668670
$config = new App();
669671
$config->proxyIPs = ['192.168.5.0/24' => 'X-Forwarded-For'];
670-
$this->request = new Request($config);
672+
Factories::injectMock('config', App::class, $config);
673+
$this->request = new Request();
671674
$this->request->populateHeaders();
672675

673676
// we should see the original forwarded address

user_guide_src/source/installation/upgrade_440.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ Mandatory File Changes
7272
Config Files
7373
============
7474

75+
app/Config/App.php
76+
------------------
77+
78+
- The property ``$proxyIPs`` must be an array. If you don't use proxy servers,
79+
it must be ``public array $proxyIPs = [];``.
80+
7581
.. _upgrade-440-config-routing:
7682

7783
app/Config/Routing.php

0 commit comments

Comments
 (0)