LocalLoginTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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 testWrongUsernameTriggersPasswordVerify(): void
  75. {
  76. // SEC_REVIEW F7: a wrong username must still go through
  77. // password_verify against an Argon2id hash, so timing does not
  78. // distinguish "username matches" from "username does not match".
  79. // We assert a generous lower bound (10 ms) — well above the
  80. // microsecond cost of a path that skips password_verify, and
  81. // well below PHP's default PASSWORD_ARGON2ID cost (~50 ms+).
  82. $this->request('GET', '/login');
  83. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  84. $body = http_build_query([
  85. 'csrf_token' => $token,
  86. 'username' => 'definitely_not_the_admin',
  87. 'password' => 'irrelevant',
  88. ]);
  89. $start = hrtime(true);
  90. $response = $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  91. $elapsedNs = hrtime(true) - $start;
  92. self::assertSame(303, $response->getStatusCode());
  93. self::assertGreaterThan(
  94. 10_000_000,
  95. $elapsedNs,
  96. 'wrong-username path took <10ms; password_verify likely skipped (F7 regression)',
  97. );
  98. }
  99. public function testUnconfiguredLocalPasswordHashStillRunsPasswordVerify(): void
  100. {
  101. // SEC_REVIEW F7 defence-in-depth: even when LOCAL_ADMIN_PASSWORD_HASH
  102. // is empty, the login path must run password_verify against the
  103. // dummy Argon2id hash so an attacker cannot probe whether a local
  104. // admin password is configured by timing.
  105. $this->bootApp(['local_admin_password_hash' => '']);
  106. $this->request('GET', '/login');
  107. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  108. $body = http_build_query([
  109. 'csrf_token' => $token,
  110. 'username' => 'admin',
  111. 'password' => 'whatever',
  112. ]);
  113. $start = hrtime(true);
  114. $response = $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  115. $elapsedNs = hrtime(true) - $start;
  116. self::assertSame(303, $response->getStatusCode());
  117. self::assertSame('/login', $response->getHeaderLine('Location'));
  118. self::assertGreaterThan(
  119. 10_000_000,
  120. $elapsedNs,
  121. 'unconfigured-hash path took <10ms; dummy password_verify likely skipped (F7 regression)',
  122. );
  123. }
  124. public function testDisabledLocalAdminRecordsThrottleFailure(): void
  125. {
  126. // SEC_REVIEW F38: hitting POST /login/local when local-admin is
  127. // disabled returned 404 with no rate-limit. An attacker could
  128. // burn worker threads on this URL. The throttle must accumulate
  129. // a per-IP failure for each disabled-path hit so the existing
  130. // 5/10/15 ladder applies even on the disabled URL.
  131. $this->bootApp(['local_admin_enabled' => false]);
  132. $this->request('GET', '/login');
  133. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  134. $body = http_build_query([
  135. 'csrf_token' => $token,
  136. 'username' => 'whatever',
  137. 'password' => 'spam',
  138. ]);
  139. $response = $this->request(
  140. 'POST',
  141. '/login/local',
  142. [],
  143. $body,
  144. 'application/x-www-form-urlencoded',
  145. ['REMOTE_ADDR' => '198.51.100.50'],
  146. );
  147. self::assertSame(404, $response->getStatusCode());
  148. // The per-IP bucket key is `('', source_ip)` — independent of the
  149. // attacker's submitted username so a rotating-username spray from
  150. // one IP all lands in the same bucket.
  151. /** @var LoginThrottle $throttle */
  152. $throttle = $this->container->get(LoginThrottle::class);
  153. self::assertFalse($throttle->isLocked('', '198.51.100.50'));
  154. // Five hits should trip the lockout regardless of submitted username.
  155. for ($i = 0; $i < 4; ++$i) {
  156. $varied = http_build_query([
  157. 'csrf_token' => $token,
  158. 'username' => 'rotating-' . $i,
  159. 'password' => 'spam',
  160. ]);
  161. $this->request(
  162. 'POST',
  163. '/login/local',
  164. [],
  165. $varied,
  166. 'application/x-www-form-urlencoded',
  167. ['REMOTE_ADDR' => '198.51.100.50'],
  168. );
  169. }
  170. self::assertTrue($throttle->isLocked('', '198.51.100.50'));
  171. }
  172. public function testDisabledLocalAdminLockedHitDoesNotIncrementBucket(): void
  173. {
  174. // After lockout, additional hammering must not grow the bucket
  175. // unbounded — we want the throttle file size + decode cost to be
  176. // bounded by the lockout ladder, not by attacker request volume.
  177. $this->bootApp(['local_admin_enabled' => false]);
  178. $this->request('GET', '/login');
  179. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  180. $body = http_build_query([
  181. 'csrf_token' => $token,
  182. 'username' => 'x',
  183. 'password' => 'x',
  184. ]);
  185. for ($i = 0; $i < 5; ++$i) {
  186. $this->request(
  187. 'POST',
  188. '/login/local',
  189. [],
  190. $body,
  191. 'application/x-www-form-urlencoded',
  192. ['REMOTE_ADDR' => '198.51.100.51'],
  193. );
  194. }
  195. /** @var LoginThrottle $throttle */
  196. $throttle = $this->container->get(LoginThrottle::class);
  197. self::assertTrue($throttle->isLocked('', '198.51.100.51'));
  198. $remainingBefore = $throttle->lockoutSecondsRemaining('', '198.51.100.51');
  199. // 50 more hits while locked — must not extend or escalate the lockout.
  200. for ($i = 0; $i < 50; ++$i) {
  201. $this->request(
  202. 'POST',
  203. '/login/local',
  204. [],
  205. $body,
  206. 'application/x-www-form-urlencoded',
  207. ['REMOTE_ADDR' => '198.51.100.51'],
  208. );
  209. }
  210. $remainingAfter = $throttle->lockoutSecondsRemaining('', '198.51.100.51');
  211. // Allow ±1s for clock drift across the calls.
  212. self::assertLessThanOrEqual($remainingBefore + 1, $remainingAfter);
  213. }
  214. public function testCsrfTokenIsRotatedAcrossLoginPrivilegeBoundary(): void
  215. {
  216. // SEC_REVIEW F40: a CSRF token minted on the pre-auth login form
  217. // must NOT carry over after a successful login. `regenerateId()`
  218. // now drops `_csrf` so the next protected GET mints a fresh
  219. // token. An attacker who scraped the pre-auth token (Referer,
  220. // sub-resource leak) cannot replay it post-auth.
  221. $this->enqueueApiResponse(200, [
  222. 'user_id' => 1,
  223. 'role' => 'admin',
  224. 'email' => null,
  225. 'display_name' => 'Local Admin',
  226. 'is_local' => true,
  227. ]);
  228. $this->request('GET', '/login');
  229. $preAuthToken = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  230. self::assertNotEmpty($preAuthToken);
  231. $body = http_build_query([
  232. 'csrf_token' => $preAuthToken,
  233. 'username' => 'admin',
  234. 'password' => 'test1234',
  235. ]);
  236. $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  237. // After the redirect-to-dashboard the session has been
  238. // privilege-elevated; the slot is gone and the next safe
  239. // request through CsrfMiddleware mints a fresh token.
  240. self::assertArrayNotHasKey(CsrfMiddleware::SESSION_KEY, $_SESSION);
  241. }
  242. public function testCsrfMissingIs403(): void
  243. {
  244. $this->request('GET', '/login');
  245. $body = http_build_query(['username' => 'admin', 'password' => 'test1234']);
  246. $response = $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  247. self::assertSame(403, $response->getStatusCode());
  248. }
  249. public function testFiveFailuresLockOutNextAttempt(): void
  250. {
  251. $this->request('GET', '/login');
  252. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  253. $bad = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'WRONG']);
  254. for ($i = 0; $i < 5; ++$i) {
  255. $this->request('POST', '/login/local', [], $bad, 'application/x-www-form-urlencoded');
  256. }
  257. // 6th attempt — even with correct credentials — gets the lockout flash.
  258. $this->enqueueApiResponse(200, [
  259. 'user_id' => 1, 'role' => 'admin', 'email' => null, 'display_name' => 'Local Admin', 'is_local' => true,
  260. ]);
  261. $good = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'test1234']);
  262. $response = $this->request('POST', '/login/local', [], $good, 'application/x-www-form-urlencoded');
  263. self::assertSame(303, $response->getStatusCode());
  264. self::assertSame('/login', $response->getHeaderLine('Location'));
  265. $flash = $_SESSION['_flash'] ?? [];
  266. self::assertNotEmpty($flash);
  267. self::assertStringContainsStringIgnoringCase('too many', $flash[0]['message']);
  268. }
  269. public function testRotatingXForwardedForDoesNotEvadePerIpLockout(): void
  270. {
  271. // SEC_REVIEW F1: an attacker spoofing X-Forwarded-For per request
  272. // must NOT mint a fresh throttle bucket. The per-IP bucket is keyed
  273. // on REMOTE_ADDR, which is constant here — so 5 failures should
  274. // still trip the lockout regardless of XFF rotation.
  275. $this->request('GET', '/login');
  276. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  277. $bad = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'WRONG']);
  278. for ($i = 0; $i < 5; ++$i) {
  279. $this->request(
  280. 'POST',
  281. '/login/local',
  282. ['X-Forwarded-For' => '203.0.113.' . $i],
  283. $bad,
  284. 'application/x-www-form-urlencoded',
  285. ['REMOTE_ADDR' => '198.51.100.7'],
  286. );
  287. }
  288. // 6th attempt with correct credentials but a fresh XFF still gets the
  289. // lockout flash because the per-(user, REMOTE_ADDR) bucket is full.
  290. $this->enqueueApiResponse(200, [
  291. 'user_id' => 1, 'role' => 'admin', 'email' => null, 'display_name' => 'Local Admin', 'is_local' => true,
  292. ]);
  293. $good = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'test1234']);
  294. $response = $this->request(
  295. 'POST',
  296. '/login/local',
  297. ['X-Forwarded-For' => '203.0.113.99'],
  298. $good,
  299. 'application/x-www-form-urlencoded',
  300. ['REMOTE_ADDR' => '198.51.100.7'],
  301. );
  302. self::assertSame(303, $response->getStatusCode());
  303. self::assertSame('/login', $response->getHeaderLine('Location'));
  304. $flash = $_SESSION['_flash'] ?? [];
  305. self::assertNotEmpty($flash);
  306. self::assertStringContainsStringIgnoringCase('too many', $flash[0]['message']);
  307. }
  308. public function testRotatingRemoteAddrEventuallyHitsPerUsernameLockout(): void
  309. {
  310. // SEC_REVIEW F2: even with proper REMOTE_ADDR, an attacker on a
  311. // residential proxy pool gets a fresh per-IP bucket per request.
  312. // The per-username (cross-IP) bucket must catch this at 25 failures.
  313. $this->request('GET', '/login');
  314. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  315. $bad = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'WRONG']);
  316. for ($i = 0; $i < 25; ++$i) {
  317. $this->request(
  318. 'POST',
  319. '/login/local',
  320. [],
  321. $bad,
  322. 'application/x-www-form-urlencoded',
  323. ['REMOTE_ADDR' => '198.51.100.' . $i],
  324. );
  325. }
  326. // Next attempt from yet another IP — even with correct credentials —
  327. // gets the lockout flash because the per-username bucket is full.
  328. $this->enqueueApiResponse(200, [
  329. 'user_id' => 1, 'role' => 'admin', 'email' => null, 'display_name' => 'Local Admin', 'is_local' => true,
  330. ]);
  331. $good = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'test1234']);
  332. $response = $this->request(
  333. 'POST',
  334. '/login/local',
  335. [],
  336. $good,
  337. 'application/x-www-form-urlencoded',
  338. ['REMOTE_ADDR' => '198.51.100.250'],
  339. );
  340. self::assertSame(303, $response->getStatusCode());
  341. self::assertSame('/login', $response->getHeaderLine('Location'));
  342. $flash = $_SESSION['_flash'] ?? [];
  343. self::assertNotEmpty($flash);
  344. self::assertStringContainsStringIgnoringCase('too many', $flash[0]['message']);
  345. }
  346. public function testFailuresArePersistedToConfiguredFilePath(): void
  347. {
  348. // SEC_REVIEW F6: confirm production wiring writes throttle state
  349. // to the on-disk path (so a worker recycle cannot wipe it). Drives
  350. // a failure through the full Slim stack, then asserts the file
  351. // exists and decodes to the expected per-IP/per-username buckets.
  352. // The cross-instance persistence guarantee itself is covered in
  353. // FileThrottleStoreTest.
  354. $this->request('GET', '/login');
  355. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  356. $bad = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'WRONG']);
  357. $this->request('POST', '/login/local', [], $bad, 'application/x-www-form-urlencoded');
  358. /** @var \DI\Container $c */
  359. $c = $this->container;
  360. $path = (string) $c->get('settings.login_throttle_path');
  361. self::assertFileExists($path);
  362. $decoded = json_decode((string) file_get_contents($path), true);
  363. self::assertIsArray($decoded);
  364. self::assertArrayHasKey('ip', $decoded);
  365. self::assertArrayHasKey('username', $decoded);
  366. self::assertArrayHasKey('admin', $decoded['username']);
  367. self::assertSame(1, $decoded['username']['admin']['count']);
  368. // And the live LoginThrottle (built via the container) sees that
  369. // count too — i.e., it shares the same store.
  370. /** @var LoginThrottle $live */
  371. $live = $c->get(LoginThrottle::class);
  372. self::assertFalse($live->isLocked('admin', '0.0.0.0'));
  373. // Top up to the per-IP threshold — uses the SAME file the prior
  374. // request wrote to.
  375. for ($i = 0; $i < 4; ++$i) {
  376. $live->recordFailure('admin', '');
  377. }
  378. self::assertTrue($live->isLocked('admin', ''));
  379. }
  380. public function testApiDownDuringUpsertFlashesError(): void
  381. {
  382. $this->enqueueApiException(new \GuzzleHttp\Exception\ConnectException(
  383. 'connection refused',
  384. new \GuzzleHttp\Psr7\Request('POST', '/api/v1/auth/users/upsert-local'),
  385. ));
  386. $this->enqueueApiException(new \GuzzleHttp\Exception\ConnectException(
  387. 'connection refused',
  388. new \GuzzleHttp\Psr7\Request('POST', '/api/v1/auth/users/upsert-local'),
  389. ));
  390. $this->request('GET', '/login');
  391. $token = (string) ($_SESSION[CsrfMiddleware::SESSION_KEY] ?? '');
  392. $body = http_build_query(['csrf_token' => $token, 'username' => 'admin', 'password' => 'test1234']);
  393. $response = $this->request('POST', '/login/local', [], $body, 'application/x-www-form-urlencoded');
  394. self::assertSame(303, $response->getStatusCode());
  395. self::assertSame('/login', $response->getHeaderLine('Location'));
  396. $flash = $_SESSION['_flash'] ?? [];
  397. self::assertNotEmpty($flash);
  398. self::assertStringContainsStringIgnoringCase('api', $flash[0]['message']);
  399. }
  400. }