TrafficLimiter.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.5.1
  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(
  136. $ipRange,
  137. ParseStringFlag::IPV4_MAYBE_NON_DECIMAL | ParseStringFlag::IPV4SUBNET_MAYBE_COMPACT | ParseStringFlag::IPV4ADDRESS_MAYBE_NON_QUAD_DOTTED
  138. );
  139. // address could not be parsed, we might not be in IP space and try a string comparison instead
  140. if (is_null($address)) {
  141. return $_SERVER[self::$_ipKey] === $ipRange;
  142. }
  143. // range could not be parsed, possibly an invalid ip range given in config
  144. if (is_null($range)) {
  145. return false;
  146. }
  147. return $address->matches($range);
  148. }
  149. /**
  150. * make sure the IP address is allowed to perfom a request
  151. *
  152. * @access public
  153. * @static
  154. * @throws Exception
  155. * @return true
  156. */
  157. public static function canPass()
  158. {
  159. // if creators are defined, the traffic limiter will only allow creation
  160. // for these, with no limits, and skip any other rules
  161. if (!empty(self::$_creators)) {
  162. $creatorIps = explode(',', self::$_creators);
  163. foreach ($creatorIps as $ipRange) {
  164. if (self::matchIp($ipRange) === true) {
  165. return true;
  166. }
  167. }
  168. throw new Exception(I18n::_('Your IP is not authorized to create pastes.'));
  169. }
  170. // disable limits if set to less then 1
  171. if (self::$_limit < 1) {
  172. return true;
  173. }
  174. // check if $_ipKey is exempted from ratelimiting
  175. if (!empty(self::$_exempted)) {
  176. $exIp_array = explode(',', self::$_exempted);
  177. foreach ($exIp_array as $ipRange) {
  178. if (self::matchIp($ipRange) === true) {
  179. return true;
  180. }
  181. }
  182. }
  183. // used as array key, which are limited in length, hence using algo with shorter range
  184. $hash = self::getHash('sha256');
  185. $now = time();
  186. $tl = (int) self::$_store->getValue('traffic_limiter', $hash);
  187. self::$_store->purgeValues('traffic_limiter', $now - self::$_limit);
  188. if ($tl > 0 && ($tl + self::$_limit >= $now)) {
  189. $result = false;
  190. } else {
  191. $tl = time();
  192. $result = true;
  193. }
  194. if (!self::$_store->setValue((string) $tl, 'traffic_limiter', $hash)) {
  195. error_log('failed to store the traffic limiter, it probably contains outdated information');
  196. }
  197. if ($result) {
  198. return true;
  199. }
  200. throw new Exception(I18n::_(
  201. 'Please wait %d seconds between each post.',
  202. self::$_limit
  203. ));
  204. }
  205. }