Skip to content

fix(auth): JWT revocation on logout (#187) - #1384

Merged
vybe merged 1 commit into
devfrom
feature/187-jwt-revocation
Jun 30, 2026
Merged

vybe merged 1 commit into
devfrom
feature/187-jwt-revocation

Conversation

@dolho

@dolho dolho commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes the pentest 3.3.4 gap: 7-day JWTs had no server-side revocation and there was no logout endpoint — an exfiltrated token kept working through logout / password change for its full life.

  • Every access token now carries a random jti (create_access_token).
  • New POST /api/auth/logout blacklists the caller's jti in Redis with TTL = the token's remaining life (self-expiring, no sweep). Idempotent; MCP-key callers are a no-op.
  • get_current_user, decode_token (WebSocket), and /api/auth/validate (nginx auth_request) all reject a revoked jti.
  • Frontend store.logout() calls the endpoint best-effort before clearing local state.

Fail-open by design — Redis down or a legacy no-jti token → treated as not revoked, so the check can never lock out a valid session; a backend restart still rotates SECRET_KEY and invalidates everything.

Out of scope (deferred, separate issue)

Reducing ACCESS_TOKEN_EXPIRE_MINUTES (7d) + a refresh-token flow — a larger UX change. This PR delivers the revocation mechanism the issue's core asks for.

Changes

  • src/backend/dependencies.py — jti claim, revoke_token_jti / is_token_revoked (fail-open Redis), revocation checks in get_current_user + decode_token
  • src/backend/routers/auth.py — POST /api/auth/logout; jti check in /api/auth/validate
  • src/frontend/src/stores/auth.js — async logout() calls the revoke endpoint first
  • docs/memory/architecture.md, tests/unit/test_187_jwt_revocation.py

Test Plan

  • Unit: pytest tests/unit/test_187_jwt_revocation.py — 6 pass (jti minted, revoke→revoked, TTL ≤ token life, past-exp no-op, fail-open on no-jti + Redis-down)
  • Live e2e: login → /api/users/me 200 → POST /api/auth/logout 200 → same token /api/users/me 401 AND /api/auth/validate 401
  • Existing auth tests unaffected (test_connector_auth green)

Related to #187 (epic #1054 Security Hardening)

Pentest 3.3.4: 7-day JWTs had no server-side revocation — an exfiltrated
token survived logout/password change for the full token life; there was no
logout endpoint at all.

- Every access token now carries a random `jti` (create_access_token).
- New `POST /api/auth/logout` blacklists the caller's `jti` in Redis with a
  TTL = the token's remaining life (self-expiring, no sweep). Idempotent;
  MCP-key callers are a no-op (keys revoke via key management).
- get_current_user, decode_token (WS), and /api/auth/validate (nginx) all
  reject a revoked jti.
- Frontend store.logout() calls the endpoint best-effort before clearing
  local state.

Fail-open by design (Redis down or a legacy no-jti token → not revoked), so
the check can never lock out a valid session; backend restart still rotates
SECRET_KEY. Token-lifetime reduction + refresh tokens left as a separate issue.

Related to #187

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@dolho
dolho requested a review from vybe June 30, 2026 12:12

@vybe vybe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/validate-pr: APPROVE. JWT revocation on logout (pentest 3.3.4). jti blacklist in Redis (self-expiring TTL), enforced in get_current_user/decode_token//api/auth/validate; fail-open by design. Strong named regression test test_187_jwt_revocation.py (6 tests, all call-sites). Architecture doc updated. Security clean, mergeable, checks green. Note: 'Related to #187' not a closing keyword — bumping #187 to status-in-dev manually.

@vybe
vybe merged commit db462ad into dev Jun 30, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants