mirror of
https://github.com/FOSSBilling/FOSSBilling.git
synced 2026-12-01 21:02:54 +09:00
Merge pull request #911 from BenNottelling/API-Rate-Limiter
Slightly improved API rate limiter
This commit is contained in:
6 files changed
+48
-11
No files matched your search
@@ -109,5 +109,22 @@ return array(
|
||||
|
||||
// How many requests allowed per time span
|
||||
'rate_limit' => 1000,
|
||||
|
||||
/**
|
||||
* Note about rate limiting login attempts:
|
||||
* When the limit is reach, a simple 2 second delay is added to the request.
|
||||
* This makes brute forcing a password basically useless while not outright blocking legitimate traffic.
|
||||
* When calculating, ensure the rate limited traffic can still make enough requests to stay rate limited
|
||||
* Ex: One request every 2 seconds is more than 20 times in 1 minute, so the IP will remain throttled
|
||||
*/
|
||||
|
||||
// Throttling delay
|
||||
'throttle_delay' => 2,
|
||||
|
||||
// Time span login for limit in seconds
|
||||
'rate_span_login' => 60,
|
||||
|
||||
// How many login requests allowed per time span
|
||||
'rate_limit_login' => 20,
|
||||
),
|
||||
);
|
||||
@@ -249,10 +249,11 @@ class Box_Request implements \Box\InjectionAwareInterface
|
||||
|
||||
/**
|
||||
* Gets most possible client IPv4 Address. This method search in $_SERVER[‘REMOTE_ADDR’] and optionally in $_SERVER[‘HTTP_X_FORWARDED_FOR’]
|
||||
* @param bool $trustForwardedHeader - should we trust forwarded header?
|
||||
* @param bool $trustForwardedHeader - No by default because this can be changed to anything extremely easy, making it unreliable for tracking and adding a potential source for external data to be executed.
|
||||
* Please see: https://stackoverflow.com/questions/3003145/how-to-get-the-client-ip-address-in-php
|
||||
* return string
|
||||
*/
|
||||
public function getClientAddress($trustForwardedHeader = true)
|
||||
public function getClientAddress($trustForwardedHeader = false)
|
||||
{
|
||||
$address = null;
|
||||
if($trustForwardedHeader) {
|
||||
|
||||
@@ -95,16 +95,24 @@ class Client implements InjectionAwareInterface
|
||||
}
|
||||
}
|
||||
|
||||
private function checkRateLimit()
|
||||
private function checkRateLimit($method=null)
|
||||
{
|
||||
$rate_span = $this->_api_config['rate_span'];
|
||||
$rate_limit = $this->_api_config['rate_limit'];
|
||||
$isLoginMethod = false;
|
||||
if($method == 'staff_login' || $method == 'client_login'){
|
||||
$rate_span = $this->_api_config['rate_span_login'];
|
||||
$rate_limit = $this->_api_config['rate_limit_login'];
|
||||
$isLoginMethod = true;
|
||||
} else {
|
||||
$rate_span = $this->_api_config['rate_span'];
|
||||
$rate_limit = $this->_api_config['rate_limit'];
|
||||
}
|
||||
|
||||
$service = $this->di['mod_service']('api');
|
||||
$requests = $service->getRequestCount(time() - $rate_span, $this->_getIp());
|
||||
$requests = $service->getRequestCount(time() - $rate_span, $this->_getIp(), $isLoginMethod);
|
||||
$requests_left = $rate_limit - $requests;
|
||||
$this->_requests_left = $requests_left;
|
||||
if ($requests_left < 0) {
|
||||
throw new \Box_Exception('Request limit reached', null, 1003);
|
||||
sleep($this->_api_config['throttle_delay']);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -151,7 +159,7 @@ class Client implements InjectionAwareInterface
|
||||
|
||||
$service = $this->di['mod_service']('api');
|
||||
$service->logRequest();
|
||||
$this->checkRateLimit();
|
||||
$this->checkRateLimit($method);
|
||||
$this->checkHttpReferer();
|
||||
$this->isRoleAllowed($role);
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ class Service implements \Box\InjectionAwareInterface
|
||||
* @param string|null $ip
|
||||
* @return int
|
||||
*/
|
||||
public function getRequestCount($since, $ip = null)
|
||||
public function getRequestCount($since, $ip = null, $isLoginMethod = false)
|
||||
{
|
||||
if (!is_numeric($since)){
|
||||
$since = strtotime($since);
|
||||
@@ -63,11 +63,19 @@ class Service implements \Box\InjectionAwareInterface
|
||||
$values = array(
|
||||
'since' => $sinceIso,
|
||||
);
|
||||
if($isLoginMethod){
|
||||
$sql="
|
||||
SELECT COUNT(id) as cclogin
|
||||
FROM api_request
|
||||
WHERE created_at > :since
|
||||
";
|
||||
} else {
|
||||
$sql="
|
||||
SELECT COUNT(id) as cc
|
||||
FROM api_request
|
||||
WHERE created_at > :since
|
||||
";
|
||||
}
|
||||
|
||||
if(null != $ip) {
|
||||
$sql .= " AND ip = :ip";
|
||||
|
||||
@@ -383,6 +383,9 @@ final class Box_Installer
|
||||
'allowed_ips' => [],
|
||||
'rate_span' => 60 * 60,
|
||||
'rate_limit' => 1000,
|
||||
'throttle_delay' => 2,
|
||||
'rate_span_login' => 60,
|
||||
'rate_limit_login' = 20,
|
||||
],
|
||||
];
|
||||
$output = '<?php ' . PHP_EOL;
|
||||
|
||||
@@ -293,7 +293,7 @@ class Box_RequestTest extends PHPUnit\Framework\TestCase
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertEquals('123.123.123.120', $request->getClientAddress());
|
||||
$this->assertEquals('123.123.123.120', $request->getClientAddress(true));
|
||||
$this->assertEquals('123.123.123.121', $request->getClientAddress(true));
|
||||
}
|
||||
|
||||
@@ -312,7 +312,7 @@ class Box_RequestTest extends PHPUnit\Framework\TestCase
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertEquals('123.123.123.1', $request->getClientAddress(false));
|
||||
$this->assertEquals('123.123.123.1', $request->getClientAddress());
|
||||
$this->assertEquals('123.123.123.2', $request->getClientAddress(false));
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user