2012-04-29 19:15:06 +02:00
|
|
|
<?php
|
2021-05-04 10:29:25 +02:00
|
|
|
|
2012-04-29 19:15:06 +02:00
|
|
|
/**
|
2016-07-11 11:58:15 +02:00
|
|
|
* PrivateBin
|
2012-04-29 19:15:06 +02:00
|
|
|
*
|
|
|
|
* a zero-knowledge paste bin
|
|
|
|
*
|
2016-07-11 11:58:15 +02:00
|
|
|
* @link https://github.com/PrivateBin/PrivateBin
|
2012-04-29 19:15:06 +02:00
|
|
|
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
2016-07-19 13:56:52 +02:00
|
|
|
* @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
2023-12-15 07:20:20 +01:00
|
|
|
* @version 1.6.2
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2016-12-12 18:43:23 +01:00
|
|
|
|
2016-12-12 18:49:08 +01:00
|
|
|
namespace PrivateBin\Persistence;
|
2016-08-09 11:54:42 +02:00
|
|
|
|
2022-02-20 11:25:19 +01:00
|
|
|
use Exception;
|
2021-05-22 11:02:54 +02:00
|
|
|
use IPLib\Factory;
|
2022-02-26 07:18:59 +01:00
|
|
|
use IPLib\ParseStringFlag;
|
2016-08-09 11:54:42 +02:00
|
|
|
use PrivateBin\Configuration;
|
2022-02-20 11:25:19 +01:00
|
|
|
use PrivateBin\I18n;
|
2016-07-21 17:09:48 +02:00
|
|
|
|
2012-04-29 19:15:06 +02:00
|
|
|
/**
|
2016-08-09 11:54:42 +02:00
|
|
|
* TrafficLimiter
|
2012-04-29 19:15:06 +02:00
|
|
|
*
|
|
|
|
* Handles traffic limiting, so no user does more than one call per 10 seconds.
|
|
|
|
*/
|
2016-08-09 11:54:42 +02:00
|
|
|
class TrafficLimiter extends AbstractPersistence
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
|
|
|
/**
|
2022-02-20 11:25:19 +01:00
|
|
|
* listed IPs are the only ones allowed to create, defaults to null
|
2015-08-16 15:55:31 +02:00
|
|
|
*
|
2012-04-29 19:15:06 +02:00
|
|
|
* @access private
|
|
|
|
* @static
|
2022-02-20 11:25:19 +01:00
|
|
|
* @var string|null
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2022-02-20 11:25:19 +01:00
|
|
|
private static $_creators = null;
|
2021-05-04 11:18:06 +02:00
|
|
|
|
2021-05-04 10:29:25 +02:00
|
|
|
/**
|
2022-02-20 09:09:20 +01:00
|
|
|
* listed IPs are exempted from limits, defaults to null
|
2021-05-04 10:29:25 +02:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
2021-05-22 11:30:17 +02:00
|
|
|
* @var string|null
|
2021-05-04 10:29:25 +02:00
|
|
|
*/
|
2022-02-20 09:09:20 +01:00
|
|
|
private static $_exempted = null;
|
2012-04-29 19:15:06 +02:00
|
|
|
|
2015-09-26 17:57:46 +02:00
|
|
|
/**
|
|
|
|
* key to fetch IP address
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private static $_ipKey = 'REMOTE_ADDR';
|
|
|
|
|
2012-04-29 19:15:06 +02:00
|
|
|
/**
|
2022-02-20 11:25:19 +01:00
|
|
|
* time limit in seconds, defaults to 10s
|
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
private static $_limit = 10;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set configuration options of the traffic limiter
|
2012-04-29 19:15:06 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2022-02-20 11:25:19 +01:00
|
|
|
* @param Configuration $conf
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2022-02-20 11:25:19 +01:00
|
|
|
public static function setConfiguration(Configuration $conf)
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2022-02-20 11:25:19 +01:00
|
|
|
self::setCreators($conf->getKey('creators', 'traffic'));
|
|
|
|
self::setExempted($conf->getKey('exempted', 'traffic'));
|
|
|
|
self::setLimit($conf->getKey('limit', 'traffic'));
|
|
|
|
|
|
|
|
if (($option = $conf->getKey('header', 'traffic')) !== '') {
|
|
|
|
$httpHeader = 'HTTP_' . $option;
|
|
|
|
if (array_key_exists($httpHeader, $_SERVER) && !empty($_SERVER[$httpHeader])) {
|
|
|
|
self::$_ipKey = $httpHeader;
|
|
|
|
}
|
|
|
|
}
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
|
2021-05-04 10:29:25 +02:00
|
|
|
/**
|
2022-02-20 11:25:19 +01:00
|
|
|
* set a list of creator IP(-ranges) as string
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
* @param string $creators
|
|
|
|
*/
|
|
|
|
public static function setCreators($creators)
|
|
|
|
{
|
|
|
|
self::$_creators = $creators;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* set a list of exempted IP(-ranges) as string
|
2021-05-04 10:29:25 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2022-02-20 09:09:20 +01:00
|
|
|
* @param string $exempted
|
2021-05-04 10:29:25 +02:00
|
|
|
*/
|
2022-02-20 09:09:20 +01:00
|
|
|
public static function setExempted($exempted)
|
2021-05-04 10:29:25 +02:00
|
|
|
{
|
2022-02-20 09:09:20 +01:00
|
|
|
self::$_exempted = $exempted;
|
2021-05-04 10:29:25 +02:00
|
|
|
}
|
|
|
|
|
2015-09-26 17:57:46 +02:00
|
|
|
/**
|
2022-02-20 11:25:19 +01:00
|
|
|
* set the time limit in seconds
|
2015-09-26 17:57:46 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2022-02-20 11:25:19 +01:00
|
|
|
* @param int $limit
|
2015-09-26 17:57:46 +02:00
|
|
|
*/
|
2022-02-20 11:25:19 +01:00
|
|
|
public static function setLimit($limit)
|
2015-09-26 17:57:46 +02:00
|
|
|
{
|
2022-02-20 11:25:19 +01:00
|
|
|
self::$_limit = $limit;
|
2015-09-26 17:57:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-10 17:41:46 +02:00
|
|
|
* get a HMAC of the current visitors IP address
|
2015-09-26 17:57:46 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2016-08-10 17:41:46 +02:00
|
|
|
* @param string $algo
|
2015-09-26 17:57:46 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2016-08-10 17:41:46 +02:00
|
|
|
public static function getHash($algo = 'sha512')
|
2015-09-26 17:57:46 +02:00
|
|
|
{
|
2016-08-10 17:41:46 +02:00
|
|
|
return hash_hmac($algo, $_SERVER[self::$_ipKey], ServerSalt::get());
|
2015-09-26 17:57:46 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 12:13:03 +02:00
|
|
|
/**
|
2022-02-20 11:25:19 +01:00
|
|
|
* validate $_ipKey against configured ipranges. If matched we will ignore the ip
|
2021-05-06 12:13:03 +02:00
|
|
|
*
|
|
|
|
* @access private
|
|
|
|
* @static
|
2021-05-06 12:18:44 +02:00
|
|
|
* @param string $ipRange
|
|
|
|
* @return bool
|
2021-05-06 12:13:03 +02:00
|
|
|
*/
|
|
|
|
private static function matchIp($ipRange = null)
|
|
|
|
{
|
2021-05-22 11:30:17 +02:00
|
|
|
if (is_string($ipRange)) {
|
|
|
|
$ipRange = trim($ipRange);
|
|
|
|
}
|
2022-02-26 07:18:59 +01:00
|
|
|
$address = Factory::parseAddressString($_SERVER[self::$_ipKey]);
|
2022-02-28 16:24:06 +01:00
|
|
|
$range = Factory::parseRangeString(
|
|
|
|
$ipRange,
|
|
|
|
ParseStringFlag::IPV4_MAYBE_NON_DECIMAL | ParseStringFlag::IPV4SUBNET_MAYBE_COMPACT | ParseStringFlag::IPV4ADDRESS_MAYBE_NON_QUAD_DOTTED
|
|
|
|
);
|
2021-05-19 08:47:35 +02:00
|
|
|
|
2021-05-22 10:59:47 +02:00
|
|
|
// address could not be parsed, we might not be in IP space and try a string comparison instead
|
2021-05-22 11:35:53 +02:00
|
|
|
if (is_null($address)) {
|
2021-05-22 10:59:47 +02:00
|
|
|
return $_SERVER[self::$_ipKey] === $ipRange;
|
|
|
|
}
|
|
|
|
// range could not be parsed, possibly an invalid ip range given in config
|
2021-05-22 11:35:53 +02:00
|
|
|
if (is_null($range)) {
|
2021-05-19 09:01:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-19 08:47:35 +02:00
|
|
|
|
2021-06-13 08:26:05 +02:00
|
|
|
return $address->matches($range);
|
2021-05-06 12:13:03 +02:00
|
|
|
}
|
|
|
|
|
2012-04-29 19:15:06 +02:00
|
|
|
/**
|
2022-02-20 11:25:19 +01:00
|
|
|
* make sure the IP address is allowed to perfom a request
|
2012-04-29 19:15:06 +02:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @static
|
2022-02-20 11:25:19 +01:00
|
|
|
* @throws Exception
|
|
|
|
* @return true
|
2012-04-29 19:15:06 +02:00
|
|
|
*/
|
2015-09-26 17:57:46 +02:00
|
|
|
public static function canPass()
|
2012-04-29 19:15:06 +02:00
|
|
|
{
|
2022-02-20 11:25:19 +01:00
|
|
|
// if creators are defined, the traffic limiter will only allow creation
|
|
|
|
// for these, with no limits, and skip any other rules
|
|
|
|
if (!empty(self::$_creators)) {
|
|
|
|
$creatorIps = explode(',', self::$_creators);
|
|
|
|
foreach ($creatorIps as $ipRange) {
|
|
|
|
if (self::matchIp($ipRange) === true) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Exception(I18n::_('Your IP is not authorized to create pastes.'));
|
|
|
|
}
|
|
|
|
|
2015-09-26 17:57:46 +02:00
|
|
|
// disable limits if set to less then 1
|
2016-07-26 08:19:35 +02:00
|
|
|
if (self::$_limit < 1) {
|
|
|
|
return true;
|
|
|
|
}
|
2021-05-06 12:18:44 +02:00
|
|
|
|
2022-02-20 11:25:19 +01:00
|
|
|
// check if $_ipKey is exempted from ratelimiting
|
2022-02-20 09:09:20 +01:00
|
|
|
if (!empty(self::$_exempted)) {
|
|
|
|
$exIp_array = explode(',', self::$_exempted);
|
2021-05-04 10:29:25 +02:00
|
|
|
foreach ($exIp_array as $ipRange) {
|
2021-05-06 12:18:44 +02:00
|
|
|
if (self::matchIp($ipRange) === true) {
|
2021-05-04 10:29:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-30 23:54:42 +01:00
|
|
|
|
2022-02-20 09:30:41 +01:00
|
|
|
// used as array key, which are limited in length, hence using algo with shorter range
|
2016-08-10 17:41:46 +02:00
|
|
|
$hash = self::getHash('sha256');
|
2021-06-09 19:16:22 +02:00
|
|
|
$now = time();
|
|
|
|
$tl = (int) self::$_store->getValue('traffic_limiter', $hash);
|
2021-06-08 07:49:22 +02:00
|
|
|
self::$_store->purgeValues('traffic_limiter', $now - self::$_limit);
|
|
|
|
if ($tl > 0 && ($tl + self::$_limit >= $now)) {
|
2012-04-29 19:15:06 +02:00
|
|
|
$result = false;
|
|
|
|
} else {
|
2021-06-08 07:49:22 +02:00
|
|
|
$tl = time();
|
|
|
|
$result = true;
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
2021-06-16 05:32:45 +02:00
|
|
|
if (!self::$_store->setValue((string) $tl, 'traffic_limiter', $hash)) {
|
|
|
|
error_log('failed to store the traffic limiter, it probably contains outdated information');
|
|
|
|
}
|
2022-02-20 12:25:55 +01:00
|
|
|
if ($result) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-02-20 11:25:19 +01:00
|
|
|
throw new Exception(I18n::_(
|
|
|
|
'Please wait %d seconds between each post.',
|
|
|
|
self::$_limit
|
|
|
|
));
|
2012-04-29 19:15:06 +02:00
|
|
|
}
|
|
|
|
}
|