From 57047d23ea2a41f548bd56286e3a7cfb63aadb50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 30 Mar 2021 20:38:18 +0200 Subject: [PATCH 1/4] [FEATURE] Ip access block --- README.md | 26 +++++++ src/Pecee/Http/Middleware/IpBlockAccess.php | 43 +++++++++++ .../SimpleRouter/CustomMiddlewareTest.php | 71 +++++++++++++++++++ .../Dummy/Middleware/IpBlockMiddleware.php | 14 ++++ 4 files changed, 154 insertions(+) create mode 100644 src/Pecee/Http/Middleware/IpBlockAccess.php create mode 100644 tests/Pecee/SimpleRouter/CustomMiddlewareTest.php create mode 100644 tests/Pecee/SimpleRouter/Dummy/Middleware/IpBlockMiddleware.php diff --git a/README.md b/README.md index 4ef8f16..6eda57a 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ You can donate any amount of your choice by [clicking here](https://www.paypal.c - [Custom EventHandlers](#custom-eventhandlers) - [Advanced](#advanced) - [Disable multiple route rendering](#disable-multiple-route-rendering) + - [Restrict access to IP](#restrict-access-to-ip) - [Url rewriting](#url-rewriting) - [Changing current route](#changing-current-route) - [Bootmanager: loading routes dynamically](#bootmanager-loading-routes-dynamically) @@ -1370,6 +1371,31 @@ By default the router will try to execute all routes that matches a given url. T This behavior can be easily disabled by setting `SimpleRouter::enableMultiRouteRendering(false)` in your `routes.php` file. This is the same behavior as version 3 and below. +## Restrict access to IP + +You can white- and blacklist access to IP's using the build in `IpBlockAccess` middleware. + +Create your own Middleware and extend the `IpBlockAccess` class. + +You can use `*` to restrict access to a range of ips. + +```php +use \Pecee\Http\Middleware\IpBlockAccess; + +class IpBlockMiddleware extends IpBlockAccess { + + protected $ipBlacklist = [ + '5.5.5.5', + '8.8.*', + ]; + + protected $ipWhitelist = [ + '8.8.2.2', + ]; + +} +``` + ## Url rewriting ### Changing current route diff --git a/src/Pecee/Http/Middleware/IpBlockAccess.php b/src/Pecee/Http/Middleware/IpBlockAccess.php new file mode 100644 index 0000000..f19d821 --- /dev/null +++ b/src/Pecee/Http/Middleware/IpBlockAccess.php @@ -0,0 +1,43 @@ +ipWhitelist, true) === true) { + return true; + } + + foreach ($this->ipBlacklist as $blackIp) { + + // Blocks range (8.8.*) + if ($blackIp[strlen($blackIp) - 1] === '*' && strpos($ip, trim($blackIp, '*')) === 0) { + return false; + } + + // Blocks exact match + if ($blackIp === $ip) { + return false; + } + + } + + return true; + } + + public function handle(Request $request): void + { + if($this->validate((string)$request->getIp()) === false) { + throw new HttpException(sprintf('Restricted ip. Access to %s has been blocked', $request->getIp()), 403); + } + } +} \ No newline at end of file diff --git a/tests/Pecee/SimpleRouter/CustomMiddlewareTest.php b/tests/Pecee/SimpleRouter/CustomMiddlewareTest.php new file mode 100644 index 0000000..bf19094 --- /dev/null +++ b/tests/Pecee/SimpleRouter/CustomMiddlewareTest.php @@ -0,0 +1,71 @@ +expectException(\Pecee\SimpleRouter\Exceptions\HttpException::class); + + global $_SERVER; + + // Test exact ip + + $_SERVER['remote-addr'] = '5.5.5.5'; + + TestRouter::group(['middleware' => IpBlockMiddleware::class], function() { + TestRouter::get('/fail', 'DummyController@method1'); + }); + + TestRouter::debug('/fail'); + + // Test ip-range + + $_SERVER['remote-addr'] = '8.8.4.4'; + + TestRouter::router()->reset(); + + TestRouter::group(['middleware' => IpBlockMiddleware::class], function() { + TestRouter::get('/fail', 'DummyController@method1'); + }); + + TestRouter::debug('/fail'); + + } + + public function testIpSuccess() { + + global $_SERVER; + + // Test ip that is not blocked + + $_SERVER['remote-addr'] = '6.6.6.6'; + + TestRouter::router()->reset(); + + TestRouter::group(['middleware' => IpBlockMiddleware::class], function() { + TestRouter::get('/success', 'DummyController@method1'); + }); + + TestRouter::debug('/success'); + + // Test ip in whitelist + + $_SERVER['remote-addr'] = '8.8.2.2'; + + TestRouter::router()->reset(); + + TestRouter::group(['middleware' => IpBlockMiddleware::class], function() { + TestRouter::get('/success', 'DummyController@method1'); + }); + + TestRouter::debug('/success'); + + $this->assertTrue(true); + + } + +} \ No newline at end of file diff --git a/tests/Pecee/SimpleRouter/Dummy/Middleware/IpBlockMiddleware.php b/tests/Pecee/SimpleRouter/Dummy/Middleware/IpBlockMiddleware.php new file mode 100644 index 0000000..ee525cf --- /dev/null +++ b/tests/Pecee/SimpleRouter/Dummy/Middleware/IpBlockMiddleware.php @@ -0,0 +1,14 @@ + Date: Tue, 30 Mar 2021 20:44:40 +0200 Subject: [PATCH 2/4] Changed name of IpBlockAccess to IpRestrictAccess & updated documentation. --- README.md | 13 +++++++++---- .../{IpBlockAccess.php => IpRestrictAccess.php} | 2 +- tests/Pecee/SimpleRouter/CustomMiddlewareTest.php | 10 +++++----- ...BlockMiddleware.php => IpRestrictMiddleware.php} | 2 +- 4 files changed, 16 insertions(+), 11 deletions(-) rename src/Pecee/Http/Middleware/{IpBlockAccess.php => IpRestrictAccess.php} (94%) rename tests/Pecee/SimpleRouter/Dummy/Middleware/{IpBlockMiddleware.php => IpRestrictMiddleware.php} (64%) diff --git a/README.md b/README.md index 6eda57a..df796aa 100644 --- a/README.md +++ b/README.md @@ -1373,16 +1373,19 @@ This behavior can be easily disabled by setting `SimpleRouter::enableMultiRouteR ## Restrict access to IP -You can white- and blacklist access to IP's using the build in `IpBlockAccess` middleware. +You can white- and blacklist access to IP's using the build in `IpRestrictAccess` middleware. -Create your own Middleware and extend the `IpBlockAccess` class. +Create your own custom Middleware and extend the `IpRestrictAccess` class. + +The `IpRestrictAccess` class contains two properties `ipBlacklist` and `ipWhitelist` that can be added +to your middleware to change which IP's has blocked or allowed access. You can use `*` to restrict access to a range of ips. ```php -use \Pecee\Http\Middleware\IpBlockAccess; +use \Pecee\Http\Middleware\IpRestrictAccess; -class IpBlockMiddleware extends IpBlockAccess { +class IpBlockerMiddleware extends IpRestrictAccess { protected $ipBlacklist = [ '5.5.5.5', @@ -1396,6 +1399,8 @@ class IpBlockMiddleware extends IpBlockAccess { } ``` +You can the middleware to multiple routes by adding your [middleware to a groups](#middleware). + ## Url rewriting ### Changing current route diff --git a/src/Pecee/Http/Middleware/IpBlockAccess.php b/src/Pecee/Http/Middleware/IpRestrictAccess.php similarity index 94% rename from src/Pecee/Http/Middleware/IpBlockAccess.php rename to src/Pecee/Http/Middleware/IpRestrictAccess.php index f19d821..483ce22 100644 --- a/src/Pecee/Http/Middleware/IpBlockAccess.php +++ b/src/Pecee/Http/Middleware/IpRestrictAccess.php @@ -5,7 +5,7 @@ namespace Pecee\Http\Middleware; use Pecee\Http\Request; use Pecee\SimpleRouter\Exceptions\HttpException; -abstract class IpBlockAccess implements IMiddleware +abstract class IpRestrictAccess implements IMiddleware { protected $ipBlacklist = []; protected $ipWhitelist = []; diff --git a/tests/Pecee/SimpleRouter/CustomMiddlewareTest.php b/tests/Pecee/SimpleRouter/CustomMiddlewareTest.php index bf19094..7345986 100644 --- a/tests/Pecee/SimpleRouter/CustomMiddlewareTest.php +++ b/tests/Pecee/SimpleRouter/CustomMiddlewareTest.php @@ -1,7 +1,7 @@ IpBlockMiddleware::class], function() { + TestRouter::group(['middleware' => IpRestrictMiddleware::class], function() { TestRouter::get('/fail', 'DummyController@method1'); }); @@ -28,7 +28,7 @@ class CustomMiddlewareTest extends \PHPUnit\Framework\TestCase TestRouter::router()->reset(); - TestRouter::group(['middleware' => IpBlockMiddleware::class], function() { + TestRouter::group(['middleware' => IpRestrictMiddleware::class], function() { TestRouter::get('/fail', 'DummyController@method1'); }); @@ -46,7 +46,7 @@ class CustomMiddlewareTest extends \PHPUnit\Framework\TestCase TestRouter::router()->reset(); - TestRouter::group(['middleware' => IpBlockMiddleware::class], function() { + TestRouter::group(['middleware' => IpRestrictMiddleware::class], function() { TestRouter::get('/success', 'DummyController@method1'); }); @@ -58,7 +58,7 @@ class CustomMiddlewareTest extends \PHPUnit\Framework\TestCase TestRouter::router()->reset(); - TestRouter::group(['middleware' => IpBlockMiddleware::class], function() { + TestRouter::group(['middleware' => IpRestrictMiddleware::class], function() { TestRouter::get('/success', 'DummyController@method1'); }); diff --git a/tests/Pecee/SimpleRouter/Dummy/Middleware/IpBlockMiddleware.php b/tests/Pecee/SimpleRouter/Dummy/Middleware/IpRestrictMiddleware.php similarity index 64% rename from tests/Pecee/SimpleRouter/Dummy/Middleware/IpBlockMiddleware.php rename to tests/Pecee/SimpleRouter/Dummy/Middleware/IpRestrictMiddleware.php index ee525cf..df0d526 100644 --- a/tests/Pecee/SimpleRouter/Dummy/Middleware/IpBlockMiddleware.php +++ b/tests/Pecee/SimpleRouter/Dummy/Middleware/IpRestrictMiddleware.php @@ -1,6 +1,6 @@ Date: Tue, 30 Mar 2021 20:48:37 +0200 Subject: [PATCH 3/4] Updated readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index df796aa..bfdd153 100644 --- a/README.md +++ b/README.md @@ -1378,7 +1378,7 @@ You can white- and blacklist access to IP's using the build in `IpRestrictAccess Create your own custom Middleware and extend the `IpRestrictAccess` class. The `IpRestrictAccess` class contains two properties `ipBlacklist` and `ipWhitelist` that can be added -to your middleware to change which IP's has blocked or allowed access. +to your middleware to change which IP's that have restricted access. You can use `*` to restrict access to a range of ips. @@ -1399,7 +1399,7 @@ class IpBlockerMiddleware extends IpRestrictAccess { } ``` -You can the middleware to multiple routes by adding your [middleware to a groups](#middleware). +You can add the middleware to multiple routes by adding your [middleware to a groups](#middleware). ## Url rewriting From d92d50ecdc71df244317c75c894fdad409deace1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Wed, 31 Mar 2021 03:08:01 +0200 Subject: [PATCH 4/4] Updated features in documentation --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 858c029..4b3a504 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,8 @@ You can find the demo-project here: [https://github.com/skipperbent/simple-route - Sub-domain routing - Custom boot managers to rewrite urls to "nicer" ones. - Input manager; easily manage `GET`, `POST` and `FILE` values. +- IP based restrictions. +- Easily extendable. ## Installation