Skip to content

Commit

Permalink
Use CIDR mask in IP whitelist check
Browse files Browse the repository at this point in the history
  • Loading branch information
pokgak authored and LKaemmerling committed Feb 9, 2021
1 parent ff7e998 commit 5d985dc
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Http/Middleware/IPWhitelistingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Symfony\Component\HttpFoundation\IpUtils;

class IPWhitelistingMiddleware
{
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);
Expand Down
8 changes: 8 additions & 0 deletions tests/Http/Middleware/IPWhitelistingMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
];
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"]);
}
}

0 comments on commit 5d985dc

Please sign in to comment.