Repository navigation
fix: scope staff PIN lockouts per client IP and make JWT invalidation deterministic - #1510
Conversation
|
ⓘ Your Qodo trial ends soon. Ask your workspace admin to set up billing to keep reviews running after the trial. Manage billing |
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
|
PR Summary by QodoScope PIN lockouts per client IP and make JWT invalidation deterministic
AI Description
Diagram
High-Level Assessment
Files changed (13)
|
Code Review by Qodo
1. Old sessions survive a backward clock jump
|
…dation deterministic
…ackup restore staleness
…ss Shards 1, 2, and 3 (which cascaded to End-to-End Playwright Tests and Required Checks Gate): 1. Invariant: Any JWT minted after a user's credential update (`tokens_valid_after`) must carry a monotonic sub-second `iat` strictly greater than `tokens_valid_after`, and any E2E test that updates a user's credentials (`PUT /api/staff/:id`) must not reuse a token minted for that user prior to the credential update. 2. Root causes and fixes: - `frontend/e2e/helpers/test-auth.ts` (`getE2eToken`): The shared E2E test JWT helper still stamped `iat: Math.floor(Date.now() / 1000)` (an integer second). Under the deterministic `isTokenStale` check (`Math.floor(iat) <= Math.floor(validAfterMs / 1000)` for integer `iat`), any `getE2eToken()` call within the same UTC second after a credential update (such as `ops-admin/order-history.spec.ts` after `dashboard-reconciliation.spec.ts` cleared the owner PIN, `ops-admin/staff-roles.spec.ts` after `orders-lifecycle.spec.ts` cleared the owner PIN, and `approval-manager-selection.spec.ts` `afterEach`) was rejected as stale with `401`. Updated `getE2eToken()` to stamp a monotonic sub-second `iat` (`nextE2eTokenIatSeconds()`). - `frontend/e2e/pin-quick-switch.spec.ts`: `test.beforeAll` minted `ownerToken` / `ownerHeaders`, updated `e2e-owner`'s PIN via `PUT /api/staff/e2e-owner` (which updates `tokens_valid_after` and invalidates prior tokens for `e2e-owner`), and then reused the pre-change `ownerHeaders` on subsequent `/api/staff` and cleanup requests. Refreshed `ownerToken` and `ownerHeaders` immediately after the owner PIN update. 3. Verification: - `npm run build` (passed) - `npm run build:frontend` (passed) - `npm run lint` (passed, 0 errors) - `npx playwright test --config=playwright.config.ts --project=chromium --retries=0 e2e/pin-quick-switch.spec.ts e2e/approval-manager-selection.spec.ts e2e/ops-admin/dashboard-reconciliation.spec.ts e2e/ops-admin/order-history.spec.ts e2e/ops-admin/orders-lifecycle.spec.ts e2e/ops-admin/staff-roles.spec.ts --grep-invert='FINDING:|a paid order offers a refund, and issuing it settles against the recorded sale'` (29 passed, 0 failed) - `npx playwright test --config=playwright.config.ts --project=chromium --retries=0 e2e/pin-quick-switch.spec.ts` (1 passed on fresh server) - `npx ts-node --transpile-only -P tests/tsconfig.json tests/jwt-logout-lifecycle.test.ts` (passed) - `npx ts-node --transpile-only -P tests/tsconfig.json tests/pin-quick-switch.test.ts` (passed) - `git diff --check` (passed)
…Validation (Linux)` (which cascaded to `Required Checks Gate`): 1. **Invariant**: Every JS/TS file touched by the pull request must pass Oxfmt whole-file formatting verification (`scripts/ci/check-changed-format.cjs`). 2. **Root cause & fix**: - [test-auth.ts](file:///Users/gurkiratkhaira/.no-mistakes/worktrees/de2296f2e6f8/01M6WH5SC31ZK3NKWMJR7GZCKG/frontend/e2e/helpers/test-auth.ts): In the previous round, `frontend/e2e/helpers/test-auth.ts` was modified without running `oxfmt --write` across the full file, leaving unformatted lines in `ordersLayoutToken` and `setLanguage` that failed `node scripts/ci/check-changed-format.cjs`. Formatted `frontend/e2e/helpers/test-auth.ts` with `oxfmt`. 3. **Verification**: - `node scripts/ci/check-changed-format.cjs --base 7726206` (passed across all 11 root and 2 frontend changed files) - `npm run lint` (passed, 0 errors) - `npm run lint:budget` (passed) - `git diff --check` (passed)
3a240f2 to
e4d9f82
Compare
Intent
Proceed with sequential local implementation Batch 2 (Items 4 and 6 of the revised security plan), subject to these decisions and acceptance criteria:
(client IP, user ID), retaining the existing five-attempt/15-minute policy and per-account verification serialization.iatplus<=completely settles ordering.npm run lint,npm run build, andnpm test; run browser/Electron checks for affected flows and documentation checks for changed contracts. Report fresh results, limitations, final diff, and git status.What Changed
POST /api/auth/pin-loginfailed-attempt lockouts to(normalized client IP, user ID)vianormalizeIpForPinRateLimit(collapsing non-loopback IPv6 addresses to/64while keeping individual IPv4 addresses and loopback in their own buckets), removing the install-wide per-account lockout counter while retaining per-user verification serialization (withPinVerificationLock), endpoint volume limits, andauth_audit_logattribution.nextAuthTimestampMs,nextTokenIatSeconds, andnextTokensValidAfterTimestamp) bounded above persistedusers.tokens_valid_aftervalues, stamping sub-secondiatclaims across all seven JWT issuers (:3001login/PIN-login/tenant-select/refresh/setup,:3002KDS login, and:3003Server App login) and updatingisTokenStaleto compare sub-secondiatagainsttokens_valid_afterwith<=at millisecond resolution (and integeriator whole-second timestamps with<=at second resolution).Risk Assessment
✅ Low: The changes cleanly scope staff PIN lockouts per client IP (with IPv6 /64 prefix normalization and per-account verification serialization) and enforce deterministic sub-second JWT invalidation across all seven issuers using a shared monotonic clock backed by persisted tokens_valid_after state.
Testing
Kept the normalizeIpForRateLimit import in main/routes/auth.ts and ran the targeted regression test suites (jwt-logout-lifecycle, pin-quick-switch, issue-127-password-recovery, master-pin, restore-from-file-api, and cloud-password-recovery) against live Express API, standalone KDS, and Server App instances backed by isolated SQLite databases; all scenarios passed and execution logs were captured in the evidence directory. No UI layout or visual surface was changed in this backend security and auth lifecycle update.
Evidence: Targeted PIN Lockout & JWT Token Lifecycle Regression Log
Step 4 (tests/pin-quick-switch.test.ts): ✓ Concurrent wrong-PIN burst allows exactly 5 attempts (401) before lockout ✓ Remaining 3 concurrent wrong-PIN requests receive 429 rate-limited ✓ Client IP A attempt 1..5 (10.10.0.1) returns 401 and decrements attempts_remaining (4..0) ✓ 6th attempt from client IP A (10.10.0.1) returns 429 for (IP A, userId) ✓ Audit log records rate_limited on 6th attempt from client IP A (10.10.0.1) ✓ Client IP B (10.10.0.2) can still authenticate as cashierId with valid PIN while IP A is locked out ✓ Loopback (127.0.0.1) can still authenticate as cashierId while client IP A is locked out ✓ Loopback (127.0.0.1) is subject to the same 5-attempt lockout on its own IP ✓ 6th attempt from another SLAAC address in the same IPv6 /64 (2001:db8:1:2::30) is locked out with 429 ✓ Client on a different IPv6 /64 prefix (2001:db8:1:3::10) is not locked out and can authenticate Step 10 (tests/jwt-logout-lifecycle.test.ts): ✓ Legacy integer-second iat token minted in the same UTC second as tokens_valid_after is stale ✓ Legacy integer-second iat token minted in a later UTC second than tokens_valid_after is valid ✓ Pre-change token is rejected immediately in the same frozen millisecond after password change ✓ Fresh post-change login token in the same frozen millisecond is accepted by /api/auth/me ✓ Pre-change token is rejected after credential change even when Date.now() jumped backward by > 10s ✓ Fresh login token after simulated process restart is accepted by /api/auth/me ✓ Issuers 1-7 (login, pin-login, tenants/select, refresh, setup/initialize, server-app login, kds-server login) stamp sub-second iat, reject pre-change tokens in the same frozen millisecond after credential change, and accept fresh post-change tokensPipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
✅ **Review** - passed
✅ No issues found.
🔧 **Test** - 2 issues found → no changes applied ✅
git -C ~/.no-mistakes/worktrees/de2296f2e6f8/01M6WNAPBRXTBM0AP027X1PY18 statusandgit -C ~/.no-mistakes/worktrees/de2296f2e6f8/01M6WNAPBRXTBM0AP027X1PY18 diff). Respond with fix to validate it, or abort.🔧 No changes applied.
✅ Re-checked - no issues remain.
node tests/run-electron-node-test.cjs tests/jwt-logout-lifecycle.test.tsnode tests/run-electron-node-test.cjs tests/pin-quick-switch.test.tsnode tests/run-electron-node-test.cjs tests/issue-127-password-recovery.test.tsnode tests/run-electron-node-test.cjs tests/master-pin.test.tsnode tests/run-electron-node-test.cjs tests/restore-from-file-api.test.tsnode tests/run-electron-node-test.cjs tests/cloud-password-recovery.test.ts✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.