-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow using hashed IP addresses (#9)
* [fix] Use correct psr-4 root for test sources * [feat] Add option to hash IP addresses before storing them * [refactor] Separate test setup and tests * [chore] Add tests for IP hashing * [chore] Update documentation (add `hash_ips`)
- Loading branch information
1 parent
2cc5c33
commit 7596d9e
Showing
7 changed files
with
185 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace LosMiddlewareTest\RateLimit; | ||
|
||
use LosMiddleware\RateLimit\RateLimitMiddleware; | ||
use LosMiddleware\RateLimit\RateLimitOptions; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Laminas\Diactoros\Response\JsonResponse; | ||
use Laminas\Diactoros\ServerRequest; | ||
|
||
class HashIpsTest extends TestSetup | ||
{ | ||
protected function setUp() : void | ||
{ | ||
$options = new RateLimitOptions([ | ||
'max_requests' => 2, | ||
'reset_time' => 10, | ||
'ip_max_requests' => 2, | ||
'ip_reset_time' => 10, | ||
'api_header' => 'X-Api-Key', | ||
'trust_forwarded' => true, | ||
'prefer_forwarded' => false, | ||
'forwarded_headers_allowed' => [ | ||
'Client-Ip', | ||
'Forwarded', | ||
'Forwarded-For', | ||
'X-Cluster-Client-Ip', | ||
'X-Forwarded', | ||
'X-Forwarded-For', | ||
], | ||
'forwarded_ip_index' => null, | ||
'hash_ips' => true, | ||
]); | ||
|
||
$problemResponse = $this->getMockProblemResponse(); | ||
$storage = $this->getMockStorage(); | ||
$this->middleware = new RateLimitMiddleware($storage, $problemResponse, $options); | ||
} | ||
|
||
public function testHashIp() | ||
{ | ||
$defaultSalt = 'Los%Rate'; | ||
$clientIp = '192.168.1.1'; | ||
|
||
$request = new ServerRequest(['REMOTE_ADDR' => '127.0.0.1']); | ||
$request = $request->withHeader('X-Forwarded-For', $clientIp); | ||
|
||
$handler = $this->createMock(RequestHandlerInterface::class); | ||
$handler->method('handle')->willReturn(new JsonResponse([])); | ||
$this->middleware->process($request, $handler); | ||
|
||
$this->assertArrayHasKey(\md5($defaultSalt . $clientIp), $this->cache); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace LosMiddlewareTest\RateLimit; | ||
|
||
use Laminas\Diactoros\Response\JsonResponse; | ||
use LosMiddleware\RateLimit\RateLimitMiddleware; | ||
use LosMiddleware\RateLimit\RateLimitOptions; | ||
use Mezzio\ProblemDetails\ProblemDetailsResponseFactory; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\SimpleCache\CacheInterface; | ||
|
||
class TestSetup extends TestCase | ||
{ | ||
/** @var array */ | ||
protected $cache = []; | ||
|
||
/** @var RateLimitMiddleware */ | ||
protected $middleware; | ||
|
||
protected function setUp(): void | ||
{ | ||
$options = new RateLimitOptions([ | ||
'max_requests' => 2, | ||
'reset_time' => 10, | ||
'ip_max_requests' => 2, | ||
'ip_reset_time' => 10, | ||
'api_header' => 'X-Api-Key', | ||
'trust_forwarded' => true, | ||
'prefer_forwarded' => false, | ||
'forwarded_headers_allowed' => [ | ||
'Client-Ip', | ||
'Forwarded', | ||
'Forwarded-For', | ||
'X-Cluster-Client-Ip', | ||
'X-Forwarded', | ||
'X-Forwarded-For', | ||
], | ||
'forwarded_ip_index' => null, | ||
]); | ||
|
||
$problemResponse = $this->getMockProblemResponse(); | ||
$storage = $this->getMockStorage(); | ||
$this->middleware = new RateLimitMiddleware( | ||
$storage, | ||
$problemResponse, | ||
$options | ||
); | ||
} | ||
|
||
/** | ||
* @param null|mixed $default | ||
* | ||
* @return null|mixed | ||
*/ | ||
public function getCache(string $key, $default = null) | ||
{ | ||
return $this->cache[$key] ?? $default; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
*/ | ||
public function setCache(string $key, $value): void | ||
{ | ||
$this->cache[$key] = $value; | ||
} | ||
|
||
protected function getMockProblemResponse() | ||
{ | ||
$problemResponse = $this->createMock( | ||
ProblemDetailsResponseFactory::class | ||
); | ||
$problemResponse->method('createResponseFromThrowable')->willReturn( | ||
new JsonResponse([], 429) | ||
); | ||
|
||
return $problemResponse; | ||
} | ||
|
||
protected function getMockStorage() | ||
{ | ||
$storage = $this->createMock(CacheInterface::class); | ||
$storage->method('get')->will( | ||
$this->returnCallback([$this, 'getCache']) | ||
); | ||
$storage->method('set')->will( | ||
$this->returnCallback([$this, 'setCache']) | ||
); | ||
|
||
return $storage; | ||
} | ||
} |