ThrottlerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Security\Bruteforce;
  23. use OC\AppFramework\Utility\TimeFactory;
  24. use OC\Security\Bruteforce\Throttler;
  25. use OCP\IConfig;
  26. use OCP\IDBConnection;
  27. use Psr\Log\LoggerInterface;
  28. use Test\TestCase;
  29. /**
  30. * Based on the unit tests from Paragonie's Airship CMS
  31. * Ref: https://github.com/paragonie/airship/blob/7e5bad7e3c0fbbf324c11f963fd1f80e59762606/test/unit/Engine/Security/AirBrakeTest.php
  32. *
  33. * @package Test\Security\Bruteforce
  34. */
  35. class ThrottlerTest extends TestCase {
  36. /** @var Throttler */
  37. private $throttler;
  38. /** @var IDBConnection */
  39. private $dbConnection;
  40. /** @var LoggerInterface */
  41. private $logger;
  42. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  43. private $config;
  44. protected function setUp(): void {
  45. $this->dbConnection = $this->createMock(IDBConnection::class);
  46. $this->logger = $this->createMock(LoggerInterface::class);
  47. $this->config = $this->createMock(IConfig::class);
  48. $this->throttler = new Throttler(
  49. $this->dbConnection,
  50. new TimeFactory(),
  51. $this->logger,
  52. $this->config
  53. );
  54. parent::setUp();
  55. }
  56. public function testCutoff() {
  57. // precisely 31 second shy of 12 hours
  58. $cutoff = self::invokePrivate($this->throttler, 'getCutoff', [43169]);
  59. $this->assertSame(0, $cutoff->y);
  60. $this->assertSame(0, $cutoff->m);
  61. $this->assertSame(0, $cutoff->d);
  62. $this->assertSame(11, $cutoff->h);
  63. $this->assertSame(59, $cutoff->i);
  64. $this->assertSame(29, $cutoff->s);
  65. $cutoff = self::invokePrivate($this->throttler, 'getCutoff', [86401]);
  66. $this->assertSame(0, $cutoff->y);
  67. $this->assertSame(0, $cutoff->m);
  68. $this->assertSame(1, $cutoff->d);
  69. $this->assertSame(0, $cutoff->h);
  70. $this->assertSame(0, $cutoff->i);
  71. // Leap second tolerance:
  72. $this->assertLessThan(2, $cutoff->s);
  73. }
  74. public function dataIsIPWhitelisted() {
  75. return [
  76. [
  77. '10.10.10.10',
  78. [
  79. 'whitelist_0' => '10.10.10.0/24',
  80. ],
  81. true,
  82. ],
  83. [
  84. '10.10.10.10',
  85. [
  86. 'whitelist_0' => '192.168.0.0/16',
  87. ],
  88. false,
  89. ],
  90. [
  91. '10.10.10.10',
  92. [
  93. 'whitelist_0' => '192.168.0.0/16',
  94. 'whitelist_1' => '10.10.10.0/24',
  95. ],
  96. true,
  97. ],
  98. [
  99. '10.10.10.10',
  100. [
  101. 'whitelist_0' => '10.10.10.11/31',
  102. ],
  103. true,
  104. ],
  105. [
  106. '10.10.10.10',
  107. [
  108. 'whitelist_0' => '10.10.10.9/31',
  109. ],
  110. false,
  111. ],
  112. [
  113. '10.10.10.10',
  114. [
  115. 'whitelist_0' => '10.10.10.15/29',
  116. ],
  117. true,
  118. ],
  119. [
  120. 'dead:beef:cafe::1',
  121. [
  122. 'whitelist_0' => '192.168.0.0/16',
  123. 'whitelist_1' => '10.10.10.0/24',
  124. 'whitelist_2' => 'deaf:beef:cafe:1234::/64'
  125. ],
  126. false,
  127. ],
  128. [
  129. 'dead:beef:cafe::1',
  130. [
  131. 'whitelist_0' => '192.168.0.0/16',
  132. 'whitelist_1' => '10.10.10.0/24',
  133. 'whitelist_2' => 'deaf:beef::/64'
  134. ],
  135. false,
  136. ],
  137. [
  138. 'dead:beef:cafe::1',
  139. [
  140. 'whitelist_0' => '192.168.0.0/16',
  141. 'whitelist_1' => '10.10.10.0/24',
  142. 'whitelist_2' => 'deaf:cafe::/8'
  143. ],
  144. true,
  145. ],
  146. [
  147. 'dead:beef:cafe::1111',
  148. [
  149. 'whitelist_0' => 'dead:beef:cafe::1100/123',
  150. ],
  151. true,
  152. ],
  153. [
  154. 'invalid',
  155. [],
  156. false,
  157. ],
  158. ];
  159. }
  160. /**
  161. * @param string $ip
  162. * @param string[] $whitelists
  163. * @param bool $isWhiteListed
  164. * @param bool $enabled
  165. */
  166. private function isIpWhiteListedHelper($ip,
  167. $whitelists,
  168. $isWhiteListed,
  169. $enabled) {
  170. $this->config->method('getAppKeys')
  171. ->with($this->equalTo('bruteForce'))
  172. ->willReturn(array_keys($whitelists));
  173. $this->config
  174. ->expects($this->once())
  175. ->method('getSystemValue')
  176. ->with('auth.bruteforce.protection.enabled', true)
  177. ->willReturn($enabled);
  178. $this->config->method('getAppValue')
  179. ->willReturnCallback(function ($app, $key, $default) use ($whitelists) {
  180. if ($app !== 'bruteForce') {
  181. return $default;
  182. }
  183. if (isset($whitelists[$key])) {
  184. return $whitelists[$key];
  185. }
  186. return $default;
  187. });
  188. $this->assertSame(
  189. ($enabled === false) ? true : $isWhiteListed,
  190. self::invokePrivate($this->throttler, 'isIPWhitelisted', [$ip])
  191. );
  192. }
  193. /**
  194. * @dataProvider dataIsIPWhitelisted
  195. *
  196. * @param string $ip
  197. * @param string[] $whitelists
  198. * @param bool $isWhiteListed
  199. */
  200. public function testIsIpWhiteListedWithEnabledProtection($ip,
  201. $whitelists,
  202. $isWhiteListed) {
  203. $this->isIpWhiteListedHelper(
  204. $ip,
  205. $whitelists,
  206. $isWhiteListed,
  207. true
  208. );
  209. }
  210. /**
  211. * @dataProvider dataIsIPWhitelisted
  212. *
  213. * @param string $ip
  214. * @param string[] $whitelists
  215. * @param bool $isWhiteListed
  216. */
  217. public function testIsIpWhiteListedWithDisabledProtection($ip,
  218. $whitelists,
  219. $isWhiteListed) {
  220. $this->isIpWhiteListedHelper(
  221. $ip,
  222. $whitelists,
  223. $isWhiteListed,
  224. false
  225. );
  226. }
  227. }