Commit Graph

274 Commits

Author SHA1 Message Date
Olivier 'reivilibre bac118ab12 Fix compat token refresh giving back a consumed token 2026-02-13 15:16:43 +00:00
Quentin Gliech 378f24b118 Clear out last active IP on each sessions after 30 days (#5448) 2026-01-26 13:57:01 +01:00
Quentin Gliech dbdb2970ed Refactor inactive IP cleanup to use pagination
This should avoid dead many dead tuples when processing batches of
sessions to cleanup
2026-01-23 18:52:33 +01:00
Quentin Gliech b0e836eb44 Cleanup finished user/browser sessions (#5444) 2026-01-23 18:27:45 +01:00
Quentin Gliech 40cb5b0094 Cleanup finished OAuth 2.0 sessions (#5443) 2026-01-23 18:23:55 +01:00
Quentin Gliech f842f33a66 Re-enable the upstream authentication sessions cleanup job (#5439) 2026-01-23 18:17:11 +01:00
Quentin Gliech 5abc7f3f69 Include pagination params in the tracing fields of cleanup methods 2026-01-23 16:30:26 +01:00
Quentin Gliech 26caee7c99 Add cleanup jobs for inactive session IP addresses
This adds three new scheduled cleanup jobs that clear the last_active_ip
field from sessions that have been inactive for more than 30 days:

- CleanupInactiveOAuth2SessionIpsJob
- CleanupInactiveCompatSessionIpsJob
- CleanupInactiveUserSessionIpsJob

This helps with data minimization by not retaining IP addresses longer
2026-01-23 16:29:29 +01:00
Quentin Gliech 5bca9726b6 Add cleanup job for finished user sessions
Implements hard deletion of user/browser sessions that have been finished for more than 30 days, but only after all child sessions are cleaned up.

User sessions can only be deleted when no child sessions exist, ensuring backchannel logout propagation continues to work correctly.
2026-01-22 15:44:57 +01:00
Quentin Gliech a66d652a70 Add cleanup job for finished OAuth2 sessions
Implements hard deletion of OAuth2 sessions that have been finished for more than 30 days, including their associated access and refresh tokens.
2026-01-22 15:44:56 +01:00
Quentin Gliech 0486c6e05d Use the user_session_id on upstream authorisations for filtering instead
of authentications

This makes it one less table to read
2026-01-21 14:49:07 +01:00
Quentin Gliech e095938da5 Only cleanup orphan upstream authorization sessions
This includes sessions that were never completed, and sessions where
user_session was cleaned up. This is to avoid breaking features like
OIDC Backchannel Logout after 30 days.
2026-01-21 12:25:42 +01:00
Quentin Gliech 39adf61089 Track user session authenticated through upstream auth sessions
This will help us avoid clearing upstream authorization sessions that
might still be useful to keep around for OIDC Backchannel Logouts
2026-01-21 12:19:05 +01:00
Quentin Gliech 0d5cc65c52 Fix foreign key constraint when cleaning up upstream OAuth 2.0 links
https://sentry.tools.element.io/organizations/element/issues/11222736/
2026-01-20 14:29:27 +01:00
Quentin Gliech 7272813925 Implement cleanup job for queue jobs
Add scheduled cleanup job that removes old completed and failed queue
jobs after 30 days. Jobs are kept for debugging purposes.

Includes migration to change the next_attempt_id FK constraint from NO
ACTION to SET NULL, allowing cleanup of retry chains without breaking
foreign key constraints.

One caveat is that cleanup is based on their creation time, *not* when
they got completed/failed. This means that if the job takes a long time
(as in, several days) to get scheduled, it might get cleared as soon as
it runs. This is fine for now, we may want to revisit this if we start
scheduling jobs far in the future
2026-01-19 12:25:04 +01:00
Quentin Gliech a721302201 Implement cleanup jobs for upstream OAuth sessions and links
Add two cleanup jobs scheduled hourly:

1. Upstream OAuth authorization sessions - removes sessions after 30 days
2. Orphaned upstream OAuth links - removes links after 7 days where user_id IS NULL. These are links created during upstream OAuth 2.0 login but never associated with a user
2026-01-19 12:24:13 +01:00
Quentin Gliech 766ff6c8f3 Implement cleanup job for email authentications
Add scheduled cleanup job that removes old user email authentications
after 7 days. Runs every hour.
2026-01-16 17:56:16 +01:00
Quentin Gliech 2ae95e30ec Implement cleanup job for user recovery sessions
Add scheduled cleanup job that removes old user recovery sessions after
7 days. Runs hourly.

Implementation uses ULID cursor-based pagination with no additional
indexes needed. Child tickets cascade-delete automatically.
2026-01-16 17:46:01 +01:00
Quentin Gliech 0aaa9f79b4 Implement cleanup job for OAuth2 device code grants
Add cleanup job that removes device code grants older than 7 days.
Uses ULID cursor-based pagination for efficiency.

- Add cleanup method to OAuth2DeviceCodeGrantRepository
- Add CleanupOAuthDeviceCodeGrantsJob task
- Register handler and schedule to run hourly
2026-01-16 17:40:11 +01:00
Quentin Gliech 53010538bc Implement cleanup job for OAuth2 authorization grants
Add cleanup job that removes authorization grants older than 7 days.
Uses ULID cursor-based pagination for efficiency.

- Add cleanup method to OAuth2AuthorizationGrantRepository trait
- Add CleanupOAuthAuthorizationGrantsJob task
- Register handler and schedule to run hourly
2026-01-16 17:39:38 +01:00
Quentin Gliech edb6ba9606 Cleanup finished compat sessions after 30 days 2026-01-15 12:29:43 +01:00
Quentin Gliech e33f3f1e25 Cleanup old user registrations from the database 2026-01-14 14:01:10 +01:00
Quentin Gliech b693d789ae Remove imported unsupported threepids when deactivating a user (#5406) 2026-01-13 17:30:20 +01:00
Quentin Gliech 5ab1db2765 Hard delete expired, revoked and consumed OAuth 2.0 tokens after some time (#5409) 2026-01-13 17:23:16 +01:00
Quentin Gliech 8c50547aea Cleanup consumed refresh tokens 2026-01-12 11:18:18 +01:00
Quentin Gliech 7349da0889 Cleanup revoked refresh tokens 2026-01-09 18:37:09 +01:00
Quentin Gliech fba290e706 Cleanup expired OAuth 2.0 access tokens 2026-01-09 13:38:50 +01:00
Quentin Gliech 3e7cb60f63 Clean up revoked access tokens in batches 2026-01-08 19:03:09 +01:00
Quentin Gliech 48a826fe4c Remove imported unsupported threepids when deactivating a user 2026-01-08 15:33:50 +01:00
Quentin Gliech 8934f60953 Remove leftovers from OAuth 2.0 consent tracking cleanup 2026-01-08 15:17:37 +01:00
Quentin Gliech 21f1b8a427 Fix typos
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-04 16:32:13 +01:00
Quentin Gliech 4ad55caa90 Better error message when checksum in database is invalid 2025-12-04 15:14:14 +01:00
Quentin Gliech d1d23798bd Don't error out if a migration is missing
This allows us to roll back to older versions of MAS.
2025-12-04 15:13:42 +01:00
Quentin Gliech f6051fdbd9 Better lock and handle missing and modified migrations
This rewrites the database migration code to:

 - avoid deadlocks when running multiple migration processes at the same
   time with a `CREATE INDEX CONCURRENTLY` statement
 - allow us to remove some migrations from the code base and mark them as
   intentionally removed
 - allow us to modify some migrations and declare alternate checksums
   for previous versions of the migration
2025-12-04 14:44:16 +01:00
Olivier 'reivilibre 3a342b6165 Add experimental and preliminary policy-driven session limiting when logging in compatibility sessions. (#5287) 2025-12-02 15:50:04 +00:00
Quentin Gliech 61ee8dae87 Fix test name 2025-11-27 16:05:13 +01:00
Olivier 'reivilibre 3d50eaec9f Make finish_sessions_to_replace_device return whether any were finished 2025-11-25 18:41:14 +00:00
Quentin Gliech ac4f66920c Store upstream OAuth sessions on user registrations
This will allow us creating user registrations from upstream OAuth auth
sessions
2025-11-21 19:31:37 +01:00
Quentin Gliech f7c8a28592 Allow completing user email authentications using an upstream session
This will let us push emails in user registrations using an upstream
session
2025-11-21 19:28:26 +01:00
Olivier 'reivilibre a01c0af73e fix up test that relied on it being broken 2025-10-23 16:02:19 +01:00
Olivier 'reivilibre 80feaffe16 Add comments for the filters 2025-10-22 14:22:10 +01:00
Olivier 'reivilibre be40e1bed7 Add revoke_bulk for personal sessions storage 2025-10-22 11:27:10 +01:00
reivilibre eeba7e192c Personal Sessions: add create, list, get, revoke, regenerate Admin APIs (#5141)
Introduces some admin API endpoints for Personal Sessions.

- add: Creates a personal session along with its first personal access token, returning both. This is currently the only way to get a personal access token.
- get: Shows the information about a personal session
- list: Shows many personal sessions
- revoke: Revokes a personal session, so it can't be used anymore
- regenerate: Revoke the active personal access token for a session and issue a new one to replace it.
2025-10-22 11:20:02 +01:00
Olivier 'reivilibre 52c04c173f Add expires filter to personal sessions list 2025-10-21 10:10:14 +01:00
Olivier 'reivilibre 78b010d3f6 find_active_by_session: take &PersonalSession 2025-10-21 09:43:46 +01:00
Olivier 'reivilibre 893e4265c3 Implement activity tracking for personal sessions 2025-10-20 17:23:31 +01:00
Olivier 'reivilibre 01c89cd122 Delete owned PATs & personal sessions when pruning OAuth2 clients 2025-10-20 14:33:30 +01:00
Olivier 'reivilibre 353d23474c When revoking a personal session, also revoke its PAT 2025-10-20 14:33:30 +01:00
Olivier 'reivilibre 98c765cf6b storage: include PATs alongside personal sessions 2025-10-20 14:33:30 +01:00
Olivier 'reivilibre 34b3462a47 storage: introduce find_active_for_session for PATs 2025-10-20 13:06:41 +01:00