1
0

LoginThrottleTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Unit\Auth;
  4. use App\Auth\LoginThrottle;
  5. use Monolog\Handler\NullHandler;
  6. use Monolog\Handler\TestHandler;
  7. use Monolog\Logger;
  8. use PHPUnit\Framework\TestCase;
  9. use Psr\Log\LoggerInterface;
  10. /**
  11. * Brute-force lockout progression: 5/10/15 attempts trigger 60/300/1800-second
  12. * locks. The (username, ip) tuple gates the bucket so the legit admin from
  13. * another IP isn't locked out by an attacker spraying one address.
  14. */
  15. final class LoginThrottleTest extends TestCase
  16. {
  17. public function testFastRetryUnderFive(): void
  18. {
  19. $t = $this->throttle();
  20. for ($i = 0; $i < 4; ++$i) {
  21. $t->recordFailure('admin', '10.0.0.1');
  22. }
  23. self::assertFalse($t->isLocked('admin', '10.0.0.1'));
  24. }
  25. public function testFifthFailureLocksForOneMinute(): void
  26. {
  27. $t = $this->throttle();
  28. for ($i = 0; $i < 5; ++$i) {
  29. $t->recordFailure('admin', '10.0.0.1');
  30. }
  31. self::assertTrue($t->isLocked('admin', '10.0.0.1'));
  32. self::assertGreaterThanOrEqual(60, $t->lockoutSecondsRemaining('admin', '10.0.0.1'));
  33. self::assertLessThanOrEqual(60, $t->lockoutSecondsRemaining('admin', '10.0.0.1'));
  34. }
  35. public function testTenthFailureLocksForFiveMinutes(): void
  36. {
  37. $now = 1000000;
  38. $t = $this->throttle($now);
  39. for ($i = 0; $i < 10; ++$i) {
  40. $t->recordFailure('admin', '10.0.0.1');
  41. }
  42. self::assertSame(300, $t->lockoutSecondsRemaining('admin', '10.0.0.1'));
  43. }
  44. public function testFifteenthFailureLocksForThirtyMinutes(): void
  45. {
  46. $now = 1000000;
  47. $t = $this->throttle($now);
  48. for ($i = 0; $i < 15; ++$i) {
  49. $t->recordFailure('admin', '10.0.0.1');
  50. }
  51. self::assertSame(1800, $t->lockoutSecondsRemaining('admin', '10.0.0.1'));
  52. }
  53. public function testLockoutExpiresAfterTimeAdvance(): void
  54. {
  55. $now = 1000000;
  56. $t = new LoginThrottle($this->logger(), function () use (&$now): int {
  57. return $now;
  58. });
  59. for ($i = 0; $i < 5; ++$i) {
  60. $t->recordFailure('admin', '10.0.0.1');
  61. }
  62. self::assertTrue($t->isLocked('admin', '10.0.0.1'));
  63. $now += 61;
  64. self::assertFalse($t->isLocked('admin', '10.0.0.1'));
  65. }
  66. public function testDifferentIpHasIndependentBucket(): void
  67. {
  68. $t = $this->throttle();
  69. for ($i = 0; $i < 5; ++$i) {
  70. $t->recordFailure('admin', '10.0.0.1');
  71. }
  72. self::assertTrue($t->isLocked('admin', '10.0.0.1'));
  73. self::assertFalse($t->isLocked('admin', '10.0.0.2'));
  74. }
  75. public function testClearResetsBucket(): void
  76. {
  77. $t = $this->throttle();
  78. for ($i = 0; $i < 5; ++$i) {
  79. $t->recordFailure('admin', '10.0.0.1');
  80. }
  81. $t->clear('admin', '10.0.0.1');
  82. self::assertFalse($t->isLocked('admin', '10.0.0.1'));
  83. self::assertSame(0, $t->lockoutSecondsRemaining('admin', '10.0.0.1'));
  84. }
  85. public function testUsernameCaseDoesNotMultiplyBuckets(): void
  86. {
  87. $t = $this->throttle();
  88. for ($i = 0; $i < 3; ++$i) {
  89. $t->recordFailure('admin', '10.0.0.1');
  90. }
  91. for ($i = 0; $i < 2; ++$i) {
  92. $t->recordFailure('ADMIN', '10.0.0.1');
  93. }
  94. self::assertTrue($t->isLocked('Admin', '10.0.0.1'));
  95. }
  96. public function testPerUsernameBucketLocksOutAcrossDistinctIps(): void
  97. {
  98. $t = $this->throttle();
  99. // 24 failures from 24 distinct IPs — each is a fresh per-IP bucket
  100. // (only 1 attempt apiece), but the per-username counter accumulates.
  101. for ($i = 0; $i < 24; ++$i) {
  102. $t->recordFailure('admin', '10.0.0.' . $i);
  103. }
  104. self::assertFalse($t->isLocked('admin', '10.0.0.99'));
  105. // 25th attempt from yet another IP trips the per-username lockout.
  106. $t->recordFailure('admin', '10.0.0.99');
  107. self::assertTrue($t->isLocked('admin', '10.0.1.1'));
  108. }
  109. public function testPerUsernameLadderProgresses(): void
  110. {
  111. $now = 1000000;
  112. $t = $this->throttle($now);
  113. for ($i = 0; $i < 25; ++$i) {
  114. $t->recordFailure('admin', '10.0.0.' . $i);
  115. }
  116. self::assertSame(60, $t->lockoutSecondsRemaining('admin', '10.99.0.1'));
  117. for ($i = 25; $i < 50; ++$i) {
  118. $t->recordFailure('admin', '10.0.0.' . $i);
  119. }
  120. self::assertSame(300, $t->lockoutSecondsRemaining('admin', '10.99.0.1'));
  121. for ($i = 50; $i < 100; ++$i) {
  122. $t->recordFailure('admin', '10.0.0.' . $i);
  123. }
  124. self::assertSame(1800, $t->lockoutSecondsRemaining('admin', '10.99.0.1'));
  125. }
  126. public function testLockoutSecondsRemainingReturnsLargerOfBuckets(): void
  127. {
  128. $now = 1000000;
  129. $t = $this->throttle($now);
  130. // 5 failures from one IP → per-IP locked for 60s.
  131. for ($i = 0; $i < 5; ++$i) {
  132. $t->recordFailure('admin', '10.0.0.1');
  133. }
  134. // Plus 45 more from distinct IPs → per-username at count=50 → 300s.
  135. for ($i = 0; $i < 45; ++$i) {
  136. $t->recordFailure('admin', '10.1.0.' . $i);
  137. }
  138. // Asked from yet-another-IP, only the per-username lock applies — 300s.
  139. self::assertSame(300, $t->lockoutSecondsRemaining('admin', '10.99.0.1'));
  140. // Asked from the per-IP bucket's IP, 300s still wins (max of 60/300).
  141. self::assertSame(300, $t->lockoutSecondsRemaining('admin', '10.0.0.1'));
  142. }
  143. public function testClearResetsBothBuckets(): void
  144. {
  145. $t = $this->throttle();
  146. // Build per-username pressure.
  147. for ($i = 0; $i < 25; ++$i) {
  148. $t->recordFailure('admin', '10.0.0.' . $i);
  149. }
  150. self::assertTrue($t->isLocked('admin', '10.99.0.1'));
  151. $t->clear('admin', '10.0.0.0');
  152. self::assertFalse($t->isLocked('admin', '10.99.0.1'));
  153. self::assertSame(0, $t->lockoutSecondsRemaining('admin', '10.99.0.1'));
  154. }
  155. public function testEmptyUsernameStillBucketsPerUsername(): void
  156. {
  157. $t = $this->throttle();
  158. for ($i = 0; $i < 25; ++$i) {
  159. $t->recordFailure('', '10.0.0.' . $i);
  160. }
  161. self::assertTrue($t->isLocked('', '10.99.0.1'));
  162. }
  163. public function testRecordFailureLogsFingerprintsNotRawIdentifiers(): void
  164. {
  165. // SEC_REVIEW F34: a SIEM operator reading these logs must not see
  166. // the attempted username (which can be a password typed in the
  167. // wrong field) or the raw client IP. They must see a stable
  168. // fingerprint instead so triage can still correlate.
  169. $handler = new TestHandler();
  170. $logger = new Logger('test');
  171. $logger->pushHandler($handler);
  172. $t = new LoginThrottle($logger);
  173. $rawUser = 'hunter2-typed-into-username-field';
  174. $rawIp = '198.51.100.77';
  175. for ($i = 0; $i < 5; ++$i) {
  176. $t->recordFailure($rawUser, $rawIp);
  177. }
  178. self::assertNotEmpty($handler->getRecords(), 'expected lockout/failure events to be logged');
  179. foreach ($handler->getRecords() as $record) {
  180. $serialized = json_encode([
  181. 'message' => $record->message,
  182. 'context' => $record->context,
  183. ], \JSON_THROW_ON_ERROR);
  184. self::assertStringNotContainsString($rawUser, $serialized);
  185. self::assertStringNotContainsString($rawIp, $serialized);
  186. self::assertArrayHasKey('username_fp', $record->context);
  187. self::assertArrayHasKey('source_ip_fp', $record->context);
  188. self::assertArrayNotHasKey('username', $record->context);
  189. self::assertArrayNotHasKey('source_ip', $record->context);
  190. }
  191. }
  192. private function throttle(?int $fixedTime = null): LoginThrottle
  193. {
  194. if ($fixedTime === null) {
  195. return new LoginThrottle($this->logger());
  196. }
  197. return new LoginThrottle($this->logger(), static fn (): int => $fixedTime);
  198. }
  199. private function logger(): LoggerInterface
  200. {
  201. $l = new Logger('test');
  202. $l->pushHandler(new NullHandler());
  203. return $l;
  204. }
  205. }