diff --git a/src/Http/Middleware/IPWhitelistingMiddleware.php b/src/Http/Middleware/IPWhitelistingMiddleware.php index 6c46e91..e247773 100644 --- a/src/Http/Middleware/IPWhitelistingMiddleware.php +++ b/src/Http/Middleware/IPWhitelistingMiddleware.php @@ -5,6 +5,7 @@ use Illuminate\Http\Request; use Illuminate\Http\Response; +use Symfony\Component\HttpFoundation\IpUtils; class IPWhitelistingMiddleware { @@ -12,7 +13,7 @@ public function handle(Request $request, \Closure $next): Response { if (!empty(config('horizon-exporter.ip_whitelist'))) { $clientIp = $request->ip(); - if (in_array($clientIp, config('horizon-exporter.ip_whitelist'))) { + if (IpUtils::checkIp($clientIp, config('horizon-exporter.ip_whitelist'))) { return $next($request); } else { abort(403); diff --git a/tests/Http/Middleware/IPWhitelistingMiddlewareTest.php b/tests/Http/Middleware/IPWhitelistingMiddlewareTest.php index 6318d87..89de635 100644 --- a/tests/Http/Middleware/IPWhitelistingMiddlewareTest.php +++ b/tests/Http/Middleware/IPWhitelistingMiddlewareTest.php @@ -38,6 +38,14 @@ public function testCases() [ "127.0.0.2", Response::HTTP_FORBIDDEN + ], + [ + "10.0.0.1", + Response::HTTP_OK + ], + [ + "10.0.1.1", + Response::HTTP_FORBIDDEN ] ]; } diff --git a/tests/TestCase.php b/tests/TestCase.php index 07afbf2..3c7847a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -8,6 +8,6 @@ class TestCase extends \Orchestra\Testbench\TestCase protected function getEnvironmentSetUp($app) { $app['config']->set('horizon-exporter.exporters', [NoopExporter::class]); - $app['config']->set('horizon-exporter.ip_whitelist', ["127.0.0.1"]); + $app['config']->set('horizon-exporter.ip_whitelist', ["127.0.0.1", "10.0.0.0/24"]); } }