Skip to content

Commit 4a1bb69

Browse files
authored
Merge pull request #234 from dotkernel/issue-231
refactor checking for a public or private ip
2 parents c83fa8b + 4358e9f commit 4a1bb69

3 files changed

Lines changed: 30 additions & 31 deletions

File tree

src/App/src/Service/IpService.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
use function filter_var;
88
use function getenv;
99

10+
use const FILTER_FLAG_IPV4;
1011
use const FILTER_FLAG_IPV6;
1112
use const FILTER_FLAG_NO_PRIV_RANGE;
13+
use const FILTER_FLAG_NO_RES_RANGE;
1214
use const FILTER_VALIDATE_IP;
1315

1416
class IpService
@@ -17,20 +19,20 @@ public static function getUserIp(array $server): mixed
1719
{
1820
if (! empty($server)) {
1921
// check if HTTP_X_FORWARDED_FOR is public network IP
20-
if (isset($server['HTTP_X_FORWARDED_FOR']) && self::validIp($server['HTTP_X_FORWARDED_FOR']) === 'public') {
22+
if (isset($server['HTTP_X_FORWARDED_FOR']) && self::isPublicIp($server['HTTP_X_FORWARDED_FOR'])) {
2123
$realIp = $server['HTTP_X_FORWARDED_FOR'];
2224
// check if HTTP_CLIENT_IP is public network IP
23-
} elseif (isset($server['HTTP_CLIENT_IP']) && self::validIp($server['HTTP_CLIENT_IP']) === 'public') {
25+
} elseif (isset($server['HTTP_CLIENT_IP']) && self::isPublicIp($server['HTTP_CLIENT_IP'])) {
2426
$realIp = $server['HTTP_CLIENT_IP'];
2527
} else {
2628
$realIp = $server['REMOTE_ADDR'];
2729
}
2830
} else {
2931
// check if HTTP_X_FORWARDED_FOR is public network IP
30-
if (getenv('HTTP_X_FORWARDED_FOR') && self::validIp(getenv('HTTP_X_FORWARDED_FOR')) === 'public') {
32+
if (getenv('HTTP_X_FORWARDED_FOR') && self::isPublicIp(getenv('HTTP_X_FORWARDED_FOR'))) {
3133
$realIp = getenv('HTTP_X_FORWARDED_FOR');
3234
// check if HTTP_CLIENT_IP is public network IP
33-
} elseif (getenv('HTTP_CLIENT_IP') && self::validIp(getenv('HTTP_CLIENT_IP')) === 'public') {
35+
} elseif (getenv('HTTP_CLIENT_IP') && self::isPublicIp(getenv('HTTP_CLIENT_IP'))) {
3436
$realIp = getenv('HTTP_CLIENT_IP');
3537
} else {
3638
$realIp = getenv('REMOTE_ADDR');
@@ -40,27 +42,15 @@ public static function getUserIp(array $server): mixed
4042
return $realIp;
4143
}
4244

43-
/**
44-
* @return false|string
45-
*/
46-
public static function validIp(string $ip): bool|string
45+
public static function isPublicIp(string $ipAddress): bool
4746
{
48-
// special cases that return private are the loop-back address and IPv6 addresses
49-
if ($ip === '127.0.0.1' || filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
50-
return 'private';
51-
}
52-
53-
// check if the ip is valid
54-
if (filter_var($ip, FILTER_VALIDATE_IP)) {
55-
// check whether it's private or not
56-
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
57-
return 'public';
58-
}
59-
60-
return 'private';
61-
}
62-
63-
// not a valid ip
64-
return false;
47+
return filter_var(
48+
$ipAddress,
49+
FILTER_VALIDATE_IP,
50+
FILTER_FLAG_IPV4 |
51+
FILTER_FLAG_IPV6 |
52+
FILTER_FLAG_NO_PRIV_RANGE |
53+
FILTER_FLAG_NO_RES_RANGE
54+
) === $ipAddress;
6555
}
6656
}

test/Unit/Admin/Controller/AdminControllerTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ class AdminControllerTest extends UnitTest
2323
*/
2424
public function testWillCreate(): void
2525
{
26+
$logger = new Logger([
27+
'writers' => [
28+
'FileWriter' => [
29+
'name' => 'null',
30+
],
31+
],
32+
]);
2633
$adminController = new AdminController(
2734
$this->createMock(AdminServiceInterface::class),
2835
$this->createMock(RouterInterface::class),
@@ -31,7 +38,7 @@ public function testWillCreate(): void
3138
$this->createMock(FlashMessengerInterface::class),
3239
$this->createMock(FormsPlugin::class),
3340
$this->createMock(AdminForm::class),
34-
$this->createMock(Logger::class),
41+
$logger
3542
);
3643
$this->assertInstanceOf(AdminController::class, $adminController);
3744
}

test/Unit/App/Service/IpServiceTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ public function testWillGetUserIpFromEnv(): void
4949
putenv('REMOTE_ADDR');
5050
}
5151

52-
public function testWillDetectIfValidIp(): void
52+
public function testWillDetectPublicIp(): void
5353
{
54-
$this->assertSame('private', IpService::validIp('127.0.0.1'));
54+
$this->assertFalse(IpService::isPublicIp("127.0.0.1"));
55+
$this->assertFalse(IpService::isPublicIp("10.0.0.0"));
56+
$this->assertFalse(IpService::isPublicIp("::1"));
57+
$this->assertFalse(IpService::isPublicIp("fd12:3456:789a:1::1"));
5558

56-
$this->assertSame('public', IpService::validIp($this->ipAddress));
57-
58-
$this->assertFalse(IpService::validIp('test'));
59+
$this->assertTrue(IpService::isPublicIp("8.8.8.8")); // google
60+
$this->assertTrue(IpService::isPublicIp("2607:f8b0:4003:c00::6a")); //google
5961
}
6062
}

0 commit comments

Comments
 (0)