1
0

ThrottlerTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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\Security\Bruteforce\Backend\DatabaseBackend;
  24. use OC\Security\Bruteforce\Throttler;
  25. use OCP\AppFramework\Utility\ITimeFactory;
  26. use OCP\IConfig;
  27. use OCP\IDBConnection;
  28. use Psr\Log\LoggerInterface;
  29. use Test\TestCase;
  30. /**
  31. * Based on the unit tests from Paragonie's Airship CMS
  32. * Ref: https://github.com/paragonie/airship/blob/7e5bad7e3c0fbbf324c11f963fd1f80e59762606/test/unit/Engine/Security/AirBrakeTest.php
  33. *
  34. * @package Test\Security\Bruteforce
  35. */
  36. class ThrottlerTest extends TestCase {
  37. /** @var Throttler */
  38. private $throttler;
  39. /** @var IDBConnection */
  40. private $dbConnection;
  41. /** @var ITimeFactory */
  42. private $timeFactory;
  43. /** @var LoggerInterface */
  44. private $logger;
  45. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  46. private $config;
  47. protected function setUp(): void {
  48. $this->dbConnection = $this->createMock(IDBConnection::class);
  49. $this->timeFactory = $this->createMock(ITimeFactory::class);
  50. $this->logger = $this->createMock(LoggerInterface::class);
  51. $this->config = $this->createMock(IConfig::class);
  52. $this->throttler = new Throttler(
  53. $this->timeFactory,
  54. $this->logger,
  55. $this->config,
  56. new DatabaseBackend($this->dbConnection)
  57. );
  58. parent::setUp();
  59. }
  60. public function dataIsIPWhitelisted() {
  61. return [
  62. [
  63. '10.10.10.10',
  64. [
  65. 'whitelist_0' => '10.10.10.0/24',
  66. ],
  67. true,
  68. ],
  69. [
  70. '10.10.10.10',
  71. [
  72. 'whitelist_0' => '192.168.0.0/16',
  73. ],
  74. false,
  75. ],
  76. [
  77. '10.10.10.10',
  78. [
  79. 'whitelist_0' => '192.168.0.0/16',
  80. 'whitelist_1' => '10.10.10.0/24',
  81. ],
  82. true,
  83. ],
  84. [
  85. '10.10.10.10',
  86. [
  87. 'whitelist_0' => '10.10.10.11/31',
  88. ],
  89. true,
  90. ],
  91. [
  92. '10.10.10.10',
  93. [
  94. 'whitelist_0' => '10.10.10.9/31',
  95. ],
  96. false,
  97. ],
  98. [
  99. '10.10.10.10',
  100. [
  101. 'whitelist_0' => '10.10.10.15/29',
  102. ],
  103. true,
  104. ],
  105. [
  106. 'dead:beef:cafe::1',
  107. [
  108. 'whitelist_0' => '192.168.0.0/16',
  109. 'whitelist_1' => '10.10.10.0/24',
  110. 'whitelist_2' => 'deaf:beef:cafe:1234::/64'
  111. ],
  112. false,
  113. ],
  114. [
  115. 'dead:beef:cafe::1',
  116. [
  117. 'whitelist_0' => '192.168.0.0/16',
  118. 'whitelist_1' => '10.10.10.0/24',
  119. 'whitelist_2' => 'deaf:beef::/64'
  120. ],
  121. false,
  122. ],
  123. [
  124. 'dead:beef:cafe::1',
  125. [
  126. 'whitelist_0' => '192.168.0.0/16',
  127. 'whitelist_1' => '10.10.10.0/24',
  128. 'whitelist_2' => 'deaf:cafe::/8'
  129. ],
  130. true,
  131. ],
  132. [
  133. 'dead:beef:cafe::1111',
  134. [
  135. 'whitelist_0' => 'dead:beef:cafe::1100/123',
  136. ],
  137. true,
  138. ],
  139. [
  140. 'invalid',
  141. [],
  142. false,
  143. ],
  144. ];
  145. }
  146. /**
  147. * @param string $ip
  148. * @param string[] $whitelists
  149. * @param bool $isWhiteListed
  150. * @param bool $enabled
  151. */
  152. private function isIpWhiteListedHelper($ip,
  153. $whitelists,
  154. $isWhiteListed,
  155. $enabled) {
  156. $this->config->method('getAppKeys')
  157. ->with($this->equalTo('bruteForce'))
  158. ->willReturn(array_keys($whitelists));
  159. $this->config
  160. ->expects($this->once())
  161. ->method('getSystemValueBool')
  162. ->with('auth.bruteforce.protection.enabled', true)
  163. ->willReturn($enabled);
  164. $this->config->method('getAppValue')
  165. ->willReturnCallback(function ($app, $key, $default) use ($whitelists) {
  166. if ($app !== 'bruteForce') {
  167. return $default;
  168. }
  169. if (isset($whitelists[$key])) {
  170. return $whitelists[$key];
  171. }
  172. return $default;
  173. });
  174. $this->assertSame(
  175. ($enabled === false) ? true : $isWhiteListed,
  176. self::invokePrivate($this->throttler, 'isBypassListed', [$ip])
  177. );
  178. }
  179. /**
  180. * @dataProvider dataIsIPWhitelisted
  181. *
  182. * @param string $ip
  183. * @param string[] $whitelists
  184. * @param bool $isWhiteListed
  185. */
  186. public function testIsIpWhiteListedWithEnabledProtection($ip,
  187. $whitelists,
  188. $isWhiteListed) {
  189. $this->isIpWhiteListedHelper(
  190. $ip,
  191. $whitelists,
  192. $isWhiteListed,
  193. true
  194. );
  195. }
  196. /**
  197. * @dataProvider dataIsIPWhitelisted
  198. *
  199. * @param string $ip
  200. * @param string[] $whitelists
  201. * @param bool $isWhiteListed
  202. */
  203. public function testIsIpWhiteListedWithDisabledProtection($ip,
  204. $whitelists,
  205. $isWhiteListed) {
  206. $this->isIpWhiteListedHelper(
  207. $ip,
  208. $whitelists,
  209. $isWhiteListed,
  210. false
  211. );
  212. }
  213. }