Skip to content

Commit 96b7965

Browse files
committed
Deprecate passing null as $requestIp to IpUtils::checkIp(), checkIp4() and checkIp6()
1 parent c5a62a9 commit 96b7965

File tree

4 files changed

+46
-3
lines changed

4 files changed

+46
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
5.4
55
---
66

7+
* Deprecate passing `null` as `$requestIp` to `IpUtils::__checkIp()`, `IpUtils::__checkIp4()` or `IpUtils::__checkIp6()`, pass an empty string instead.
78
* Add the `litespeed_finish_request` method to work with Litespeed
89
* Deprecate `upload_progress.*` and `url_rewriter.tags` session options
910

IpUtils.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ private function __construct()
3737
public static function checkIp(?string $requestIp, $ips)
3838
{
3939
if (null === $requestIp) {
40+
trigger_deprecation('symfony/http-foundation', '5.4', 'Passing null as $requestIp to "%s()" is deprecated, pass an empty string instead.', __METHOD__);
41+
4042
return false;
4143
}
4244

@@ -65,6 +67,12 @@ public static function checkIp(?string $requestIp, $ips)
6567
*/
6668
public static function checkIp4(?string $requestIp, string $ip)
6769
{
70+
if (null === $requestIp) {
71+
trigger_deprecation('symfony/http-foundation', '5.4', 'Passing null as $requestIp to "%s()" is deprecated, pass an empty string instead.', __METHOD__);
72+
73+
return false;
74+
}
75+
6876
$cacheKey = $requestIp.'-'.$ip;
6977
if (isset(self::$checkedIps[$cacheKey])) {
7078
return self::$checkedIps[$cacheKey];
@@ -112,6 +120,12 @@ public static function checkIp4(?string $requestIp, string $ip)
112120
*/
113121
public static function checkIp6(?string $requestIp, string $ip)
114122
{
123+
if (null === $requestIp) {
124+
trigger_deprecation('symfony/http-foundation', '5.4', 'Passing null as $requestIp to "%s()" is deprecated, pass an empty string instead.', __METHOD__);
125+
126+
return false;
127+
}
128+
115129
$cacheKey = $requestIp.'-'.$ip;
116130
if (isset(self::$checkedIps[$cacheKey])) {
117131
return self::$checkedIps[$cacheKey];

RequestMatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public function matches(Request $request)
185185
return false;
186186
}
187187

188-
if (IpUtils::checkIp($request->getClientIp(), $this->ips)) {
188+
if (IpUtils::checkIp($request->getClientIp() ?? '', $this->ips)) {
189189
return true;
190190
}
191191

Tests/IpUtilsTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
namespace Symfony\Component\HttpFoundation\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1516
use Symfony\Component\HttpFoundation\IpUtils;
1617

1718
class IpUtilsTest extends TestCase
1819
{
20+
use ExpectDeprecationTrait;
21+
1922
/**
2023
* @dataProvider getIpv4Data
2124
*/
@@ -40,7 +43,6 @@ public function getIpv4Data()
4043
[false, '1.2.3.4', '256.256.256/0'], // invalid CIDR notation
4144
[false, 'an_invalid_ip', '192.168.1.0/24'],
4245
[false, '', '1.2.3.4/1'],
43-
[false, null, '1.2.3.4/1'],
4446
];
4547
}
4648

@@ -72,10 +74,36 @@ public function getIpv6Data()
7274
[false, '}__test|O:21:"JDatabaseDriverMysqli":3:{s:2', '::1'],
7375
[false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'],
7476
[false, '', '::1'],
75-
[false, null, '::1'],
7677
];
7778
}
7879

80+
/**
81+
* @group legacy
82+
*/
83+
public function testIpTriggersDeprecationOnNull()
84+
{
85+
$this->expectDeprecation('Since symfony/http-foundation 5.4: Passing null as $requestIp to "Symfony\Component\HttpFoundation\IpUtils::checkIp()" is deprecated, pass an empty string instead.');
86+
$this->assertFalse(IpUtils::checkIp(null, '192.168.1.1'));
87+
}
88+
89+
/**
90+
* @group legacy
91+
*/
92+
public function testIp4TriggersDeprecationOnNull()
93+
{
94+
$this->expectDeprecation('Since symfony/http-foundation 5.4: Passing null as $requestIp to "Symfony\Component\HttpFoundation\IpUtils::checkIp4()" is deprecated, pass an empty string instead.');
95+
$this->assertFalse(IpUtils::checkIp4(null, '192.168.1.1'));
96+
}
97+
98+
/**
99+
* @group legacy
100+
*/
101+
public function testIp6TriggersDeprecationOnNull()
102+
{
103+
$this->expectDeprecation('Since symfony/http-foundation 5.4: Passing null as $requestIp to "Symfony\Component\HttpFoundation\IpUtils::checkIp6()" is deprecated, pass an empty string instead.');
104+
$this->assertFalse(IpUtils::checkIp6(null, '2a01:198:603:0::/65'));
105+
}
106+
79107
/**
80108
* @requires extension sockets
81109
*/

0 commit comments

Comments
 (0)