Skip to content

fix(auth): delete auth tokens past their expiration date - #66625

Open
SKroman108 wants to merge 1 commit into
nextcloud:masterfrom
SKroman108:fix/delete-expired-auth-tokens
Open

SKroman108 wants to merge 1 commit into
nextcloud:masterfrom
SKroman108:fix/delete-expired-auth-tokens

Conversation

@SKroman108

Copy link
Copy Markdown

Summary

Fixes #61833.

Auth tokens with an explicit expires timestamp are rejected by PublicKeyTokenProvider::getToken() once it has passed, but invalidateOldTokens() only looks at last_activity. A PERMANENT_TOKEN with an expiry therefore stays in oc_authtoken, and in Settings → Security → Devices & sessions, for token_auth_token_retention (one year by default).

The Passwords app creates such a token for every web UI session. On my instance (Nextcloud 35.0.2) this left 310 "Passwords Session …" entries for one user, all expired. 306 of them expired more than a week ago.

Change

  • PublicKeyTokenMapper::invalidateExpired(int $expiredBefore) deletes tokens with expires > 0 that expired before the given time. It skips WIPE_TOKEN, which already has its own retention (token_auth_wipe_token_retention).
  • invalidateOldTokens(), which already runs from the hourly clean-up job, calls it with a grace period from the new system config token_auth_expired_token_retention (default 7 days). The grace period keeps the current behaviour from Session::isTokenPassword(): a client still sending a recently expired token gets a quiet rejection, not a failed login for brute-force protection.
  • Tokens without an expiry (expires NULL/0) are not affected.

TODO

  • Document token_auth_expired_token_retention next to the other token_auth_* options in the admin docs, if wanted

Checklist

  • Code is properly formatted
  • Sign-off message is added to all commits
  • Tests (unit, integration, api and/or acceptance) are included: a mapper test with expired/recent/valid/wipe tokens and an updated provider test
  • Screenshots before/after for front-end changes — not applicable
  • Documentation (manuals or wiki) has been updated or is not required
  • Backports requested where applicable (ex: critical bugfixes)

I only checked syntax locally (php -l) and ran the delete condition as a read-only SELECT on a real instance. The unit tests will run in CI.

🤖 Generated with Claude Code

Tokens with an explicit expiration date are rejected once it has passed,
but invalidateOldTokens() only looks at last_activity, so a permanent
token with an expiry stays in oc_authtoken and in Devices & sessions for
a year. Apps that create short-lived tokens (e.g. Passwords web UI
sessions) pile up hundreds of dead entries.

Delete them in the hourly clean-up once they expired more than
token_auth_expired_token_retention seconds ago (default 7 days). The
grace period keeps the existing behaviour that a client still sending an
expired token is rejected quietly instead of counting as a failed login.
Remote wipe tokens keep their own retention.

Fixes nextcloud#61833

Signed-off-by: SKroman108 <138336203+SKroman108@users.noreply.github.com>
@SKroman108
SKroman108 requested review from a team as code owners November 14, 2026 23:07
@SKroman108
SKroman108 requested review from artonge, come-nc and provokateurin and removed request for a team November 14, 2026 23:07

This branch has not been deployed

No deployments
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.

[Bug]: Explicitly expired permanent auth tokens remain listed and retained

1 participant