Skip to content

Reject sessions issued before a password change - #301

Merged
BaaRai merged 5 commits into
BaaRai:mainfrom
Protonmatter:protonmatter/session-epoch
Nov 17, 2026
Merged

BaaRai merged 5 commits into
BaaRai:mainfrom
Protonmatter:protonmatter/session-epoch

Conversation

@Protonmatter

Copy link
Copy Markdown
Contributor

Summary

  • Split from Keep control-plane state correct when a write or a restart fails #293.
  • A session issued before auth.password_epoch is rejected. A deployment that has never stored an epoch still accepts existing tokens.
  • A failed password read no longer mints a setup token.
  • Filling the login limiter no longer evicts an active lockout.
  • A same-origin event stream sends the cookie and does not put the token in the query string.

Test plan

  • Change the password and confirm the previous token is rejected while a newly issued token works.
  • With the password-hash read failing, confirm startup does not log a setup token.
  • Fill the login limiter with a blocked IP and confirm that IP stays blocked.

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks for taking on session revocation, it is the right fix for a compromise scenario. The epoch check in verifyJWT is right (server/auth.go:186). In the four cases below, sessions that a password change should revoke keep working.

1. reset-password.sh does not write the epoch. The script only upserts auth.password_hash (reset-password.sh:143-150), then says no restart is needed (reset-password.sh:239). Sessions issued before an emergency reset stay valid. The running server would not see a new epoch anyway, because loadPwdEpoch reads it only at startup (server/auth.go:309-327).
Fix: have the script write auth.password_epoch in the same SQL, and either reload the epoch at runtime or tell the operator to restart.

2. Same-second gap. The epoch is stored in whole seconds (server/auth.go:336) and a token is rejected only when IssuedAt < notBefore (server/auth.go:186). A token issued earlier in the same second as the change still passes, as the comment at server/auth.go:329-330 admits.
Fix: use a cutoff that cannot tie, for example a password version claim in the token compared with a stored counter, or a finer-grained timestamp.

3. Both the write and the read fail open.

  • Write: if SetSetting(authPwdEpochKey, ...) fails, bumpPwdEpoch only logs and returns without updating the in-memory epoch (server/auth.go:337-340), and the handler still answers 200 {"ok":true} (server/auth.go:466-467).
  • Read: any GetSetting error in loadPwdEpoch is logged and returns (server/auth.go:313-316). The cutoff stays at 0, which accepts every valid token, for the whole life of the process. initSetupToken retries 3 times (server/auth.go:278-291); the epoch read does not retry.

Fix: write the hash and the epoch in one transaction and return 500 on failure. At startup, retry the read, then refuse to start or reject tokens until the epoch is known.

4. Open SSE streams survive the change. The token is checked once, when the request enters requireAuth (server/auth.go:244). For example, streamLogs and streamActivity then loop until the client disconnects (server/server.go:3185-3202 and 3280 onward), so a stream opened with a revoked token keeps receiving data.
Fix: close the streams on a password change, or document that revocation applies to new requests only. Re-checking the token on each ping is another option.

Please add handler-level tests: change the password with token A, then A gets 401 on requireAuth and a fresh login passes; plus one test per failure path in point 3.

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks, this is the right shape: a monotonic version in the same transaction as the hash, reset-password.sh writing it and asking for a restart, and the three streams re-checking the token.

One gap remains: loadPasswordVersion runs only at startup. If that read still fails after the three attempts, pwdState stays unreadable for the life of the process: requireAuth rejects every session, login answers 503, and nothing ever re-reads the version. The instance is locked until a restart, and the CHANGELOG sentence "until a read succeeds" is not true today. Please either retry the read on login when the state is unreadable (fail closed, but self-heal when the database comes back), or change the sentence to "until the process restarts".

@BaaRai BaaRai left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks, the retry on login is the self-heal path: a failed startup read no longer locks the instance until a restart, and the wording now matches what the code does. Approved.

A failed password read no longer mints a setup token, and filling the login limiter no longer drops an active lockout.
…ne second.

The hash and version commit together, a failed read rejects tokens, and the reset script tells the operator to restart.
@BaaRai
BaaRai force-pushed the protonmatter/session-epoch branch from bab334e to 134562c Compare November 17, 2026 21:54
…tter/session-epoch

# Conflicts:
#	CHANGELOG.md
@Protonmatter

Copy link
Copy Markdown
Contributor Author

Let me know if this is good to merge.

@BaaRai
BaaRai merged commit e80a33c into BaaRai:main Nov 17, 2026
5 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