TrafficLimiter.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * PrivateBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link https://github.com/PrivateBin/PrivateBin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license https://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 1.3.5
  11. */
  12. namespace PrivateBin\Persistence;
  13. use Exception;
  14. use IPLib\Factory;
  15. use IPLib\ParseStringFlag;
  16. use PrivateBin\Configuration;
  17. use PrivateBin\I18n;
  18. /**
  19. * TrafficLimiter
  20. *
  21. * Handles traffic limiting, so no user does more than one call per 10 seconds.
  22. */
  23. class TrafficLimiter extends AbstractPersistence
  24. {
  25. /**
  26. * listed IPs are the only ones allowed to create, defaults to null
  27. *
  28. * @access private
  29. * @static
  30. * @var string|null
  31. */
  32. private static $_creators = null;
  33. /**
  34. * listed IPs are exempted from limits, defaults to null
  35. *
  36. * @access private
  37. * @static
  38. * @var string|null
  39. */
  40. private static $_exempted = null;
  41. /**
  42. * key to fetch IP address
  43. *
  44. * @access private
  45. * @static
  46. * @var string
  47. */
  48. private static $_ipKey = 'REMOTE_ADDR';
  49. /**
  50. * time limit in seconds, defaults to 10s
  51. *
  52. * @access private
  53. * @static
  54. * @var int
  55. */
  56. private static $_limit = 10;
  57. /**
  58. * set configuration options of the traffic limiter
  59. *
  60. * @access public
  61. * @static
  62. * @param Configuration $conf
  63. */
  64. public static function setConfiguration(Configuration $conf)
  65. {
  66. self::setCreators($conf->getKey('creators', 'traffic'));
  67. self::setExempted($conf->getKey('exempted', 'traffic'));
  68. self::setLimit($conf->getKey('limit', 'traffic'));
  69. if (($option = $conf->getKey('header', 'traffic')) !== '') {
  70. $httpHeader = 'HTTP_' . $option;
  71. if (array_key_exists($httpHeader, $_SERVER) && !empty($_SERVER[$httpHeader])) {
  72. self::$_ipKey = $httpHeader;
  73. }
  74. }
  75. }
  76. /**
  77. * set a list of creator IP(-ranges) as string
  78. *
  79. * @access public
  80. * @static
  81. * @param string $creators
  82. */
  83. public static function setCreators($creators)
  84. {
  85. self::$_creators = $creators;
  86. }
  87. /**
  88. * set a list of exempted IP(-ranges) as string
  89. *
  90. * @access public
  91. * @static
  92. * @param string $exempted
  93. */
  94. public static function setExempted($exempted)
  95. {
  96. self::$_exempted = $exempted;
  97. }
  98. /**
  99. * set the time limit in seconds
  100. *
  101. * @access public
  102. * @static
  103. * @param int $limit
  104. */
  105. public static function setLimit($limit)
  106. {
  107. self::$_limit = $limit;
  108. }
  109. /**
  110. * get a HMAC of the current visitors IP address
  111. *
  112. * @access public
  113. * @static
  114. * @param string $algo
  115. * @return string
  116. */
  117. public static function getHash($algo = 'sha512')
  118. {
  119. return hash_hmac($algo, $_SERVER[self::$_ipKey], ServerSalt::get());
  120. }
  121. /**
  122. * validate $_ipKey against configured ipranges. If matched we will ignore the ip
  123. *
  124. * @access private
  125. * @static
  126. * @param string $ipRange
  127. * @return bool
  128. */
  129. private static function matchIp($ipRange = null)
  130. {
  131. if (is_string($ipRange)) {
  132. $ipRange = trim($ipRange);
  133. }
  134. $address = Factory::parseAddressString($_SERVER[self::$_ipKey]);
  135. $range = Factory::parseRangeString($ipRange, ParseStringFlag::IPV4_MAYBE_NON_DECIMAL);
  136. // address could not be parsed, we might not be in IP space and try a string comparison instead
  137. if (is_null($address)) {
  138. return $_SERVER[self::$_ipKey] === $ipRange;
  139. }
  140. // range could not be parsed, possibly an invalid ip range given in config
  141. if (is_null($range)) {
  142. return false;
  143. }
  144. return $address->matches($range);
  145. }
  146. /**
  147. * make sure the IP address is allowed to perfom a request
  148. *
  149. * @access public
  150. * @static
  151. * @throws Exception
  152. * @return true
  153. */
  154. public static function canPass()
  155. {
  156. // if creators are defined, the traffic limiter will only allow creation
  157. // for these, with no limits, and skip any other rules
  158. if (!empty(self::$_creators)) {
  159. $creatorIps = explode(',', self::$_creators);
  160. foreach ($creatorIps as $ipRange) {
  161. if (self::matchIp($ipRange) === true) {
  162. return true;
  163. }
  164. }
  165. throw new Exception(I18n::_('Your IP is not authorized to create pastes.'));
  166. }
  167. // disable limits if set to less then 1
  168. if (self::$_limit < 1) {
  169. return true;
  170. }
  171. // check if $_ipKey is exempted from ratelimiting
  172. if (!empty(self::$_exempted)) {
  173. $exIp_array = explode(',', self::$_exempted);
  174. foreach ($exIp_array as $ipRange) {
  175. if (self::matchIp($ipRange) === true) {
  176. return true;
  177. }
  178. }
  179. }
  180. // used as array key, which are limited in length, hence using algo with shorter range
  181. $hash = self::getHash('sha256');
  182. $now = time();
  183. $tl = (int) self::$_store->getValue('traffic_limiter', $hash);
  184. self::$_store->purgeValues('traffic_limiter', $now - self::$_limit);
  185. if ($tl > 0 && ($tl + self::$_limit >= $now)) {
  186. $result = false;
  187. } else {
  188. $tl = time();
  189. $result = true;
  190. }
  191. if (!self::$_store->setValue((string) $tl, 'traffic_limiter', $hash)) {
  192. error_log('failed to store the traffic limiter, it probably contains outdated information');
  193. }
  194. if ($result) {
  195. return true;
  196. }
  197. throw new Exception(I18n::_(
  198. 'Please wait %d seconds between each post.',
  199. self::$_limit
  200. ));
  201. }
  202. }