LocalLoginTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Integration\Auth;
  4. use App\Auth\LoginThrottle;
  5. use App\Http\CsrfMiddleware;
  6. use App\Tests\Integration\Support\AppTestCase;
  7. /**
  8. * Drive the local-admin login flow against the real Slim app + a mocked
  9. * api-side `upsertLocal` response. Exercises CSRF, throttle, redirect,
  10. * session-set, and api-down handling.
  11. */
  12. final class LocalLoginTest extends AppTestCase
  13. {
  14. protected function setUp(): void
  15. {
  16. $this->bootApp();
  17. }
  18. public function testGetLoginRendersForm(): void
  19. {
  20. $response = $this->request('GET', '/login');
  21. self::assertSame(200, $response->getStatusCode());
  22. $body = (string) $response->getBody();
  23. self::assertStringContainsString('Sign in', $body);
  24. // Local sign-in toggle present (oidc disabled in this fixture).
  25. self::assertStringContainsString('name="username"', $body);
  26. self::assertStringContainsString('csrf_token', $body);
  27. }
  28. public function testCorrectCredentialsLogInAndRedirectToMe(): void
  29. {
  30. $this->enqueueApiResponse(200, [
  31. 'user_id' => 1,
  32. 'role' => 'admin',
  33. 'email' => null,
  34. 'display_name' => 'Local Admin',
  35. 'is_local' => true,
  36. ]);
  37. // Need a session + csrf token; first GET /login to set one up.
  38. $this->request('GET', '/login');
  39. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  40. self::assertNotEmpty($token);
  41. $body = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'test1234']);
  42. $response = $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  43. self::assertSame(303, $response->getStatusCode());
  44. self::assertSame('/app/dashboard', $response->getHeaderLine('Location'));
  45. self::assertNotNull($_SESSION['_user'] ?? null);
  46. self::assertSame('admin', $_SESSION['_user']['role']);
  47. }
  48. public function testWrongPasswordRedirectsBackToLoginWithFlash(): void
  49. {
  50. $this->request('GET', '/login');
  51. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  52. $body = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'WRONG']);
  53. $response = $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  54. self::assertSame(303, $response->getStatusCode());
  55. self::assertSame('/login', $response->getHeaderLine('Location'));
  56. $flash = $_SESSION['_flash'] ?? [];
  57. self::assertNotEmpty($flash);
  58. self::assertSame('error', $flash[0]['type']);
  59. }
  60. public function testWrongUsernameAlsoRecordsFailure(): void
  61. {
  62. $this->request('GET', '/login');
  63. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  64. $body = http_build_query(['csrf_token' => $token, 'username' => 'someone', 'password' => 'test1234']);
  65. $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  66. // Failure recorded on the LoginThrottle (not in the session).
  67. // Five more attempts from the same IP for the same bogus user
  68. // would lock the bucket; one shot doesn't, so we just verify
  69. // the next attempt isn't locked yet.
  70. /** @var LoginThrottle $throttle */
  71. $throttle = $this->container->get(LoginThrottle::class);
  72. self::assertFalse($throttle->isLocked('someone', ''));
  73. }
  74. public function testCsrfMissingIs403(): void
  75. {
  76. $this->request('GET', '/login');
  77. $body = http_build_query(['username' => 'admin', 'password' => 'test1234']);
  78. $response = $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  79. self::assertSame(403, $response->getStatusCode());
  80. }
  81. public function testFiveFailuresLockOutNextAttempt(): void
  82. {
  83. $this->request('GET', '/login');
  84. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  85. $bad = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'WRONG']);
  86. for ($i = 0; $i < 5; ++$i) {
  87. $this->request('POST', '/login/local', [], $bad, 'application/x-www-form-urlencoded');
  88. }
  89. // 6th attempt — even with correct credentials — gets the lockout flash.
  90. $this->enqueueApiResponse(200, [
  91. 'user_id' => 1, 'role' => 'admin', 'email' => null, 'display_name' => 'Local Admin', 'is_local' => true,
  92. ]);
  93. $good = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'test1234']);
  94. $response = $this->request('POST', '/login/local', [], $good, 'application/x-www-form-urlencoded');
  95. self::assertSame(303, $response->getStatusCode());
  96. self::assertSame('/login', $response->getHeaderLine('Location'));
  97. $flash = $_SESSION['_flash'] ?? [];
  98. self::assertNotEmpty($flash);
  99. self::assertStringContainsStringIgnoringCase('too many', $flash[0]['message']);
  100. }
  101. public function testRotatingXForwardedForDoesNotEvadePerIpLockout(): void
  102. {
  103. // SEC_REVIEW F1: an attacker spoofing X-Forwarded-For per request
  104. // must NOT mint a fresh throttle bucket. The per-IP bucket is keyed
  105. // on REMOTE_ADDR, which is constant here — so 5 failures should
  106. // still trip the lockout regardless of XFF rotation.
  107. $this->request('GET', '/login');
  108. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  109. $bad = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'WRONG']);
  110. for ($i = 0; $i < 5; ++$i) {
  111. $this->request(
  112. 'POST',
  113. '/login/local',
  114. ['X-Forwarded-For' => '203.0.113.' . $i],
  115. $bad,
  116. 'application/x-www-form-urlencoded',
  117. ['REMOTE_ADDR' => '198.51.100.7'],
  118. );
  119. }
  120. // 6th attempt with correct credentials but a fresh XFF still gets the
  121. // lockout flash because the per-(user, REMOTE_ADDR) bucket is full.
  122. $this->enqueueApiResponse(200, [
  123. 'user_id' => 1, 'role' => 'admin', 'email' => null, 'display_name' => 'Local Admin', 'is_local' => true,
  124. ]);
  125. $good = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'test1234']);
  126. $response = $this->request(
  127. 'POST',
  128. '/login/local',
  129. ['X-Forwarded-For' => '203.0.113.99'],
  130. $good,
  131. 'application/x-www-form-urlencoded',
  132. ['REMOTE_ADDR' => '198.51.100.7'],
  133. );
  134. self::assertSame(303, $response->getStatusCode());
  135. self::assertSame('/login', $response->getHeaderLine('Location'));
  136. $flash = $_SESSION['_flash'] ?? [];
  137. self::assertNotEmpty($flash);
  138. self::assertStringContainsStringIgnoringCase('too many', $flash[0]['message']);
  139. }
  140. public function testRotatingRemoteAddrEventuallyHitsPerUsernameLockout(): void
  141. {
  142. // SEC_REVIEW F2: even with proper REMOTE_ADDR, an attacker on a
  143. // residential proxy pool gets a fresh per-IP bucket per request.
  144. // The per-username (cross-IP) bucket must catch this at 25 failures.
  145. $this->request('GET', '/login');
  146. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  147. $bad = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'WRONG']);
  148. for ($i = 0; $i < 25; ++$i) {
  149. $this->request(
  150. 'POST',
  151. '/login/local',
  152. [],
  153. $bad,
  154. 'application/x-www-form-urlencoded',
  155. ['REMOTE_ADDR' => '198.51.100.' . $i],
  156. );
  157. }
  158. // Next attempt from yet another IP — even with correct credentials —
  159. // gets the lockout flash because the per-username bucket is full.
  160. $this->enqueueApiResponse(200, [
  161. 'user_id' => 1, 'role' => 'admin', 'email' => null, 'display_name' => 'Local Admin', 'is_local' => true,
  162. ]);
  163. $good = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'test1234']);
  164. $response = $this->request(
  165. 'POST',
  166. '/login/local',
  167. [],
  168. $good,
  169. 'application/x-www-form-urlencoded',
  170. ['REMOTE_ADDR' => '198.51.100.250'],
  171. );
  172. self::assertSame(303, $response->getStatusCode());
  173. self::assertSame('/login', $response->getHeaderLine('Location'));
  174. $flash = $_SESSION['_flash'] ?? [];
  175. self::assertNotEmpty($flash);
  176. self::assertStringContainsStringIgnoringCase('too many', $flash[0]['message']);
  177. }
  178. public function testFailuresArePersistedToConfiguredFilePath(): void
  179. {
  180. // SEC_REVIEW F6: confirm production wiring writes throttle state
  181. // to the on-disk path (so a worker recycle cannot wipe it). Drives
  182. // a failure through the full Slim stack, then asserts the file
  183. // exists and decodes to the expected per-IP/per-username buckets.
  184. // The cross-instance persistence guarantee itself is covered in
  185. // FileThrottleStoreTest.
  186. $this->request('GET', '/login');
  187. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  188. $bad = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'WRONG']);
  189. $this->request('POST', '/login/local', [], $bad, 'application/x-www-form-urlencoded');
  190. /** @var \DI\Container $c */
  191. $c = $this->container;
  192. $path = (string) $c->get('settings.login_throttle_path');
  193. self::assertFileExists($path);
  194. $decoded = json_decode((string) file_get_contents($path), true);
  195. self::assertIsArray($decoded);
  196. self::assertArrayHasKey('ip', $decoded);
  197. self::assertArrayHasKey('username', $decoded);
  198. self::assertArrayHasKey('admin', $decoded['username']);
  199. self::assertSame(1, $decoded['username']['admin']['count']);
  200. // And the live LoginThrottle (built via the container) sees that
  201. // count too — i.e., it shares the same store.
  202. /** @var LoginThrottle $live */
  203. $live = $c->get(LoginThrottle::class);
  204. self::assertFalse($live->isLocked('admin', '0.0.0.0'));
  205. // Top up to the per-IP threshold — uses the SAME file the prior
  206. // request wrote to.
  207. for ($i = 0; $i < 4; ++$i) {
  208. $live->recordFailure('admin', '');
  209. }
  210. self::assertTrue($live->isLocked('admin', ''));
  211. }
  212. public function testApiDownDuringUpsertFlashesError(): void
  213. {
  214. $this->enqueueApiException(new \GuzzleHttp\Exception\ConnectException(
  215. 'connection refused',
  216. new \GuzzleHttp\Psr7\Request('POST', '/api/v1/auth/users/upsert-local'),
  217. ));
  218. $this->enqueueApiException(new \GuzzleHttp\Exception\ConnectException(
  219. 'connection refused',
  220. new \GuzzleHttp\Psr7\Request('POST', '/api/v1/auth/users/upsert-local'),
  221. ));
  222. $this->request('GET', '/login');
  223. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  224. $body = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'test1234']);
  225. $response = $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  226. self::assertSame(303, $response->getStatusCode());
  227. self::assertSame('/login', $response->getHeaderLine('Location'));
  228. $flash = $_SESSION['_flash'] ?? [];
  229. self::assertNotEmpty($flash);
  230. self::assertStringContainsStringIgnoringCase('api', $flash[0]['message']);
  231. }
  232. }