RocketChat / RocketChat/Rocket.Chat
Enhancement: Implement Collation Index for case-insensitive username lookups
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 46.1k
- Forks
- 13.9k
- Avg merge
- 3d 3h
- Merged PRs (30d)
- 130
Description
Following the database performance optimization in PR #39745 , the unindexed regex fallback search for CAS logins (new RegExp('^' + username + '$', 'i')) was replaced with a direct exact match. This successfully mitigated the risk of O(N) collection scans and CPU spikes during authentication.
However, this change introduces strict case-sensitivity. A CAS provider sending "JohnDoe" will no longer fall back to match an existing Rocket.Chat user stored as "johndoe".
Proposed Solution
To restore case-insensitive matching without sacrificing database performance, we should implement a Collation Index on the Users collection's username field at the database level.
By applying a collation index with strength: 2 (e.g., { locale: 'en', strength: 2 }), MongoDB can perform case-insensitive lookups while natively utilizing the b-tree index.
Alternatives Considered
- Normalizing at write/read (
.toLowerCase()): While safe for new setups, doing this retroactively would require a massive database migration to normalize all existing mixed-case usernames. A Collation Index avoids this data migration entirely. - Reverting to Regex: Unacceptable due to the severe performance penalty on large instances.
Additional Context
This architectural gap was highlighted during the automated review of the CAS regex removal. Tracking this here so we can natively support performant, case-insensitive lookups across the platform.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Review PR #39745 and the CAS login username lookup using the Users collection to understand the direct-match change. Implement and verify a MongoDB collation index with strength 2 for username lookups; done means mixed-case CAS usernames match existing accounts without reverting to regex scans.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- mongodb
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100