Skip to content

Conversation

@reivilibre
Copy link
Contributor

@reivilibre reivilibre commented Nov 6, 2025

Part of: #4339

Intended for commit-by-commit review.

Introduces a soft and hard session limit. At login time, we count the user's existing sessions (OAuth 2, Compat and self-owned Personal) and refuse creation of a new one if the limit is reached. This is configurable by policy.

The soft limit will be used for interactive logins where the user could be shown a web UI to remove devices, whereas the hard limit will be used for m.login.password logins where the user can't be shown such a UI.

Limitations:

  • Only applies at OAuth 2 login time.
  • Does not yet show a nice UI to rectify the solution, just a shouty policy violation screen.
  • Triggers a false positive when the login includes device replacement. (The limit shouldn't apply if you are replacing a device.)

These limitations will be handled in future PRs, but for now this is part of the experimental config section.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Nov 6, 2025

Deploying matrix-authentication-service-docs with  Cloudflare Pages  Cloudflare Pages

Latest commit: 08959fa
Status:🚫  Build failed.

View logs

@reivilibre reivilibre force-pushed the rei/policy_driven_session_limit branch from b6ad7a4 to 9f3d978 Compare November 6, 2025 09:18
@reivilibre reivilibre force-pushed the rei/policy_driven_session_limit branch from 9f3d978 to 203bf44 Compare November 6, 2025 10:12
@reivilibre reivilibre force-pushed the rei/policy_driven_session_limit branch from 203bf44 to 071830e Compare November 6, 2025 10:13
@reivilibre reivilibre marked this pull request as ready for review November 6, 2025 10:21
@reivilibre reivilibre requested a review from a team as a code owner November 6, 2025 10:21
Copy link
Member

@sandhose sandhose left a comment

Choose a reason for hiding this comment

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

I'm going to assume TODO after this is mostly dealing with the compat API

Comment on lines 121 to 122
pub soft_limit: u64,
pub hard_limit: u64,
Copy link
Member

Choose a reason for hiding this comment

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

TBD, how does it behave when a limit is set to zero? Would it be worth using NonZeroU64?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

#[derive(Debug)]
#[derive(Serialize, Debug)]
Copy link
Member

Choose a reason for hiding this comment

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

alternatively this could have a serializeable static part plus rest as two separate properties, which feels a smidge less 'smart', but also fine with this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok you tricked me into writing DataBase

Copy link
Contributor Author

Choose a reason for hiding this comment

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


let session_counts = count_user_sessions_for_limiting(&mut repo, &session.user)
.await
.map_err(|e| RouteError::Internal(e.into()))?;
Copy link
Member

Choose a reason for hiding this comment

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

surprised RouteError doesn't implement From that error?

Copy link
Member

Choose a reason for hiding this comment

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

oh, anyhow, that's why. Fine then

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to be fair, maybe I should have double-thought it and passed through the database error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This comment was marked as resolved.

@reivilibre reivilibre force-pushed the rei/policy_driven_session_limit branch from e0d7fb9 to 08959fa Compare November 6, 2025 15:29
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.

3 participants